Quick.Arrays.pas 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. { ***************************************************************************
  2. Copyright (c) 2016-2019 Kike Pérez
  3. Unit : Quick.Arrays
  4. Description : Multifuntional Arrays
  5. Author : Kike Pérez
  6. Version : 1.2
  7. Created : 24/03/2019
  8. Modified : 31/08/2019
  9. This file is part of QuickLib: https://github.com/exilon/QuickLib
  10. ***************************************************************************
  11. Licensed under the Apache License, Version 2.0 (the "License");
  12. you may not use this file except in compliance with the License.
  13. You may obtain a copy of the License at
  14. http://www.apache.org/licenses/LICENSE-2.0
  15. Unless required by applicable law or agreed to in writing, software
  16. distributed under the License is distributed on an "AS IS" BASIS,
  17. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  18. See the License for the specific language governing permissions and
  19. limitations under the License.
  20. *************************************************************************** }
  21. unit Quick.Arrays;
  22. {$i QuickLib.inc}
  23. interface
  24. uses
  25. SysUtils,
  26. Generics.Defaults,
  27. Generics.Collections,
  28. Quick.Value;
  29. type
  30. TXArray<T> = record
  31. type
  32. TEnumerator = class(Generics.Collections.TEnumerator<T>)
  33. private
  34. fArray : ^TArray<T>;
  35. fIndex : Integer;
  36. protected
  37. function DoGetCurrent: T; override;
  38. function DoMoveNext: Boolean; override;
  39. public
  40. constructor Create(var aArray: TArray<T>);
  41. end;
  42. private
  43. fArray : TArray<T>;
  44. function GetItem(Index : Integer) : T;
  45. procedure SetItem(Index : Integer; const aValue : T);
  46. function GetCapacity: Integer;
  47. function GetCount: Integer;
  48. procedure SetCapacity(const Value: Integer);
  49. public
  50. function GetEnumerator: TEnumerator<T>;
  51. property AsArray : TArray<T> read fArray;
  52. property Items[Index : Integer] : T read GetItem write SetItem; default;
  53. property Count : Integer read GetCount;
  54. property Capacity : Integer read GetCapacity write SetCapacity;
  55. function Add(aValue : T) : Integer;
  56. procedure Insert(aItem : T; aIndex : Integer);
  57. procedure Delete(aIndex : Integer);
  58. procedure Remove(aItem : T);
  59. function Contains(aItem : T) : Boolean;
  60. function IndexOf(aItem : T) : Integer;
  61. class operator Implicit(const Value : TxArray<T>) : TArray<T>;
  62. class operator Implicit(const Value : TArray<T>) : TxArray<T>;
  63. end;
  64. TPair = record
  65. Name : string;
  66. Value : string;
  67. constructor Create(const aName, aValue : string);
  68. end;
  69. TPairArray = TArray<TPair>;
  70. TPairXArray = TXArray<TPair>;
  71. TFlexArray = TXArray<TFlexValue>;
  72. TFlexPairArray = TArray<TFlexPair>;
  73. TFlexPairArrayHelper = record helper for TFlexPairArray
  74. public
  75. function GetValue(const aName : string) : TFlexValue;
  76. function GetPair(const aName : string) : TFlexPair;
  77. function Add(aFlexPair : TFlexPair) : Integer; overload;
  78. function Add(const aName: string; aValue : TFlexValue): Integer; overload;
  79. function Exists(const aName : string) : Boolean;
  80. function Remove(const aName : string) : Boolean;
  81. function Count : Integer;
  82. end;
  83. implementation
  84. { TxArray }
  85. function TxArray<T>.GetItem(Index : Integer) : T;
  86. begin
  87. Result := fArray[Index];
  88. end;
  89. procedure TXArray<T>.SetCapacity(const Value: Integer);
  90. begin
  91. if Value = High(fArray) then Exit;
  92. if Value < 0 then SetLength(fArray,1)
  93. else SetLength(fArray, Value);
  94. end;
  95. procedure TxArray<T>.SetItem(Index : Integer; const aValue : T);
  96. begin
  97. fArray[Index] := aValue;
  98. end;
  99. function TXArray<T>.GetCapacity: Integer;
  100. begin
  101. Result := High(fArray) + 1;
  102. end;
  103. function TXArray<T>.GetCount: Integer;
  104. begin
  105. Result := High(fArray) + 1;
  106. end;
  107. function TxArray<T>.GetEnumerator: TEnumerator<T>;
  108. begin
  109. Result := TEnumerator.Create(fArray);
  110. end;
  111. function TxArray<T>.Add(aValue : T) : Integer;
  112. begin
  113. SetLength(fArray, Length(fArray) + 1);
  114. fArray[High(fArray)] := aValue;
  115. Result := High(fArray);
  116. end;
  117. procedure TxArray<T>.Delete(aIndex : Integer);
  118. begin
  119. System.Delete(fArray,aIndex,1);
  120. end;
  121. procedure TxArray<T>.Remove(aItem : T);
  122. var
  123. nindex : Integer;
  124. begin
  125. nindex := IndexOf(aItem);
  126. if nindex > -1 then System.Delete(fArray,nindex,1);
  127. end;
  128. procedure TxArray<T>.Insert(aItem : T; aIndex : Integer);
  129. begin
  130. System.Insert(aItem,fArray,aIndex);
  131. end;
  132. function TxArray<T>.Contains(aItem : T) : Boolean;
  133. var
  134. icomparer : IEqualityComparer<T>;
  135. i : Integer;
  136. begin
  137. Result := False;
  138. icomparer := TEqualityComparer<T>.Default;
  139. for i := Low(fArray) to High(fArray) do
  140. begin
  141. if icomparer.Equals(fArray[i],aItem) then Exit(True);
  142. end;
  143. end;
  144. function TxArray<T>.IndexOf(aItem : T) : Integer;
  145. var
  146. icomparer : IEqualityComparer<T>;
  147. i : Integer;
  148. begin
  149. icomparer := TEqualityComparer<T>.Default;
  150. for i := Low(fArray) to High(fArray) do
  151. begin
  152. if icomparer.Equals(fArray[i],aItem) then Exit(i);
  153. end;
  154. Result := -1;
  155. end;
  156. class operator TxArray<T>.Implicit(const Value : TxArray<T>) : TArray<T>;
  157. begin
  158. Result := Value.fArray;
  159. end;
  160. class operator TXArray<T>.Implicit(const Value: TArray<T>): TxArray<T>;
  161. begin
  162. Result.fArray := Value;
  163. end;
  164. { TXArray<T>.TEnumerator }
  165. constructor TXArray<T>.TEnumerator.Create(var aArray: TArray<T>);
  166. begin
  167. fIndex := -1;
  168. fArray := @aArray;
  169. end;
  170. function TXArray<T>.TEnumerator.DoGetCurrent: T;
  171. begin
  172. Result := TArray<T>(fArray^)[fIndex];
  173. end;
  174. function TXArray<T>.TEnumerator.DoMoveNext: Boolean;
  175. begin
  176. Inc(fIndex);
  177. Result := fIndex < High(TArray<T>(fArray^))+1;
  178. end;
  179. { TFlexPairArrayHelper }
  180. function TFlexPairArrayHelper.Add(const aName: string; aValue : TFlexValue): Integer;
  181. begin
  182. SetLength(Self,Length(Self)+1);
  183. Self[High(Self)].Name := aName;
  184. Self[High(Self)].Value := aValue;
  185. Result := High(Self);
  186. end;
  187. function TFlexPairArrayHelper.Count: Integer;
  188. begin
  189. Result := High(Self) + 1;
  190. end;
  191. function TFlexPairArrayHelper.Add(aFlexPair: TFlexPair): Integer;
  192. begin
  193. SetLength(Self,Length(Self)+1);
  194. Self[High(Self)] := aFlexPair;
  195. Result := High(Self);
  196. end;
  197. function TFlexPairArrayHelper.Exists(const aName: string): Boolean;
  198. var
  199. i : Integer;
  200. begin
  201. Result := False;
  202. for i := Low(Self) to High(Self) do
  203. begin
  204. if CompareText(Self[i].Name,aName) = 0 then Exit(True)
  205. end;
  206. end;
  207. function TFlexPairArrayHelper.GetPair(const aName: string): TFlexPair;
  208. var
  209. i : Integer;
  210. begin
  211. for i := Low(Self) to High(Self) do
  212. begin
  213. if CompareText(Self[i].Name,aName) = 0 then Exit(Self[i]);
  214. end;
  215. end;
  216. function TFlexPairArrayHelper.GetValue(const aName: string): TFlexValue;
  217. var
  218. i : Integer;
  219. begin
  220. for i := Low(Self) to High(Self) do
  221. begin
  222. if CompareText(Self[i].Name,aName) = 0 then Exit(Self[i].Value);
  223. end;
  224. end;
  225. function TFlexPairArrayHelper.Remove(const aName: string): Boolean;
  226. var
  227. i : Integer;
  228. begin
  229. for i := Low(Self) to High(Self) do
  230. begin
  231. if CompareText(Self[i].Name,aName) = 0 then
  232. begin
  233. System.Delete(Self,i,1);
  234. Exit(True);
  235. end;
  236. end;
  237. end;
  238. { TPair }
  239. constructor TPair.Create(const aName, aValue: string);
  240. begin
  241. Name := aName;
  242. Value := aValue;
  243. end;
  244. end.