source: trunk/Packages/TemplateGenerics/Generic/GenericDictionary.inc

Last change on this file was 18, checked in by chronos, 12 years ago
  • Used external packages are now stored in uncompressed form rather in zipped files. This allow better package version synchronisation.
File size: 3.0 KB
Line 
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
58function TGDictionary.GetKey(Index: TGDictionaryIndex): TGPairKey;
59begin
60 Result := Items[Index].Key;
61end;
62
63function TGDictionary.GetValue(Key: TGPairKey): TGPairValue;
64begin
65 Result := Items[SearchKey(Key)].Value;
66end;
67
68procedure TGDictionary.PutKey(Index: TGDictionaryIndex;
69 const AValue: TGPairKey);
70var
71 Item: TGPair;
72begin
73 //Items[Index].Key := AValue;
74 Item := Items[Index];
75 Item.Key := AValue;
76 Items[Index] := Item;
77end;
78
79procedure TGDictionary.PutValue(Key: TGPairKey;
80 const AValue: TGPairValue);
81var
82 Item: TGPair;
83 Index: TGDictionaryIndex;
84begin
85 //Items[SearchKey(Index)].Value := AValue;
86 Index := SearchKey(Key);
87 Item := Items[Index];
88 Item.Value := AValue;
89 Items[Index] := Item;
90end;
91
92function TGDictionary.SearchKey(Key: TGPairKey): TGDictionaryIndex;
93begin
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;
101end;
102
103procedure TGDictionary.Add(Key: TGPairKey; Value: TGPairValue);
104var
105 NewPair: TGPair;
106begin
107 NewPair.Key := Key;
108 NewPair.Value := Value;
109 inherited Add(NewPair);
110end;
111
112{$UNDEF IMPLEMENTATION}
113{$ENDIF}
Note: See TracBrowser for help on using the repository browser.