1 | unit GenericDictionary;
|
---|
2 |
|
---|
3 | {$mode delphi}
|
---|
4 |
|
---|
5 | interface
|
---|
6 |
|
---|
7 | uses
|
---|
8 | GenericList, fgl;
|
---|
9 |
|
---|
10 | type
|
---|
11 | TGPair<TKey, TValue> = record
|
---|
12 | Key: TKey;
|
---|
13 | Value: TValue;
|
---|
14 | end;
|
---|
15 |
|
---|
16 | // Construction of complext generic types not supported by FPC:
|
---|
17 | // TGDictionay<TKey, TValue> = class(TGList<TGPair<TKey, TValue>>)
|
---|
18 | TGDictionary<TKey, TValue> = class
|
---|
19 | private
|
---|
20 | type
|
---|
21 | TPair = TGPair<TKey, TValue>;
|
---|
22 | var
|
---|
23 | FList: TGList<TPair>;
|
---|
24 | function GetCount: Integer;
|
---|
25 | function GetKey(Index: Integer): TKey;
|
---|
26 | function GetValue(Key: TKey): TValue;
|
---|
27 | procedure SetCount(const AValue: Integer);
|
---|
28 | procedure PutKey(Index: Integer; const AValue: TKey);
|
---|
29 | procedure PutValue(Key: TKey; const AValue: TValue);
|
---|
30 | public
|
---|
31 | function SearchKey(Key: TKey): Integer;
|
---|
32 | procedure Add(Key: TKey; Value: TValue);
|
---|
33 | constructor Create;
|
---|
34 | destructor Destroy; override;
|
---|
35 | property Values[Index: TKey]: TValue
|
---|
36 | read GetValue write PutValue;
|
---|
37 | property Keys[Index: Integer]: TKey
|
---|
38 | read GetKey write PutKey;
|
---|
39 | property List: TGList<TPair> read FList;
|
---|
40 | property Count: Integer read GetCount write SetCount;
|
---|
41 | end;
|
---|
42 |
|
---|
43 |
|
---|
44 | implementation
|
---|
45 |
|
---|
46 | constructor TGDictionary<TKey, TValue>.Create;
|
---|
47 | begin
|
---|
48 | FList := TGList<TPair>.Create;
|
---|
49 | end;
|
---|
50 |
|
---|
51 | destructor TGDictionary<TKey, TValue>.Destroy;
|
---|
52 | begin
|
---|
53 | FList.Free;
|
---|
54 | end;
|
---|
55 |
|
---|
56 | function TGDictionary<TKey, TValue>.GetCount: Integer;
|
---|
57 | begin
|
---|
58 | Result := FList.Count;
|
---|
59 | end;
|
---|
60 |
|
---|
61 | procedure TGDictionary<TKey, TValue>.SetCount(const AValue: Integer);
|
---|
62 | begin
|
---|
63 | FList.Count := AValue;
|
---|
64 | end;
|
---|
65 |
|
---|
66 | function TGDictionary<TKey, TValue>.GetKey(Index: Integer): TKey;
|
---|
67 | begin
|
---|
68 | Result := FList.Items[Index].Key;
|
---|
69 | end;
|
---|
70 |
|
---|
71 | function TGDictionary<TKey, TValue>.GetValue(Key: TKey): TValue;
|
---|
72 | begin
|
---|
73 | Result := FList.Items[SearchKey(Key)].Value;
|
---|
74 | end;
|
---|
75 |
|
---|
76 | procedure TGDictionary<TKey, TValue>.PutKey(Index: Integer;
|
---|
77 | const AValue: TKey);
|
---|
78 | var
|
---|
79 | Item: TPair;
|
---|
80 | begin
|
---|
81 | //Items[Index].Key := AValue;
|
---|
82 | Item := FList.Items[Index];
|
---|
83 | Item.Key := AValue;
|
---|
84 | FList.Items[Index] := Item;
|
---|
85 | end;
|
---|
86 |
|
---|
87 | procedure TGDictionary<TKey, TValue>.PutValue(Key: TKey;
|
---|
88 | const AValue: TValue);
|
---|
89 | var
|
---|
90 | Item: TPair;
|
---|
91 | Index: Integer;
|
---|
92 | begin
|
---|
93 | //Items[SearchKey(Index)].Value := AValue;
|
---|
94 | Index := SearchKey(Key);
|
---|
95 | Item := FList.Items[Index];
|
---|
96 | Item.Value := AValue;
|
---|
97 | FList.Items[Index] := Item;
|
---|
98 | end;
|
---|
99 |
|
---|
100 | function TGDictionary<TKey, TValue>.SearchKey(Key: TKey): Integer;
|
---|
101 | begin
|
---|
102 | Result := 0;
|
---|
103 | while Result < FList.Count do begin
|
---|
104 | if FList.Items[Result].Key = Key then begin
|
---|
105 | Break;
|
---|
106 | end;
|
---|
107 | Result := Result + 1;
|
---|
108 | end;
|
---|
109 | end;
|
---|
110 |
|
---|
111 | procedure TGDictionary<TKey, TValue>.Add(Key: TKey; Value: TValue);
|
---|
112 | var
|
---|
113 | NewPair: TPair;
|
---|
114 | begin
|
---|
115 | NewPair.Key := Key;
|
---|
116 | NewPair.Value := Value;
|
---|
117 | FList.Add(NewPair);
|
---|
118 | end;
|
---|
119 |
|
---|
120 | end.
|
---|