View Javadoc

1   // BSD License (http://www.galagosearch.org/license)
2   
3   package org.galagosearch.core.util;
4   
5   public class IntArray {
6       int[] _array;
7       int _position;
8   
9       public IntArray(int capacity) {
10          _array = new int[capacity];
11          _position = 0;
12      }
13  
14      public IntArray() {
15          this(16);
16      }
17  
18      public void add(int value) {
19          if(_position == _array.length) {
20              // grow array if we're out of space
21              _array = _copyArray(_array.length * 2);
22          }
23  
24          _array[_position] = value;
25          _position += 1;
26      }
27  
28      public int[] getBuffer() {
29          return _array;
30      }
31  
32      public int getPosition() {
33          return _position;
34      }
35  
36      private int[] _copyArray(int newSize) {
37          int[] result = new int[newSize];
38          System.arraycopy(_array, 0, result, 0, _position);
39          return result;
40      }
41  
42      public int[] toArray() {
43          return _copyArray(_position);
44      }
45  }