2
0

Quick.Arrays.Helper.pas 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. { ***************************************************************************
  2. Copyright (c) 2016-2019 Kike Pérez
  3. Unit : Quick.Arrays.Helper
  4. Description : Array helpers
  5. Author : Kike Pérez
  6. Version : 1.0
  7. Created : 24/03/2019
  8. Modified : 29/03/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.Helper;
  22. {$i QuickLib.inc}
  23. interface
  24. uses
  25. Generics.Defaults;
  26. type
  27. TArrayHelper<T> = class
  28. public
  29. class function Count(var aArray : TArray<T>) : Integer;
  30. class procedure Add(var aArray : TArray<T>; aItem : T); static;
  31. class procedure Insert(var aArray : TArray<T>; aItem : T; aIndex : Integer); static;
  32. class procedure Remove(var aArray : TArray<T>; aIndex : Integer); static;
  33. class function Contains(var aArray : TArray<T>; aItem : T) : Boolean;
  34. class function IndexOf(var aArray : TArray<T>; aItem : T) : Integer;
  35. end;
  36. {$IFDEF FPC}
  37. TStringArray = TArray<string>;
  38. {$ENDIF}
  39. TStringArrayHelper = record Helper for {$IFNDEF FPC}TArray<string>{$ELSE}TStringArray{$ENDIF}
  40. function Count : Integer;
  41. procedure Add(const aValue : string);
  42. procedure Insert(const aValue : string; aIndex : Integer);
  43. procedure Remove(aIndex : Integer);
  44. function Contains(const aItem : string) : Boolean;
  45. function IndexOf(const aItem : string) : Integer;
  46. end;
  47. {$IFDEF FPC}
  48. TIntegerArray = TArray<Integer>;
  49. {$ENDIF}
  50. TIntegerArrayHelper = record Helper for {$IFNDEF FPC}TArray<Integer>{$ELSE}TIntegerArray{$ENDIF}
  51. function Count : Integer;
  52. procedure Add(aValue : Integer);
  53. procedure Insert(const aValue : Integer; aIndex : Integer);
  54. procedure Remove(aIndex : Integer);
  55. function Contains(aItem : Integer) : Boolean;
  56. function IndexOf(aItem : Integer) : Integer;
  57. end;
  58. implementation
  59. { TArray }
  60. class function TArrayHelper<T>.Count(var aArray : TArray<T>) : Integer;
  61. begin
  62. Result := High(aArray)+1;
  63. end;
  64. class procedure TArrayHelper<T>.Add(var aArray : TArray<T>; aItem : T);
  65. begin
  66. SetLength(aArray, Length(aArray) + 1);
  67. aArray[High(aArray)] := aItem;
  68. end;
  69. class procedure TArrayHelper<T>.Remove(var aArray : TArray<T>; aIndex : Integer);
  70. begin
  71. {$IFDEF DELPHIXE7_UP}
  72. System.Delete(aArray,aIndex,1);
  73. {$ELSE}
  74. TArrayUtil<T>.Delete(aArray,aIndex);
  75. {$ENDIF}
  76. end;
  77. class procedure TArrayHelper<T>.Insert(var aArray : TArray<T>; aItem : T; aIndex : Integer);
  78. begin
  79. System.Insert(aItem,aArray,aIndex);
  80. end;
  81. class function TArrayHelper<T>.Contains(var aArray : TArray<T>; aItem : T) : Boolean;
  82. var
  83. icomparer : IEqualityComparer<T>;
  84. i : Integer;
  85. begin
  86. Result := False;
  87. icomparer := TEqualityComparer<T>.Default;
  88. for i := Low(aArray) to High(aArray) do
  89. begin
  90. if icomparer.Equals(aArray[i],aItem) then Exit(True);
  91. end;
  92. end;
  93. class function TArrayHelper<T>.IndexOf(var aArray : TArray<T>; aItem : T) : Integer;
  94. var
  95. icomparer : IEqualityComparer<T>;
  96. i : Integer;
  97. begin
  98. icomparer := TEqualityComparer<T>.Default;
  99. for i := Low(aArray) to High(aArray) do
  100. begin
  101. if icomparer.Equals(aArray[i],aItem) then Exit(i);
  102. end;
  103. Result := -1;
  104. end;
  105. { TStringArrayHelper }
  106. function TStringArrayHelper.Count : Integer;
  107. begin
  108. Result := TArrayHelper<string>.Count(Self);
  109. end;
  110. procedure TStringArrayHelper.Add(const aValue : string);
  111. begin
  112. TArrayHelper<string>.Add(Self,aValue);
  113. end;
  114. procedure TStringArrayHelper.Insert(const aValue : string; aIndex : Integer);
  115. begin
  116. TArrayHelper<string>.Insert(Self,aValue,aIndex);
  117. end;
  118. procedure TStringArrayHelper.Remove(aIndex : Integer);
  119. begin
  120. TArrayHelper<string>.Remove(Self,aIndex);
  121. end;
  122. function TStringArrayHelper.Contains(const aItem : string) : Boolean;
  123. begin
  124. Result := TArrayHelper<string>.Contains(Self,aItem);
  125. end;
  126. function TStringArrayHelper.IndexOf(const aItem : string) : Integer;
  127. begin
  128. Result := TArrayHelper<string>.IndexOf(Self,aItem);
  129. end;
  130. { TIntegerArrayHelper }
  131. function TIntegerArrayHelper.Count : Integer;
  132. begin
  133. Result := TArrayHelper<Integer>.Count(Self);
  134. end;
  135. procedure TIntegerArrayHelper.Add(aValue : Integer);
  136. begin
  137. TArrayHelper<Integer>.Add(Self,aValue);
  138. end;
  139. procedure TIntegerArrayHelper.Insert(const aValue : Integer; aIndex : Integer);
  140. begin
  141. TArrayHelper<Integer>.Insert(Self,aValue,aIndex);
  142. end;
  143. procedure TIntegerArrayHelper.Remove(aIndex : Integer);
  144. begin
  145. TArrayHelper<Integer>.Remove(Self,aIndex);
  146. end;
  147. function TIntegerArrayHelper.Contains(aItem : Integer) : Boolean;
  148. begin
  149. Result := TArrayHelper<Integer>.Contains(Self,aItem);
  150. end;
  151. function TIntegerArrayHelper.IndexOf(aItem : Integer) : Integer;
  152. begin
  153. Result := TArrayHelper<Integer>.IndexOf(Self,aItem);
  154. end;
  155. end.