blob: c0fe32319467585138b96ca7c52dca8f80eba933 [file] [log] [blame]
Jens Geyerd5436f52014-10-03 19:50:38 +02001(*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 *)
19
20unit Thrift.Collections;
21
22interface
23
24uses
Jens Geyer8f7487e2019-05-09 22:21:32 +020025 SysUtils, Generics.Collections, Generics.Defaults, Thrift.Utils;
Jens Geyerd5436f52014-10-03 19:50:38 +020026
27type
28
29{$IF CompilerVersion < 21.0}
30 TArray<T> = array of T;
31{$IFEND}
32
Jens Geyer8f7487e2019-05-09 22:21:32 +020033 IThriftContainer = interface( ISupportsToString)
34 ['{E05C0F9D-A4F5-491D-AADA-C926B4BDB6E4}']
Jens Geyerd5436f52014-10-03 19:50:38 +020035 end;
36
Jens Geyer8f7487e2019-05-09 22:21:32 +020037
Jens Geyerd5436f52014-10-03 19:50:38 +020038 IThriftDictionary<TKey,TValue> = interface(IThriftContainer)
39 ['{25EDD506-F9D1-4008-A40F-5940364B7E46}']
40 function GetEnumerator: TEnumerator<TPair<TKey,TValue>>;
41
42 function GetKeys: TDictionary<TKey,TValue>.TKeyCollection;
43 function GetValues: TDictionary<TKey,TValue>.TValueCollection;
44 function GetItem(const Key: TKey): TValue;
45 procedure SetItem(const Key: TKey; const Value: TValue);
46 function GetCount: Integer;
47
48 procedure Add(const Key: TKey; const Value: TValue);
49 procedure Remove(const Key: TKey);
50{$IF CompilerVersion >= 21.0}
51 function ExtractPair(const Key: TKey): TPair<TKey,TValue>;
52{$IFEND}
53 procedure Clear;
54 procedure TrimExcess;
55 function TryGetValue(const Key: TKey; out Value: TValue): Boolean;
56 procedure AddOrSetValue(const Key: TKey; const Value: TValue);
57 function ContainsKey(const Key: TKey): Boolean;
58 function ContainsValue(const Value: TValue): Boolean;
59 function ToArray: TArray<TPair<TKey,TValue>>;
60
61 property Items[const Key: TKey]: TValue read GetItem write SetItem; default;
62 property Count: Integer read GetCount;
63 property Keys: TDictionary<TKey,TValue>.TKeyCollection read GetKeys;
64 property Values: TDictionary<TKey,TValue>.TValueCollection read GetValues;
65 end;
66
Jens Geyer8f7487e2019-05-09 22:21:32 +020067 TThriftDictionaryImpl<TKey,TValue> = class( TInterfacedObject, IThriftDictionary<TKey,TValue>, IThriftContainer, ISupportsToString)
Jens Geyerfad7fd32019-11-09 23:24:52 +010068 strict private
Jens Geyerf5627532023-02-24 21:25:28 +010069 FDictionary : TDictionary<TKey,TValue>;
Jens Geyerfad7fd32019-11-09 23:24:52 +010070 strict protected
Jens Geyerd5436f52014-10-03 19:50:38 +020071 function GetEnumerator: TEnumerator<TPair<TKey,TValue>>;
72
73 function GetKeys: TDictionary<TKey,TValue>.TKeyCollection;
74 function GetValues: TDictionary<TKey,TValue>.TValueCollection;
75 function GetItem(const Key: TKey): TValue;
76 procedure SetItem(const Key: TKey; const Value: TValue);
77 function GetCount: Integer;
78
79 procedure Add(const Key: TKey; const Value: TValue);
80 procedure Remove(const Key: TKey);
81{$IF CompilerVersion >= 21.0}
82 function ExtractPair(const Key: TKey): TPair<TKey,TValue>;
83{$IFEND}
84 procedure Clear;
85 procedure TrimExcess;
86 function TryGetValue(const Key: TKey; out Value: TValue): Boolean;
87 procedure AddOrSetValue(const Key: TKey; const Value: TValue);
88 function ContainsKey(const Key: TKey): Boolean;
89 function ContainsValue(const Value: TValue): Boolean;
90 function ToArray: TArray<TPair<TKey,TValue>>;
91 property Items[const Key: TKey]: TValue read GetItem write SetItem; default;
92 property Count: Integer read GetCount;
93 property Keys: TDictionary<TKey,TValue>.TKeyCollection read GetKeys;
94 property Values: TDictionary<TKey,TValue>.TValueCollection read GetValues;
95 public
Jens Geyerf5627532023-02-24 21:25:28 +010096 constructor Create( const aCapacity: Integer = 0); overload;
97 constructor Create( const aCapacity: Integer; const aComparer : IEqualityComparer<TKey>); overload;
Jens Geyerd5436f52014-10-03 19:50:38 +020098 destructor Destroy; override;
Jens Geyer8f7487e2019-05-09 22:21:32 +020099 function ToString : string; override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200100 end;
101
102 IThriftList<T> = interface(IThriftContainer)
103 ['{29BEEE31-9CB4-401B-AA04-5148A75F473B}']
104 function GetEnumerator: TEnumerator<T>;
105 function GetCapacity: Integer;
106 procedure SetCapacity(Value: Integer);
107 function GetCount: Integer;
108 procedure SetCount(Value: Integer);
109 function GetItem(Index: Integer): T;
110 procedure SetItem(Index: Integer; const Value: T);
111 function Add(const Value: T): Integer;
112 procedure AddRange(const Values: array of T); overload;
113 procedure AddRange(const Collection: IEnumerable<T>); overload;
114 procedure AddRange(Collection: TEnumerable<T>); overload;
115 procedure Insert(Index: Integer; const Value: T);
116 procedure InsertRange(Index: Integer; const Values: array of T); overload;
117 procedure InsertRange(Index: Integer; const Collection: IEnumerable<T>); overload;
118 procedure InsertRange(Index: Integer; const Collection: TEnumerable<T>); overload;
119 function Remove(const Value: T): Integer;
120 procedure Delete(Index: Integer);
121 procedure DeleteRange(AIndex, ACount: Integer);
122 function Extract(const Value: T): T;
123{$IF CompilerVersion >= 21.0}
124 procedure Exchange(Index1, Index2: Integer);
125 procedure Move(CurIndex, NewIndex: Integer);
126 function First: T;
127 function Last: T;
128{$IFEND}
129 procedure Clear;
130 function Contains(const Value: T): Boolean;
131 function IndexOf(const Value: T): Integer;
132 function LastIndexOf(const Value: T): Integer;
133 procedure Reverse;
134 procedure Sort; overload;
135 procedure Sort(const AComparer: IComparer<T>); overload;
136 function BinarySearch(const Item: T; out Index: Integer): Boolean; overload;
137 function BinarySearch(const Item: T; out Index: Integer; const AComparer: IComparer<T>): Boolean; overload;
138 procedure TrimExcess;
139 function ToArray: TArray<T>;
140 property Capacity: Integer read GetCapacity write SetCapacity;
141 property Count: Integer read GetCount write SetCount;
142 property Items[Index: Integer]: T read GetItem write SetItem; default;
143 end;
144
Jens Geyer8f7487e2019-05-09 22:21:32 +0200145 TThriftListImpl<T> = class( TInterfacedObject, IThriftList<T>, IThriftContainer, ISupportsToString)
Jens Geyerfad7fd32019-11-09 23:24:52 +0100146 strict private
Jens Geyerd5436f52014-10-03 19:50:38 +0200147 FList : TList<T>;
Jens Geyerfad7fd32019-11-09 23:24:52 +0100148 strict protected
Jens Geyerd5436f52014-10-03 19:50:38 +0200149 function GetEnumerator: TEnumerator<T>;
150 function GetCapacity: Integer;
151 procedure SetCapacity(Value: Integer);
152 function GetCount: Integer;
153 procedure SetCount(Value: Integer);
154 function GetItem(Index: Integer): T;
155 procedure SetItem(Index: Integer; const Value: T);
156 function Add(const Value: T): Integer;
157 procedure AddRange(const Values: array of T); overload;
158 procedure AddRange(const Collection: IEnumerable<T>); overload;
159 procedure AddRange(Collection: TEnumerable<T>); overload;
160 procedure Insert(Index: Integer; const Value: T);
161 procedure InsertRange(Index: Integer; const Values: array of T); overload;
162 procedure InsertRange(Index: Integer; const Collection: IEnumerable<T>); overload;
163 procedure InsertRange(Index: Integer; const Collection: TEnumerable<T>); overload;
164 function Remove(const Value: T): Integer;
165 procedure Delete(Index: Integer);
166 procedure DeleteRange(AIndex, ACount: Integer);
167 function Extract(const Value: T): T;
168{$IF CompilerVersion >= 21.0}
169 procedure Exchange(Index1, Index2: Integer);
170 procedure Move(CurIndex, NewIndex: Integer);
171 function First: T;
172 function Last: T;
173{$IFEND}
174 procedure Clear;
175 function Contains(const Value: T): Boolean;
176 function IndexOf(const Value: T): Integer;
177 function LastIndexOf(const Value: T): Integer;
178 procedure Reverse;
179 procedure Sort; overload;
180 procedure Sort(const AComparer: IComparer<T>); overload;
181 function BinarySearch(const Item: T; out Index: Integer): Boolean; overload;
182 function BinarySearch(const Item: T; out Index: Integer; const AComparer: IComparer<T>): Boolean; overload;
183 procedure TrimExcess;
184 function ToArray: TArray<T>;
185 property Capacity: Integer read GetCapacity write SetCapacity;
186 property Count: Integer read GetCount write SetCount;
187 property Items[Index: Integer]: T read GetItem write SetItem; default;
188 public
Jens Geyerf5627532023-02-24 21:25:28 +0100189 constructor Create( const aCapacity: Integer = 0);
Jens Geyerd5436f52014-10-03 19:50:38 +0200190 destructor Destroy; override;
Jens Geyer8f7487e2019-05-09 22:21:32 +0200191 function ToString : string; override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200192 end;
193
Jens Geyerda5ef362024-05-24 21:09:42 +0200194 IThriftHashSet<T> = interface(IThriftContainer)
195 ['{733E2B57-C374-4359-BBD5-2B9CD8DF737C}']
196 function GetEnumerator: TEnumerator<T>;
Jens Geyerd5436f52014-10-03 19:50:38 +0200197 function GetCount: Integer;
198 property Count: Integer read GetCount;
Jens Geyerda5ef362024-05-24 21:09:42 +0200199 function Add( const item: T) : Boolean;
Jens Geyerd5436f52014-10-03 19:50:38 +0200200 procedure Clear;
Jens Geyerda5ef362024-05-24 21:09:42 +0200201 function Contains( const item: T): Boolean;
202 function Remove( const item: T): Boolean;
Jens Geyerd5436f52014-10-03 19:50:38 +0200203 end;
204
Jens Geyer683263d2022-09-03 18:52:35 +0200205 // compatibility
Jens Geyerda5ef362024-05-24 21:09:42 +0200206 IHashSet<T> = interface( IThriftHashSet<T>)
Jens Geyer683263d2022-09-03 18:52:35 +0200207 ['{C3CF557F-21D9-4524-B899-D3145B0389BB}']
208 end deprecated 'use IThriftHashSet<T>';
209
210
211 {$WARN SYMBOL_DEPRECATED OFF}
Jens Geyerda5ef362024-05-24 21:09:42 +0200212 TThriftHashSetImpl<T> = class( TInterfacedObject, IHashSet<T>, IThriftHashSet<T>, IThriftContainer, ISupportsToString)
Jens Geyer683263d2022-09-03 18:52:35 +0200213 {$WARN SYMBOL_DEPRECATED DEFAULT}
Jens Geyerfad7fd32019-11-09 23:24:52 +0100214 strict private
Jens Geyerda5ef362024-05-24 21:09:42 +0200215 FDictionary : TDictionary<T,Byte>; // there is no THashSet<T> in older Delphi versions
Jens Geyerfad7fd32019-11-09 23:24:52 +0100216 strict protected
Jens Geyerda5ef362024-05-24 21:09:42 +0200217 function GetEnumerator: TEnumerator<T>;
Jens Geyerd5436f52014-10-03 19:50:38 +0200218 function GetCount: Integer;
219 property Count: Integer read GetCount;
Jens Geyerda5ef362024-05-24 21:09:42 +0200220 function Add( const item: T) : Boolean;
Jens Geyerd5436f52014-10-03 19:50:38 +0200221 procedure Clear;
Jens Geyerda5ef362024-05-24 21:09:42 +0200222 function Contains( const item: T): Boolean;
223 function Remove( const item: T): Boolean;
Jens Geyerd5436f52014-10-03 19:50:38 +0200224 public
Jens Geyerf5627532023-02-24 21:25:28 +0100225 constructor Create( const aCapacity: Integer = 0); overload;
Jens Geyerda5ef362024-05-24 21:09:42 +0200226 constructor Create( const aCapacity: Integer; const aComparer : IEqualityComparer<T>); overload;
Jens Geyer8f7487e2019-05-09 22:21:32 +0200227 function ToString : string; override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200228 end;
229
Jens Geyer683263d2022-09-03 18:52:35 +0200230 // compatibility
Jens Geyerda5ef362024-05-24 21:09:42 +0200231 THashSetImpl<T> = class( TThriftHashSetImpl<T>)
Jens Geyer683263d2022-09-03 18:52:35 +0200232 end deprecated 'use TThriftHashSetImpl<T>';
233
Jens Geyerd5436f52014-10-03 19:50:38 +0200234implementation
235
Jens Geyerda5ef362024-05-24 21:09:42 +0200236{ TThriftHashSetImpl<T>. }
Jens Geyerd5436f52014-10-03 19:50:38 +0200237
Jens Geyerda5ef362024-05-24 21:09:42 +0200238function TThriftHashSetImpl<T>.Add( const item: T) : Boolean;
Jens Geyerd5436f52014-10-03 19:50:38 +0200239begin
Jens Geyerda5ef362024-05-24 21:09:42 +0200240 result := not FDictionary.ContainsKey(item);
241 if result then FDictionary.Add( item, 0);
Jens Geyerd5436f52014-10-03 19:50:38 +0200242end;
243
Jens Geyerda5ef362024-05-24 21:09:42 +0200244procedure TThriftHashSetImpl<T>.Clear;
Jens Geyerd5436f52014-10-03 19:50:38 +0200245begin
246 FDictionary.Clear;
247end;
248
Jens Geyerda5ef362024-05-24 21:09:42 +0200249function TThriftHashSetImpl<T>.Contains( const item: T): Boolean;
Jens Geyerd5436f52014-10-03 19:50:38 +0200250begin
251 Result := FDictionary.ContainsKey(item);
252end;
253
Jens Geyerda5ef362024-05-24 21:09:42 +0200254constructor TThriftHashSetImpl<T>.Create( const aCapacity: Integer);
Jens Geyerd5436f52014-10-03 19:50:38 +0200255begin
Jens Geyerf5627532023-02-24 21:25:28 +0100256 inherited Create;
Jens Geyerda5ef362024-05-24 21:09:42 +0200257 FDictionary := TDictionary<T,Byte>.Create( aCapacity);
Jens Geyerf5627532023-02-24 21:25:28 +0100258end;
259
Jens Geyerda5ef362024-05-24 21:09:42 +0200260constructor TThriftHashSetImpl<T>.Create( const aCapacity: Integer; const aComparer : IEqualityComparer<T>);
Jens Geyerf5627532023-02-24 21:25:28 +0100261begin
262 inherited Create;
Jens Geyerda5ef362024-05-24 21:09:42 +0200263 FDictionary := TDictionary<T,Byte>.Create( aCapacity, aComparer);
Jens Geyerd5436f52014-10-03 19:50:38 +0200264end;
265
Jens Geyerda5ef362024-05-24 21:09:42 +0200266function TThriftHashSetImpl<T>.GetCount: Integer;
Jens Geyerd5436f52014-10-03 19:50:38 +0200267begin
268 Result := FDictionary.Count;
269end;
270
Jens Geyerda5ef362024-05-24 21:09:42 +0200271function TThriftHashSetImpl<T>.GetEnumerator: TEnumerator<T>;
Jens Geyerd5436f52014-10-03 19:50:38 +0200272begin
273 Result := FDictionary.Keys.GetEnumerator;
274end;
275
Jens Geyerda5ef362024-05-24 21:09:42 +0200276function TThriftHashSetImpl<T>.Remove( const item: T): Boolean;
Jens Geyerd5436f52014-10-03 19:50:38 +0200277begin
Jens Geyerda5ef362024-05-24 21:09:42 +0200278 Result := FDictionary.ContainsKey( item);
279 if Result then FDictionary.Remove( item );
Jens Geyerd5436f52014-10-03 19:50:38 +0200280end;
281
Jens Geyerda5ef362024-05-24 21:09:42 +0200282function TThriftHashSetImpl<T>.ToString : string;
283var elm : T;
Jens Geyer8f7487e2019-05-09 22:21:32 +0200284 sb : TThriftStringBuilder;
285 first : Boolean;
286begin
287 sb := TThriftStringBuilder.Create('{');
288 try
289 first := TRUE;
290 for elm in FDictionary.Keys do begin
291 if first
292 then first := FALSE
293 else sb.Append(', ');
294
Jens Geyerda5ef362024-05-24 21:09:42 +0200295 sb.Append( StringUtils<T>.ToString(elm));
Jens Geyer8f7487e2019-05-09 22:21:32 +0200296 end;
297 sb.Append('}');
298 Result := sb.ToString;
299 finally
300 sb.Free;
301 end;
302end;
303
Jens Geyerd5436f52014-10-03 19:50:38 +0200304{ TThriftDictionaryImpl<TKey, TValue> }
305
306procedure TThriftDictionaryImpl<TKey, TValue>.Add(const Key: TKey;
307 const Value: TValue);
308begin
Jens Geyerf5627532023-02-24 21:25:28 +0100309 FDictionary.Add( Key, Value);
Jens Geyerd5436f52014-10-03 19:50:38 +0200310end;
311
312procedure TThriftDictionaryImpl<TKey, TValue>.AddOrSetValue(const Key: TKey;
313 const Value: TValue);
314begin
Jens Geyerf5627532023-02-24 21:25:28 +0100315 FDictionary.AddOrSetValue( Key, Value);
Jens Geyerd5436f52014-10-03 19:50:38 +0200316end;
317
318procedure TThriftDictionaryImpl<TKey, TValue>.Clear;
319begin
Jens Geyerf5627532023-02-24 21:25:28 +0100320 FDictionary.Clear;
Jens Geyerd5436f52014-10-03 19:50:38 +0200321end;
322
323function TThriftDictionaryImpl<TKey, TValue>.ContainsKey(
324 const Key: TKey): Boolean;
325begin
Jens Geyerf5627532023-02-24 21:25:28 +0100326 Result := FDictionary.ContainsKey( Key );
Jens Geyerd5436f52014-10-03 19:50:38 +0200327end;
328
329function TThriftDictionaryImpl<TKey, TValue>.ContainsValue(
330 const Value: TValue): Boolean;
331begin
Jens Geyerf5627532023-02-24 21:25:28 +0100332 Result := FDictionary.ContainsValue( Value );
Jens Geyerd5436f52014-10-03 19:50:38 +0200333end;
334
Jens Geyerf5627532023-02-24 21:25:28 +0100335constructor TThriftDictionaryImpl<TKey, TValue>.Create(const aCapacity: Integer);
Jens Geyerd5436f52014-10-03 19:50:38 +0200336begin
337 inherited Create;
Jens Geyerf5627532023-02-24 21:25:28 +0100338 FDictionary := TDictionary<TKey,TValue>.Create( aCapacity);
339end;
340
341constructor TThriftDictionaryImpl<TKey, TValue>.Create(const aCapacity: Integer; const aComparer : IEqualityComparer<TKey>);
342begin
343 inherited Create;
344 FDictionary := TDictionary<TKey,TValue>.Create( aCapacity, aComparer);
Jens Geyerd5436f52014-10-03 19:50:38 +0200345end;
346
347destructor TThriftDictionaryImpl<TKey, TValue>.Destroy;
348begin
Jens Geyerf5627532023-02-24 21:25:28 +0100349 FDictionary.Free;
Jens Geyerd5436f52014-10-03 19:50:38 +0200350 inherited;
351end;
352
353{$IF CompilerVersion >= 21.0}
354function TThriftDictionaryImpl<TKey, TValue>.ExtractPair( const Key: TKey): TPair<TKey, TValue>;
355begin
Jens Geyerf5627532023-02-24 21:25:28 +0100356 Result := FDictionary.ExtractPair( Key);
Jens Geyerd5436f52014-10-03 19:50:38 +0200357end;
358{$IFEND}
359
360function TThriftDictionaryImpl<TKey, TValue>.GetCount: Integer;
361begin
Jens Geyerf5627532023-02-24 21:25:28 +0100362 Result := FDictionary.Count;
Jens Geyerd5436f52014-10-03 19:50:38 +0200363end;
364
365function TThriftDictionaryImpl<TKey, TValue>.GetEnumerator: TEnumerator<TPair<TKey, TValue>>;
366begin
Jens Geyerf5627532023-02-24 21:25:28 +0100367 Result := FDictionary.GetEnumerator;
Jens Geyerd5436f52014-10-03 19:50:38 +0200368end;
369
370function TThriftDictionaryImpl<TKey, TValue>.GetItem(const Key: TKey): TValue;
371begin
Jens Geyerf5627532023-02-24 21:25:28 +0100372 Result := FDictionary.Items[Key];
Jens Geyerd5436f52014-10-03 19:50:38 +0200373end;
374
375function TThriftDictionaryImpl<TKey, TValue>.GetKeys: TDictionary<TKey, TValue>.TKeyCollection;
376begin
Jens Geyerf5627532023-02-24 21:25:28 +0100377 Result := FDictionary.Keys;
Jens Geyerd5436f52014-10-03 19:50:38 +0200378end;
379
380function TThriftDictionaryImpl<TKey, TValue>.GetValues: TDictionary<TKey, TValue>.TValueCollection;
381begin
Jens Geyerf5627532023-02-24 21:25:28 +0100382 Result := FDictionary.Values;
Jens Geyerd5436f52014-10-03 19:50:38 +0200383end;
384
385procedure TThriftDictionaryImpl<TKey, TValue>.Remove(const Key: TKey);
386begin
Jens Geyerf5627532023-02-24 21:25:28 +0100387 FDictionary.Remove( Key );
Jens Geyerd5436f52014-10-03 19:50:38 +0200388end;
389
390procedure TThriftDictionaryImpl<TKey, TValue>.SetItem(const Key: TKey;
391 const Value: TValue);
392begin
Jens Geyerf5627532023-02-24 21:25:28 +0100393 FDictionary.AddOrSetValue( Key, Value);
Jens Geyerd5436f52014-10-03 19:50:38 +0200394end;
395
396function TThriftDictionaryImpl<TKey, TValue>.ToArray: TArray<TPair<TKey, TValue>>;
397{$IF CompilerVersion < 22.0}
398var
399 x : TPair<TKey, TValue>;
400 i : Integer;
401{$IFEND}
402begin
403{$IF CompilerVersion < 22.0}
404 SetLength(Result, Count);
405 i := 0;
406 for x in FDictionaly do
407 begin
408 Result[i] := x;
409 Inc( i );
410 end;
411{$ELSE}
Jens Geyerf5627532023-02-24 21:25:28 +0100412 Result := FDictionary.ToArray;
Jens Geyerd5436f52014-10-03 19:50:38 +0200413{$IFEND}
414end;
415
Jens Geyer8f7487e2019-05-09 22:21:32 +0200416function TThriftDictionaryImpl<TKey, TValue>.ToString : string;
417var pair : TPair<TKey, TValue>;
418 sb : TThriftStringBuilder;
419 first : Boolean;
420begin
421 sb := TThriftStringBuilder.Create('{');
422 try
423 first := TRUE;
Jens Geyerf5627532023-02-24 21:25:28 +0100424 for pair in FDictionary do begin
Jens Geyer8f7487e2019-05-09 22:21:32 +0200425 if first
426 then first := FALSE
427 else sb.Append(', ');
428
429 sb.Append( '(');
430 sb.Append( StringUtils<TKey>.ToString(pair.Key));
431 sb.Append(' => ');
432 sb.Append( StringUtils<TValue>.ToString(pair.Value));
433 sb.Append(')');
434 end;
435 sb.Append('}');
436 Result := sb.ToString;
437 finally
438 sb.Free;
439 end;
440end;
441
Jens Geyerd5436f52014-10-03 19:50:38 +0200442procedure TThriftDictionaryImpl<TKey, TValue>.TrimExcess;
443begin
Jens Geyerf5627532023-02-24 21:25:28 +0100444 FDictionary.TrimExcess;
Jens Geyerd5436f52014-10-03 19:50:38 +0200445end;
446
447function TThriftDictionaryImpl<TKey, TValue>.TryGetValue(const Key: TKey;
448 out Value: TValue): Boolean;
449begin
Jens Geyerf5627532023-02-24 21:25:28 +0100450 Result := FDictionary.TryGetValue( Key, Value);
Jens Geyerd5436f52014-10-03 19:50:38 +0200451end;
452
453{ TThriftListImpl<T> }
454
455function TThriftListImpl<T>.Add(const Value: T): Integer;
456begin
457 Result := FList.Add( Value );
458end;
459
460procedure TThriftListImpl<T>.AddRange(Collection: TEnumerable<T>);
461begin
462 FList.AddRange( Collection );
463end;
464
465procedure TThriftListImpl<T>.AddRange(const Collection: IEnumerable<T>);
466begin
467 FList.AddRange( Collection );
468end;
469
470procedure TThriftListImpl<T>.AddRange(const Values: array of T);
471begin
472 FList.AddRange( Values );
473end;
474
475function TThriftListImpl<T>.BinarySearch(const Item: T;
476 out Index: Integer): Boolean;
477begin
478 Result := FList.BinarySearch( Item, Index);
479end;
480
481function TThriftListImpl<T>.BinarySearch(const Item: T; out Index: Integer;
482 const AComparer: IComparer<T>): Boolean;
483begin
484 Result := FList.BinarySearch( Item, Index, AComparer);
485end;
486
487procedure TThriftListImpl<T>.Clear;
488begin
489 FList.Clear;
490end;
491
492function TThriftListImpl<T>.Contains(const Value: T): Boolean;
493begin
494 Result := FList.Contains( Value );
495end;
496
Jens Geyerf5627532023-02-24 21:25:28 +0100497constructor TThriftListImpl<T>.Create( const aCapacity: Integer);
Jens Geyerd5436f52014-10-03 19:50:38 +0200498begin
Jens Geyerf5627532023-02-24 21:25:28 +0100499 inherited Create;
Jens Geyerd5436f52014-10-03 19:50:38 +0200500 FList := TList<T>.Create;
Jens Geyerf5627532023-02-24 21:25:28 +0100501
502 if aCapacity > 0
503 then FList.Capacity := aCapacity;
Jens Geyerd5436f52014-10-03 19:50:38 +0200504end;
505
506procedure TThriftListImpl<T>.Delete(Index: Integer);
507begin
508 FList.Delete( Index )
509end;
510
511procedure TThriftListImpl<T>.DeleteRange(AIndex, ACount: Integer);
512begin
513 FList.DeleteRange( AIndex, ACount)
514end;
515
516destructor TThriftListImpl<T>.Destroy;
517begin
518 FList.Free;
519 inherited;
520end;
521
522{$IF CompilerVersion >= 21.0}
523procedure TThriftListImpl<T>.Exchange(Index1, Index2: Integer);
524begin
525 FList.Exchange( Index1, Index2 )
526end;
527{$IFEND}
528
529function TThriftListImpl<T>.Extract(const Value: T): T;
530begin
531 Result := FList.Extract( Value )
532end;
533
534{$IF CompilerVersion >= 21.0}
535function TThriftListImpl<T>.First: T;
536begin
537 Result := FList.First;
538end;
539{$IFEND}
540
541function TThriftListImpl<T>.GetCapacity: Integer;
542begin
543 Result := FList.Capacity;
544end;
545
546function TThriftListImpl<T>.GetCount: Integer;
547begin
548 Result := FList.Count;
549end;
550
551function TThriftListImpl<T>.GetEnumerator: TEnumerator<T>;
552begin
553 Result := FList.GetEnumerator;
554end;
555
556function TThriftListImpl<T>.GetItem(Index: Integer): T;
557begin
558 Result := FList[Index];
559end;
560
561function TThriftListImpl<T>.IndexOf(const Value: T): Integer;
562begin
563 Result := FList.IndexOf( Value );
564end;
565
566procedure TThriftListImpl<T>.Insert(Index: Integer; const Value: T);
567begin
568 FList.Insert( Index, Value);
569end;
570
571procedure TThriftListImpl<T>.InsertRange(Index: Integer;
572 const Collection: TEnumerable<T>);
573begin
574 FList.InsertRange( Index, Collection );
575end;
576
577procedure TThriftListImpl<T>.InsertRange(Index: Integer;
578 const Values: array of T);
579begin
580 FList.InsertRange( Index, Values);
581end;
582
583procedure TThriftListImpl<T>.InsertRange(Index: Integer;
584 const Collection: IEnumerable<T>);
585begin
586 FList.InsertRange( Index, Collection );
587end;
588
589{$IF CompilerVersion >= 21.0}
590function TThriftListImpl<T>.Last: T;
591begin
592 Result := FList.Last;
593end;
594{$IFEND}
595
596function TThriftListImpl<T>.LastIndexOf(const Value: T): Integer;
597begin
598 Result := FList.LastIndexOf( Value );
599end;
600
601{$IF CompilerVersion >= 21.0}
602procedure TThriftListImpl<T>.Move(CurIndex, NewIndex: Integer);
603begin
604 FList.Move( CurIndex, NewIndex);
605end;
606{$IFEND}
607
608function TThriftListImpl<T>.Remove(const Value: T): Integer;
609begin
610 Result := FList.Remove( Value );
611end;
612
613procedure TThriftListImpl<T>.Reverse;
614begin
615 FList.Reverse;
616end;
617
618procedure TThriftListImpl<T>.SetCapacity(Value: Integer);
619begin
620 FList.Capacity := Value;
621end;
622
623procedure TThriftListImpl<T>.SetCount(Value: Integer);
624begin
625 FList.Count := Value;
626end;
627
628procedure TThriftListImpl<T>.SetItem(Index: Integer; const Value: T);
629begin
630 FList[Index] := Value;
631end;
632
633procedure TThriftListImpl<T>.Sort;
634begin
635 FList.Sort;
636end;
637
638procedure TThriftListImpl<T>.Sort(const AComparer: IComparer<T>);
639begin
Alex-Rud693e19c2019-07-30 14:51:56 +0300640 FList.Sort(AComparer);
Jens Geyerd5436f52014-10-03 19:50:38 +0200641end;
642
643function TThriftListImpl<T>.ToArray: TArray<T>;
644{$IF CompilerVersion < 22.0}
645var
646 x : T;
647 i : Integer;
648{$IFEND}
649begin
650{$IF CompilerVersion < 22.0}
651 SetLength(Result, Count);
652 i := 0;
653 for x in FList do
654 begin
655 Result[i] := x;
656 Inc( i );
657 end;
658{$ELSE}
659 Result := FList.ToArray;
660{$IFEND}
661end;
662
Jens Geyer8f7487e2019-05-09 22:21:32 +0200663function TThriftListImpl<T>.ToString : string;
664var elm : T;
665 sb : TThriftStringBuilder;
666 first : Boolean;
667begin
668 sb := TThriftStringBuilder.Create('{');
669 try
670 first := TRUE;
671 for elm in FList do begin
672 if first
673 then first := FALSE
674 else sb.Append(', ');
675
676 sb.Append( StringUtils<T>.ToString(elm));
677 end;
678 sb.Append('}');
679 Result := sb.ToString;
680 finally
681 sb.Free;
682 end;
683end;
684
Jens Geyerd5436f52014-10-03 19:50:38 +0200685procedure TThriftListImpl<T>.TrimExcess;
686begin
687 FList.TrimExcess;
688end;
689
690end.