Repository navigation
Expand file tree
/
Copy pathDocSetIterator.cs
More file actions
451 lines (367 loc) · 9.18 KB
/
Copy pathDocSetIterator.cs
File metadata and controls
451 lines (367 loc) · 9.18 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
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
444
445
446
447
448
449
450
451
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
namespace Sylphe.IR
{
public abstract class DocSetIterator
{
protected static readonly int Uninitialized = -1;
protected static readonly int NoMoreDocs = int.MaxValue;
protected DocSetIterator(string tag = null)
{
Tag = tag ?? string.Empty;
Doc = Uninitialized;
}
private string Tag { get; }
/// <summary>ID of the current doc or NoMoreDocs</summary>
/// <remarks>
/// Making this a field (not a poroperty) at least doubles performance
/// in Debug builds, but leaves it roughly the same in Release builds;
/// presumably, release build optimizations eliminate the method call.
/// </remarks>
public int Doc { get; protected set; }
/// <summary>Advance to and return the next doc ID or NoMoreDocs</summary>
public abstract int NextDoc();
/// <summary>
/// Advance to and return the next doc ID after the current and
/// greater than or equal to the given <paramref name="target"/>.
/// </summary>
public virtual int Advance(int target)
{
int doc;
while ((doc = NextDoc()) < target) { }
return Doc = doc;
}
public IEnumerable<int> GetAll()
{
int doc;
while ((doc = NextDoc()) != NoMoreDocs)
{
yield return doc;
}
}
public override string ToString()
{
return $"Doc = {Doc}, Tag = {Tag}";
}
}
public class ListIterator : DocSetIterator
{
private readonly int[] _list;
private readonly int _count;
private int _index;
public ListIterator(IEnumerable<int> sortedList, string tag = null)
: base(tag)
{
_list = sortedList?.ToArray() ?? throw new ArgumentNullException();
_count = _list.Length;
_index = 0;
}
public override int NextDoc()
{
if (_index < _count)
{
return Doc = _list[_index++];
}
return Doc = NoMoreDocs;
}
}
public class EnumerationIterator : DocSetIterator
{
private IEnumerator<int> _enumerator;
public EnumerationIterator(IEnumerable<int> sortedEnum, string tag = null)
: base(tag)
{
if (sortedEnum == null)
throw new ArgumentNullException(nameof(sortedEnum));
_enumerator = sortedEnum.GetEnumerator();
}
public override int NextDoc()
{
if (_enumerator == null)
{
return NoMoreDocs;
}
if (_enumerator.MoveNext())
{
return Doc = _enumerator.Current;
}
_enumerator = null;
return Doc = NoMoreDocs;
}
}
public class EmptyIterator : DocSetIterator
{
public EmptyIterator(string tag = null) : base(tag)
{
Doc = NoMoreDocs;
}
public override int NextDoc()
{
return NoMoreDocs;
}
}
public class ButNotIterator : DocSetIterator
{
private DocSetIterator _candidates;
private DocSetIterator _exclusions;
public ButNotIterator(DocSetIterator candidates, DocSetIterator exclusions)
{
_candidates = candidates; // can be null
_exclusions = exclusions; // can be null
}
public override int NextDoc()
{
if (_candidates == null)
{
return Doc = NoMoreDocs;
}
Doc = _candidates.NextDoc();
if (Doc == NoMoreDocs)
{
_candidates = null; // exhausted
return Doc;
}
if (_exclusions == null)
{
return Doc; // nothing more to subtract
}
return Doc = NextNonExcluded();
}
public override int Advance(int target)
{
if (_candidates == null)
{
return Doc = NoMoreDocs;
}
if (_exclusions == null)
{
return Doc = _candidates.Advance(target);
}
if (_candidates.Advance(target) == NoMoreDocs)
{
_candidates = null;
return Doc = NoMoreDocs;
}
return Doc = NextNonExcluded();
}
private int NextNonExcluded()
{
Debug.Assert(_candidates != null);
Debug.Assert(_exclusions != null);
var candidate = _candidates.Doc;
var excluded = _exclusions.Doc;
do
{
if (candidate < excluded)
{
return candidate; // candidate before next exclusion
}
if (candidate > excluded)
{
excluded = _exclusions.Advance(candidate);
if (excluded == NoMoreDocs)
{
_exclusions = null; // no more exclusions
return candidate;
}
if (candidate < excluded)
{
return candidate; // candidate before next exclusion
}
}
// else: candidate == excluded, try next candidate
}
while ((candidate = _candidates.NextDoc()) != NoMoreDocs);
_candidates = null; // no more candidates
return NoMoreDocs;
}
}
public class ConjunctionIterator : DocSetIterator
{
private readonly DocSetIterator[] _iterators;
public ConjunctionIterator(params DocSetIterator[] iterators)
{
if (iterators == null)
throw new ArgumentNullException();
if (iterators.Length < 1)
throw new ArgumentException("Need at least one iterators");
foreach (var scorer in iterators)
{
// prime each sub-iterator
var doc = scorer.NextDoc();
if (doc == NoMoreDocs)
{
// if any iterator is empty,
// then the conjunction is empty
Doc = NoMoreDocs;
return;
}
}
// Order iterators by first doc, ascending
Array.Sort(iterators, (a, b) => a.Doc - b.Doc);
// The invariant will be that all iterators are
// always positioned at the same commen doc,
// which will eventually be NoMoreDocs for all.
// The call to StepToCommonDoc() will establish
// and maintain this invariant.
if (StepToCommonDoc(iterators) == NoMoreDocs)
{
// no common doc among all scorers
Doc = NoMoreDocs;
return;
}
_iterators = iterators;
Doc = Uninitialized;
}
private static int StepToCommonDoc(IReadOnlyList<DocSetIterator> iterators)
{
var index = 0;
var doc = iterators[iterators.Count - 1].Doc;
DocSetIterator iterator;
while ((iterator = iterators[index]).Doc < doc)
{
doc = iterator.Advance(doc);
index += 1;
if (index >= iterators.Count) index = 0;
}
return doc;
}
public override int NextDoc()
{
if (Doc == NoMoreDocs)
{
return NoMoreDocs;
}
if (Doc < 0)
{
return Doc = _iterators[_iterators.Length - 1].Doc;
}
_iterators[_iterators.Length - 1].NextDoc();
return Doc = StepToCommonDoc(_iterators);
}
public override int Advance(int target)
{
if (Doc == NoMoreDocs)
{
return NoMoreDocs;
}
if (_iterators[_iterators.Length - 1].Doc < target)
{
_iterators[_iterators.Length - 1].Advance(target);
}
return Doc = StepToCommonDoc(_iterators);
}
}
public class DisjunctionIterator : DocSetIterator
{
private readonly DocSetIterator[] _heap;
private int _heapSize;
// Maintain a heap of active iterators such that
// the iterator with the min doc is at heap's root
public DisjunctionIterator(params DocSetIterator[] iterators)
{
if (iterators == null)
throw new ArgumentNullException();
// slot at index 0 remains unused
_heap = new DocSetIterator[iterators.Length + 1];
// prime all iterators and build the heap
var n = 0;
foreach (var iterator in iterators)
{
var doc = iterator.NextDoc();
if (doc != NoMoreDocs)
{
_heap[++n] = iterator;
}
}
// a sorted array has the heap property
Array.Sort(_heap, 1, n, new IteratorComparer());
_heapSize = n;
Doc = n > 0 ? Uninitialized : NoMoreDocs;
}
public override int NextDoc()
{
if (_heapSize == 0)
{
return NoMoreDocs; // all lists exhausted
}
while (_heap[1].Doc == Doc)
{
var doc = _heap[1].NextDoc();
if (doc == NoMoreDocs)
{
_heap[1] = _heap[_heapSize];
_heapSize -= 1;
if (_heapSize == 0)
{
// last doc list exhausted
return Doc = NoMoreDocs;
}
}
DownHeap(1);
}
return Doc = _heap[1].Doc;
}
public override int Advance(int target)
{
if (_heapSize == 0)
{
return NoMoreDocs; // all lists exhausted
}
while (_heap[1].Doc < target)
{
var doc = _heap[1].Advance(target);
if (doc == NoMoreDocs)
{
_heap[1] = _heap[_heapSize];
_heapSize -= 1; // remove exhausted list
if (_heapSize == 0)
{
// last doc list exhausted
return Doc = NoMoreDocs;
}
}
DownHeap(1);
}
return Doc = _heap[1].Doc;
}
/// <summary>
/// Restore the heap property after heap[k] changed its priority.
/// If the root changed its priority (the usual case), k is 1.
/// </summary>
private void DownHeap(int k)
{
// Inside the while loop, there are two calls to the comparer.
// There is an alternative implementation with only one call
// inside the loop and an UpHeap operation after the loop.
var item = _heap[k]; // save top item
var itemDoc = item.Doc;
var limit = _heapSize >> 1; // div 2
while (k <= limit)
{
// Pick smaller child:
var child = k << 1; // times 2, left child
if (child < _heapSize && _heap[child + 1].Doc < _heap[child].Doc)
{
child++; // right child
}
if (itemDoc < _heap[child].Doc) break;
_heap[k] = _heap[child]; // shift child up
k = child;
}
_heap[k] = item; // restore saved item
}
private class IteratorComparer : IComparer<DocSetIterator>
{
public int Compare(DocSetIterator x, DocSetIterator y)
{
if (x == null && y == null) return 0;
if (x == null) return -1;
if (y == null) return +1;
return Comparer<int>.Default.Compare(x.Doc, y.Doc);
}
}
}
}