Changeset 320 for Generics/NativeGenerics/Units/GenericDictionary.pas
- Timestamp:
- Feb 7, 2012, 2:03:20 PM (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
Generics/NativeGenerics/Units/GenericDictionary.pas
r132 r320 14 14 end; 15 15 16 TGDictionary<T Pair> = class(TGList<TPair>)16 TGDictionary<TKey, TValue> = class 17 17 private 18 18 type 19 TGDictionaryIndex = Integer; 19 TIndex = NativeInt; 20 TDictionaryPair = TGPair<TKey, TValue>; 20 21 var 21 function GetKey(Index: TGDictionaryIndex): TPair.TKey; 22 function GetValue(Key: TPair.TKey): TPair.TValue; 23 procedure PutKey(Index: TGDictionaryIndex; const AValue: TPair.TKey); 24 procedure PutValue(Key: TPair.TKey; const AValue: TPair.TValue); 22 FList: TGList<TDictionaryPair>; 23 function GetKey(Index: TIndex): TKey; 24 function GetValue(Key: TKey): TValue; 25 procedure PutKey(Index: TIndex; const AValue: TKey); 26 procedure PutValue(Key: TKey; const AValue: TValue); 25 27 public 26 function SearchKey(Key: TPair.TKey): TGDictionaryIndex; 27 procedure Add(Key: TPair.TKey; Value: TPair.TValue); 28 property Values[Index: TPair.TKey]: TPair.TValue 28 constructor Create; 29 destructor Destroy; override; 30 function SearchKey(Key: TKey): TIndex; 31 procedure Add(Key: TKey; Value: TValue); 32 property Values[Index: TKey]: TValue 29 33 read GetValue write PutValue; 30 property Keys[Index: T GDictionaryIndex]: TPair.TKey34 property Keys[Index: TIndex]: TKey 31 35 read GetKey write PutKey; 36 property List: TGList<TDictionaryPair> read FList; 32 37 end; 33 38 … … 35 40 36 41 37 function TGDictionary<TPair>.GetKey(Index: TGDictionaryIndex): TPair.TKey;42 constructor TGDictionary<TKey, TValue>.Create; 38 43 begin 39 Result := Items[Index].Key;44 FList := TGList<TDictionaryPair>.Create; 40 45 end; 41 46 42 function TGDictionary<TPair>.GetValue(Key: TPair.TKey): TPair.TValue;47 destructor TGDictionary<TKey, TValue>.Destroy; 43 48 begin 44 Result := Items[SearchKey(Key)].Value;49 FList.Free; 45 50 end; 46 51 47 procedure TGDictionary<TPair>.PutKey(Index: TGDictionaryIndex; 48 const AValue: TPair.TKey); 52 function TGDictionary<TKey, TValue>.GetKey(Index: TIndex): TKey; 53 begin 54 Result := FList.Items[Index].Key; 55 end; 56 57 function TGDictionary<TKey, TValue>.GetValue(Key: TKey): TValue; 58 begin 59 Result := FList.Items[SearchKey(Key)].Value; 60 end; 61 62 procedure TGDictionary<TKey, TValue>.PutKey(Index: TIndex; 63 const AValue: TKey); 49 64 var 50 Item: T Pair;65 Item: TDictionaryPair; 51 66 begin 52 67 //Items[Index].Key := AValue; 53 Item := Items[Index];68 Item := FList.Items[Index]; 54 69 Item.Key := AValue; 55 Items[Index] := Item;70 FList.Items[Index] := Item; 56 71 end; 57 72 58 procedure TGDictionary<T Pair>.PutValue(Key: TPair.TKey;59 const AValue: T Pair.TValue);73 procedure TGDictionary<TKey, TValue>.PutValue(Key: TKey; 74 const AValue: TValue); 60 75 var 61 Item: T Pair;62 Index: T GDictionaryIndex;76 Item: TDictionaryPair; 77 Index: TIndex; 63 78 begin 64 79 //Items[SearchKey(Index)].Value := AValue; 65 80 Index := SearchKey(Key); 66 Item := Items[Index];81 Item := FList.Items[Index]; 67 82 Item.Value := AValue; 68 Items[Index] := Item;83 FList.Items[Index] := Item; 69 84 end; 70 85 71 function TGDictionary<T Pair>.SearchKey(Key: TPair.TKey): TGDictionaryIndex;86 function TGDictionary<TKey, TValue>.SearchKey(Key: TKey): TIndex; 72 87 begin 73 88 Result := 0; 74 while Result < Count do begin75 if Items[Result].Key = Key then begin89 while Result < FList.Count do begin 90 if FList.Items[Result].Key = Key then begin 76 91 Break; 77 92 end; … … 80 95 end; 81 96 82 procedure TGDictionary<T Pair>.Add(Key: TPair.TKey; Value: TPair.TValue);97 procedure TGDictionary<TKey, TValue>.Add(Key: TKey; Value: TValue); 83 98 var 84 NewPair: T Pair;99 NewPair: TDictionaryPair; 85 100 begin 86 101 NewPair.Key := Key; 87 102 NewPair.Value := Value; 88 inheritedAdd(NewPair);103 FList.Add(NewPair); 89 104 end; 90 105
Note:
See TracChangeset
for help on using the changeset viewer.