blob: 66ff1c00540d1ae689a4091aea50773dd856f661 [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;
Roger Meier333bbf32012-01-08 21:51:08 +0000198 procedure Add( const item: TValue);
Jake Farrell7ae13e12011-10-18 14:35:26 +0000199 procedure Clear;
Roger Meier333bbf32012-01-08 21:51:08 +0000200 function Contains( const item: TValue): Boolean;
Jake Farrell7ae13e12011-10-18 14:35:26 +0000201 procedure CopyTo(var A: TArray<TValue>; arrayIndex: Integer);
Roger Meier333bbf32012-01-08 21:51:08 +0000202 function Remove( const item: TValue ): Boolean;
Jake Farrell7ae13e12011-10-18 14:35:26 +0000203 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;
Roger Meier333bbf32012-01-08 21:51:08 +0000215 procedure Add( const item: TValue);
Jake Farrell7ae13e12011-10-18 14:35:26 +0000216 procedure Clear;
Roger Meier333bbf32012-01-08 21:51:08 +0000217 function Contains( const item: TValue): Boolean;
Jake Farrell7ae13e12011-10-18 14:35:26 +0000218 procedure CopyTo(var A: TArray<TValue>; arrayIndex: Integer);
Roger Meier333bbf32012-01-08 21:51:08 +0000219 function Remove( const item: TValue ): Boolean;
Jake Farrell7ae13e12011-10-18 14:35:26 +0000220 public
221 constructor Create;
222 end;
223
224implementation
225
226{ THashSetImpl<TValue> }
227
Roger Meier333bbf32012-01-08 21:51:08 +0000228procedure THashSetImpl<TValue>.Add( const item: TValue);
Jake Farrell7ae13e12011-10-18 14:35:26 +0000229begin
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
Roger Meier333bbf32012-01-08 21:51:08 +0000241function THashSetImpl<TValue>.Contains( const item: TValue): Boolean;
Jake Farrell7ae13e12011-10-18 14:35:26 +0000242begin
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
Roger Meier333bbf32012-01-08 21:51:08 +0000280function THashSetImpl<TValue>.Remove( const item: TValue): Boolean;
Jake Farrell7ae13e12011-10-18 14:35:26 +0000281begin
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}
Roger Meier333bbf32012-01-08 21:51:08 +0000333function TThriftDictionaryImpl<TKey, TValue>.ExtractPair( const Key: TKey): TPair<TKey, TValue>;
Jake Farrell7ae13e12011-10-18 14:35:26 +0000334begin
335 Result := FDictionaly.ExtractPair( Key);
336end;
337{$IFEND}
338
339function TThriftDictionaryImpl<TKey, TValue>.GetCount: Integer;
340begin
341 Result := FDictionaly.Count;
342end;
343
344function TThriftDictionaryImpl<TKey, TValue>.GetEnumerator: TEnumerator<TPair<TKey, TValue>>;
345begin
346 Result := FDictionaly.GetEnumerator;
347end;
348
349function TThriftDictionaryImpl<TKey, TValue>.GetItem(const Key: TKey): TValue;
350begin
351 Result := FDictionaly.Items[Key];
352end;
353
354function TThriftDictionaryImpl<TKey, TValue>.GetKeys: TDictionary<TKey, TValue>.TKeyCollection;
355begin
356 Result := FDictionaly.Keys;
357end;
358
359function TThriftDictionaryImpl<TKey, TValue>.GetValues: TDictionary<TKey, TValue>.TValueCollection;
360begin
361 Result := FDictionaly.Values;
362end;
363
364procedure TThriftDictionaryImpl<TKey, TValue>.Remove(const Key: TKey);
365begin
366 FDictionaly.Remove( Key );
367end;
368
369procedure TThriftDictionaryImpl<TKey, TValue>.SetItem(const Key: TKey;
370 const Value: TValue);
371begin
372 FDictionaly.AddOrSetValue( Key, Value);
373end;
374
375function TThriftDictionaryImpl<TKey, TValue>.ToArray: TArray<TPair<TKey, TValue>>;
376{$IF CompilerVersion < 22.0}
377var
378 x : TPair<TKey, TValue>;
379 i : Integer;
380{$IFEND}
381begin
382{$IF CompilerVersion < 22.0}
383 SetLength(Result, Count);
384 i := 0;
385 for x in FDictionaly do
386 begin
387 Result[i] := x;
388 Inc( i );
389 end;
390{$ELSE}
391 Result := FDictionaly.ToArray;
392{$IFEND}
393end;
394
395procedure TThriftDictionaryImpl<TKey, TValue>.TrimExcess;
396begin
397 FDictionaly.TrimExcess;
398end;
399
400function TThriftDictionaryImpl<TKey, TValue>.TryGetValue(const Key: TKey;
401 out Value: TValue): Boolean;
402begin
403 Result := FDictionaly.TryGetValue( Key, Value);
404end;
405
406{ TThriftListImpl<T> }
407
408function TThriftListImpl<T>.Add(const Value: T): Integer;
409begin
410 Result := FList.Add( Value );
411end;
412
413procedure TThriftListImpl<T>.AddRange(Collection: TEnumerable<T>);
414begin
415 FList.AddRange( Collection );
416end;
417
418procedure TThriftListImpl<T>.AddRange(const Collection: IEnumerable<T>);
419begin
420 FList.AddRange( Collection );
421end;
422
423procedure TThriftListImpl<T>.AddRange(const Values: array of T);
424begin
425 FList.AddRange( Values );
426end;
427
428function TThriftListImpl<T>.BinarySearch(const Item: T;
429 out Index: Integer): Boolean;
430begin
431 Result := FList.BinarySearch( Item, Index);
432end;
433
434function TThriftListImpl<T>.BinarySearch(const Item: T; out Index: Integer;
435 const AComparer: IComparer<T>): Boolean;
436begin
437 Result := FList.BinarySearch( Item, Index, AComparer);
438end;
439
440procedure TThriftListImpl<T>.Clear;
441begin
442 FList.Clear;
443end;
444
445function TThriftListImpl<T>.Contains(const Value: T): Boolean;
446begin
447 Result := FList.Contains( Value );
448end;
449
450constructor TThriftListImpl<T>.Create;
451begin
452 FList := TList<T>.Create;
453end;
454
455procedure TThriftListImpl<T>.Delete(Index: Integer);
456begin
457 FList.Delete( Index )
458end;
459
460procedure TThriftListImpl<T>.DeleteRange(AIndex, ACount: Integer);
461begin
462 FList.DeleteRange( AIndex, ACount)
463end;
464
465destructor TThriftListImpl<T>.Destroy;
466begin
467 FList.Free;
468 inherited;
469end;
470
471{$IF CompilerVersion >= 21.0}
472procedure TThriftListImpl<T>.Exchange(Index1, Index2: Integer);
473begin
474 FList.Exchange( Index1, Index2 )
475end;
476{$IFEND}
477
478function TThriftListImpl<T>.Extract(const Value: T): T;
479begin
480 Result := FList.Extract( Value )
481end;
482
483{$IF CompilerVersion >= 21.0}
484function TThriftListImpl<T>.First: T;
485begin
486 Result := FList.First;
487end;
488{$IFEND}
489
490function TThriftListImpl<T>.GetCapacity: Integer;
491begin
492 Result := FList.Capacity;
493end;
494
495function TThriftListImpl<T>.GetCount: Integer;
496begin
497 Result := FList.Count;
498end;
499
500function TThriftListImpl<T>.GetEnumerator: TEnumerator<T>;
501begin
502 Result := FList.GetEnumerator;
503end;
504
505function TThriftListImpl<T>.GetItem(Index: Integer): T;
506begin
507 Result := FList[Index];
508end;
509
510function TThriftListImpl<T>.IndexOf(const Value: T): Integer;
511begin
512 Result := FList.IndexOf( Value );
513end;
514
515procedure TThriftListImpl<T>.Insert(Index: Integer; const Value: T);
516begin
517 FList.Insert( Index, Value);
518end;
519
520procedure TThriftListImpl<T>.InsertRange(Index: Integer;
521 const Collection: TEnumerable<T>);
522begin
523 FList.InsertRange( Index, Collection );
524end;
525
526procedure TThriftListImpl<T>.InsertRange(Index: Integer;
527 const Values: array of T);
528begin
529 FList.InsertRange( Index, Values);
530end;
531
532procedure TThriftListImpl<T>.InsertRange(Index: Integer;
533 const Collection: IEnumerable<T>);
534begin
535 FList.InsertRange( Index, Collection );
536end;
537
538{$IF CompilerVersion >= 21.0}
539function TThriftListImpl<T>.Last: T;
540begin
541 Result := FList.Last;
542end;
543{$IFEND}
544
545function TThriftListImpl<T>.LastIndexOf(const Value: T): Integer;
546begin
547 Result := FList.LastIndexOf( Value );
548end;
549
550{$IF CompilerVersion >= 21.0}
551procedure TThriftListImpl<T>.Move(CurIndex, NewIndex: Integer);
552begin
553 FList.Move( CurIndex, NewIndex);
554end;
555{$IFEND}
556
557function TThriftListImpl<T>.Remove(const Value: T): Integer;
558begin
559 Result := FList.Remove( Value );
560end;
561
562procedure TThriftListImpl<T>.Reverse;
563begin
564 FList.Reverse;
565end;
566
567procedure TThriftListImpl<T>.SetCapacity(Value: Integer);
568begin
569 FList.Capacity := Value;
570end;
571
572procedure TThriftListImpl<T>.SetCount(Value: Integer);
573begin
574 FList.Count := Value;
575end;
576
577procedure TThriftListImpl<T>.SetItem(Index: Integer; const Value: T);
578begin
579 FList[Index] := Value;
580end;
581
582procedure TThriftListImpl<T>.Sort;
583begin
584 FList.Sort;
585end;
586
587procedure TThriftListImpl<T>.Sort(const AComparer: IComparer<T>);
588begin
589 FList.Sort;
590end;
591
592function TThriftListImpl<T>.ToArray: TArray<T>;
593{$IF CompilerVersion < 22.0}
594var
595 x : T;
596 i : Integer;
597{$IFEND}
598begin
599{$IF CompilerVersion < 22.0}
600 SetLength(Result, Count);
601 i := 0;
602 for x in FList do
603 begin
604 Result[i] := x;
605 Inc( i );
606 end;
607{$ELSE}
608 Result := FList.ToArray;
609{$IFEND}
610end;
611
612procedure TThriftListImpl<T>.TrimExcess;
613begin
614 FList.TrimExcess;
615end;
616
617end.