-
Notifications
You must be signed in to change notification settings - Fork 623
/
OpenBitSetIterator.cs
176 lines (159 loc) · 5.7 KB
/
OpenBitSetIterator.cs
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
using J2N.Numerics;
using System.Runtime.CompilerServices;
namespace Lucene.Net.Util
{
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using DocIdSetIterator = Lucene.Net.Search.DocIdSetIterator;
/// <summary>
/// An iterator to iterate over set bits in an <see cref="OpenBitSet"/>.
/// this is faster than <see cref="OpenBitSet.NextSetBit(long)"/> for iterating over the complete set of bits,
/// especially when the density of the bits set is high.
/// </summary>
public class OpenBitSetIterator : DocIdSetIterator
{
// hmmm, what about an iterator that finds zeros though,
// or a reverse iterator... should they be separate classes
// for efficiency, or have a common root interface? (or
// maybe both? could ask for a SetBitsIterator, etc...
internal readonly long[] arr;
internal readonly int words;
private int i = -1;
private long word;
private int wordShift;
private int indexArray;
private int curDocId = -1;
public OpenBitSetIterator(OpenBitSet obs)
: this(obs.GetBits(), obs.NumWords)
{
}
public OpenBitSetIterator(long[] bits, int numWords)
{
arr = bits;
words = numWords;
}
// 64 bit shifts
private void Shift()
{
if ((int)word == 0)
{
wordShift += 32;
word = word.TripleShift(32);
}
if ((word & 0x0000FFFF) == 0)
{
wordShift += 16;
word = word.TripleShift(16);
}
if ((word & 0x000000FF) == 0)
{
wordShift += 8;
word = word.TripleShift(8);
}
indexArray = BitUtil.BitList((byte)word);
}
//*** alternate shift implementations
// // 32 bit shifts, but a long shift needed at the end
// private void shift2() {
// int y = (int)word;
// if (y==0) {wordShift +=32; y = (int)(word >>>32); }
// if ((y & 0x0000FFFF) == 0) { wordShift +=16; y>>>=16; }
// if ((y & 0x000000FF) == 0) { wordShift +=8; y>>>=8; }
// indexArray = bitlist[y & 0xff];
// word >>>= (wordShift +1);
// }
//
// private void shift3() {
// int lower = (int)word;
// int lowByte = lower & 0xff;
// if (lowByte != 0) {
// indexArray=bitlist[lowByte];
// return;
// }
// shift();
// }
// *****
public override int NextDoc()
{
if (indexArray == 0)
{
if (word != 0)
{
word = word.TripleShift(8);
wordShift += 8;
}
while (word == 0)
{
if (++i >= words)
{
return curDocId = NO_MORE_DOCS;
}
word = arr[i];
wordShift = -1; // loop invariant code motion should move this
}
// after the first time, should I go with a linear search, or
// stick with the binary search in shift?
Shift();
}
int bitIndex = (indexArray & 0x0f) + wordShift;
indexArray = indexArray.TripleShift(4);
// should i<<6 be cached as a separate variable?
// it would only save one cycle in the best circumstances.
return curDocId = (i << 6) + bitIndex;
}
public override int Advance(int target)
{
indexArray = 0;
i = target >> 6;
if (i >= words)
{
word = 0; // setup so next() will also return -1
return curDocId = NO_MORE_DOCS;
}
wordShift = target & 0x3f;
word = arr[i].TripleShift(wordShift);
if (word != 0)
{
wordShift--; // compensate for 1 based arrIndex
}
else
{
while (word == 0)
{
if (++i >= words)
{
return curDocId = NO_MORE_DOCS;
}
word = arr[i];
}
wordShift = -1;
}
Shift();
int bitIndex = (indexArray & 0x0f) + wordShift;
indexArray = indexArray.TripleShift(4);
// should i<<6 be cached as a separate variable?
// it would only save one cycle in the best circumstances.
return curDocId = (i << 6) + bitIndex;
}
public override int DocID => curDocId;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override long GetCost()
{
return words / 64;
}
}
}