Skip to content

Commit 636d478

Browse files
committed
Better top<E>
1 parent ed9d262 commit 636d478

5 files changed

Lines changed: 781 additions & 441 deletions

File tree

common/src/java/com/github/oeuvres/alix/lucene/spans/OffsetsCollector.java

Lines changed: 48 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,14 @@ public final class OffsetsCollector implements SpanCollector {
6868

6969
/** Number of leaves collected in the current span. */
7070
private int size;
71+
72+
/**
73+
* Constructs a collector with a default initial capacity of 4 leaf terms.
74+
* Suitable for use as a pre-allocated slot in {@link com.github.oeuvres.alix.util.TopSlot}.
75+
*/
76+
public OffsetsCollector() {
77+
data = new int[4 * STRIDE];
78+
}
7179

7280
/**
7381
* Constructs a collector with a given initial capacity.
@@ -101,23 +109,32 @@ public void collectLeaf(final PostingsEnum postings, final int position, final T
101109
}
102110

103111
/**
104-
* {@inheritDoc}
112+
* Copies all collected leaves from this collector into {@code dest},
113+
* replacing any previous content in {@code dest}.
105114
*
106-
* <p>Resets the leaf count to zero. The backing array is retained for reuse.
107-
* Must be called by the caller before each {@link Spans#collect} invocation.
115+
* <p>Used to populate a pre-allocated slot returned by
116+
* {@link com.github.oeuvres.alix.util.TopSlot#insert(double)} without object creation.</p>
117+
*
118+
* @param dest destination collector; must not be {@code null}
108119
*/
109-
@Override
110-
public void reset() {
111-
size = 0;
120+
public void copyTo(final OffsetsCollector dest) {
121+
final int needed = size * STRIDE;
122+
if (dest.data.length < needed) {
123+
dest.data = Arrays.copyOf(data, needed);
124+
} else {
125+
System.arraycopy(data, 0, dest.data, 0, needed);
126+
}
127+
dest.size = size;
112128
}
113-
129+
114130
/**
115-
* Returns the number of leaf terms collected for the current span match.
131+
* Returns the character end offset of the {@code i}-th collected leaf.
116132
*
117-
* @return leaf count, {@code 0} if {@link #reset()} was called and no collection has occurred
133+
* @param i zero-based leaf index; must be in {@code [0, size())}
134+
* @return character end offset, or {@code -1} if offsets were not requested
118135
*/
119-
public int size() {
120-
return size;
136+
public int endOffset(final int i) {
137+
return data[i * STRIDE + 2];
121138
}
122139

123140
/**
@@ -131,23 +148,23 @@ public int position(final int i) {
131148
}
132149

133150
/**
134-
* Returns the character start offset of the {@code i}-th collected leaf.
151+
* {@inheritDoc}
135152
*
136-
* @param i zero-based leaf index; must be in {@code [0, size())}
137-
* @return character start offset, or {@code -1} if offsets were not requested
153+
* <p>Resets the leaf count to zero. The backing array is retained for reuse.
154+
* Must be called by the caller before each {@link Spans#collect} invocation.
138155
*/
139-
public int startOffset(final int i) {
140-
return data[i * STRIDE + 1];
156+
@Override
157+
public void reset() {
158+
size = 0;
141159
}
142160

143161
/**
144-
* Returns the character end offset of the {@code i}-th collected leaf.
162+
* Returns the number of leaf terms collected for the current span match.
145163
*
146-
* @param i zero-based leaf index; must be in {@code [0, size())}
147-
* @return character end offset, or {@code -1} if offsets were not requested
164+
* @return leaf count, {@code 0} if {@link #reset()} was called and no collection has occurred
148165
*/
149-
public int endOffset(final int i) {
150-
return data[i * STRIDE + 2];
166+
public int size() {
167+
return size;
151168
}
152169

153170
/**
@@ -199,6 +216,16 @@ public void sort() {
199216
}
200217
}
201218

219+
/**
220+
* Returns the character start offset of the {@code i}-th collected leaf.
221+
*
222+
* @param i zero-based leaf index; must be in {@code [0, size())}
223+
* @return character start offset, or {@code -1} if offsets were not requested
224+
*/
225+
public int startOffset(final int i) {
226+
return data[i * STRIDE + 1];
227+
}
228+
202229
/**
203230
* Returns a debug string listing all collected leaves.
204231
*

search/src/java/com/github/oeuvres/alix/lucene/search/Doc.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
import static com.github.oeuvres.alix.common.Names.*;
5858
import com.github.oeuvres.alix.common.TagFilter;
5959
import com.github.oeuvres.alix.lucene.terms.BytesDic;
60-
import com.github.oeuvres.alix.util.Top;
60+
import com.github.oeuvres.alix.util.TopSlot;
6161

6262
/**
6363
* Tools to display a document
@@ -503,12 +503,12 @@ public String id()
503503
* @throws IllegalAccessException
504504
* @throws InstantiationException
505505
*/
506-
public Top<String> intersect(final String field, final int docId2, final BytesRefHash stopwords)
506+
public TopSlot<String> intersect(final String field, final int docId2, final BytesRefHash stopwords)
507507
throws IOException, NoSuchFieldException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException
508508
{
509509
// new lucene API, not tested
510510
Terms vek2 = alix.reader().termVectors().get(docId2, field);
511-
Top<String> top = new Top<String>(String.class, 100);
511+
TopSlot<String> top = new TopSlot<String>(String.class, 100);
512512
FieldText ftext = alix.fieldText(field);
513513
int len1 = ftext.occsByDoc(docId);
514514
int len2 = ftext.occsByDoc(docId2);

0 commit comments

Comments
 (0)