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