This repository has been archived by the owner on Aug 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathsearch_index.pas
444 lines (355 loc) · 8.93 KB
/
search_index.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
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
{ This file is a part of Map editor for VCMI project
Copyright (C) 2016-2017 Alexander Shishkin [email protected]
This source is free software; you can redistribute it and/or modify it under the terms of the GNU General Public
License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later
version.
This code is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
details.
A copy of the GNU General Public License is available on the World Wide Web at
<http://www.gnu.org/copyleft/gpl.html>. You can also obtain it by writing to the Free Software Foundation, Inc., 59
Temple Place - Suite 330, Boston, MA 02111-1307, USA.
}
unit search_index;
{$I compilersetup.inc}
interface
uses
Classes, SysUtils, fgl, gset, gvector, math, RegExpr, LazUTF8, editor_utils, editor_types, editor_gl;
type
{$INTERFACES CORBA}
{ ISearchResult }
ISearchResult = interface
procedure Add(AObject: TObject);
procedure Clear;
function GetCount: SizeInt;
property Count: SizeInt read GetCount;
procedure RenderIcon(AIndex:SizeInt; AState: TLocalState; AX, AY, dim:integer; color: TPlayer);
end;
{ TObjPtrLess }
TObjPtrLess = class
public
class function c(a,b: TObject): boolean;
end;
{ TSearchIndexBusket }
TSearchIndexBusket = class
public
type
TDataVector = specialize TVector<TObject>;
TBusketData = specialize TSet<TObject, TObjPtrLess>;
var
data: TBusketData;
constructor Create();
destructor Destroy; override;
procedure AddItem(AItem: TObject);
procedure RemoveItem(AItem: TObject);
procedure Intersect(ATarget:TBusketData);
procedure SaveTo(ATarget:TBusketData);
end;
{ TSearchIndexMap }
TSearchIndexMap = class(specialize TFPGMap<string,TSearchIndexBusket>)
protected
procedure Deref(Item: Pointer); override;
public
constructor Create;
//true if found
function Find(AKeyWord: String; out IdxLow: integer; out IdxHigh: integer): boolean; overload;
end;
{ TSearchIndex }
TSearchIndex = class
private
type
TObjectsSet = specialize TSet<TObject, TObjPtrLess>;
private
FAll: TObjectsSet;
FMap: TSearchIndexMap;
FTextTokenizer: TRegExpr;
procedure AddToIndex(AKeyWord: String; AItem: TObject);
procedure Find(AKeyWord: String; ATarget: TSearchIndexBusket.TBusketData);
procedure Intersect(AKeyWord: String; ATarget: TSearchIndexBusket.TBusketData);
procedure SelectAll(AResult: ISearchResult);
public
constructor Create();
destructor Destroy; override;
procedure AddToIndex(ARawKeyWords: TStrings; AItem: TObject);
procedure RemoveFromIndex(AItem: TObject);
// AInput = space separated words or empty string to get all
procedure Find(AInput: string; AResult: ISearchResult);
end;
implementation
function CompareSearchIndexBusket(const d1,d2: TSearchIndexBusket): integer;
begin
Result := PtrInt(d1) - PtrInt(d2);
end;
{ TSearchIndex }
constructor TSearchIndex.Create;
begin
FMap := TSearchIndexMap.Create();
FTextTokenizer := TRegExpr.Create('[_\-\s\.:]+');
FTextTokenizer.Compile;
FAll := TObjectsSet.Create;
end;
destructor TSearchIndex.Destroy;
begin
FAll.Free;
FTextTokenizer.Free;
FMap.Free;
inherited Destroy;
end;
procedure TSearchIndex.AddToIndex(AKeyWord: String; AItem: TObject);
var
busket: TSearchIndexBusket;
idx: Integer;
begin
idx := -1;
if not Fmap.Find(AKeyWord, idx) then
begin
busket := TSearchIndexBusket.Create();
FMap.Add(AKeyWord, busket);
end
else
begin
busket := FMap.Data[idx];
end;
busket.AddItem(AItem);
end;
procedure TSearchIndex.Find(AKeyWord: String; ATarget: TSearchIndexBusket.TBusketData);
var
idx_low, idx_high, i: Integer;
begin
idx_low := -1;
idx_high := -1;
if Fmap.Find(AKeyWord, idx_low, idx_high)then
begin
for i := idx_low to idx_high do
begin
FMap.Data[i].SaveTo(ATarget);
end;
end;
end;
procedure TSearchIndex.Intersect(AKeyWord: String; ATarget: TSearchIndexBusket.TBusketData);
var
idx_low, idx_high, i: Integer;
temp : TSearchIndexBusket;
begin
idx_low := -1;
idx_high := -1;
if Fmap.Find(AKeyWord, idx_low, idx_high)then
begin
temp := TSearchIndexBusket.Create;
for i := idx_low to idx_high do
begin
FMap.Data[i].SaveTo(temp.data);
end;
temp.Intersect(ATarget);
temp.Free;
end
else
begin
while not ATarget.IsEmpty do
begin
ATarget.Delete(ATarget.NMin^.Data);
end;
end;
end;
procedure TSearchIndex.AddToIndex(ARawKeyWords: TStrings; AItem: TObject);
var
keywords: TStringList;
RawKeyWord, KeyWord: String;
begin
FAll.Insert(AItem);
keywords := TStringList.Create;
keywords.Sorted:=true;
keywords.Duplicates:=dupIgnore;
try
for RawKeyWord in ARawKeyWords do
begin
KeyWord := NormalizeKeyWord(RawKeyWord);
FTextTokenizer.Split(KeyWord, keywords);
end;
for KeyWord in keywords do
begin
AddToIndex(KeyWord, AItem);
end;
finally
keywords.Free;
end;
end;
procedure TSearchIndex.RemoveFromIndex(AItem: TObject);
var
i: Integer;
begin
FAll.Delete(AItem);
//TODO: optimize
for i := 0 to FMap.Count - 1 do
begin
FMap.Data[i].RemoveItem(AItem);
end;
end;
procedure TSearchIndex.Find(AInput: string; AResult: ISearchResult);
var
keywords: TStringList;
data: TSearchIndexBusket.TBusketData;
i: Integer;
it: TSearchIndexBusket.TBusketData.TIterator;
begin
AResult.Clear;
AInput := UTF8Trim(UTF8LowerCase(AInput));
if AInput = '' then
begin
SelectAll(AResult);
Exit;
end;
data := TSearchIndexBusket.TBusketData.Create;
keywords := TStringList.Create;
keywords.Sorted:=true;
keywords.Duplicates:=dupIgnore;
FTextTokenizer.Split(AInput, keywords);
Find(keywords[0], data);
if not data.IsEmpty() then
begin
for i := 1 to keywords.Count - 1 do
begin
Intersect(keywords[i], data);
if data.IsEmpty then
break;
end;
end;
it := data.Min;
if Assigned(it) then
begin
repeat
AResult.Add(it.Data);
until not it.Next;
it.free;
end;
data.Free;
keywords.Free;
end;
procedure TSearchIndex.SelectAll(AResult: ISearchResult);
var
it: TObjectsSet.TIterator;
begin
AResult.Clear;
it := FAll.Min;
if Assigned(it) then
begin
repeat
AResult.Add(it.Data);
until not it.Next;
it.free;
end;
end;
{ TSearchIndexMap }
procedure TSearchIndexMap.Deref(Item: Pointer);
begin
Finalize(string(Item^));
TSearchIndexBusket(Pointer(PByte(Item)+KeySize)^).Free;
end;
constructor TSearchIndexMap.Create;
begin
inherited Create;
OnKeyCompare := @CompareStringProxy;
OnDataCompare := @CompareSearchIndexBusket;
Sorted := True;
Duplicates:=dupError;
end;
function TSearchIndexMap.Find(AKeyWord: String; out IdxLow: integer; out IdxHigh: integer): boolean;
function PartialMatched(idx: integer): boolean;
begin
result := UTF8Pos(AKeyWord,Keys[idx]) = 1;
end;
begin
IdxLow := -1;
IdxHigh := -1;
if Find(AKeyWord, IdxLow) then
begin
IdxHigh:=IdxLow;
Result := true;
end
else
begin
//find firts true partial match
while (IdxLow < Count) and not PartialMatched(IdxLow) do
begin
Inc(IdxLow);
end;
if IdxLow >= Count then
begin
IdxLow := -1;
IdxHigh := -1;
Result := false;
Exit;
end;
//find end of matched region
IdxHigh:=IdxLow;
while (IdxHigh < Count) and PartialMatched(IdxHigh) do
begin
Inc(IdxHigh);
end;
IdxHigh:=Min(IdxHigh-1, Count-1);
Result := true;
end;
end;
{ TSearchIndexBusket }
constructor TSearchIndexBusket.Create;
begin
data := TBusketData.Create;
end;
destructor TSearchIndexBusket.Destroy;
begin
data.Free;
inherited Destroy;
end;
procedure TSearchIndexBusket.AddItem(AItem: TObject);
begin
data.Insert(AItem);
end;
procedure TSearchIndexBusket.RemoveItem(AItem: TObject);
begin
data.Delete(AItem);
end;
procedure TSearchIndexBusket.Intersect(ATarget: TBusketData);
var
it: TBusketData.TIterator;
n: TBusketData.PNode;
to_delete: TDataVector;
i: SizeInt;
begin
to_delete := TDataVector.Create;
it := ATarget.Min;
if Assigned(it) then
begin
repeat
n := data.NFind(it.Data);
if not Assigned(n) then
begin
to_delete.PushBack(it.data);
end;
until not it.Next;
it.free;
end;
for i := 0 to SizeInt(to_delete.Size) - 1 do
begin
ATarget.Delete(to_delete.Items[i]);
end;
to_delete.Free;
end;
procedure TSearchIndexBusket.SaveTo(ATarget: TBusketData);
var
it: TBusketData.TIterator;
begin
it := data.Min;
if Assigned(it) then
begin
repeat
ATarget.Insert(it.Data);
until not it.Next;
it.free;
end;
end;
{ TObjPtrLess }
class function TObjPtrLess.c(a, b: TObject): boolean;
begin
Result := PtrInt(a) < PtrInt(b);
end;
end.