@@ -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 *
0 commit comments