Browse Source

* fix for Mantis #35919: apply patch by Maciej Izak

git-svn-id: trunk@42557 -
svenbarth 6 năm trước cách đây
mục cha
commit
50fec47b9a

+ 21 - 0
packages/rtl-generics/src/generics.collections.pas

@@ -679,7 +679,10 @@ type
     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 @@ type
   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 @@ type
   protected
     property OnKeyNotify;
     property OnValueNotify;
+    property Items;
   public type
     TItemEnumerator = TKeyEnumerator;
   public
@@ -3319,6 +3325,21 @@ begin
   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;

+ 25 - 0
packages/rtl-generics/tests/tests.generics.trees.pas

@@ -42,6 +42,7 @@ type
     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 @@ implementation
 
 type
   TStringsTree = TIndexedAVLTree<string>;
+  TMapTree = TAVLTreeMap<string, Integer>;
 
 { TTestTrees }
 
@@ -138,6 +140,29 @@ begin
   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>;