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