-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSystem.Generics.Dictionary.pas
More file actions
182 lines (149 loc) · 5.42 KB
/
Copy pathSystem.Generics.Dictionary.pas
File metadata and controls
182 lines (149 loc) · 5.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
unit System.Generics.Dictionary;
interface
uses
System.SysUtils, System.Generics.Collections, System.Generics.Defaults;
type
/// <remarks>
/// Provides access to dictionary.
/// </remarks>
IDictionary<TKey,TValue> = interface
/// <remarks>
/// Removes all items from the dictionary
/// </remarks>
procedure Clear;
/// <remarks>
/// Determines whether the IDictionary<TKey,TValue> contains an element with the specified key.
/// </remarks>
function ContainsKey(Key: TKey): Boolean;
/// <remarks>
/// Determines whether the IDictionary<TKey,TValue> contains an element with the specified value.
/// </remarks>
function ContainsValue(Value: TValue): Boolean;
/// <remarks>
/// Adds an element with the provided key and value to the IDictionary<TKey,TValue>.
/// </remarks>
procedure Add(Key: TKey; Value: TValue);
/// <remarks>
/// Adds or replace an element with the provided key and value to the IDictionary<TKey,TValue>.
/// </remarks>
procedure AddOrSetValue(Key: TKey; Value: TValue);
/// <remarks>
///
/// </remarks>
function TryGetValue(const Key: TKey; out Value: TValue): Boolean;
/// <remarks>
/// Returns an enumerator that iterates through a collection.
/// </remarks>
function GetEnumerator: System.Generics.Collections.TDictionary<TKey,TValue>.TPairEnumerator;
/// <remarks>
/// Gets the number of elements contained in the dictionary
/// </remarks>
function Count: Integer;
/// <remarks>
/// Gets the element with the specified key.
/// </remarks>
function GetItem(const Key: TKey): TValue;
/// <remarks>
/// Sets the element with the specified key.
/// </remarks>
procedure SetItem(const Key: TKey; const Value: TValue);
/// <remarks>
/// Gets or sets the element with the specified key.
/// </remarks>
property Items[const Key: TKey]: TValue read GetItem write SetItem; default;
end;
TDictionary<TKey,TValue> = class(TInterfacedObject, IDictionary<TKey,TValue>)
private
FDictionary: System.Generics.Collections.TDictionary<TKey,TValue>;
function GetItem(const Key: TKey): TValue;
procedure SetItem(const Key: TKey; const Value: TValue);
public
constructor Create(ACapacity: Integer = 0); overload;
constructor Create(const AComparer: IEqualityComparer<TKey>); overload;
constructor Create(ACapacity: Integer; const AComparer: IEqualityComparer<TKey>); overload;
constructor Create(const Collection: TEnumerable<TPair<TKey,TValue>>); overload;
constructor Create(const Collection: TEnumerable<TPair<TKey,TValue>>; const AComparer: IEqualityComparer<TKey>); overload;
destructor Destroy; override;
procedure Clear;
function ContainsKey(Key: TKey): Boolean;
function ContainsValue(Value: TValue): Boolean;
procedure Add(Key: TKey; Value: TValue);
procedure AddOrSetValue(Key: TKey; Value: TValue);
function Count: Integer;
function TryGetValue(const Key: TKey; out Value: TValue): Boolean;
function GetEnumerator: System.Generics.Collections.TDictionary<TKey,TValue>.TPairEnumerator;
end;
implementation
{ TDictionary<TKey, TValue> }
procedure TDictionary<TKey, TValue>.Add(Key: TKey; Value: TValue);
begin
FDictionary.Add(key, value);
end;
procedure TDictionary<TKey, TValue>.AddOrSetValue(Key: TKey; Value: TValue);
begin
FDictionary.AddOrSetValue(Key, Value);
end;
procedure TDictionary<TKey, TValue>.Clear;
begin
FDictionary.Clear;
end;
function TDictionary<TKey, TValue>.ContainsKey(Key: TKey): Boolean;
begin
Result := FDictionary.ContainsKey(Key);
end;
function TDictionary<TKey, TValue>.ContainsValue(Value: TValue): Boolean;
begin
FDictionary.ContainsValue(Value)
end;
function TDictionary<TKey, TValue>.Count: Integer;
begin
Result := FDictionary.Count;
end;
constructor TDictionary<TKey, TValue>.Create(ACapacity: Integer);
begin
inherited Create;
Create(ACapacity, Nil);
end;
constructor TDictionary<TKey, TValue>.Create(const AComparer: IEqualityComparer<TKey>);
begin
inherited Create;
FDictionary := System.Generics.Collections.TDictionary<TKey,TValue>.Create(AComparer);
end;
constructor TDictionary<TKey, TValue>.Create(ACapacity: Integer;
const AComparer: IEqualityComparer<TKey>);
begin
inherited Create;
FDictionary := System.Generics.Collections.TDictionary<TKey,TValue>.Create(ACapacity, AComparer);
end;
constructor TDictionary<TKey, TValue>.Create(const Collection: TEnumerable<TPair<TKey, TValue>>);
begin
inherited Create;
Create(Collection, Nil);
end;
constructor TDictionary<TKey, TValue>.Create(const Collection: TEnumerable<TPair<TKey, TValue>>; const AComparer: IEqualityComparer<TKey>);
begin
inherited Create;
FDictionary := System.Generics.Collections.TDictionary<TKey,TValue>.Create(Collection, AComparer);
end;
destructor TDictionary<TKey, TValue>.Destroy;
begin
FreeAndNIl(FDictionary);
inherited;
end;
function TDictionary<TKey, TValue>.GetEnumerator: System.Generics.Collections.TDictionary<TKey, TValue>.TPairEnumerator;
begin
Result := FDictionary.GetEnumerator;
end;
function TDictionary<TKey, TValue>.GetItem(const Key: TKey): TValue;
begin
Result := FDictionary[Key];
end;
procedure TDictionary<TKey, TValue>.SetItem(const Key: TKey; const Value: TValue);
begin
FDictionary[Key] := Value;
end;
function TDictionary<TKey, TValue>.TryGetValue(const Key: TKey; out Value: TValue): Boolean;
begin
Result := FDictionary.TryGetValue(Key, Value);
end;
end.