View Issue Details

IDProjectCategoryView StatusLast Update
0035919FPCRTLpublic2019-08-02 15:17
ReporterMaciej IzakAssigned ToSven Barth 
PrioritynormalSeverityminorReproducibilityN/A
Status resolvedResolutionfixed 
Product Version3.3.1Product Build 
Target VersionFixed in Version3.3.1 
Summary0035919: [patch] TAVLTreeMap<TKey, TValue> Items property
DescriptionHello,

this patch is intended to allow usage of TAVLTreeMap<TKey, TValue> like TDictionary<TKey, TValue>. Test attached.
TagsNo tags attached.
Fixed in Revision42557
FPCOldBugId
FPCTarget-
Attached Files
  • AVLTreeMapItems.patch (3,267 bytes)
    Index: packages/rtl-generics/src/generics.collections.pas
    ===================================================================
    --- packages/rtl-generics/src/generics.collections.pas	(revision 42548)
    +++ packages/rtl-generics/src/generics.collections.pas	(working copy)
    @@ -679,7 +679,10 @@
         procedure ValueNotify(constref AValue: TValue; ACollectionNotification: TCollectionNotification); inline;
         procedure NodeNotify(ANode: PNode; ACollectionNotification: TCollectionNotification; ADispose: boolean); inline;
         procedure SetValue(var AValue: TValue; constref ANewValue: TValue);
    +    function GetItem(const AKey: TKey): TValue;
    +    procedure SetItem(const AKey: TKey; const AValue: TValue);
     
    +    property Items[Index: TKey]: TValue read GetItem write SetItem;
         // for reporting
         procedure WriteStr(AStream: TStream; const AText: string);
       public type
    @@ -782,6 +785,8 @@
       end;
     
       TAVLTreeMap<TKey, TValue> = class(TCustomAVLTreeMap<TKey, TValue, TEmptyRecord>)
    +  public
    +    property Items; default;
       end;
     
       TIndexedAVLTreeMap<TKey, TValue> = class(TCustomAVLTreeMap<TKey, TValue, SizeInt>)
    @@ -808,6 +813,7 @@
       protected
         property OnKeyNotify;
         property OnValueNotify;
    +    property Items;
       public type
         TItemEnumerator = TKeyEnumerator;
       public
    @@ -3319,6 +3325,21 @@
       Result := TValueCollection(FValues);
     end;
     
    +function TCustomAVLTreeMap<TREE_CONSTRAINTS>.GetItem(const AKey: TKey): TValue;
    +var
    +  LNode: PNode;
    +begin
    +  LNode := Find(AKey);
    +  if not Assigned(LNode) then
    +    raise EAVLTree.CreateRes(@SDictionaryKeyDoesNotExist);
    +  result := LNode.Value;
    +end;
    +
    +procedure TCustomAVLTreeMap<TREE_CONSTRAINTS>.SetItem(const AKey: TKey; const AValue: TValue);
    +begin
    +  Find(AKey).Value := AValue;
    +end;
    +
     constructor TCustomAVLTreeMap<TREE_CONSTRAINTS>.Create;
     begin
       FComparer := TComparer<TKey>.Default;
    Index: packages/rtl-generics/tests/tests.generics.trees.pas
    ===================================================================
    --- packages/rtl-generics/tests/tests.generics.trees.pas	(revision 42548)
    +++ packages/rtl-generics/tests/tests.generics.trees.pas	(working copy)
    @@ -42,6 +42,7 @@
         procedure Test_IndexedAVLTree_Add_General;
         procedure Test_IndexedAVLTree_Add;
         procedure Test_IndexedAVLTree_Delete;
    +    procedure Test_IndexedAVLTree_Items;
     
         procedure Test_TAVLTreeMap_Notification;
       end;
    @@ -50,6 +51,7 @@
     
     type
       TStringsTree = TIndexedAVLTree<string>;
    +  TMapTree = TAVLTreeMap<string, Integer>;
     
     { TTestTrees }
     
    @@ -138,6 +140,29 @@
       end;
     end;
     
    +procedure TTestTrees.Test_IndexedAVLTree_Items;
    +var
    +  LTree: TMapTree;
    +begin
    +  LTree := TMapTree.Create;
    +  try
    +    Check(LTree.Add('A', 1)<>nil);
    +    Check(LTree.Add('B', 2)<>nil);
    +    Check(LTree.Add('C', 3)<>nil);
    +    CheckEquals(LTree.Items['A'], 1);
    +    CheckEquals(LTree.Items['B'], 2);
    +    CheckEquals(LTree.Items['C'], 3);
    +    LTree.Items['A'] := 4;
    +    LTree.Items['B'] := 5;
    +    LTree.Items['C'] := 6;
    +    CheckEquals(LTree.Items['A'], 4);
    +    CheckEquals(LTree.Items['B'], 5);
    +    CheckEquals(LTree.Items['C'], 6);
    +  finally
    +    LTree.Free;
    +  end;
    +end;
    +
     procedure TTestTrees.Test_TAVLTreeMap_Notification;
     var
       LTree: TAVLTreeMap<string, string>;
    
    AVLTreeMapItems.patch (3,267 bytes)

Activities

Maciej Izak

2019-08-01 14:11

reporter  

AVLTreeMapItems.patch (3,267 bytes)
Index: packages/rtl-generics/src/generics.collections.pas
===================================================================
--- packages/rtl-generics/src/generics.collections.pas	(revision 42548)
+++ packages/rtl-generics/src/generics.collections.pas	(working copy)
@@ -679,7 +679,10 @@
     procedure ValueNotify(constref AValue: TValue; ACollectionNotification: TCollectionNotification); inline;
     procedure NodeNotify(ANode: PNode; ACollectionNotification: TCollectionNotification; ADispose: boolean); inline;
     procedure SetValue(var AValue: TValue; constref ANewValue: TValue);
+    function GetItem(const AKey: TKey): TValue;
+    procedure SetItem(const AKey: TKey; const AValue: TValue);
 
+    property Items[Index: TKey]: TValue read GetItem write SetItem;
     // for reporting
     procedure WriteStr(AStream: TStream; const AText: string);
   public type
@@ -782,6 +785,8 @@
   end;
 
   TAVLTreeMap<TKey, TValue> = class(TCustomAVLTreeMap<TKey, TValue, TEmptyRecord>)
+  public
+    property Items; default;
   end;
 
   TIndexedAVLTreeMap<TKey, TValue> = class(TCustomAVLTreeMap<TKey, TValue, SizeInt>)
@@ -808,6 +813,7 @@
   protected
     property OnKeyNotify;
     property OnValueNotify;
+    property Items;
   public type
     TItemEnumerator = TKeyEnumerator;
   public
@@ -3319,6 +3325,21 @@
   Result := TValueCollection(FValues);
 end;
 
+function TCustomAVLTreeMap<TREE_CONSTRAINTS>.GetItem(const AKey: TKey): TValue;
+var
+  LNode: PNode;
+begin
+  LNode := Find(AKey);
+  if not Assigned(LNode) then
+    raise EAVLTree.CreateRes(@SDictionaryKeyDoesNotExist);
+  result := LNode.Value;
+end;
+
+procedure TCustomAVLTreeMap<TREE_CONSTRAINTS>.SetItem(const AKey: TKey; const AValue: TValue);
+begin
+  Find(AKey).Value := AValue;
+end;
+
 constructor TCustomAVLTreeMap<TREE_CONSTRAINTS>.Create;
 begin
   FComparer := TComparer<TKey>.Default;
Index: packages/rtl-generics/tests/tests.generics.trees.pas
===================================================================
--- packages/rtl-generics/tests/tests.generics.trees.pas	(revision 42548)
+++ packages/rtl-generics/tests/tests.generics.trees.pas	(working copy)
@@ -42,6 +42,7 @@
     procedure Test_IndexedAVLTree_Add_General;
     procedure Test_IndexedAVLTree_Add;
     procedure Test_IndexedAVLTree_Delete;
+    procedure Test_IndexedAVLTree_Items;
 
     procedure Test_TAVLTreeMap_Notification;
   end;
@@ -50,6 +51,7 @@
 
 type
   TStringsTree = TIndexedAVLTree<string>;
+  TMapTree = TAVLTreeMap<string, Integer>;
 
 { TTestTrees }
 
@@ -138,6 +140,29 @@
   end;
 end;
 
+procedure TTestTrees.Test_IndexedAVLTree_Items;
+var
+  LTree: TMapTree;
+begin
+  LTree := TMapTree.Create;
+  try
+    Check(LTree.Add('A', 1)<>nil);
+    Check(LTree.Add('B', 2)<>nil);
+    Check(LTree.Add('C', 3)<>nil);
+    CheckEquals(LTree.Items['A'], 1);
+    CheckEquals(LTree.Items['B'], 2);
+    CheckEquals(LTree.Items['C'], 3);
+    LTree.Items['A'] := 4;
+    LTree.Items['B'] := 5;
+    LTree.Items['C'] := 6;
+    CheckEquals(LTree.Items['A'], 4);
+    CheckEquals(LTree.Items['B'], 5);
+    CheckEquals(LTree.Items['C'], 6);
+  finally
+    LTree.Free;
+  end;
+end;
+
 procedure TTestTrees.Test_TAVLTreeMap_Notification;
 var
   LTree: TAVLTreeMap<string, string>;
AVLTreeMapItems.patch (3,267 bytes)

Sven Barth

2019-08-02 15:17

manager   ~0117543

Please test and close if okay.

Issue History

Date Modified Username Field Change
2019-08-01 14:11 Maciej Izak New Issue
2019-08-01 14:11 Maciej Izak File Added: AVLTreeMapItems.patch
2019-08-02 14:37 Sven Barth Assigned To => Sven Barth
2019-08-02 14:37 Sven Barth Status new => assigned
2019-08-02 15:17 Sven Barth Status assigned => resolved
2019-08-02 15:17 Sven Barth Resolution open => fixed
2019-08-02 15:17 Sven Barth Fixed in Version => 3.3.1
2019-08-02 15:17 Sven Barth Fixed in Revision => 42557
2019-08-02 15:17 Sven Barth FPCTarget => -
2019-08-02 15:17 Sven Barth Note Added: 0117543