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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
| ///////////////////////////////////////////////////////////////////////////
// //
// Copyright (c) 2015 by Charta Software B.V. //
// All rights reserved //
// //
// Version: 1.7.0.83525 //
// Web site: https://pascal.chartasoftware.com/ //
// //
// This code and information are provided "as is" without warranty of //
// any kind. Dissemination of this information or reproduction of //
// this material is strictly forbidden unless prior written permission //
// is obtained from Charta Software B.V.. //
// //
///////////////////////////////////////////////////////////////////////////
unit Dictionary;
interface
uses
Boolean,
Collection.List,
Color.Rgb,
Condition,
FloatingPoint._64,
Integer._32,
Memory.Block,
Text;
type
TDictionaryEntry = class(TObject)
public
Key: TText;
Value: TText;
Block: TMemoryBlock;
destructor Destroy(); override;
public
type
TKeyCondition = class(TCondition<TDictionaryEntry>)
private
Key: TText;
constructor Create(const Key: TText); reintroduce; virtual;
public
function Matches(const Entry: TDictionaryEntry): TBoolean; override;
end;
end;
TDictionary = class(TObject)
private
function GetCount(): TInteger32;
function EntryWithKey(const Key: TText): TDictionaryEntry;
public
Entries: TList<TDictionaryEntry>;
constructor Create(); virtual;
destructor Destroy(); override;
procedure Add(const Key: TText; const Value: TText); overload;
procedure Add(const Key: TText; Block: TMemoryBlock; const Value: TText); overload;
procedure Remove(const Key: TText);
function KeyExists(const Key: TText): TBoolean;
function GetTextValue(const Key: TText): TText;
function GetTextValueWithDefault(const Key: TText; const Default: TText): TText;
function GetInteger32Value(const Key: TText): TInteger32;
function GetInteger32ValueWithDefault(const Key: TText; const Default: TInteger32): TInteger32;
function GetFloatingPoint64Value(const Key: TText): TFloatingPoint64;
function GetBooleanValue(const Key: TText): TBoolean;
function GetBooleanValueWithDefault(const Key: TText; const Default: TBoolean): TBoolean;
function GetColorValue(const Key: TText): TRgbColor;
function GetColorValueWithDefault(const Key: TText; const Default: TRgbColor): TRgbColor;
function GetBlock(const Key: TText): TMemoryBlock;
property Count: TInteger32 read GetCount;
end;
implementation
uses
FloatingPoint._64.TextFormat,
Integer._32.TextFormat,
Object_.List,
Text.Collation;
{ TDictionary }
procedure TDictionary.Add(const Key: TText; const Value: TText);
begin
Add(Key, nil, Value);
end;
procedure TDictionary.Add(const Key: TText; Block: TMemoryBlock; const Value: TText);
var
Entry: TDictionaryEntry;
begin
Entry := TDictionaryEntry.Create();
Entry.Key := Key;
Entry.Block := Block;
Entry.Value := Value;
Entries.Add(Entry);
end;
constructor TDictionary.Create();
begin
inherited Create();
Entries := TObjectList<TDictionaryEntry>.Create();
end;
destructor TDictionary.Destroy();
begin
Entries.Free();
inherited Destroy();
end;
function TDictionary.EntryWithKey(const Key: TText): TDictionaryEntry;
begin
Result := Entries.Find(TDictionaryEntry.TKeyCondition.Create(Key));
end;
function TDictionary.GetBooleanValue(const Key: TText): TBoolean;
begin
Result := TTextCollation.DefaultCaseInsensitive.Equal(GetTextValue(Key), 'True');
end;
function TDictionary.GetBooleanValueWithDefault(const Key: TText; const Default: TBoolean): TBoolean;
var
Entry: TDictionaryEntry;
begin
Entry := EntryWithKey(Key);
if Entry <> nil then
Result := TTextCollation.DefaultCaseInsensitive.Equal(Entry.Value, 'True')
else
Result := Default;
end;
function TDictionary.GetColorValue(const Key: TText): TRgbColor;
begin
Result := TRgbColor.New(EntryWithKey(Key).Value);
end;
function TDictionary.GetColorValueWithDefault(const Key: TText; const Default: TRgbColor): TRgbColor;
var
Entry: TDictionaryEntry;
begin
Entry := EntryWithKey(Key);
if Entry <> nil then
Result := TRgbColor.New(Entry.Value)
else
Result := Default;
end;
function TDictionary.GetCount(): TInteger32;
begin
Result := Entries.Count;
end;
function TDictionary.GetFloatingPoint64Value(const Key: TText): TFloatingPoint64;
begin
Result := TextToFloatingPoint64(GetTextValue(Key));
end;
function TDictionary.GetInteger32Value(const Key: TText): TInteger32;
begin
Result := TextToInteger32(GetTextValue(Key));
end;
function TDictionary.GetInteger32ValueWithDefault(const Key: TText; const Default: TInteger32): TInteger32;
var
Entry: TDictionaryEntry;
begin
Entry := EntryWithKey(Key);
if Entry <> nil then
Result := TextToInteger32(Entry.Value)
else
Result := Default;
end;
function TDictionary.GetBlock(const Key: TText): TMemoryBlock;
var
Entry: TDictionaryEntry;
begin
Entry := EntryWithKey(Key);
Result := Entry.Block;
end;
function TDictionary.GetTextValue(const Key: TText): TText;
var
Entry: TDictionaryEntry;
begin
Entry := EntryWithKey(Key);
Result := Entry.Value;
end;
function TDictionary.GetTextValueWithDefault(const Key: TText; const Default: TText): TText;
var
Entry: TDictionaryEntry;
begin
Entry := EntryWithKey(Key);
if Entry <> nil then
Result := Entry.Value
else
Result := Default;
end;
function TDictionary.KeyExists(const Key: TText): TBoolean;
begin
Result := EntryWithKey(Key) <> nil;
end;
procedure TDictionary.Remove(const Key: TText);
var
Index: TInteger32;
begin
Index := Entries.IndexOf(TDictionaryEntry.TKeyCondition.Create(Key));
Entries.RemoveAt(Index);
end;
{ TDictionaryEntry }
destructor TDictionaryEntry.Destroy();
begin
Block.Free();
inherited Destroy();
end;
{ TDictionaryEntry.TKeyCondition }
constructor TDictionaryEntry.TKeyCondition.Create(const Key: TText);
begin
inherited Create();
Self.Key := Key;
end;
function TDictionaryEntry.TKeyCondition.Matches(const Entry: TDictionaryEntry): TBoolean;
begin
Result := Entry.Key = Key;
end;
end.
|