-
Notifications
You must be signed in to change notification settings - Fork 19
/
uHashedStringList.pas
361 lines (321 loc) · 12.8 KB
/
uHashedStringList.pas
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
{******************************************************************************}
{ }
{ Unit: uHashedStringList.pas }
{ }
{ Scope: TStringList replacement }
{ }
{ Info: implements almost all methods of TStringList, easily extendable }
{ }
{ Copyright© Dorin Duminica }
{ }
{******************************************************************************}
unit uHashedStringList;
interface
uses
SysUtils,
Classes;
type
PStringHashRec = ^TStringHashRec;
TStringHashRec = record
Value: String;
HashSensitive: Integer;
HashInsensitive: Integer;
end;// TStringHashRec = record
PStringRec = ^TStringRec;
TStringRec = record
StringValue: PStringHashRec;
Value: PStringHashRec;
ObjectRef: TObject;
end;// TStringEntry = record
type
TurboHashedStringList = class
private
FList: TList;
function GetValue(Name: String; bCaseSensitive: Boolean): String;
procedure SetValue(Name: String; bCaseSensitive: Boolean;
const Value: String);
function GetItem(Index: Integer): PStringRec;
function GetText(Index: Integer): String;
procedure SetItem(Index: Integer; const Value: PStringRec);
procedure SetText(Index: Integer; const Value: String);
public
constructor Create;
destructor Destroy; OVERRIDE;
public
function Add(const s: String; const Value: String = ''): Integer; OVERLOAD;
function Add(const s: String; AObject: TObject): Integer; OVERLOAD;
function StringExists(const s: String): Boolean; OVERLOAD;
function Append(const s: String; const Value: String = ''): Integer; OVERLOAD;
function Append(const s: String; AObject: TObject): Integer; OVERLOAD;
function StringExists(const s: String; var atIndex: Integer): Boolean; OVERLOAD;
function Count: Integer;
function IndexOfName(const s: String): Integer; OVERLOAD;
function IndexOfName(const s: String; bCaseSensitive: Boolean): Integer; OVERLOAD;
function IndexOfValue(const s: String): Integer; OVERLOAD;
function IndexOfValue(const s: String; bCaseSensitive: Boolean): Integer; OVERLOAD;
function StringExists(const s: String; var atIndex: Integer;
const bCaseSensitive: Boolean): Boolean; OVERLOAD;
function ValueExists(const s: String): Boolean; OVERLOAD;
function ValueExists(const s: String; var atIndex: Integer): Boolean; OVERLOAD;
function ValueExists(const s: String; var atIndex: Integer;
const bCaseSensitive: Boolean): Boolean; OVERLOAD;
procedure Clear;
procedure Delete(Index: Integer; const bFreeObject: Boolean = False);
procedure Exchange(Index1, Index2: Integer);
procedure Insert(Index: Integer; const s: String; const Value: String = ''); OVERLOAD;
procedure Insert(Index: Integer; const s: String; AObject: TObject); OVERLOAD;
public
property Values[Name: String; bCaseSensitive: Boolean]: String
read GetValue write SetValue;
property Items[Index: Integer]: PStringRec
read GetItem write SetItem;
property Strings[Index: Integer]: String
read GetText write SetText; DEFAULT;
end;// TurboHashedStringList = class
implementation
uses Math;
function HashStringInsensitive(const Value: string): Integer;
var
Index : Integer;
begin
Result := 0;;
for Index := 1 to Length(Value) do
Result := ((Result shl 7) or (Result shr 25)) + Ord(UpCase(Value[Index]));
end;// function HashStringInsensitive(const Value: string): Integer;
function HashStringSensitive(const Value: string): Integer;
var
Index : Integer;
begin
Result := 0;;
for Index := 1 to Length(Value) do
Result := ((Result shl 7) or (Result shr 25)) + Ord(Value[Index]);
end;// function HashStringSensitive(const Value: string): Integer;
{ TurboHashedStringList }
function TurboHashedStringList.Add(const s, Value: String): Integer;
var
StringData: PStringRec;
begin
New(StringData);
New(StringData.StringValue);
New(StringData.Value);
StringData.StringValue.Value := s;
StringData.StringValue.HashSensitive := HashStringSensitive(s);
StringData.StringValue.HashInsensitive := HashStringInsensitive(s);
StringData.Value.Value := Value;
StringData.Value.HashSensitive := HashStringSensitive(Value);
StringData.Value.HashInsensitive := HashStringInsensitive(Value);
Result := FList.Add(StringData)
end;// function TurboHashedStringList.Add(const s, Value: String): Integer;
function TurboHashedStringList.Add(const s: String;
AObject: TObject): Integer;
begin
Result := Add(s);
PStringRec(FList[Result]).ObjectRef := AObject;
end;// function TurboHashedStringList.Add(const s: String;
function TurboHashedStringList.Append(const s, Value: String): Integer;
begin
Result := Add(s, Value);
end;// function TurboHashedStringList.Append(const s, Value: String): Integer;
function TurboHashedStringList.Append(const s: String;
AObject: TObject): Integer;
begin
Result := Add(s, AObject);
end;// function TurboHashedStringList.Append(const s: String;
procedure TurboHashedStringList.Clear;
var
Index: Integer;
StringData: PStringRec;
begin
for Index := FList.Count -1 downto 0 do
Delete(Index);
end;// procedure TurboHashedStringList.Clear;
function TurboHashedStringList.Count: Integer;
begin
Result := FList.Count;
end;// function TurboHashedStringList.Count: Integer;
constructor TurboHashedStringList.Create;
begin
FList := TList.Create;
end;// constructor TurboHashedStringList.Create;
procedure TurboHashedStringList.Delete(Index: Integer;
const bFreeObject: Boolean);
var
StringData: PStringRec;
Obj: TObject;
begin
StringData := FList[Index];
if bFreeObject then begin
Obj := StringData.ObjectRef;
FreeAndNil(Obj);
end;// if bFreeObject then begin
Dispose(StringData.StringValue);
Dispose(StringData.Value);
Dispose(StringData);
FList.Delete(Index);
end;// procedure TurboHashedStringList.Delete(Index: Integer;
destructor TurboHashedStringList.Destroy;
begin
Clear;
FreeAndNil(FList);
end;// destructor TurboHashedStringList.Destroy;
procedure TurboHashedStringList.Exchange(Index1, Index2: Integer);
var
Item1: PStringRec;
Item2: PStringRec;
TempI: PStringRec;
begin
Item1 := FList[Index1];
Item2 := FList[Index2];
TempI := Item1;
Item1 := Item2;
Item2 := TempI;
end;// procedure TurboHashedStringList.Exchange(Index1, Index2: Integer);
function TurboHashedStringList.GetItem(Index: Integer): PStringRec;
begin
Result := FList[Index];
end;// function TurboHashedStringList.GetItem(Index: Integer): PStringRec;
function TurboHashedStringList.GetText(Index: Integer): String;
begin
Result := PStringRec(FList[Index]).StringValue.Value;
end;// function TurboHashedStringList.GetText(Index: Integer): String;
function TurboHashedStringList.GetValue(Name: String;
bCaseSensitive: Boolean): String;
var
Index: Integer;
begin
Result := EmptyStr;
if StringExists(Name, Index, bCaseSensitive) then
Result := PStringRec(FList[Index]).Value.Value;
end;// function TurboHashedStringList.GetValue(Name: String;
procedure TurboHashedStringList.Insert(Index: Integer; const s, Value: String);
begin
Add(s, Value);
Exchange(Index, FList.Count -1);
end;// procedure TurboHashedStringList.Insert(Index: Integer; const s, Value: String);
function TurboHashedStringList.IndexOfName(const s: String): Integer;
begin
Result := IndexOfName(s, False);
end;// function TurboHashedStringList.IndexOfName(const s: String): Integer;
function TurboHashedStringList.IndexOfName(const s: String;
bCaseSensitive: Boolean): Integer;
begin
StringExists(s, Result, bCaseSensitive);
end;// function TurboHashedStringList.IndexOfName(const s: String;
function TurboHashedStringList.IndexOfValue(const s: String): Integer;
begin
Result := IndexOfValue(s, False);
end;// function TurboHashedStringList.IndexOfValue(const s: String): Integer;
function TurboHashedStringList.IndexOfValue(const s: String;
bCaseSensitive: Boolean): Integer;
begin
ValueExists(s, Result, bCaseSensitive);
end;// function TurboHashedStringList.IndexOfValue(const s: String;
procedure TurboHashedStringList.Insert(Index: Integer; const s: String;
AObject: TObject);
begin
Add(s, AObject);
Exchange(Index, FList.Count -1);
end;// procedure TurboHashedStringList.Insert(Index: Integer; const s: String;
procedure TurboHashedStringList.SetItem(Index: Integer;
const Value: PStringRec);
var
StringData: PStringRec;
begin
StringData := FList[Index];
Dispose(StringData);
FList[Index] := Value;
end;// procedure TurboHashedStringList.SetItem(Index: Integer;
procedure TurboHashedStringList.SetText(Index: Integer;
const Value: String);
var
StringData: PStringRec;
begin
StringData := FList[Index];
StringData.StringValue.Value := Value;
StringData.StringValue.HashSensitive := HashStringSensitive(Value);
StringData.StringValue.HashInsensitive := HashStringInsensitive(Value);
end;// procedure TurboHashedStringList.SetText(Index: Integer;
procedure TurboHashedStringList.SetValue(Name: String;
bCaseSensitive: Boolean; const Value: String);
var
Index: Integer;
StringData: PStringRec;
begin
if StringExists(Name, Index, bCaseSensitive) then begin
StringData := FList[Index];
StringData.Value.Value := Value;
StringData.Value.HashSensitive := HashStringSensitive(Value);
StringData.Value.HashInsensitive := HashStringInsensitive(Value);
end;// if StringExists(Name, Index, bCaseSensitive) then begin
end;// procedure TurboHashedStringList.SetValue(Name: String;
function TurboHashedStringList.StringExists(const s: String;
var atIndex: Integer; const bCaseSensitive: Boolean): Boolean;
var
Index: Integer;
Hash: Integer;
begin
Result := True;
if bCaseSensitive then begin
Hash := HashStringSensitive(s);
for Index := 0 to FList.Count -1 do
if PStringRec(FList[Index]).StringValue.HashSensitive = Hash then begin
atIndex := Index;
Exit;
end;// if PStringRec(FList[Index]).StringValue.HashSensitive = Hash then begin
end else begin
Hash := HashStringInsensitive(s);
for Index := 0 to FList.Count -1 do
if PStringRec(FList[Index]).StringValue.HashInsensitive = Hash then begin
atIndex := Index;
Exit;
end;// if PStringRec(FList[Index]).StringValue.HashInsensitive = Hash then begin
end;// if bCaseSensitive then begin
Result := False;
end;// function TurboHashedStringList.StringExists(const s: String;
function TurboHashedStringList.StringExists(const s: String): Boolean;
var
Index: Integer;
begin
Result := StringExists(s, Index);
end;// function TurboHashedStringList.StringExists(const s: String): Boolean;
function TurboHashedStringList.StringExists(const s: String;
var atIndex: Integer): Boolean;
begin
Result := StringExists(s, atIndex, False);
end;// function TurboHashedStringList.StringExists(const s: String;
function TurboHashedStringList.ValueExists(const s: String;
var atIndex: Integer; const bCaseSensitive: Boolean): Boolean;
var
Index: Integer;
Hash: Integer;
begin
Result := True;
if bCaseSensitive then begin
Hash := HashStringSensitive(s);
for Index := 0 to FList.Count -1 do
if PStringRec(FList[Index]).Value.HashSensitive = Hash then begin
atIndex := Index;
Exit;
end;// if PStringRec(FList[Index]).Value.HashSensitive = Hash then begin
end else begin
Hash := HashStringInsensitive(s);
for Index := 0 to FList.Count -1 do
if PStringRec(FList[Index]).Value.HashInsensitive = Hash then begin
atIndex := Index;
Exit;
end;// if PStringRec(FList[Index]).Value.HashInsensitive = Hash then begin
end;// if bCaseSensitive then begin
Result := False;
end;// function TurboHashedStringList.ValueExists(const s: String;
function TurboHashedStringList.ValueExists(const s: String;
var atIndex: Integer): Boolean;
begin
Result := ValueExists(s, atIndex, False);
end;// function TurboHashedStringList.ValueExists(const s: String;
function TurboHashedStringList.ValueExists(const s: String): Boolean;
var
Index: Integer;
begin
Result := ValueExists(s, Index);
end;// function TurboHashedStringList.ValueExists(const s: String): Boolean;
end.// unit uHashedStringList;