1 28 29 package com.caucho.util; 30 31 35 public class IntArray { 36 private int []_data; 37 private int _size; 38 39 42 public IntArray() 43 { 44 _data = new int[16]; 45 _size = 0; 46 } 47 48 51 public void clear() 52 { 53 _size = 0; 54 } 55 56 59 public int size() 60 { 61 return _size; 62 } 63 64 67 public int []getArray() 68 { 69 return _data; 70 } 71 72 75 public void add(int i) 76 { 77 if (_data.length <= _size) 78 expand(_size + 1); 79 80 _data[_size++] = i; 81 } 82 83 86 public void add(IntArray array) 87 { 88 if (_data.length <= _size) 89 expand(_size + array._size); 90 91 for (int i = 0; i < array._size; i++) 92 _data[_size++] = array._data[i]; 93 } 94 97 public void add(int i, int value) 98 { 99 expand(_size + 1); 100 101 System.arraycopy(_data, i, _data, i + 1, _size - i); 102 _data[i] = value; 103 _size++; 104 } 105 106 109 public int pop() 110 { 111 return _data[--_size]; 112 } 113 114 117 public void setLength(int size) 118 { 119 expand(size); 120 121 for (int i = _size; i < size; i++) 122 _data[i] = 0; 123 124 _size = size; 125 } 126 127 private void expand(int max) 128 { 129 while (_data.length < max) { 130 int []next = new int[_data.length * 2]; 131 132 for (int i = 0; i < _data.length; i++) 133 next[i] = _data[i]; 134 135 _data = next; 136 } 137 } 138 144 public int get(int i) 145 { 146 return _data[i]; 147 } 148 151 public int last() 152 { 153 return _data[_size - 1]; 154 } 155 158 public void set(int i, int value) 159 { 160 if (_size <= i) 161 throw new IndexOutOfBoundsException (i + " >= " + _size); 162 163 _data[i] = value; 164 } 165 168 public boolean contains(int test) 169 { 170 int []data = _data; 171 172 for (int i = _size - 1; i >= 0; i--) { 173 if (data[i] == test) 174 return true; 175 } 176 177 return false; 178 } 179 182 public boolean isSubset(IntArray subset) 183 { 184 int []subData = subset._data; 185 186 for (int i = subset._size - 1; i >= 0; i--) { 187 if (! contains(subData[i])) 188 return false; 189 } 190 191 return true; 192 } 193 197 public void union(IntArray newArray) 198 { 199 for (int i = 0; i < newArray._size; i++) { 200 if (! contains(newArray._data[i])) 201 add(newArray._data[i]); 202 } 203 } 204 205 208 public int []toArray() 209 { 210 int []value = new int[_size]; 211 212 System.arraycopy(_data, 0, value, 0, _size); 213 214 return value; 215 } 216 217 public String toString() 218 { 219 CharBuffer cb = CharBuffer.allocate(); 220 221 cb.append("["); 222 for (int i = 0; i < _size; i++) { 223 if (i != 0) 224 cb.append(", "); 225 cb.append(_data[i]); 226 } 227 cb.append("]"); 228 229 return cb.close(); 230 } 231 } 232 | Popular Tags |