From f5627536bacab4b298aa1b207a98b87357763a88 Mon Sep 17 00:00:00 2001 From: Jens Geyer Date: Fri, 24 Feb 2023 21:25:28 +0100 Subject: THRIFT-5686 Add comparer and capacity arguments to container classes Client: Delphi Patch: Jens Geyer --- lib/delphi/src/Thrift.Collections.pas | 75 +++++++++++++++++++++-------------- 1 file changed, 46 insertions(+), 29 deletions(-) diff --git a/lib/delphi/src/Thrift.Collections.pas b/lib/delphi/src/Thrift.Collections.pas index 36a8d9696..2cb2395ad 100644 --- a/lib/delphi/src/Thrift.Collections.pas +++ b/lib/delphi/src/Thrift.Collections.pas @@ -66,7 +66,7 @@ type TThriftDictionaryImpl = class( TInterfacedObject, IThriftDictionary, IThriftContainer, ISupportsToString) strict private - FDictionaly : TDictionary; + FDictionary : TDictionary; strict protected function GetEnumerator: TEnumerator>; @@ -93,7 +93,8 @@ type property Keys: TDictionary.TKeyCollection read GetKeys; property Values: TDictionary.TValueCollection read GetValues; public - constructor Create(ACapacity: Integer = 0); + constructor Create( const aCapacity: Integer = 0); overload; + constructor Create( const aCapacity: Integer; const aComparer : IEqualityComparer); overload; destructor Destroy; override; function ToString : string; override; end; @@ -185,7 +186,7 @@ type property Count: Integer read GetCount write SetCount; property Items[Index: Integer]: T read GetItem write SetItem; default; public - constructor Create; + constructor Create( const aCapacity: Integer = 0); destructor Destroy; override; function ToString : string; override; end; @@ -228,7 +229,8 @@ type procedure CopyTo(var A: TArray; arrayIndex: Integer); function Remove( const item: TValue ): Boolean; public - constructor Create; + constructor Create( const aCapacity: Integer = 0); overload; + constructor Create( const aCapacity: Integer; const aComparer : IEqualityComparer); overload; function ToString : string; override; end; @@ -271,10 +273,16 @@ begin end; end; -constructor TThriftHashSetImpl.Create; +constructor TThriftHashSetImpl.Create( const aCapacity: Integer); begin - inherited; - FDictionary := TThriftDictionaryImpl.Create; + inherited Create; + FDictionary := TThriftDictionaryImpl.Create( aCapacity); +end; + +constructor TThriftHashSetImpl.Create( const aCapacity: Integer; const aComparer : IEqualityComparer); +begin + inherited Create; + FDictionary := TThriftDictionaryImpl.Create( aCapacity, aComparer); end; function TThriftHashSetImpl.GetCount: Integer; @@ -329,85 +337,91 @@ end; procedure TThriftDictionaryImpl.Add(const Key: TKey; const Value: TValue); begin - FDictionaly.Add( Key, Value); + FDictionary.Add( Key, Value); end; procedure TThriftDictionaryImpl.AddOrSetValue(const Key: TKey; const Value: TValue); begin - FDictionaly.AddOrSetValue( Key, Value); + FDictionary.AddOrSetValue( Key, Value); end; procedure TThriftDictionaryImpl.Clear; begin - FDictionaly.Clear; + FDictionary.Clear; end; function TThriftDictionaryImpl.ContainsKey( const Key: TKey): Boolean; begin - Result := FDictionaly.ContainsKey( Key ); + Result := FDictionary.ContainsKey( Key ); end; function TThriftDictionaryImpl.ContainsValue( const Value: TValue): Boolean; begin - Result := FDictionaly.ContainsValue( Value ); + Result := FDictionary.ContainsValue( Value ); end; -constructor TThriftDictionaryImpl.Create(ACapacity: Integer); +constructor TThriftDictionaryImpl.Create(const aCapacity: Integer); begin inherited Create; - FDictionaly := TDictionary.Create( ACapacity ); + FDictionary := TDictionary.Create( aCapacity); +end; + +constructor TThriftDictionaryImpl.Create(const aCapacity: Integer; const aComparer : IEqualityComparer); +begin + inherited Create; + FDictionary := TDictionary.Create( aCapacity, aComparer); end; destructor TThriftDictionaryImpl.Destroy; begin - FDictionaly.Free; + FDictionary.Free; inherited; end; {$IF CompilerVersion >= 21.0} function TThriftDictionaryImpl.ExtractPair( const Key: TKey): TPair; begin - Result := FDictionaly.ExtractPair( Key); + Result := FDictionary.ExtractPair( Key); end; {$IFEND} function TThriftDictionaryImpl.GetCount: Integer; begin - Result := FDictionaly.Count; + Result := FDictionary.Count; end; function TThriftDictionaryImpl.GetEnumerator: TEnumerator>; begin - Result := FDictionaly.GetEnumerator; + Result := FDictionary.GetEnumerator; end; function TThriftDictionaryImpl.GetItem(const Key: TKey): TValue; begin - Result := FDictionaly.Items[Key]; + Result := FDictionary.Items[Key]; end; function TThriftDictionaryImpl.GetKeys: TDictionary.TKeyCollection; begin - Result := FDictionaly.Keys; + Result := FDictionary.Keys; end; function TThriftDictionaryImpl.GetValues: TDictionary.TValueCollection; begin - Result := FDictionaly.Values; + Result := FDictionary.Values; end; procedure TThriftDictionaryImpl.Remove(const Key: TKey); begin - FDictionaly.Remove( Key ); + FDictionary.Remove( Key ); end; procedure TThriftDictionaryImpl.SetItem(const Key: TKey; const Value: TValue); begin - FDictionaly.AddOrSetValue( Key, Value); + FDictionary.AddOrSetValue( Key, Value); end; function TThriftDictionaryImpl.ToArray: TArray>; @@ -426,7 +440,7 @@ begin Inc( i ); end; {$ELSE} - Result := FDictionaly.ToArray; + Result := FDictionary.ToArray; {$IFEND} end; @@ -438,7 +452,7 @@ begin sb := TThriftStringBuilder.Create('{'); try first := TRUE; - for pair in FDictionaly do begin + for pair in FDictionary do begin if first then first := FALSE else sb.Append(', '); @@ -458,13 +472,13 @@ end; procedure TThriftDictionaryImpl.TrimExcess; begin - FDictionaly.TrimExcess; + FDictionary.TrimExcess; end; function TThriftDictionaryImpl.TryGetValue(const Key: TKey; out Value: TValue): Boolean; begin - Result := FDictionaly.TryGetValue( Key, Value); + Result := FDictionary.TryGetValue( Key, Value); end; { TThriftListImpl } @@ -511,10 +525,13 @@ begin Result := FList.Contains( Value ); end; -constructor TThriftListImpl.Create; +constructor TThriftListImpl.Create( const aCapacity: Integer); begin - inherited; + inherited Create; FList := TList.Create; + + if aCapacity > 0 + then FList.Capacity := aCapacity; end; procedure TThriftListImpl.Delete(Index: Integer); -- cgit v1.2.1