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