Menu

[327ada]: / Source / VCL / SynEdit / SynGen / HashTableGen.pas  Maximize  Restore  History

Download this file

411 lines (359 with data), 9.8 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
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
unit HashTableGen;
{$I SynEdit.inc}
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ComCtrls;
type
TFrmHashTableGen = class(TForm)
LabelParams: TLabel;
LabelD: TLabel;
LabelC: TLabel;
LabelM: TLabel;
Label1: TLabel;
Label2: TLabel;
LabelPercentage: TLabel;
ProgressBar1: TProgressBar;
EditD: TMemo;
EditC: TMemo;
EditM: TMemo;
ButtonFindHash: TButton;
procedure ButtonFindHashClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
FKeyList: TList;
public
procedure AssignKeyWords(KeyList: TList; CaseSensitive: Boolean);
function GetHashKeyFunctionSource(ClassName: string): string;
function GetKeyWordConstantsSource(CaseSensitive: Boolean): string;
function KeyIndicesCount: Integer;
end;
const
MaxTableSize = 100000;
type
THashKeyList = class
private
FMaxHashKey: Integer;
FHashKeys: array[0..MaxTableSize - 1] of Boolean;
public
function Add(HashKey: Integer): Boolean;
procedure Clear;
end;
var
FrmHashTableGen: TFrmHashTableGen;
implementation
{$R *.dfm}
uses
StrUtils,
SynGenUnit,
SynUnicode;
{$I primenumbers.inc}
var
m: Cardinal;
FinalC, FinalD, FinalM: Cardinal;
searching: Boolean;
KeyWords: array of string;
HashKeyList: THashKeyList;
{$Q-}
function HashKey(const S: string; c, d: Cardinal): Cardinal;
var
i: Integer;
begin
Result := 0;
for i := 1 to Length(S) do
Result := Result * c + Ord(S[i]) * d;
Result := Result mod m;
end;
{$Q+}
{$Q-}
function FinalHashKey(const S: string): Cardinal;
var
i: Integer;
begin
Result := 0;
for i := 1 to Length(S) do
Result := Result * FinalC + Ord(S[i]) * FinalD;
Result := Result mod FinalM;
end;
{$Q+}
procedure WordWrapAtCol80(Words, Result: TStrings; Indentation: Integer);
var
WrappedLines: TStringList;
i: Integer;
Line: string;
begin
WrappedLines := TStringList.Create;
try
i := 0;
while i < Words.Count do
begin
Line := StringOfChar(' ', Indentation);
while (i < Words.Count) and (Length(Line) + Length(Words[i]) <= 80) do
begin
Line := Line + Words[i] + ' ';
inc(i);
end;
WrappedLines.Add(Line);
end;
Result.Assign(WrappedLines);
finally
WrappedLines.Free;
end;
end;
{ TFrmHashTableGen }
procedure TFrmHashTableGen.FormCreate(Sender: TObject);
begin
HashKeyList := THashKeyList.Create;
ProgressBar1.Max := 1000;
end;
procedure TFrmHashTableGen.FormDestroy(Sender: TObject);
begin
HashKeyList.Free;
end;
procedure TFrmHashTableGen.AssignKeyWords(KeyList: TList; CaseSensitive: Boolean);
var
i: Integer;
KeyWordsList: TStringList;
begin
FKeyList := nil;
SetLength(KeyWords, 0);
HashKeyList.Clear;
KeyWordsList := TStringList.Create;
try
KeyWordsList.Sorted := True;
KeyWordsList.Duplicates := dupIgnore;
for i := 0 to KeyList.Count - 1 do
KeyWordsList.Add(TLexKeys(KeyList[i]).KeyName);
SetLength(KeyWords, KeyWordsList.Count);
if CaseSensitive then
for i := 0 to KeyWordsList.Count - 1 do
KeyWords[i] := KeyWordsList[i]
else
for i := 0 to KeyWordsList.Count - 1 do
KeyWords[i] := SysUtils.AnsiLowerCase(KeyWordsList[i]);
FKeyList := KeyList;
finally
KeyWordsList.Free;
end;
end;
procedure TFrmHashTableGen.FormShow(Sender: TObject);
begin
m := 0;
FinalC := 0;
FinalD := 0;
FinalM := 0;
ProgressBar1.Position := 0;
EditD.Lines.Text := '0';
EditC.Lines.Text := '0';
EditM.Lines.Text := '0';
LabelPercentage.Caption := '0%';
ButtonFindHash.SetFocus;
ButtonFindHash.Caption := 'Find Hash Params';
end;
procedure TFrmHashTableGen.FormClose(Sender: TObject;
var Action: TCloseAction);
var
i: Integer;
begin
if Assigned(FKeyList) then
begin
for i := 0 to FKeyList.Count - 1 do
with TLexKeys(FKeyList[i]) do
begin
Key := FinalHashKey(SysUtils.AnsiLowerCase(KeyName));
end;
end;
end;
procedure TFrmHashTableGen.ButtonFindHashClick(Sender: TObject);
var
i, j, c, d: Integer;
collided: Boolean;
Key, smallestM: Cardinal;
procedure SearchStop;
begin
ButtonFindHash.Caption := 'Find Hash Params';
searching := False;
Close;
if FinalM = 0 then
raise Exception.Create('Cannot build the hashtable as no working hash parameters were found');
end;
begin
collided := False;
if searching then
begin
SearchStop;
Exit;
end
else
begin
ProgressBar1.Position := 0;
LabelPercentage.Caption := '0%';
Application.ProcessMessages;
if Length(KeyWords) = 0 then exit;
searching := True;
ButtonFindHash.Caption := 'Stop Search';
end;
smallestM := MaxTableSize + 1;
for d := 1 to 1000 do
begin
for c := 1 to 1000 do
for j := 0 to High(PrimeNumbers) do
begin
m := PrimeNumbers[j];
if m >= smallestM then
begin
m := smallestM;
Break;
end;
for i := Low(KeyWords) to High(KeyWords) do
begin
Key := HashKey(KeyWords[i], c, d);
collided := HashKeyList.Add(Key);
if collided then
begin
HashKeyList.Clear;
break;
end;
end;
if not collided then
begin
smallestM := m;
EditD.Lines.Text := IntToStr(d);
EditC.Lines.Text := IntToStr(c);
EditM.Lines.Text := IntToStr(m);
FinalD := d;
FinalC := c;
FinalM := m;
if m = Cardinal(Length(KeyWords)) then
begin
ProgressBar1.Position := ProgressBar1.Max;
LabelPercentage.Caption := '100%';
SearchStop;
Exit;
end;
break; // all the following solutions will only have a bigger array
end;
Application.ProcessMessages;
if not searching then
begin
SearchStop;
Exit;
end;
end;
ProgressBar1.Position := d;
LabelPercentage.Caption := FloatToStr(d / 10) + '%';
Application.ProcessMessages;
end;
SearchStop;
end;
function TFrmHashTableGen.GetHashKeyFunctionSource(ClassName: string): string;
begin
Result := '{$Q-}'#13#10;
Result := Result + Format('function %s.HashKey(Str: PWideChar): Cardinal;', [ClassName]) + #13#10;
Result := Result + 'begin'#13#10;
Result := Result + ' Result := 0;'#13#10;
Result := Result + ' while IsIdentChar(Str^) do'#13#10;
Result := Result + ' begin'#13#10;
if (FinalC = 1) and (FinalD = 1) then
Result := Result + ' Result := Result + Ord(Str^);'#13#10
else if FinalC = 1 then
Result := Result + Format(' Result := Result + Ord(Str^) * %d;', [FinalD]) + #13#10
else if FinalD = 1 then
Result := Result + Format(' Result := Result * %d + Ord(Str^);', [FinalC]) + #13#10
else
Result := Result + Format(' Result := Result * %d + Ord(Str^) * %d;', [FinalC, FinalD]) + #13#10;
Result := Result + ' inc(Str);'#13#10;
Result := Result + ' end;'#13#10;
Result := Result + ' Result := Result mod ' + IntToStr(FinalM) + ';'#13#10;
Result := Result + ' fStringLen := Str - fToIdent;'#13#10;
Result := Result + 'end;'#13#10;
Result := Result + '{$Q+}'#13#10;
end;
{$IFNDEF SYN_COMPILER_6_UP}
function DupeString(const AText: string; ACount: Integer): string;
var
P: PChar;
C: Integer;
begin
C := Length(AText);
SetLength(Result, C * ACount);
P := Pointer(Result);
if P = nil then Exit;
while ACount > 0 do
begin
Move(Pointer(AText)^, P^, C);
Inc(P, C);
Dec(ACount);
end;
end;
{$ENDIF}
function TFrmHashTableGen.GetKeyWordConstantsSource(CaseSensitive: Boolean): string;
var
i: Integer;
sl: TStringList;
LastItem: string;
begin
// write KeyWords
if not CaseSensitive then
Result := Result + ' // as this language is case-insensitive keywords *must* be in lowercase'#13#10;
Result := Result + Format(' KeyWords: array[0..%d] of string = (', [High(KeyWords)]) + #13#10;
sl := TStringList.Create;
try
for i := Low(KeyWords) to High(KeyWords) do
sl.Add(#39 + KeyWords[i] + #39 + ',');
if sl.Count > 0 then
begin
// remove comma from last line
LastItem := sl[sl.Count - 1];
Delete(LastItem, Length(LastItem), 1);
sl[sl.Count - 1] := LastItem;
WordWrapAtCol80(sl, sl, 4);
Result := Result + sl.Text;
end;
finally
sl.Free;
end;
Result := Result + ' );'#13#10;
Result := Result + #13#10;
// write KeyIndices
Result := Result + Format(' KeyIndices: array[0..%d] of Integer = (', [FinalM - 1]) + #13#10;
sl := TStringList.Create;
try
sl.Text := DupeString('-1,'#13#10, FinalM);
for i := Low(KeyWords) to High(KeyWords) do
sl[FinalHashKey(KeyWords[i])] := IntToStr(i) + ',';
if sl.Count > 0 then
begin
// remove comma from last line
LastItem := Trim(sl[sl.Count - 1]);
Delete(LastItem, Length(LastItem), 1);
sl[sl.Count - 1] := LastItem;
WordWrapAtCol80(sl, sl, 4);
Result := Result + sl.Text;
end;
finally
sl.Free;
end;
Result := Result + ' );'#13#10;
end;
function TFrmHashTableGen.KeyIndicesCount: Integer;
begin
Result := FinalM;
end;
{ THashKeyList }
function THashKeyList.Add(HashKey: Integer): Boolean;
begin
if HashKey > FMaxHashKey then
FMaxHashKey := HashKey;
Result := FHashKeys[HashKey];
FHashKeys[HashKey] := True;
end;
procedure THashKeyList.Clear;
begin
FillChar(FHashKeys, FMaxHashKey + 1, 0);
FMaxHashKey := 0;
end;
end.
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.