blob: abc401fc10b1273b279a3087a3203bd26be281a2 [file] [log] [blame]
Jake Farrell7ae13e12011-10-18 14:35:26 +00001(*
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( item: TValue);
199 procedure Clear;
200 function Contains( item: TValue): Boolean;
201 procedure CopyTo(var A: TArray<TValue>; arrayIndex: Integer);
202 function Remove( 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( item: TValue);
216 procedure Clear;
217 function Contains( item: TValue): Boolean;
218 procedure CopyTo(var A: TArray<TValue>; arrayIndex: Integer);
219 function Remove( item: TValue ): Boolean;
220 public
221 constructor Create;
222 end;
223
224implementation
225
226{ THashSetImpl<TValue> }
227
228procedure THashSetImpl<TValue>.Add(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(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(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 FDictionaly := TDictionary<TKey,TValue>.Create( ACapacity );
324end;
325
326destructor TThriftDictionaryImpl<TKey, TValue>.Destroy;
327begin
328 FDictionaly.Free;
329 inherited;
330end;
331
332{$IF CompilerVersion >= 21.0}
333function TThriftDictionaryImpl<TKey, TValue>.ExtractPair(
334 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 FList := TList<T>.Create;
454end;
455
456procedure TThriftListImpl<T>.Delete(Index: Integer);
457begin
458 FList.Delete( Index )
459end;
460
461procedure TThriftListImpl<T>.DeleteRange(AIndex, ACount: Integer);
462begin
463 FList.DeleteRange( AIndex, ACount)
464end;
465
466destructor TThriftListImpl<T>.Destroy;
467begin
468 FList.Free;
469 inherited;
470end;
471
472{$IF CompilerVersion >= 21.0}
473procedure TThriftListImpl<T>.Exchange(Index1, Index2: Integer);
474begin
475 FList.Exchange( Index1, Index2 )
476end;
477{$IFEND}
478
479function TThriftListImpl<T>.Extract(const Value: T): T;
480begin
481 Result := FList.Extract( Value )
482end;
483
484{$IF CompilerVersion >= 21.0}
485function TThriftListImpl<T>.First: T;
486begin
487 Result := FList.First;
488end;
489{$IFEND}
490
491function TThriftListImpl<T>.GetCapacity: Integer;
492begin
493 Result := FList.Capacity;
494end;
495
496function TThriftListImpl<T>.GetCount: Integer;
497begin
498 Result := FList.Count;
499end;
500
501function TThriftListImpl<T>.GetEnumerator: TEnumerator<T>;
502begin
503 Result := FList.GetEnumerator;
504end;
505
506function TThriftListImpl<T>.GetItem(Index: Integer): T;
507begin
508 Result := FList[Index];
509end;
510
511function TThriftListImpl<T>.IndexOf(const Value: T): Integer;
512begin
513 Result := FList.IndexOf( Value );
514end;
515
516procedure TThriftListImpl<T>.Insert(Index: Integer; const Value: T);
517begin
518 FList.Insert( Index, Value);
519end;
520
521procedure TThriftListImpl<T>.InsertRange(Index: Integer;
522 const Collection: TEnumerable<T>);
523begin
524 FList.InsertRange( Index, Collection );
525end;
526
527procedure TThriftListImpl<T>.InsertRange(Index: Integer;
528 const Values: array of T);
529begin
530 FList.InsertRange( Index, Values);
531end;
532
533procedure TThriftListImpl<T>.InsertRange(Index: Integer;
534 const Collection: IEnumerable<T>);
535begin
536 FList.InsertRange( Index, Collection );
537end;
538
539{$IF CompilerVersion >= 21.0}
540function TThriftListImpl<T>.Last: T;
541begin
542 Result := FList.Last;
543end;
544{$IFEND}
545
546function TThriftListImpl<T>.LastIndexOf(const Value: T): Integer;
547begin
548 Result := FList.LastIndexOf( Value );
549end;
550
551{$IF CompilerVersion >= 21.0}
552procedure TThriftListImpl<T>.Move(CurIndex, NewIndex: Integer);
553begin
554 FList.Move( CurIndex, NewIndex);
555end;
556{$IFEND}
557
558function TThriftListImpl<T>.Remove(const Value: T): Integer;
559begin
560 Result := FList.Remove( Value );
561end;
562
563procedure TThriftListImpl<T>.Reverse;
564begin
565 FList.Reverse;
566end;
567
568procedure TThriftListImpl<T>.SetCapacity(Value: Integer);
569begin
570 FList.Capacity := Value;
571end;
572
573procedure TThriftListImpl<T>.SetCount(Value: Integer);
574begin
575 FList.Count := Value;
576end;
577
578procedure TThriftListImpl<T>.SetItem(Index: Integer; const Value: T);
579begin
580 FList[Index] := Value;
581end;
582
583procedure TThriftListImpl<T>.Sort;
584begin
585 FList.Sort;
586end;
587
588procedure TThriftListImpl<T>.Sort(const AComparer: IComparer<T>);
589begin
590 FList.Sort;
591end;
592
593function TThriftListImpl<T>.ToArray: TArray<T>;
594{$IF CompilerVersion < 22.0}
595var
596 x : T;
597 i : Integer;
598{$IFEND}
599begin
600{$IF CompilerVersion < 22.0}
601 SetLength(Result, Count);
602 i := 0;
603 for x in FList do
604 begin
605 Result[i] := x;
606 Inc( i );
607 end;
608{$ELSE}
609 Result := FList.ToArray;
610{$IFEND}
611end;
612
613procedure TThriftListImpl<T>.TrimExcess;
614begin
615 FList.TrimExcess;
616end;
617
618end.