| 1 | |
|
| 2 | |
|
| 3 | |
package org.galagosearch.core.util; |
| 4 | |
|
| 5 | |
import org.galagosearch.core.retrieval.structured.Extent; |
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
public class ExtentArray { |
| 11 | |
Extent[] _array; |
| 12 | |
int _position; |
| 13 | |
|
| 14 | 164 | public ExtentArray(int capacity) { |
| 15 | 164 | _array = new Extent[capacity]; |
| 16 | 2788 | for(int i=0; i<_array.length; i++) |
| 17 | 2624 | _array[i] = new Extent(); |
| 18 | 164 | _position = 0; |
| 19 | 164 | } |
| 20 | |
|
| 21 | |
public ExtentArray() { |
| 22 | 164 | this(16); |
| 23 | 164 | } |
| 24 | |
|
| 25 | |
private void makeRoomForOneObject() { |
| 26 | 248 | if(_position == _array.length) { |
| 27 | |
|
| 28 | 0 | _array = _copyArray(_array.length * 2); |
| 29 | |
} |
| 30 | 248 | } |
| 31 | |
|
| 32 | |
public void add(Extent value) { |
| 33 | 16 | makeRoomForOneObject(); |
| 34 | |
|
| 35 | 16 | _array[_position] = value; |
| 36 | 16 | _position += 1; |
| 37 | 16 | } |
| 38 | |
|
| 39 | |
public void add(int document, int begin, int end) { |
| 40 | 204 | add(document, begin, end, 1); |
| 41 | 204 | } |
| 42 | |
|
| 43 | |
public void add(int document, int begin, int end, double weight) { |
| 44 | 232 | makeRoomForOneObject(); |
| 45 | |
|
| 46 | 232 | Extent e = _array[_position]; |
| 47 | 232 | e.document = document; |
| 48 | 232 | e.begin = begin; |
| 49 | 232 | e.end = end; |
| 50 | 232 | e.weight = weight; |
| 51 | 232 | _position += 1; |
| 52 | 232 | } |
| 53 | |
|
| 54 | |
public Extent[] getBuffer() { |
| 55 | 376 | return _array; |
| 56 | |
} |
| 57 | |
|
| 58 | |
public int getPosition() { |
| 59 | 232 | return _position; |
| 60 | |
} |
| 61 | |
|
| 62 | |
private Extent[] _copyArray(int newSize) { |
| 63 | 0 | Extent[] result = new Extent[newSize]; |
| 64 | 0 | System.arraycopy(_array, 0, result, 0, _position); |
| 65 | 0 | for(int i=_position; i<result.length; i++) |
| 66 | 0 | result[i] = new Extent(); |
| 67 | 0 | return result; |
| 68 | |
} |
| 69 | |
|
| 70 | |
public Extent[] toArray() { |
| 71 | 0 | return _copyArray(_position); |
| 72 | |
} |
| 73 | |
|
| 74 | |
public void reset() { |
| 75 | 156 | _position = 0; |
| 76 | 156 | } |
| 77 | |
} |