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

Last change on this file was 60, checked in by chronos, 12 years ago
File size: 2.5 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 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
50function TGDictionary.GetKey(Index: TGDictionaryIndex): TGPairKey;
51begin
52 Result := Items[Index].Key;
53end;
54
55function TGDictionary.GetValue(Key: TGPairKey): TGPairValue;
56begin
57 Result := Items[SearchKey(Key)].Value;
58end;
59
60procedure TGDictionary.PutKey(Index: TGDictionaryIndex;
61 const AValue: TGPairKey);
62var
63 Item: TGPair;
64begin
65 //Items[Index].Key := AValue;
66 Item := Items[Index];
67 Item.Key := AValue;
68 Items[Index] := Item;
69end;
70
71procedure TGDictionary.PutValue(Key: TGPairKey;
72 const AValue: TGPairValue);
73var
74 Item: TGPair;
75 Index: TGDictionaryIndex;
76begin
77 //Items[SearchKey(Index)].Value := AValue;
78 Index := SearchKey(Key);
79 Item := Items[Index];
80 Item.Value := AValue;
81 Items[Index] := Item;
82end;
83
84function TGDictionary.SearchKey(Key: TGPairKey): TGDictionaryIndex;
85begin
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;
93end;
94
95procedure TGDictionary.Add(Key: TGPairKey; Value: TGPairValue);
96var
97 NewPair: TGPair;
98begin
99 NewPair.Key := Key;
100 NewPair.Value := Value;
101 inherited Add(NewPair);
102end;
103
104{$UNDEF IMPLEMENTATION}
105{$ENDIF}
Note: See TracBrowser for help on using the repository browser.