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