001: /*
002: Copyright (C) 2007 Mobixess Inc. http://www.java-objects-database.com
003:
004: This file is part of the JODB (Java Objects Database) open source project.
005:
006: JODB is free software; you can redistribute it and/or modify it under
007: the terms of version 2 of the GNU General Public License as published
008: by the Free Software Foundation.
009:
010: JODB is distributed in the hope that it will be useful, but WITHOUT ANY
011: WARRANTY; without even the implied warranty of MERCHANTABILITY or
012: FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
013: for more details.
014:
015: You should have received a copy of the GNU General Public License along
016: with this program; if not, write to the Free Software Foundation, Inc.,
017: 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
018: */
019: package com.mobixess.jodb.util;
020:
021: import java.io.Serializable;
022:
023: public class IntVector implements Serializable {
024:
025: /**
026: *
027: */
028: private static final long serialVersionUID = 1L;
029:
030: public final static int DEFAULT_INCREMENT = 256;
031:
032: private int _increment;
033:
034: private int _data[];
035:
036: private int _size = 0;
037:
038: public IntVector() {
039: _increment = DEFAULT_INCREMENT;
040: _data = new int[_increment];
041: }
042:
043: public IntVector(int increment) {
044: _increment = increment;
045: _data = new int[increment];
046: }
047:
048: public IntVector(int initialSize, int increment) {
049:
050: _increment = increment;
051: _data = new int[initialSize];
052: _size = initialSize;
053: }
054:
055: public IntVector(int[] data) {
056: _data = new int[data.length];
057: _size = data.length;
058: _increment = DEFAULT_INCREMENT;
059: System.arraycopy(data, 0, _data, 0, _size);
060: }
061:
062: /**
063: * Get the length of the list.
064: *
065: * @return length of the list
066: */
067: public final int size() {
068: return _size;
069: }
070:
071: /**
072: * Get the length of the list.
073: *
074: * @return length of the list
075: */
076: public final void setSize(int sz) {
077: _size = sz;
078: }
079:
080: /**
081: * Append a int onto the vector.
082: *
083: * @param value
084: * Int to add to the list
085: */
086: public final void addElement(int value) {
087: ensureCapacity(_size + 1);
088: _data[_size] = value;
089: _size++;
090: }
091:
092: private void ensureCapacity(int size) {
093: if (size >= _data.length) {
094: int newData[] = new int[size + _increment];
095:
096: System.arraycopy(_data, 0, newData, 0, _data.length);
097:
098: _data = newData;
099: }
100: }
101:
102: /**
103: * Append several int values onto the vector.
104: *
105: * @param value
106: * Int to add to the list
107: */
108: public final void addElements(int value, int numberOfElements) {
109: ensureCapacity(_size + numberOfElements);
110:
111: for (int i = 0; i < numberOfElements; i++) {
112: _data[_size] = value;
113: _size++;
114: }
115: }
116:
117: /**
118: * Inserts the specified node in this vector at the specified index.
119: *
120: * @param value
121: * Int to insert
122: * @param at
123: * Index of where to insert
124: */
125: public final void insertElementAt(int value, int at) {
126:
127: ensureCapacity(_size + 1);
128:
129: if (at <= (_size - 1)) {
130: System.arraycopy(_data, at, _data, at + 1, _size - at);
131: }
132:
133: _data[at] = value;
134:
135: _size++;
136: }
137:
138: public final void removeAllElements() {
139:
140: for (int i = 0; i < _size; i++) {
141: _data[i] = java.lang.Integer.MIN_VALUE;
142: }
143:
144: _size = 0;
145: }
146:
147: /**
148: * Removes the first occurrence of the argument from this vector. If the
149: * object is found in this vector, each component in the vector with an
150: * index greater or equal to the object's index is shifted downward to have
151: * an index one smaller than the value it had previously.
152: *
153: * @param s
154: * Int to remove from array
155: *
156: * @return True if the int was removed, false if it was not found
157: */
158: public final boolean removeElement(int s) {
159:
160: for (int i = 0; i < _size; i++) {
161: if (_data[i] == s) {
162: if ((i + 1) < _size)
163: System.arraycopy(_data, i + 1, _data, i - 1, _size
164: - i);
165: else
166: _data[i] = java.lang.Integer.MIN_VALUE;
167:
168: _size--;
169:
170: return true;
171: }
172: }
173:
174: return false;
175: }
176:
177: /**
178: * Deletes the component at the specified index. Each component in this
179: * vector with an index greater or equal to the specified index is shifted
180: * downward to have an index one smaller than the value it had previously.
181: *
182: * @param i
183: * index of where to remove and int
184: */
185: public final void removeElementAt(int i) {
186:
187: if (i > _size)
188: System.arraycopy(_data, i + 1, _data, i, _size);
189: else
190: _data[i] = java.lang.Integer.MIN_VALUE;
191:
192: _size--;
193: }
194:
195: /**
196: * Sets the component at the specified index of this vector to be the
197: * specified object. The previous component at that position is discarded.
198: *
199: * The index must be a value greater than or equal to 0 and less than the
200: * current size of the vector.
201: *
202: * @param node
203: * object to set
204: * @param index
205: * Index of where to set the object
206: */
207: public final void setElementAt(int value, int index) {
208: _data[index] = value;
209: }
210:
211: /**
212: * Get the nth element.
213: *
214: * @param i
215: * index of object to get
216: *
217: * @return object at given index
218: */
219: public final int elementAt(int i) {
220: return _data[i];
221: }
222:
223: /**
224: * Tell if the table contains the given node.
225: *
226: * @param s
227: * object to look for
228: *
229: * @return true if the object is in the list
230: */
231: public final boolean contains(int s) {
232:
233: for (int i = 0; i < _size; i++) {
234: if (_data[i] == s)
235: return true;
236: }
237:
238: return false;
239: }
240:
241: /**
242: * Searches for the first occurence of the given argument, beginning the
243: * search at index, and testing for equality using the equals method.
244: *
245: * @param elem
246: * object to look for
247: * @param index
248: * Index of where to begin search
249: * @return the index of the first occurrence of the object argument in this
250: * vector at position index or later in the vector; returns -1 if
251: * the object is not found.
252: */
253: public final int indexOf(int elem, int index) {
254:
255: for (int i = index; i < _size; i++) {
256: if (_data[i] == elem)
257: return i;
258: }
259:
260: return java.lang.Integer.MIN_VALUE;
261: }
262:
263: /**
264: * Searches for the first occurence of the given argument, beginning the
265: * search at index, and testing for equality using the equals method.
266: *
267: * @param elem
268: * object to look for
269: * @return the index of the first occurrence of the object argument in this
270: * vector at position index or later in the vector; returns -1 if
271: * the object is not found.
272: */
273: public final int indexOf(int elem) {
274:
275: for (int i = 0; i < _size; i++) {
276: if (_data[i] == elem)
277: return i;
278: }
279:
280: return java.lang.Integer.MIN_VALUE;
281: }
282:
283: /**
284: * Searches for the first occurence of the given argument, beginning the
285: * search at index, and testing for equality using the equals method.
286: *
287: * @param elem
288: * Object to look for
289: * @return the index of the first occurrence of the object argument in this
290: * vector at position index or later in the vector; returns -1 if
291: * the object is not found.
292: */
293: public final int lastIndexOf(int elem) {
294:
295: for (int i = (_size - 1); i >= 0; i--) {
296: if (_data[i] == elem)
297: return i;
298: }
299:
300: return java.lang.Integer.MIN_VALUE;
301: }
302:
303: public int[] getDataAsArray() {
304: return _data;
305: }
306:
307: public final int binarySearch(int key) {
308: int low = 0;
309: int high = _size;
310:
311: while (low <= high) {
312: int mid = (low + high) >> 1;
313: int midVal = _data[mid];
314:
315: if (midVal < key)
316: low = mid + 1;
317: else if (midVal > key)
318: high = mid - 1;
319: else
320: return mid; // key found
321: }
322: return -(low + 1); // key not found.
323: }
324:
325: }
|