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