Quick.Arrays.pas 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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 : 11/04/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. end;
  68. TPairArray = TXArray<TPair>;
  69. TFlexArray = TXArray<TFlexValue>;
  70. TFlexPairArray = TArray<TFlexPair>;
  71. TFlexPairArrayHelper = record helper for TFlexPairArray
  72. public
  73. function GetValue(const aName : string) : TFlexValue;
  74. function GetPair(const aName : string) : TFlexPair;
  75. function Add(aFlexPair : TFlexPair) : Integer; overload;
  76. function Add(const aName: string; aValue : TFlexValue): Integer; overload;
  77. function Exists(const aName : string) : Boolean;
  78. function Remove(const aName : string) : Boolean;
  79. function Count : Integer;
  80. end;
  81. implementation
  82. { TxArray }
  83. function TxArray<T>.GetItem(Index : Integer) : T;
  84. begin
  85. Result := fArray[Index];
  86. end;
  87. procedure TXArray<T>.SetCapacity(const Value: Integer);
  88. begin
  89. if Value = High(fArray) then Exit;
  90. if Value < 0 then SetLength(fArray,1)
  91. else SetLength(fArray, Value);
  92. end;
  93. procedure TxArray<T>.SetItem(Index : Integer; const aValue : T);
  94. begin
  95. fArray[Index] := aValue;
  96. end;
  97. function TXArray<T>.GetCapacity: Integer;
  98. begin
  99. Result := High(fArray) + 1;
  100. end;
  101. function TXArray<T>.GetCount: Integer;
  102. begin
  103. Result := High(fArray) + 1;
  104. end;
  105. function TxArray<T>.GetEnumerator: TEnumerator<T>;
  106. begin
  107. Result := TEnumerator.Create(fArray);
  108. end;
  109. function TxArray<T>.Add(aValue : T) : Integer;
  110. begin
  111. SetLength(fArray, Length(fArray) + 1);
  112. fArray[High(fArray)] := aValue;
  113. Result := High(fArray);
  114. end;
  115. procedure TxArray<T>.Delete(aIndex : Integer);
  116. begin
  117. System.Delete(fArray,aIndex,1);
  118. end;
  119. procedure TxArray<T>.Remove(aItem : T);
  120. var
  121. nindex : Integer;
  122. begin
  123. nindex := IndexOf(aItem);
  124. if nindex > -1 then System.Delete(fArray,nindex,1);
  125. end;
  126. procedure TxArray<T>.Insert(aItem : T; aIndex : Integer);
  127. begin
  128. System.Insert(aItem,fArray,aIndex);
  129. end;
  130. function TxArray<T>.Contains(aItem : T) : Boolean;
  131. var
  132. icomparer : IEqualityComparer<T>;
  133. i : Integer;
  134. begin
  135. Result := False;
  136. icomparer := TEqualityComparer<T>.Default;
  137. for i := Low(fArray) to High(fArray) do
  138. begin
  139. if icomparer.Equals(fArray[i],aItem) then Exit(True);
  140. end;
  141. end;
  142. function TxArray<T>.IndexOf(aItem : T) : Integer;
  143. var
  144. icomparer : IEqualityComparer<T>;
  145. i : Integer;
  146. begin
  147. icomparer := TEqualityComparer<T>.Default;
  148. for i := Low(fArray) to High(fArray) do
  149. begin
  150. if icomparer.Equals(fArray[i],aItem) then Exit(i);
  151. end;
  152. Result := -1;
  153. end;
  154. class operator TxArray<T>.Implicit(const Value : TxArray<T>) : TArray<T>;
  155. begin
  156. Result := Value.fArray;
  157. end;
  158. class operator TXArray<T>.Implicit(const Value: TArray<T>): TxArray<T>;
  159. begin
  160. Result.fArray := Value;
  161. end;
  162. { TXArray<T>.TEnumerator }
  163. constructor TXArray<T>.TEnumerator.Create(var aArray: TArray<T>);
  164. begin
  165. fIndex := -1;
  166. fArray := @aArray;
  167. end;
  168. function TXArray<T>.TEnumerator.DoGetCurrent: T;
  169. begin
  170. Result := TArray<T>(fArray^)[fIndex];
  171. end;
  172. function TXArray<T>.TEnumerator.DoMoveNext: Boolean;
  173. begin
  174. Inc(fIndex);
  175. Result := fIndex < High(TArray<T>(fArray^))+1;
  176. end;
  177. { TFlexPairArrayHelper }
  178. function TFlexPairArrayHelper.Add(const aName: string; aValue : TFlexValue): Integer;
  179. begin
  180. SetLength(Self,Length(Self)+1);
  181. Self[High(Self)].Name := aName;
  182. Self[High(Self)].Value := aValue;
  183. Result := High(Self);
  184. end;
  185. function TFlexPairArrayHelper.Count: Integer;
  186. begin
  187. Result := High(Self) + 1;
  188. end;
  189. function TFlexPairArrayHelper.Add(aFlexPair: TFlexPair): Integer;
  190. begin
  191. Result := High(Self);
  192. end;
  193. function TFlexPairArrayHelper.Exists(const aName: string): Boolean;
  194. var
  195. i : Integer;
  196. begin
  197. Result := False;
  198. for i := Low(Self) to High(Self) do
  199. begin
  200. if CompareText(Self[i].Name,aName) = 0 then Exit(True)
  201. end;
  202. end;
  203. function TFlexPairArrayHelper.GetPair(const aName: string): TFlexPair;
  204. var
  205. i : Integer;
  206. begin
  207. for i := Low(Self) to High(Self) do
  208. begin
  209. if CompareText(Self[i].Name,aName) = 0 then Exit(Self[i]);
  210. end;
  211. end;
  212. function TFlexPairArrayHelper.GetValue(const aName: string): TFlexValue;
  213. var
  214. i : Integer;
  215. begin
  216. for i := Low(Self) to High(Self) do
  217. begin
  218. if CompareText(Self[i].Name,aName) = 0 then Exit(Self[i].Value);
  219. end;
  220. end;
  221. function TFlexPairArrayHelper.Remove(const aName: string): Boolean;
  222. var
  223. i : Integer;
  224. begin
  225. for i := Low(Self) to High(Self) do
  226. begin
  227. if CompareText(Self[i].Name,aName) = 0 then
  228. begin
  229. System.Delete(Self,i,1);
  230. Exit(True);
  231. end;
  232. end;
  233. end;
  234. end.