blob: 2cb2395adcc6e73ae074465c185691196120a610 [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 Geyer683263d2022-09-03 18:52:35 +0200194 IThriftHashSet<TValue> = interface(IThriftContainer)
Jens Geyerd5436f52014-10-03 19:50:38 +0200195 ['{0923A3B5-D4D4-48A8-91AD-40238E2EAD66}']
196 function GetEnumerator: TEnumerator<TValue>;
197 function GetIsReadOnly: Boolean;
198 function GetCount: Integer;
199 property Count: Integer read GetCount;
200 property IsReadOnly: Boolean read GetIsReadOnly;
201 procedure Add( const item: TValue);
202 procedure Clear;
203 function Contains( const item: TValue): Boolean;
204 procedure CopyTo(var A: TArray<TValue>; arrayIndex: Integer);
205 function Remove( const item: TValue ): Boolean;
206 end;
207
Jens Geyer683263d2022-09-03 18:52:35 +0200208 // compatibility
209 IHashSet<TValue> = interface( IThriftHashSet<TValue>)
210 ['{C3CF557F-21D9-4524-B899-D3145B0389BB}']
211 end deprecated 'use IThriftHashSet<T>';
212
213
214 {$WARN SYMBOL_DEPRECATED OFF}
215 TThriftHashSetImpl<TValue> = class( TInterfacedObject, IHashSet<TValue>, IThriftHashSet<TValue>, IThriftContainer, ISupportsToString)
216 {$WARN SYMBOL_DEPRECATED DEFAULT}
Jens Geyerfad7fd32019-11-09 23:24:52 +0100217 strict private
Jens Geyerd5436f52014-10-03 19:50:38 +0200218 FDictionary : IThriftDictionary<TValue,Integer>;
219 FIsReadOnly: Boolean;
Jens Geyerfad7fd32019-11-09 23:24:52 +0100220 strict protected
Jens Geyerd5436f52014-10-03 19:50:38 +0200221 function GetEnumerator: TEnumerator<TValue>;
222 function GetIsReadOnly: Boolean;
223 function GetCount: Integer;
224 property Count: Integer read GetCount;
225 property IsReadOnly: Boolean read FIsReadOnly;
226 procedure Add( const item: TValue);
227 procedure Clear;
228 function Contains( const item: TValue): Boolean;
229 procedure CopyTo(var A: TArray<TValue>; arrayIndex: Integer);
230 function Remove( const item: TValue ): Boolean;
231 public
Jens Geyerf5627532023-02-24 21:25:28 +0100232 constructor Create( const aCapacity: Integer = 0); overload;
233 constructor Create( const aCapacity: Integer; const aComparer : IEqualityComparer<TValue>); overload;
Jens Geyer8f7487e2019-05-09 22:21:32 +0200234 function ToString : string; override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200235 end;
236
Jens Geyer683263d2022-09-03 18:52:35 +0200237 // compatibility
238 THashSetImpl<TValue> = class( TThriftHashSetImpl<TValue>)
239 end deprecated 'use TThriftHashSetImpl<T>';
240
Jens Geyerd5436f52014-10-03 19:50:38 +0200241implementation
242
Jens Geyer683263d2022-09-03 18:52:35 +0200243{ TThriftHashSetImpl<TValue>. }
Jens Geyerd5436f52014-10-03 19:50:38 +0200244
Jens Geyer683263d2022-09-03 18:52:35 +0200245procedure TThriftHashSetImpl<TValue>.Add( const item: TValue);
Jens Geyerd5436f52014-10-03 19:50:38 +0200246begin
247 if not FDictionary.ContainsKey(item) then
248 begin
249 FDictionary.Add( item, 0);
250 end;
251end;
252
Jens Geyer683263d2022-09-03 18:52:35 +0200253procedure TThriftHashSetImpl<TValue>.Clear;
Jens Geyerd5436f52014-10-03 19:50:38 +0200254begin
255 FDictionary.Clear;
256end;
257
Jens Geyer683263d2022-09-03 18:52:35 +0200258function TThriftHashSetImpl<TValue>.Contains( const item: TValue): Boolean;
Jens Geyerd5436f52014-10-03 19:50:38 +0200259begin
260 Result := FDictionary.ContainsKey(item);
261end;
262
Jens Geyer683263d2022-09-03 18:52:35 +0200263procedure TThriftHashSetImpl<TValue>.CopyTo(var A: TArray<TValue>; arrayIndex: Integer);
Jens Geyerd5436f52014-10-03 19:50:38 +0200264var
265 i : Integer;
266 Enumlator : TEnumerator<TValue>;
267begin
268 Enumlator := GetEnumerator;
269 while Enumlator.MoveNext do
270 begin
271 A[arrayIndex] := Enumlator.Current;
272 Inc(arrayIndex);
273 end;
274end;
275
Jens Geyerf5627532023-02-24 21:25:28 +0100276constructor TThriftHashSetImpl<TValue>.Create( const aCapacity: Integer);
Jens Geyerd5436f52014-10-03 19:50:38 +0200277begin
Jens Geyerf5627532023-02-24 21:25:28 +0100278 inherited Create;
279 FDictionary := TThriftDictionaryImpl<TValue,Integer>.Create( aCapacity);
280end;
281
282constructor TThriftHashSetImpl<TValue>.Create( const aCapacity: Integer; const aComparer : IEqualityComparer<TValue>);
283begin
284 inherited Create;
285 FDictionary := TThriftDictionaryImpl<TValue,Integer>.Create( aCapacity, aComparer);
Jens Geyerd5436f52014-10-03 19:50:38 +0200286end;
287
Jens Geyer683263d2022-09-03 18:52:35 +0200288function TThriftHashSetImpl<TValue>.GetCount: Integer;
Jens Geyerd5436f52014-10-03 19:50:38 +0200289begin
290 Result := FDictionary.Count;
291end;
292
Jens Geyer683263d2022-09-03 18:52:35 +0200293function TThriftHashSetImpl<TValue>.GetEnumerator: TEnumerator<TValue>;
Jens Geyerd5436f52014-10-03 19:50:38 +0200294begin
295 Result := FDictionary.Keys.GetEnumerator;
296end;
297
Jens Geyer683263d2022-09-03 18:52:35 +0200298function TThriftHashSetImpl<TValue>.GetIsReadOnly: Boolean;
Jens Geyerd5436f52014-10-03 19:50:38 +0200299begin
300 Result := FIsReadOnly;
301end;
302
Jens Geyer683263d2022-09-03 18:52:35 +0200303function TThriftHashSetImpl<TValue>.Remove( const item: TValue): Boolean;
Jens Geyerd5436f52014-10-03 19:50:38 +0200304begin
305 Result := False;
306 if FDictionary.ContainsKey( item ) then
307 begin
308 FDictionary.Remove( item );
309 Result := not FDictionary.ContainsKey( item );
310 end;
311end;
312
Jens Geyer683263d2022-09-03 18:52:35 +0200313function TThriftHashSetImpl<TValue>.ToString : string;
Jens Geyer8f7487e2019-05-09 22:21:32 +0200314var elm : TValue;
315 sb : TThriftStringBuilder;
316 first : Boolean;
317begin
318 sb := TThriftStringBuilder.Create('{');
319 try
320 first := TRUE;
321 for elm in FDictionary.Keys do begin
322 if first
323 then first := FALSE
324 else sb.Append(', ');
325
326 sb.Append( StringUtils<TValue>.ToString(elm));
327 end;
328 sb.Append('}');
329 Result := sb.ToString;
330 finally
331 sb.Free;
332 end;
333end;
334
Jens Geyerd5436f52014-10-03 19:50:38 +0200335{ TThriftDictionaryImpl<TKey, TValue> }
336
337procedure TThriftDictionaryImpl<TKey, TValue>.Add(const Key: TKey;
338 const Value: TValue);
339begin
Jens Geyerf5627532023-02-24 21:25:28 +0100340 FDictionary.Add( Key, Value);
Jens Geyerd5436f52014-10-03 19:50:38 +0200341end;
342
343procedure TThriftDictionaryImpl<TKey, TValue>.AddOrSetValue(const Key: TKey;
344 const Value: TValue);
345begin
Jens Geyerf5627532023-02-24 21:25:28 +0100346 FDictionary.AddOrSetValue( Key, Value);
Jens Geyerd5436f52014-10-03 19:50:38 +0200347end;
348
349procedure TThriftDictionaryImpl<TKey, TValue>.Clear;
350begin
Jens Geyerf5627532023-02-24 21:25:28 +0100351 FDictionary.Clear;
Jens Geyerd5436f52014-10-03 19:50:38 +0200352end;
353
354function TThriftDictionaryImpl<TKey, TValue>.ContainsKey(
355 const Key: TKey): Boolean;
356begin
Jens Geyerf5627532023-02-24 21:25:28 +0100357 Result := FDictionary.ContainsKey( Key );
Jens Geyerd5436f52014-10-03 19:50:38 +0200358end;
359
360function TThriftDictionaryImpl<TKey, TValue>.ContainsValue(
361 const Value: TValue): Boolean;
362begin
Jens Geyerf5627532023-02-24 21:25:28 +0100363 Result := FDictionary.ContainsValue( Value );
Jens Geyerd5436f52014-10-03 19:50:38 +0200364end;
365
Jens Geyerf5627532023-02-24 21:25:28 +0100366constructor TThriftDictionaryImpl<TKey, TValue>.Create(const aCapacity: Integer);
Jens Geyerd5436f52014-10-03 19:50:38 +0200367begin
368 inherited Create;
Jens Geyerf5627532023-02-24 21:25:28 +0100369 FDictionary := TDictionary<TKey,TValue>.Create( aCapacity);
370end;
371
372constructor TThriftDictionaryImpl<TKey, TValue>.Create(const aCapacity: Integer; const aComparer : IEqualityComparer<TKey>);
373begin
374 inherited Create;
375 FDictionary := TDictionary<TKey,TValue>.Create( aCapacity, aComparer);
Jens Geyerd5436f52014-10-03 19:50:38 +0200376end;
377
378destructor TThriftDictionaryImpl<TKey, TValue>.Destroy;
379begin
Jens Geyerf5627532023-02-24 21:25:28 +0100380 FDictionary.Free;
Jens Geyerd5436f52014-10-03 19:50:38 +0200381 inherited;
382end;
383
384{$IF CompilerVersion >= 21.0}
385function TThriftDictionaryImpl<TKey, TValue>.ExtractPair( const Key: TKey): TPair<TKey, TValue>;
386begin
Jens Geyerf5627532023-02-24 21:25:28 +0100387 Result := FDictionary.ExtractPair( Key);
Jens Geyerd5436f52014-10-03 19:50:38 +0200388end;
389{$IFEND}
390
391function TThriftDictionaryImpl<TKey, TValue>.GetCount: Integer;
392begin
Jens Geyerf5627532023-02-24 21:25:28 +0100393 Result := FDictionary.Count;
Jens Geyerd5436f52014-10-03 19:50:38 +0200394end;
395
396function TThriftDictionaryImpl<TKey, TValue>.GetEnumerator: TEnumerator<TPair<TKey, TValue>>;
397begin
Jens Geyerf5627532023-02-24 21:25:28 +0100398 Result := FDictionary.GetEnumerator;
Jens Geyerd5436f52014-10-03 19:50:38 +0200399end;
400
401function TThriftDictionaryImpl<TKey, TValue>.GetItem(const Key: TKey): TValue;
402begin
Jens Geyerf5627532023-02-24 21:25:28 +0100403 Result := FDictionary.Items[Key];
Jens Geyerd5436f52014-10-03 19:50:38 +0200404end;
405
406function TThriftDictionaryImpl<TKey, TValue>.GetKeys: TDictionary<TKey, TValue>.TKeyCollection;
407begin
Jens Geyerf5627532023-02-24 21:25:28 +0100408 Result := FDictionary.Keys;
Jens Geyerd5436f52014-10-03 19:50:38 +0200409end;
410
411function TThriftDictionaryImpl<TKey, TValue>.GetValues: TDictionary<TKey, TValue>.TValueCollection;
412begin
Jens Geyerf5627532023-02-24 21:25:28 +0100413 Result := FDictionary.Values;
Jens Geyerd5436f52014-10-03 19:50:38 +0200414end;
415
416procedure TThriftDictionaryImpl<TKey, TValue>.Remove(const Key: TKey);
417begin
Jens Geyerf5627532023-02-24 21:25:28 +0100418 FDictionary.Remove( Key );
Jens Geyerd5436f52014-10-03 19:50:38 +0200419end;
420
421procedure TThriftDictionaryImpl<TKey, TValue>.SetItem(const Key: TKey;
422 const Value: TValue);
423begin
Jens Geyerf5627532023-02-24 21:25:28 +0100424 FDictionary.AddOrSetValue( Key, Value);
Jens Geyerd5436f52014-10-03 19:50:38 +0200425end;
426
427function TThriftDictionaryImpl<TKey, TValue>.ToArray: TArray<TPair<TKey, TValue>>;
428{$IF CompilerVersion < 22.0}
429var
430 x : TPair<TKey, TValue>;
431 i : Integer;
432{$IFEND}
433begin
434{$IF CompilerVersion < 22.0}
435 SetLength(Result, Count);
436 i := 0;
437 for x in FDictionaly do
438 begin
439 Result[i] := x;
440 Inc( i );
441 end;
442{$ELSE}
Jens Geyerf5627532023-02-24 21:25:28 +0100443 Result := FDictionary.ToArray;
Jens Geyerd5436f52014-10-03 19:50:38 +0200444{$IFEND}
445end;
446
Jens Geyer8f7487e2019-05-09 22:21:32 +0200447function TThriftDictionaryImpl<TKey, TValue>.ToString : string;
448var pair : TPair<TKey, TValue>;
449 sb : TThriftStringBuilder;
450 first : Boolean;
451begin
452 sb := TThriftStringBuilder.Create('{');
453 try
454 first := TRUE;
Jens Geyerf5627532023-02-24 21:25:28 +0100455 for pair in FDictionary do begin
Jens Geyer8f7487e2019-05-09 22:21:32 +0200456 if first
457 then first := FALSE
458 else sb.Append(', ');
459
460 sb.Append( '(');
461 sb.Append( StringUtils<TKey>.ToString(pair.Key));
462 sb.Append(' => ');
463 sb.Append( StringUtils<TValue>.ToString(pair.Value));
464 sb.Append(')');
465 end;
466 sb.Append('}');
467 Result := sb.ToString;
468 finally
469 sb.Free;
470 end;
471end;
472
Jens Geyerd5436f52014-10-03 19:50:38 +0200473procedure TThriftDictionaryImpl<TKey, TValue>.TrimExcess;
474begin
Jens Geyerf5627532023-02-24 21:25:28 +0100475 FDictionary.TrimExcess;
Jens Geyerd5436f52014-10-03 19:50:38 +0200476end;
477
478function TThriftDictionaryImpl<TKey, TValue>.TryGetValue(const Key: TKey;
479 out Value: TValue): Boolean;
480begin
Jens Geyerf5627532023-02-24 21:25:28 +0100481 Result := FDictionary.TryGetValue( Key, Value);
Jens Geyerd5436f52014-10-03 19:50:38 +0200482end;
483
484{ TThriftListImpl<T> }
485
486function TThriftListImpl<T>.Add(const Value: T): Integer;
487begin
488 Result := FList.Add( Value );
489end;
490
491procedure TThriftListImpl<T>.AddRange(Collection: TEnumerable<T>);
492begin
493 FList.AddRange( Collection );
494end;
495
496procedure TThriftListImpl<T>.AddRange(const Collection: IEnumerable<T>);
497begin
498 FList.AddRange( Collection );
499end;
500
501procedure TThriftListImpl<T>.AddRange(const Values: array of T);
502begin
503 FList.AddRange( Values );
504end;
505
506function TThriftListImpl<T>.BinarySearch(const Item: T;
507 out Index: Integer): Boolean;
508begin
509 Result := FList.BinarySearch( Item, Index);
510end;
511
512function TThriftListImpl<T>.BinarySearch(const Item: T; out Index: Integer;
513 const AComparer: IComparer<T>): Boolean;
514begin
515 Result := FList.BinarySearch( Item, Index, AComparer);
516end;
517
518procedure TThriftListImpl<T>.Clear;
519begin
520 FList.Clear;
521end;
522
523function TThriftListImpl<T>.Contains(const Value: T): Boolean;
524begin
525 Result := FList.Contains( Value );
526end;
527
Jens Geyerf5627532023-02-24 21:25:28 +0100528constructor TThriftListImpl<T>.Create( const aCapacity: Integer);
Jens Geyerd5436f52014-10-03 19:50:38 +0200529begin
Jens Geyerf5627532023-02-24 21:25:28 +0100530 inherited Create;
Jens Geyerd5436f52014-10-03 19:50:38 +0200531 FList := TList<T>.Create;
Jens Geyerf5627532023-02-24 21:25:28 +0100532
533 if aCapacity > 0
534 then FList.Capacity := aCapacity;
Jens Geyerd5436f52014-10-03 19:50:38 +0200535end;
536
537procedure TThriftListImpl<T>.Delete(Index: Integer);
538begin
539 FList.Delete( Index )
540end;
541
542procedure TThriftListImpl<T>.DeleteRange(AIndex, ACount: Integer);
543begin
544 FList.DeleteRange( AIndex, ACount)
545end;
546
547destructor TThriftListImpl<T>.Destroy;
548begin
549 FList.Free;
550 inherited;
551end;
552
553{$IF CompilerVersion >= 21.0}
554procedure TThriftListImpl<T>.Exchange(Index1, Index2: Integer);
555begin
556 FList.Exchange( Index1, Index2 )
557end;
558{$IFEND}
559
560function TThriftListImpl<T>.Extract(const Value: T): T;
561begin
562 Result := FList.Extract( Value )
563end;
564
565{$IF CompilerVersion >= 21.0}
566function TThriftListImpl<T>.First: T;
567begin
568 Result := FList.First;
569end;
570{$IFEND}
571
572function TThriftListImpl<T>.GetCapacity: Integer;
573begin
574 Result := FList.Capacity;
575end;
576
577function TThriftListImpl<T>.GetCount: Integer;
578begin
579 Result := FList.Count;
580end;
581
582function TThriftListImpl<T>.GetEnumerator: TEnumerator<T>;
583begin
584 Result := FList.GetEnumerator;
585end;
586
587function TThriftListImpl<T>.GetItem(Index: Integer): T;
588begin
589 Result := FList[Index];
590end;
591
592function TThriftListImpl<T>.IndexOf(const Value: T): Integer;
593begin
594 Result := FList.IndexOf( Value );
595end;
596
597procedure TThriftListImpl<T>.Insert(Index: Integer; const Value: T);
598begin
599 FList.Insert( Index, Value);
600end;
601
602procedure TThriftListImpl<T>.InsertRange(Index: Integer;
603 const Collection: TEnumerable<T>);
604begin
605 FList.InsertRange( Index, Collection );
606end;
607
608procedure TThriftListImpl<T>.InsertRange(Index: Integer;
609 const Values: array of T);
610begin
611 FList.InsertRange( Index, Values);
612end;
613
614procedure TThriftListImpl<T>.InsertRange(Index: Integer;
615 const Collection: IEnumerable<T>);
616begin
617 FList.InsertRange( Index, Collection );
618end;
619
620{$IF CompilerVersion >= 21.0}
621function TThriftListImpl<T>.Last: T;
622begin
623 Result := FList.Last;
624end;
625{$IFEND}
626
627function TThriftListImpl<T>.LastIndexOf(const Value: T): Integer;
628begin
629 Result := FList.LastIndexOf( Value );
630end;
631
632{$IF CompilerVersion >= 21.0}
633procedure TThriftListImpl<T>.Move(CurIndex, NewIndex: Integer);
634begin
635 FList.Move( CurIndex, NewIndex);
636end;
637{$IFEND}
638
639function TThriftListImpl<T>.Remove(const Value: T): Integer;
640begin
641 Result := FList.Remove( Value );
642end;
643
644procedure TThriftListImpl<T>.Reverse;
645begin
646 FList.Reverse;
647end;
648
649procedure TThriftListImpl<T>.SetCapacity(Value: Integer);
650begin
651 FList.Capacity := Value;
652end;
653
654procedure TThriftListImpl<T>.SetCount(Value: Integer);
655begin
656 FList.Count := Value;
657end;
658
659procedure TThriftListImpl<T>.SetItem(Index: Integer; const Value: T);
660begin
661 FList[Index] := Value;
662end;
663
664procedure TThriftListImpl<T>.Sort;
665begin
666 FList.Sort;
667end;
668
669procedure TThriftListImpl<T>.Sort(const AComparer: IComparer<T>);
670begin
Alex-Rud693e19c2019-07-30 14:51:56 +0300671 FList.Sort(AComparer);
Jens Geyerd5436f52014-10-03 19:50:38 +0200672end;
673
674function TThriftListImpl<T>.ToArray: TArray<T>;
675{$IF CompilerVersion < 22.0}
676var
677 x : T;
678 i : Integer;
679{$IFEND}
680begin
681{$IF CompilerVersion < 22.0}
682 SetLength(Result, Count);
683 i := 0;
684 for x in FList do
685 begin
686 Result[i] := x;
687 Inc( i );
688 end;
689{$ELSE}
690 Result := FList.ToArray;
691{$IFEND}
692end;
693
Jens Geyer8f7487e2019-05-09 22:21:32 +0200694function TThriftListImpl<T>.ToString : string;
695var elm : T;
696 sb : TThriftStringBuilder;
697 first : Boolean;
698begin
699 sb := TThriftStringBuilder.Create('{');
700 try
701 first := TRUE;
702 for elm in FList do begin
703 if first
704 then first := FALSE
705 else sb.Append(', ');
706
707 sb.Append( StringUtils<T>.ToString(elm));
708 end;
709 sb.Append('}');
710 Result := sb.ToString;
711 finally
712 sb.Free;
713 end;
714end;
715
Jens Geyerd5436f52014-10-03 19:50:38 +0200716procedure TThriftListImpl<T>.TrimExcess;
717begin
718 FList.TrimExcess;
719end;
720
721end.