| 1 | {$IFDEF INTERFACE}
|
|---|
| 2 |
|
|---|
| 3 | TGDictionary = class;
|
|---|
| 4 |
|
|---|
| 5 | TGPair = record
|
|---|
| 6 | Key: TGPairKey;
|
|---|
| 7 | Value: TGPairValue;
|
|---|
| 8 | end;
|
|---|
| 9 |
|
|---|
| 10 | {$DEFINE TGListIndex := TGDictionaryIndex}
|
|---|
| 11 | {$DEFINE TGListItem := TGPair}
|
|---|
| 12 | {$DEFINE TGList := TGDictionaryList}
|
|---|
| 13 | {$DEFINE TGListSortCompare := TGDictionarySortCompare}
|
|---|
| 14 | {$DEFINE TGListToStringConverter := TGDictionaryToStringConverter}
|
|---|
| 15 | {$DEFINE TGListFromStringConverter := TGDictionaryFromStringConverter}
|
|---|
| 16 | {$DEFINE TGListItemArray := TGDictionaryItemArray}
|
|---|
| 17 | {$DEFINE INTERFACE}
|
|---|
| 18 | {$I 'GenericList.inc'}
|
|---|
| 19 |
|
|---|
| 20 | // TGDictionary<TGDictionaryIndex, TGPair<TGPairKey, TGPairValue>> = class(TGList)
|
|---|
| 21 | TGDictionary = class(TGList)
|
|---|
| 22 | private
|
|---|
| 23 | function GetKey(Index: TGDictionaryIndex): TGPairKey;
|
|---|
| 24 | function GetValue(Key: TGPairKey): TGPairValue;
|
|---|
| 25 | procedure PutKey(Index: TGDictionaryIndex; const AValue: TGPairKey);
|
|---|
| 26 | procedure PutValue(Key: TGPairKey; const AValue: TGPairValue);
|
|---|
| 27 | public
|
|---|
| 28 | function SearchKey(Key: TGPairKey): TGDictionaryIndex;
|
|---|
| 29 | procedure Add(Key: TGPairKey; Value: TGPairValue);
|
|---|
| 30 | property Values[Index: TGPairKey]: TGPairValue
|
|---|
| 31 | read GetValue write PutValue;
|
|---|
| 32 | property Keys[Index: TGDictionaryIndex]: TGPairKey
|
|---|
| 33 | read GetKey write PutKey;
|
|---|
| 34 | end;
|
|---|
| 35 |
|
|---|
| 36 | {$UNDEF INTERFACE}
|
|---|
| 37 | {$ENDIF}
|
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 | {$IFDEF IMPLEMENTATION_USES}
|
|---|
| 41 | {$I '..\Generic\GenericList.inc'}
|
|---|
| 42 | {$UNDEF IMPLEMENTATION_USES}
|
|---|
| 43 | {$ENDIF}
|
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 | {$IFDEF IMPLEMENTATION}
|
|---|
| 47 |
|
|---|
| 48 | {$DEFINE TGListIndex := TGDictionaryIndex}
|
|---|
| 49 | {$DEFINE TGListItem := TGPair}
|
|---|
| 50 | {$DEFINE TGList := TGDictionaryList}
|
|---|
| 51 | {$DEFINE TGListSortCompare := TGDictionarySortCompare}
|
|---|
| 52 | {$DEFINE TGListToStringConverter := TGDictionaryToStringConverter}
|
|---|
| 53 | {$DEFINE TGListFromStringConverter := TGDictionaryFromStringConverter}
|
|---|
| 54 | {$DEFINE TGListItemArray := TGDictionaryItemArray}
|
|---|
| 55 | {$DEFINE IMPLEMENTATION}
|
|---|
| 56 | {$I 'GenericList.inc'}
|
|---|
| 57 |
|
|---|
| 58 | function TGDictionary.GetKey(Index: TGDictionaryIndex): TGPairKey;
|
|---|
| 59 | begin
|
|---|
| 60 | Result := Items[Index].Key;
|
|---|
| 61 | end;
|
|---|
| 62 |
|
|---|
| 63 | function TGDictionary.GetValue(Key: TGPairKey): TGPairValue;
|
|---|
| 64 | begin
|
|---|
| 65 | Result := Items[SearchKey(Key)].Value;
|
|---|
| 66 | end;
|
|---|
| 67 |
|
|---|
| 68 | procedure TGDictionary.PutKey(Index: TGDictionaryIndex;
|
|---|
| 69 | const AValue: TGPairKey);
|
|---|
| 70 | var
|
|---|
| 71 | Item: TGPair;
|
|---|
| 72 | begin
|
|---|
| 73 | //Items[Index].Key := AValue;
|
|---|
| 74 | Item := Items[Index];
|
|---|
| 75 | Item.Key := AValue;
|
|---|
| 76 | Items[Index] := Item;
|
|---|
| 77 | end;
|
|---|
| 78 |
|
|---|
| 79 | procedure TGDictionary.PutValue(Key: TGPairKey;
|
|---|
| 80 | const AValue: TGPairValue);
|
|---|
| 81 | var
|
|---|
| 82 | Item: TGPair;
|
|---|
| 83 | Index: TGDictionaryIndex;
|
|---|
| 84 | begin
|
|---|
| 85 | //Items[SearchKey(Index)].Value := AValue;
|
|---|
| 86 | Index := SearchKey(Key);
|
|---|
| 87 | Item := Items[Index];
|
|---|
| 88 | Item.Value := AValue;
|
|---|
| 89 | Items[Index] := Item;
|
|---|
| 90 | end;
|
|---|
| 91 |
|
|---|
| 92 | function TGDictionary.SearchKey(Key: TGPairKey): TGDictionaryIndex;
|
|---|
| 93 | begin
|
|---|
| 94 | Result := 0;
|
|---|
| 95 | while Result < Count do begin
|
|---|
| 96 | if Items[Result].Key = Key then begin
|
|---|
| 97 | Break;
|
|---|
| 98 | end;
|
|---|
| 99 | Result := Result + 1;
|
|---|
| 100 | end;
|
|---|
| 101 | end;
|
|---|
| 102 |
|
|---|
| 103 | procedure TGDictionary.Add(Key: TGPairKey; Value: TGPairValue);
|
|---|
| 104 | var
|
|---|
| 105 | NewPair: TGPair;
|
|---|
| 106 | begin
|
|---|
| 107 | NewPair.Key := Key;
|
|---|
| 108 | NewPair.Value := Value;
|
|---|
| 109 | inherited Add(NewPair);
|
|---|
| 110 | end;
|
|---|
| 111 |
|
|---|
| 112 | {$UNDEF IMPLEMENTATION}
|
|---|
| 113 | {$ENDIF}
|
|---|