Quick.Arrays.pas 6.5 KB

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