blob: b2206cbf0a1dde45b38a5dd83531db4e4d091709 [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
25 Generics.Collections, Generics.Defaults, Thrift.Utils;
26
27type
28
29{$IF CompilerVersion < 21.0}
30 TArray<T> = array of T;
31{$IFEND}
32
33 IThriftContainer = interface
34 ['{93DEF5A0-D162-461A-AB22-5B4EE0734050}']
35 function ToString: string;
36 end;
37
38 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
67 TThriftDictionaryImpl<TKey,TValue> = class( TInterfacedObject, IThriftDictionary<TKey,TValue>)
68 private
69 FDictionaly : TDictionary<TKey,TValue>;
70 protected
71 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
96 constructor Create(ACapacity: Integer = 0);
97 destructor Destroy; override;
98 end;
99
100 IThriftList<T> = interface(IThriftContainer)
101 ['{29BEEE31-9CB4-401B-AA04-5148A75F473B}']
102 function GetEnumerator: TEnumerator<T>;
103 function GetCapacity: Integer;
104 procedure SetCapacity(Value: Integer);
105 function GetCount: Integer;
106 procedure SetCount(Value: Integer);
107 function GetItem(Index: Integer): T;
108 procedure SetItem(Index: Integer; const Value: T);
109 function Add(const Value: T): Integer;
110 procedure AddRange(const Values: array of T); overload;
111 procedure AddRange(const Collection: IEnumerable<T>); overload;
112 procedure AddRange(Collection: TEnumerable<T>); overload;
113 procedure Insert(Index: Integer; const Value: T);
114 procedure InsertRange(Index: Integer; const Values: array of T); overload;
115 procedure InsertRange(Index: Integer; const Collection: IEnumerable<T>); overload;
116 procedure InsertRange(Index: Integer; const Collection: TEnumerable<T>); overload;
117 function Remove(const Value: T): Integer;
118 procedure Delete(Index: Integer);
119 procedure DeleteRange(AIndex, ACount: Integer);
120 function Extract(const Value: T): T;
121{$IF CompilerVersion >= 21.0}
122 procedure Exchange(Index1, Index2: Integer);
123 procedure Move(CurIndex, NewIndex: Integer);
124 function First: T;
125 function Last: T;
126{$IFEND}
127 procedure Clear;
128 function Contains(const Value: T): Boolean;
129 function IndexOf(const Value: T): Integer;
130 function LastIndexOf(const Value: T): Integer;
131 procedure Reverse;
132 procedure Sort; overload;
133 procedure Sort(const AComparer: IComparer<T>); overload;
134 function BinarySearch(const Item: T; out Index: Integer): Boolean; overload;
135 function BinarySearch(const Item: T; out Index: Integer; const AComparer: IComparer<T>): Boolean; overload;
136 procedure TrimExcess;
137 function ToArray: TArray<T>;
138 property Capacity: Integer read GetCapacity write SetCapacity;
139 property Count: Integer read GetCount write SetCount;
140 property Items[Index: Integer]: T read GetItem write SetItem; default;
141 end;
142
143 TThriftListImpl<T> = class( TInterfacedObject, IThriftList<T>)
144 private
145 FList : TList<T>;
146 protected
147 function GetEnumerator: TEnumerator<T>;
148 function GetCapacity: Integer;
149 procedure SetCapacity(Value: Integer);
150 function GetCount: Integer;
151 procedure SetCount(Value: Integer);
152 function GetItem(Index: Integer): T;
153 procedure SetItem(Index: Integer; const Value: T);
154 function Add(const Value: T): Integer;
155 procedure AddRange(const Values: array of T); overload;
156 procedure AddRange(const Collection: IEnumerable<T>); overload;
157 procedure AddRange(Collection: TEnumerable<T>); overload;
158 procedure Insert(Index: Integer; const Value: T);
159 procedure InsertRange(Index: Integer; const Values: array of T); overload;
160 procedure InsertRange(Index: Integer; const Collection: IEnumerable<T>); overload;
161 procedure InsertRange(Index: Integer; const Collection: TEnumerable<T>); overload;
162 function Remove(const Value: T): Integer;
163 procedure Delete(Index: Integer);
164 procedure DeleteRange(AIndex, ACount: Integer);
165 function Extract(const Value: T): T;
166{$IF CompilerVersion >= 21.0}
167 procedure Exchange(Index1, Index2: Integer);
168 procedure Move(CurIndex, NewIndex: Integer);
169 function First: T;
170 function Last: T;
171{$IFEND}
172 procedure Clear;
173 function Contains(const Value: T): Boolean;
174 function IndexOf(const Value: T): Integer;
175 function LastIndexOf(const Value: T): Integer;
176 procedure Reverse;
177 procedure Sort; overload;
178 procedure Sort(const AComparer: IComparer<T>); overload;
179 function BinarySearch(const Item: T; out Index: Integer): Boolean; overload;
180 function BinarySearch(const Item: T; out Index: Integer; const AComparer: IComparer<T>): Boolean; overload;
181 procedure TrimExcess;
182 function ToArray: TArray<T>;
183 property Capacity: Integer read GetCapacity write SetCapacity;
184 property Count: Integer read GetCount write SetCount;
185 property Items[Index: Integer]: T read GetItem write SetItem; default;
186 public
187 constructor Create;
188 destructor Destroy; override;
189 end;
190
191 IHashSet<TValue> = interface(IThriftContainer)
192 ['{0923A3B5-D4D4-48A8-91AD-40238E2EAD66}']
193 function GetEnumerator: TEnumerator<TValue>;
194 function GetIsReadOnly: Boolean;
195 function GetCount: Integer;
196 property Count: Integer read GetCount;
197 property IsReadOnly: Boolean read GetIsReadOnly;
198 procedure Add( const item: TValue);
199 procedure Clear;
200 function Contains( const item: TValue): Boolean;
201 procedure CopyTo(var A: TArray<TValue>; arrayIndex: Integer);
202 function Remove( const item: TValue ): Boolean;
203 end;
204
205 THashSetImpl<TValue> = class( TInterfacedObject, IHashSet<TValue>)
206 private
207 FDictionary : IThriftDictionary<TValue,Integer>;
208 FIsReadOnly: Boolean;
209 protected
210 function GetEnumerator: TEnumerator<TValue>;
211 function GetIsReadOnly: Boolean;
212 function GetCount: Integer;
213 property Count: Integer read GetCount;
214 property IsReadOnly: Boolean read FIsReadOnly;
215 procedure Add( const item: TValue);
216 procedure Clear;
217 function Contains( const item: TValue): Boolean;
218 procedure CopyTo(var A: TArray<TValue>; arrayIndex: Integer);
219 function Remove( const item: TValue ): Boolean;
220 public
221 constructor Create;
222 end;
223
224implementation
225
226{ THashSetImpl<TValue> }
227
228procedure THashSetImpl<TValue>.Add( const item: TValue);
229begin
230 if not FDictionary.ContainsKey(item) then
231 begin
232 FDictionary.Add( item, 0);
233 end;
234end;
235
236procedure THashSetImpl<TValue>.Clear;
237begin
238 FDictionary.Clear;
239end;
240
241function THashSetImpl<TValue>.Contains( const item: TValue): Boolean;
242begin
243 Result := FDictionary.ContainsKey(item);
244end;
245
246procedure THashSetImpl<TValue>.CopyTo(var A: TArray<TValue>; arrayIndex: Integer);
247var
248 i : Integer;
249 Enumlator : TEnumerator<TValue>;
250begin
251 Enumlator := GetEnumerator;
252 while Enumlator.MoveNext do
253 begin
254 A[arrayIndex] := Enumlator.Current;
255 Inc(arrayIndex);
256 end;
257end;
258
259constructor THashSetImpl<TValue>.Create;
260begin
261 inherited;
262 FDictionary := TThriftDictionaryImpl<TValue,Integer>.Create;
263end;
264
265function THashSetImpl<TValue>.GetCount: Integer;
266begin
267 Result := FDictionary.Count;
268end;
269
270function THashSetImpl<TValue>.GetEnumerator: TEnumerator<TValue>;
271begin
272 Result := FDictionary.Keys.GetEnumerator;
273end;
274
275function THashSetImpl<TValue>.GetIsReadOnly: Boolean;
276begin
277 Result := FIsReadOnly;
278end;
279
280function THashSetImpl<TValue>.Remove( const item: TValue): Boolean;
281begin
282 Result := False;
283 if FDictionary.ContainsKey( item ) then
284 begin
285 FDictionary.Remove( item );
286 Result := not FDictionary.ContainsKey( item );
287 end;
288end;
289
290{ TThriftDictionaryImpl<TKey, TValue> }
291
292procedure TThriftDictionaryImpl<TKey, TValue>.Add(const Key: TKey;
293 const Value: TValue);
294begin
295 FDictionaly.Add( Key, Value);
296end;
297
298procedure TThriftDictionaryImpl<TKey, TValue>.AddOrSetValue(const Key: TKey;
299 const Value: TValue);
300begin
301 FDictionaly.AddOrSetValue( Key, Value);
302end;
303
304procedure TThriftDictionaryImpl<TKey, TValue>.Clear;
305begin
306 FDictionaly.Clear;
307end;
308
309function TThriftDictionaryImpl<TKey, TValue>.ContainsKey(
310 const Key: TKey): Boolean;
311begin
312 Result := FDictionaly.ContainsKey( Key );
313end;
314
315function TThriftDictionaryImpl<TKey, TValue>.ContainsValue(
316 const Value: TValue): Boolean;
317begin
318 Result := FDictionaly.ContainsValue( Value );
319end;
320
321constructor TThriftDictionaryImpl<TKey, TValue>.Create(ACapacity: Integer);
322begin
323 inherited Create;
324 FDictionaly := TDictionary<TKey,TValue>.Create( ACapacity );
325end;
326
327destructor TThriftDictionaryImpl<TKey, TValue>.Destroy;
328begin
329 FDictionaly.Free;
330 inherited;
331end;
332
333{$IF CompilerVersion >= 21.0}
334function TThriftDictionaryImpl<TKey, TValue>.ExtractPair( const Key: TKey): TPair<TKey, TValue>;
335begin
336 Result := FDictionaly.ExtractPair( Key);
337end;
338{$IFEND}
339
340function TThriftDictionaryImpl<TKey, TValue>.GetCount: Integer;
341begin
342 Result := FDictionaly.Count;
343end;
344
345function TThriftDictionaryImpl<TKey, TValue>.GetEnumerator: TEnumerator<TPair<TKey, TValue>>;
346begin
347 Result := FDictionaly.GetEnumerator;
348end;
349
350function TThriftDictionaryImpl<TKey, TValue>.GetItem(const Key: TKey): TValue;
351begin
352 Result := FDictionaly.Items[Key];
353end;
354
355function TThriftDictionaryImpl<TKey, TValue>.GetKeys: TDictionary<TKey, TValue>.TKeyCollection;
356begin
357 Result := FDictionaly.Keys;
358end;
359
360function TThriftDictionaryImpl<TKey, TValue>.GetValues: TDictionary<TKey, TValue>.TValueCollection;
361begin
362 Result := FDictionaly.Values;
363end;
364
365procedure TThriftDictionaryImpl<TKey, TValue>.Remove(const Key: TKey);
366begin
367 FDictionaly.Remove( Key );
368end;
369
370procedure TThriftDictionaryImpl<TKey, TValue>.SetItem(const Key: TKey;
371 const Value: TValue);
372begin
373 FDictionaly.AddOrSetValue( Key, Value);
374end;
375
376function TThriftDictionaryImpl<TKey, TValue>.ToArray: TArray<TPair<TKey, TValue>>;
377{$IF CompilerVersion < 22.0}
378var
379 x : TPair<TKey, TValue>;
380 i : Integer;
381{$IFEND}
382begin
383{$IF CompilerVersion < 22.0}
384 SetLength(Result, Count);
385 i := 0;
386 for x in FDictionaly do
387 begin
388 Result[i] := x;
389 Inc( i );
390 end;
391{$ELSE}
392 Result := FDictionaly.ToArray;
393{$IFEND}
394end;
395
396procedure TThriftDictionaryImpl<TKey, TValue>.TrimExcess;
397begin
398 FDictionaly.TrimExcess;
399end;
400
401function TThriftDictionaryImpl<TKey, TValue>.TryGetValue(const Key: TKey;
402 out Value: TValue): Boolean;
403begin
404 Result := FDictionaly.TryGetValue( Key, Value);
405end;
406
407{ TThriftListImpl<T> }
408
409function TThriftListImpl<T>.Add(const Value: T): Integer;
410begin
411 Result := FList.Add( Value );
412end;
413
414procedure TThriftListImpl<T>.AddRange(Collection: TEnumerable<T>);
415begin
416 FList.AddRange( Collection );
417end;
418
419procedure TThriftListImpl<T>.AddRange(const Collection: IEnumerable<T>);
420begin
421 FList.AddRange( Collection );
422end;
423
424procedure TThriftListImpl<T>.AddRange(const Values: array of T);
425begin
426 FList.AddRange( Values );
427end;
428
429function TThriftListImpl<T>.BinarySearch(const Item: T;
430 out Index: Integer): Boolean;
431begin
432 Result := FList.BinarySearch( Item, Index);
433end;
434
435function TThriftListImpl<T>.BinarySearch(const Item: T; out Index: Integer;
436 const AComparer: IComparer<T>): Boolean;
437begin
438 Result := FList.BinarySearch( Item, Index, AComparer);
439end;
440
441procedure TThriftListImpl<T>.Clear;
442begin
443 FList.Clear;
444end;
445
446function TThriftListImpl<T>.Contains(const Value: T): Boolean;
447begin
448 Result := FList.Contains( Value );
449end;
450
451constructor TThriftListImpl<T>.Create;
452begin
453 inherited;
454 FList := TList<T>.Create;
455end;
456
457procedure TThriftListImpl<T>.Delete(Index: Integer);
458begin
459 FList.Delete( Index )
460end;
461
462procedure TThriftListImpl<T>.DeleteRange(AIndex, ACount: Integer);
463begin
464 FList.DeleteRange( AIndex, ACount)
465end;
466
467destructor TThriftListImpl<T>.Destroy;
468begin
469 FList.Free;
470 inherited;
471end;
472
473{$IF CompilerVersion >= 21.0}
474procedure TThriftListImpl<T>.Exchange(Index1, Index2: Integer);
475begin
476 FList.Exchange( Index1, Index2 )
477end;
478{$IFEND}
479
480function TThriftListImpl<T>.Extract(const Value: T): T;
481begin
482 Result := FList.Extract( Value )
483end;
484
485{$IF CompilerVersion >= 21.0}
486function TThriftListImpl<T>.First: T;
487begin
488 Result := FList.First;
489end;
490{$IFEND}
491
492function TThriftListImpl<T>.GetCapacity: Integer;
493begin
494 Result := FList.Capacity;
495end;
496
497function TThriftListImpl<T>.GetCount: Integer;
498begin
499 Result := FList.Count;
500end;
501
502function TThriftListImpl<T>.GetEnumerator: TEnumerator<T>;
503begin
504 Result := FList.GetEnumerator;
505end;
506
507function TThriftListImpl<T>.GetItem(Index: Integer): T;
508begin
509 Result := FList[Index];
510end;
511
512function TThriftListImpl<T>.IndexOf(const Value: T): Integer;
513begin
514 Result := FList.IndexOf( Value );
515end;
516
517procedure TThriftListImpl<T>.Insert(Index: Integer; const Value: T);
518begin
519 FList.Insert( Index, Value);
520end;
521
522procedure TThriftListImpl<T>.InsertRange(Index: Integer;
523 const Collection: TEnumerable<T>);
524begin
525 FList.InsertRange( Index, Collection );
526end;
527
528procedure TThriftListImpl<T>.InsertRange(Index: Integer;
529 const Values: array of T);
530begin
531 FList.InsertRange( Index, Values);
532end;
533
534procedure TThriftListImpl<T>.InsertRange(Index: Integer;
535 const Collection: IEnumerable<T>);
536begin
537 FList.InsertRange( Index, Collection );
538end;
539
540{$IF CompilerVersion >= 21.0}
541function TThriftListImpl<T>.Last: T;
542begin
543 Result := FList.Last;
544end;
545{$IFEND}
546
547function TThriftListImpl<T>.LastIndexOf(const Value: T): Integer;
548begin
549 Result := FList.LastIndexOf( Value );
550end;
551
552{$IF CompilerVersion >= 21.0}
553procedure TThriftListImpl<T>.Move(CurIndex, NewIndex: Integer);
554begin
555 FList.Move( CurIndex, NewIndex);
556end;
557{$IFEND}
558
559function TThriftListImpl<T>.Remove(const Value: T): Integer;
560begin
561 Result := FList.Remove( Value );
562end;
563
564procedure TThriftListImpl<T>.Reverse;
565begin
566 FList.Reverse;
567end;
568
569procedure TThriftListImpl<T>.SetCapacity(Value: Integer);
570begin
571 FList.Capacity := Value;
572end;
573
574procedure TThriftListImpl<T>.SetCount(Value: Integer);
575begin
576 FList.Count := Value;
577end;
578
579procedure TThriftListImpl<T>.SetItem(Index: Integer; const Value: T);
580begin
581 FList[Index] := Value;
582end;
583
584procedure TThriftListImpl<T>.Sort;
585begin
586 FList.Sort;
587end;
588
589procedure TThriftListImpl<T>.Sort(const AComparer: IComparer<T>);
590begin
591 FList.Sort;
592end;
593
594function TThriftListImpl<T>.ToArray: TArray<T>;
595{$IF CompilerVersion < 22.0}
596var
597 x : T;
598 i : Integer;
599{$IFEND}
600begin
601{$IF CompilerVersion < 22.0}
602 SetLength(Result, Count);
603 i := 0;
604 for x in FList do
605 begin
606 Result[i] := x;
607 Inc( i );
608 end;
609{$ELSE}
610 Result := FList.ToArray;
611{$IFEND}
612end;
613
614procedure TThriftListImpl<T>.TrimExcess;
615begin
616 FList.TrimExcess;
617end;
618
619end.