blob: 0500d4936a2215eb8698f2b4f30057f1570f7606 [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 Geyer9c0bdcb2024-05-25 08:58:48 +0200227 destructor Destroy; override;
Jens Geyer8f7487e2019-05-09 22:21:32 +0200228 function ToString : string; override;
Jens Geyerd5436f52014-10-03 19:50:38 +0200229 end;
230
Jens Geyer683263d2022-09-03 18:52:35 +0200231 // compatibility
Jens Geyerda5ef362024-05-24 21:09:42 +0200232 THashSetImpl<T> = class( TThriftHashSetImpl<T>)
Jens Geyer683263d2022-09-03 18:52:35 +0200233 end deprecated 'use TThriftHashSetImpl<T>';
234
Jens Geyerd5436f52014-10-03 19:50:38 +0200235implementation
236
Jens Geyerda5ef362024-05-24 21:09:42 +0200237{ TThriftHashSetImpl<T>. }
Jens Geyerd5436f52014-10-03 19:50:38 +0200238
Jens Geyerda5ef362024-05-24 21:09:42 +0200239function TThriftHashSetImpl<T>.Add( const item: T) : Boolean;
Jens Geyerd5436f52014-10-03 19:50:38 +0200240begin
Jens Geyerda5ef362024-05-24 21:09:42 +0200241 result := not FDictionary.ContainsKey(item);
242 if result then FDictionary.Add( item, 0);
Jens Geyerd5436f52014-10-03 19:50:38 +0200243end;
244
Jens Geyerda5ef362024-05-24 21:09:42 +0200245procedure TThriftHashSetImpl<T>.Clear;
Jens Geyerd5436f52014-10-03 19:50:38 +0200246begin
247 FDictionary.Clear;
248end;
249
Jens Geyerda5ef362024-05-24 21:09:42 +0200250function TThriftHashSetImpl<T>.Contains( const item: T): Boolean;
Jens Geyerd5436f52014-10-03 19:50:38 +0200251begin
252 Result := FDictionary.ContainsKey(item);
253end;
254
Jens Geyerda5ef362024-05-24 21:09:42 +0200255constructor TThriftHashSetImpl<T>.Create( const aCapacity: Integer);
Jens Geyerd5436f52014-10-03 19:50:38 +0200256begin
Jens Geyerf5627532023-02-24 21:25:28 +0100257 inherited Create;
Jens Geyerda5ef362024-05-24 21:09:42 +0200258 FDictionary := TDictionary<T,Byte>.Create( aCapacity);
Jens Geyerf5627532023-02-24 21:25:28 +0100259end;
260
Jens Geyerda5ef362024-05-24 21:09:42 +0200261constructor TThriftHashSetImpl<T>.Create( const aCapacity: Integer; const aComparer : IEqualityComparer<T>);
Jens Geyerf5627532023-02-24 21:25:28 +0100262begin
263 inherited Create;
Jens Geyerda5ef362024-05-24 21:09:42 +0200264 FDictionary := TDictionary<T,Byte>.Create( aCapacity, aComparer);
Jens Geyerd5436f52014-10-03 19:50:38 +0200265end;
266
Jens Geyer9c0bdcb2024-05-25 08:58:48 +0200267
268destructor TThriftHashSetImpl<T>.Destroy;
269begin
270 FDictionary.Free;
271 inherited Destroy;
272end;
273
274
Jens Geyerda5ef362024-05-24 21:09:42 +0200275function TThriftHashSetImpl<T>.GetCount: Integer;
Jens Geyerd5436f52014-10-03 19:50:38 +0200276begin
277 Result := FDictionary.Count;
278end;
279
Jens Geyerda5ef362024-05-24 21:09:42 +0200280function TThriftHashSetImpl<T>.GetEnumerator: TEnumerator<T>;
Jens Geyerd5436f52014-10-03 19:50:38 +0200281begin
282 Result := FDictionary.Keys.GetEnumerator;
283end;
284
Jens Geyerda5ef362024-05-24 21:09:42 +0200285function TThriftHashSetImpl<T>.Remove( const item: T): Boolean;
Jens Geyerd5436f52014-10-03 19:50:38 +0200286begin
Jens Geyerda5ef362024-05-24 21:09:42 +0200287 Result := FDictionary.ContainsKey( item);
288 if Result then FDictionary.Remove( item );
Jens Geyerd5436f52014-10-03 19:50:38 +0200289end;
290
Jens Geyerda5ef362024-05-24 21:09:42 +0200291function TThriftHashSetImpl<T>.ToString : string;
292var elm : T;
Jens Geyer8f7487e2019-05-09 22:21:32 +0200293 sb : TThriftStringBuilder;
294 first : Boolean;
295begin
296 sb := TThriftStringBuilder.Create('{');
297 try
298 first := TRUE;
299 for elm in FDictionary.Keys do begin
300 if first
301 then first := FALSE
302 else sb.Append(', ');
303
Jens Geyerda5ef362024-05-24 21:09:42 +0200304 sb.Append( StringUtils<T>.ToString(elm));
Jens Geyer8f7487e2019-05-09 22:21:32 +0200305 end;
306 sb.Append('}');
307 Result := sb.ToString;
308 finally
309 sb.Free;
310 end;
311end;
312
Jens Geyerd5436f52014-10-03 19:50:38 +0200313{ TThriftDictionaryImpl<TKey, TValue> }
314
315procedure TThriftDictionaryImpl<TKey, TValue>.Add(const Key: TKey;
316 const Value: TValue);
317begin
Jens Geyerf5627532023-02-24 21:25:28 +0100318 FDictionary.Add( Key, Value);
Jens Geyerd5436f52014-10-03 19:50:38 +0200319end;
320
321procedure TThriftDictionaryImpl<TKey, TValue>.AddOrSetValue(const Key: TKey;
322 const Value: TValue);
323begin
Jens Geyerf5627532023-02-24 21:25:28 +0100324 FDictionary.AddOrSetValue( Key, Value);
Jens Geyerd5436f52014-10-03 19:50:38 +0200325end;
326
327procedure TThriftDictionaryImpl<TKey, TValue>.Clear;
328begin
Jens Geyerf5627532023-02-24 21:25:28 +0100329 FDictionary.Clear;
Jens Geyerd5436f52014-10-03 19:50:38 +0200330end;
331
332function TThriftDictionaryImpl<TKey, TValue>.ContainsKey(
333 const Key: TKey): Boolean;
334begin
Jens Geyerf5627532023-02-24 21:25:28 +0100335 Result := FDictionary.ContainsKey( Key );
Jens Geyerd5436f52014-10-03 19:50:38 +0200336end;
337
338function TThriftDictionaryImpl<TKey, TValue>.ContainsValue(
339 const Value: TValue): Boolean;
340begin
Jens Geyerf5627532023-02-24 21:25:28 +0100341 Result := FDictionary.ContainsValue( Value );
Jens Geyerd5436f52014-10-03 19:50:38 +0200342end;
343
Jens Geyerf5627532023-02-24 21:25:28 +0100344constructor TThriftDictionaryImpl<TKey, TValue>.Create(const aCapacity: Integer);
Jens Geyerd5436f52014-10-03 19:50:38 +0200345begin
346 inherited Create;
Jens Geyerf5627532023-02-24 21:25:28 +0100347 FDictionary := TDictionary<TKey,TValue>.Create( aCapacity);
348end;
349
350constructor TThriftDictionaryImpl<TKey, TValue>.Create(const aCapacity: Integer; const aComparer : IEqualityComparer<TKey>);
351begin
352 inherited Create;
353 FDictionary := TDictionary<TKey,TValue>.Create( aCapacity, aComparer);
Jens Geyerd5436f52014-10-03 19:50:38 +0200354end;
355
356destructor TThriftDictionaryImpl<TKey, TValue>.Destroy;
357begin
Jens Geyerf5627532023-02-24 21:25:28 +0100358 FDictionary.Free;
Jens Geyerd5436f52014-10-03 19:50:38 +0200359 inherited;
360end;
361
362{$IF CompilerVersion >= 21.0}
363function TThriftDictionaryImpl<TKey, TValue>.ExtractPair( const Key: TKey): TPair<TKey, TValue>;
364begin
Jens Geyerf5627532023-02-24 21:25:28 +0100365 Result := FDictionary.ExtractPair( Key);
Jens Geyerd5436f52014-10-03 19:50:38 +0200366end;
367{$IFEND}
368
369function TThriftDictionaryImpl<TKey, TValue>.GetCount: Integer;
370begin
Jens Geyerf5627532023-02-24 21:25:28 +0100371 Result := FDictionary.Count;
Jens Geyerd5436f52014-10-03 19:50:38 +0200372end;
373
374function TThriftDictionaryImpl<TKey, TValue>.GetEnumerator: TEnumerator<TPair<TKey, TValue>>;
375begin
Jens Geyerf5627532023-02-24 21:25:28 +0100376 Result := FDictionary.GetEnumerator;
Jens Geyerd5436f52014-10-03 19:50:38 +0200377end;
378
379function TThriftDictionaryImpl<TKey, TValue>.GetItem(const Key: TKey): TValue;
380begin
Jens Geyerf5627532023-02-24 21:25:28 +0100381 Result := FDictionary.Items[Key];
Jens Geyerd5436f52014-10-03 19:50:38 +0200382end;
383
384function TThriftDictionaryImpl<TKey, TValue>.GetKeys: TDictionary<TKey, TValue>.TKeyCollection;
385begin
Jens Geyerf5627532023-02-24 21:25:28 +0100386 Result := FDictionary.Keys;
Jens Geyerd5436f52014-10-03 19:50:38 +0200387end;
388
389function TThriftDictionaryImpl<TKey, TValue>.GetValues: TDictionary<TKey, TValue>.TValueCollection;
390begin
Jens Geyerf5627532023-02-24 21:25:28 +0100391 Result := FDictionary.Values;
Jens Geyerd5436f52014-10-03 19:50:38 +0200392end;
393
394procedure TThriftDictionaryImpl<TKey, TValue>.Remove(const Key: TKey);
395begin
Jens Geyerf5627532023-02-24 21:25:28 +0100396 FDictionary.Remove( Key );
Jens Geyerd5436f52014-10-03 19:50:38 +0200397end;
398
399procedure TThriftDictionaryImpl<TKey, TValue>.SetItem(const Key: TKey;
400 const Value: TValue);
401begin
Jens Geyerf5627532023-02-24 21:25:28 +0100402 FDictionary.AddOrSetValue( Key, Value);
Jens Geyerd5436f52014-10-03 19:50:38 +0200403end;
404
405function TThriftDictionaryImpl<TKey, TValue>.ToArray: TArray<TPair<TKey, TValue>>;
406{$IF CompilerVersion < 22.0}
407var
408 x : TPair<TKey, TValue>;
409 i : Integer;
410{$IFEND}
411begin
412{$IF CompilerVersion < 22.0}
413 SetLength(Result, Count);
414 i := 0;
415 for x in FDictionaly do
416 begin
417 Result[i] := x;
418 Inc( i );
419 end;
420{$ELSE}
Jens Geyerf5627532023-02-24 21:25:28 +0100421 Result := FDictionary.ToArray;
Jens Geyerd5436f52014-10-03 19:50:38 +0200422{$IFEND}
423end;
424
Jens Geyer8f7487e2019-05-09 22:21:32 +0200425function TThriftDictionaryImpl<TKey, TValue>.ToString : string;
426var pair : TPair<TKey, TValue>;
427 sb : TThriftStringBuilder;
428 first : Boolean;
429begin
430 sb := TThriftStringBuilder.Create('{');
431 try
432 first := TRUE;
Jens Geyerf5627532023-02-24 21:25:28 +0100433 for pair in FDictionary do begin
Jens Geyer8f7487e2019-05-09 22:21:32 +0200434 if first
435 then first := FALSE
436 else sb.Append(', ');
437
438 sb.Append( '(');
439 sb.Append( StringUtils<TKey>.ToString(pair.Key));
440 sb.Append(' => ');
441 sb.Append( StringUtils<TValue>.ToString(pair.Value));
442 sb.Append(')');
443 end;
444 sb.Append('}');
445 Result := sb.ToString;
446 finally
447 sb.Free;
448 end;
449end;
450
Jens Geyerd5436f52014-10-03 19:50:38 +0200451procedure TThriftDictionaryImpl<TKey, TValue>.TrimExcess;
452begin
Jens Geyerf5627532023-02-24 21:25:28 +0100453 FDictionary.TrimExcess;
Jens Geyerd5436f52014-10-03 19:50:38 +0200454end;
455
456function TThriftDictionaryImpl<TKey, TValue>.TryGetValue(const Key: TKey;
457 out Value: TValue): Boolean;
458begin
Jens Geyerf5627532023-02-24 21:25:28 +0100459 Result := FDictionary.TryGetValue( Key, Value);
Jens Geyerd5436f52014-10-03 19:50:38 +0200460end;
461
462{ TThriftListImpl<T> }
463
464function TThriftListImpl<T>.Add(const Value: T): Integer;
465begin
466 Result := FList.Add( Value );
467end;
468
469procedure TThriftListImpl<T>.AddRange(Collection: TEnumerable<T>);
470begin
471 FList.AddRange( Collection );
472end;
473
474procedure TThriftListImpl<T>.AddRange(const Collection: IEnumerable<T>);
475begin
476 FList.AddRange( Collection );
477end;
478
479procedure TThriftListImpl<T>.AddRange(const Values: array of T);
480begin
481 FList.AddRange( Values );
482end;
483
484function TThriftListImpl<T>.BinarySearch(const Item: T;
485 out Index: Integer): Boolean;
486begin
487 Result := FList.BinarySearch( Item, Index);
488end;
489
490function TThriftListImpl<T>.BinarySearch(const Item: T; out Index: Integer;
491 const AComparer: IComparer<T>): Boolean;
492begin
493 Result := FList.BinarySearch( Item, Index, AComparer);
494end;
495
496procedure TThriftListImpl<T>.Clear;
497begin
498 FList.Clear;
499end;
500
501function TThriftListImpl<T>.Contains(const Value: T): Boolean;
502begin
503 Result := FList.Contains( Value );
504end;
505
Jens Geyerf5627532023-02-24 21:25:28 +0100506constructor TThriftListImpl<T>.Create( const aCapacity: Integer);
Jens Geyerd5436f52014-10-03 19:50:38 +0200507begin
Jens Geyerf5627532023-02-24 21:25:28 +0100508 inherited Create;
Jens Geyerd5436f52014-10-03 19:50:38 +0200509 FList := TList<T>.Create;
Jens Geyerf5627532023-02-24 21:25:28 +0100510
511 if aCapacity > 0
512 then FList.Capacity := aCapacity;
Jens Geyerd5436f52014-10-03 19:50:38 +0200513end;
514
515procedure TThriftListImpl<T>.Delete(Index: Integer);
516begin
517 FList.Delete( Index )
518end;
519
520procedure TThriftListImpl<T>.DeleteRange(AIndex, ACount: Integer);
521begin
522 FList.DeleteRange( AIndex, ACount)
523end;
524
525destructor TThriftListImpl<T>.Destroy;
526begin
527 FList.Free;
528 inherited;
529end;
530
531{$IF CompilerVersion >= 21.0}
532procedure TThriftListImpl<T>.Exchange(Index1, Index2: Integer);
533begin
534 FList.Exchange( Index1, Index2 )
535end;
536{$IFEND}
537
538function TThriftListImpl<T>.Extract(const Value: T): T;
539begin
540 Result := FList.Extract( Value )
541end;
542
543{$IF CompilerVersion >= 21.0}
544function TThriftListImpl<T>.First: T;
545begin
546 Result := FList.First;
547end;
548{$IFEND}
549
550function TThriftListImpl<T>.GetCapacity: Integer;
551begin
552 Result := FList.Capacity;
553end;
554
555function TThriftListImpl<T>.GetCount: Integer;
556begin
557 Result := FList.Count;
558end;
559
560function TThriftListImpl<T>.GetEnumerator: TEnumerator<T>;
561begin
562 Result := FList.GetEnumerator;
563end;
564
565function TThriftListImpl<T>.GetItem(Index: Integer): T;
566begin
567 Result := FList[Index];
568end;
569
570function TThriftListImpl<T>.IndexOf(const Value: T): Integer;
571begin
572 Result := FList.IndexOf( Value );
573end;
574
575procedure TThriftListImpl<T>.Insert(Index: Integer; const Value: T);
576begin
577 FList.Insert( Index, Value);
578end;
579
580procedure TThriftListImpl<T>.InsertRange(Index: Integer;
581 const Collection: TEnumerable<T>);
582begin
583 FList.InsertRange( Index, Collection );
584end;
585
586procedure TThriftListImpl<T>.InsertRange(Index: Integer;
587 const Values: array of T);
588begin
589 FList.InsertRange( Index, Values);
590end;
591
592procedure TThriftListImpl<T>.InsertRange(Index: Integer;
593 const Collection: IEnumerable<T>);
594begin
595 FList.InsertRange( Index, Collection );
596end;
597
598{$IF CompilerVersion >= 21.0}
599function TThriftListImpl<T>.Last: T;
600begin
601 Result := FList.Last;
602end;
603{$IFEND}
604
605function TThriftListImpl<T>.LastIndexOf(const Value: T): Integer;
606begin
607 Result := FList.LastIndexOf( Value );
608end;
609
610{$IF CompilerVersion >= 21.0}
611procedure TThriftListImpl<T>.Move(CurIndex, NewIndex: Integer);
612begin
613 FList.Move( CurIndex, NewIndex);
614end;
615{$IFEND}
616
617function TThriftListImpl<T>.Remove(const Value: T): Integer;
618begin
619 Result := FList.Remove( Value );
620end;
621
622procedure TThriftListImpl<T>.Reverse;
623begin
624 FList.Reverse;
625end;
626
627procedure TThriftListImpl<T>.SetCapacity(Value: Integer);
628begin
629 FList.Capacity := Value;
630end;
631
632procedure TThriftListImpl<T>.SetCount(Value: Integer);
633begin
634 FList.Count := Value;
635end;
636
637procedure TThriftListImpl<T>.SetItem(Index: Integer; const Value: T);
638begin
639 FList[Index] := Value;
640end;
641
642procedure TThriftListImpl<T>.Sort;
643begin
644 FList.Sort;
645end;
646
647procedure TThriftListImpl<T>.Sort(const AComparer: IComparer<T>);
648begin
Alex-Rud693e19c2019-07-30 14:51:56 +0300649 FList.Sort(AComparer);
Jens Geyerd5436f52014-10-03 19:50:38 +0200650end;
651
652function TThriftListImpl<T>.ToArray: TArray<T>;
653{$IF CompilerVersion < 22.0}
654var
655 x : T;
656 i : Integer;
657{$IFEND}
658begin
659{$IF CompilerVersion < 22.0}
660 SetLength(Result, Count);
661 i := 0;
662 for x in FList do
663 begin
664 Result[i] := x;
665 Inc( i );
666 end;
667{$ELSE}
668 Result := FList.ToArray;
669{$IFEND}
670end;
671
Jens Geyer8f7487e2019-05-09 22:21:32 +0200672function TThriftListImpl<T>.ToString : string;
673var elm : T;
674 sb : TThriftStringBuilder;
675 first : Boolean;
676begin
677 sb := TThriftStringBuilder.Create('{');
678 try
679 first := TRUE;
680 for elm in FList do begin
681 if first
682 then first := FALSE
683 else sb.Append(', ');
684
685 sb.Append( StringUtils<T>.ToString(elm));
686 end;
687 sb.Append('}');
688 Result := sb.ToString;
689 finally
690 sb.Free;
691 end;
692end;
693
Jens Geyerd5436f52014-10-03 19:50:38 +0200694procedure TThriftListImpl<T>.TrimExcess;
695begin
696 FList.TrimExcess;
697end;
698
699end.