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 LongVector 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 long _data[];
035:
036: private int _size = 0;
037:
038: public LongVector() {
039: _increment = DEFAULT_INCREMENT;
040: _data = new long[_increment];
041: }
042:
043: public LongVector(int increment) {
044: _increment = increment;
045: _data = new long[increment];
046: }
047:
048: public LongVector(int initialSize, int increment) {
049:
050: _increment = increment;
051: _data = new long[initialSize];
052: _size = initialSize;
053: }
054:
055: public LongVector(long[] data) {
056: _data = new long[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(long 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: long newData[] = new long[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(long 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(long 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: *
154: * @return True if the int was removed, false if it was not found
155: */
156: public final boolean removeElement(long s) {
157:
158: for (int i = 0; i < _size; i++) {
159: if (_data[i] == s) {
160: if ((i + 1) < _size)
161: System.arraycopy(_data, i + 1, _data, i - 1, _size
162: - i);
163: else
164: _data[i] = java.lang.Integer.MIN_VALUE;
165:
166: _size--;
167:
168: return true;
169: }
170: }
171:
172: return false;
173: }
174:
175: /**
176: * Deletes the component at the specified index. Each component in this
177: * vector with an index greater or equal to the specified index is shifted
178: * downward to have an index one smaller than the value it had previously.
179: *
180: * @param i
181: * index of where to remove
182: */
183: public final void removeElementAt(int i) {
184:
185: if (i > _size)
186: System.arraycopy(_data, i + 1, _data, i, _size);
187: else
188: _data[i] = java.lang.Integer.MIN_VALUE;
189:
190: _size--;
191: }
192:
193: /**
194: * Sets the component at the specified index of this vector to be the
195: * specified object. The previous component at that position is discarded.
196: *
197: * The index must be a value greater than or equal to 0 and less than the
198: * current size of the vector.
199: *
200: * @param node
201: * object to set
202: * @param index
203: * Index of where to set the object
204: */
205: public final void setElementAt(long value, int index) {
206: _data[index] = value;
207: }
208:
209: /**
210: * Get the nth element.
211: *
212: * @param i
213: * index of object to get
214: *
215: * @return object at given index
216: */
217: public final long elementAt(int i) {
218: return _data[i];
219: }
220:
221: /**
222: * Tell if the table contains the given node.
223: *
224: * @param s
225: * object to look for
226: *
227: * @return true if the object is in the list
228: */
229: public final boolean contains(long s) {
230:
231: for (int i = 0; i < _size; i++) {
232: if (_data[i] == s)
233: return true;
234: }
235:
236: return false;
237: }
238:
239: /**
240: * Searches for the first occurence of the given argument, beginning the
241: * search at index, and testing for equality using the equals method.
242: *
243: * @param elem
244: * object to look for
245: * @param index
246: * Index of where to begin search
247: * @return the index of the first occurrence of the object argument in this
248: * vector at position index or later in the vector; returns -1 if
249: * the object is not found.
250: */
251: public final int indexOf(long elem, int index) {
252:
253: for (int i = index; i < _size; i++) {
254: if (_data[i] == elem)
255: return i;
256: }
257:
258: return java.lang.Integer.MIN_VALUE;
259: }
260:
261: /**
262: * Searches for the first occurence of the given argument, beginning the
263: * search at index, and testing for equality using the equals method.
264: *
265: * @param elem
266: * object to look for
267: * @return the index of the first occurrence of the object argument in this
268: * vector at position index or later in the vector; returns -1 if
269: * the object is not found.
270: */
271: public final int indexOf(long elem) {
272:
273: for (int i = 0; i < _size; i++) {
274: if (_data[i] == elem)
275: return i;
276: }
277:
278: return java.lang.Integer.MIN_VALUE;
279: }
280:
281: /**
282: * Searches for the first occurence of the given argument, beginning the
283: * search at index, and testing for equality using the equals method.
284: *
285: * @param elem
286: * Object to look for
287: * @return the index of the first occurrence of the object argument in this
288: * vector at position index or later in the vector; returns -1 if
289: * the object is not found.
290: */
291: public final int lastIndexOf(long elem) {
292:
293: for (int i = (_size - 1); i >= 0; i--) {
294: if (_data[i] == elem)
295: return i;
296: }
297:
298: return java.lang.Integer.MIN_VALUE;
299: }
300:
301: public long[] getDataAsArray() {
302: if (_data.length == _size) {
303: return _data;
304: }
305: long[] result = new long[_size];
306: System.arraycopy(_data, 0, result, 0, _size);
307: return result;
308: }
309:
310: public final int binarySearch(long key) {
311: int low = 0;
312: int high = _size;
313:
314: while (low <= high) {
315: int mid = (low + high) >> 1;
316: long midVal = _data[mid];
317:
318: if (midVal < key)
319: low = mid + 1;
320: else if (midVal > key)
321: high = mid - 1;
322: else
323: return mid; // key found
324: }
325: return -(low + 1); // key not found.
326: }
327:
328: }
|