Quick.Arrays.pas 6.1 KB

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