source: Generics/TemplateGenerics/Generic/GenericDictionary.inc@ 87

Last change on this file since 87 was 86, checked in by george, 15 years ago
  • Modified: In all files include directive changed to shorter form {$I 'file.inc'}.
  • Fixed: Export directory Generic as include dir.
File size: 2.7 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 := TDictionarySortCompare}
14{$DEFINE TGListStringConverter := TDictionaryStringConverter}
15{$DEFINE INTERFACE}
16{$I 'GenericList.inc'}
17
18 // TGDictionary<TGDictionaryIndex, TGPair<TGPairKey, TGPairValue>> = class(TGList)
19 TGDictionary = class(TGList)
20 private
21 function GetKey(Index: TGDictionaryIndex): TGPairKey;
22 function GetValue(Key: TGPairKey): TGPairValue;
23 procedure PutKey(Index: TGDictionaryIndex; const AValue: TGPairKey);
24 procedure PutValue(Key: TGPairKey; const AValue: TGPairValue);
25 public
26 function SearchKey(Key: TGPairKey): TGDictionaryIndex;
27 procedure Add(Key: TGPairKey; Value: TGPairValue);
28 property Values[Index: TGPairKey]: TGPairValue
29 read GetValue write PutValue;
30 property Keys[Index: TGDictionaryIndex]: TGPairKey
31 read GetKey write PutKey;
32 end;
33
34{$UNDEF INTERFACE}
35{$ENDIF}
36
37{$IFDEF IMPLEMENTATION}
38
39{$UNDEF IMPLEMENTATION}
40{$DEFINE IMPLEMENTATION_USES}
41{$I '..\Generic\GenericList.inc'}
42
43{$DEFINE TGListIndex := TGDictionaryIndex}
44{$DEFINE TGListItem := TGPair}
45{$DEFINE TGList := TGDictionaryList}
46{$DEFINE TGListSortCompare := TDictionarySortCompare}
47{$DEFINE TGListStringConverter := TDictionaryStringConverter}
48{$DEFINE IMPLEMENTATION}
49{$I 'GenericList.inc'}
50
51function TGDictionary.GetKey(Index: TGDictionaryIndex): TGPairKey;
52begin
53 Result := Items[Index].Key;
54end;
55
56function TGDictionary.GetValue(Key: TGPairKey): TGPairValue;
57begin
58 Result := Items[SearchKey(Key)].Value;
59end;
60
61procedure TGDictionary.PutKey(Index: TGDictionaryIndex;
62 const AValue: TGPairKey);
63var
64 Item: TGPair;
65begin
66 //Items[Index].Key := AValue;
67 Item := Items[Index];
68 Item.Key := AValue;
69 Items[Index] := Item;
70end;
71
72procedure TGDictionary.PutValue(Key: TGPairKey;
73 const AValue: TGPairValue);
74var
75 Item: TGPair;
76 Index: TGDictionaryIndex;
77begin
78 //Items[SearchKey(Index)].Value := AValue;
79 Index := SearchKey(Key);
80 Item := Items[Index];
81 Item.Value := AValue;
82 Items[Index] := Item;
83end;
84
85function TGDictionary.SearchKey(Key: TGPairKey): TGDictionaryIndex;
86begin
87 Result := 0;
88 while Result < Count do begin
89 if Items[Result].Key = Key then begin
90 Break;
91 end;
92 Result := Result + 1;
93 end;
94end;
95
96procedure TGDictionary.Add(Key: TGPairKey; Value: TGPairValue);
97var
98 NewPair: TGPair;
99begin
100 NewPair.Key := Key;
101 NewPair.Value := Value;
102 inherited Add(NewPair);
103end;
104
105{$UNDEF IMPLEMENTATION}
106{$ENDIF}
Note: See TracBrowser for help on using the repository browser.