source: Generics/NativeGenerics/Generic/GenericDictionary.pas

Last change on this file was 496, checked in by chronos, 7 years ago
  • Modified: New native generics classes working under FPC 3.0 transformed from TemplateGenerics package.
File size: 2.7 KB
Line 
1unit GenericDictionary;
2
3{$mode delphi}
4
5interface
6
7uses
8 GenericList, fgl;
9
10type
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
44implementation
45
46constructor TGDictionary<TKey, TValue>.Create;
47begin
48 FList := TGList<TPair>.Create;
49end;
50
51destructor TGDictionary<TKey, TValue>.Destroy;
52begin
53 FList.Free;
54end;
55
56function TGDictionary<TKey, TValue>.GetCount: Integer;
57begin
58 Result := FList.Count;
59end;
60
61procedure TGDictionary<TKey, TValue>.SetCount(const AValue: Integer);
62begin
63 FList.Count := AValue;
64end;
65
66function TGDictionary<TKey, TValue>.GetKey(Index: Integer): TKey;
67begin
68 Result := FList.Items[Index].Key;
69end;
70
71function TGDictionary<TKey, TValue>.GetValue(Key: TKey): TValue;
72begin
73 Result := FList.Items[SearchKey(Key)].Value;
74end;
75
76procedure TGDictionary<TKey, TValue>.PutKey(Index: Integer;
77 const AValue: TKey);
78var
79 Item: TPair;
80begin
81 //Items[Index].Key := AValue;
82 Item := FList.Items[Index];
83 Item.Key := AValue;
84 FList.Items[Index] := Item;
85end;
86
87procedure TGDictionary<TKey, TValue>.PutValue(Key: TKey;
88 const AValue: TValue);
89var
90 Item: TPair;
91 Index: Integer;
92begin
93 //Items[SearchKey(Index)].Value := AValue;
94 Index := SearchKey(Key);
95 Item := FList.Items[Index];
96 Item.Value := AValue;
97 FList.Items[Index] := Item;
98end;
99
100function TGDictionary<TKey, TValue>.SearchKey(Key: TKey): Integer;
101begin
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;
109end;
110
111procedure TGDictionary<TKey, TValue>.Add(Key: TKey; Value: TValue);
112var
113 NewPair: TPair;
114begin
115 NewPair.Key := Key;
116 NewPair.Value := Value;
117 FList.Add(NewPair);
118end;
119
120end.
Note: See TracBrowser for help on using the repository browser.