01: /*
02: * Copyright (c) 1998 - 2005 Versant Corporation
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * Versant Corporation - initial API and implementation
10: */
11: package com.versant.core.util;
12:
13: /**
14: * Growable int[]. This is based com.sosnoski.util.array.IntArray from
15: * Sosnoski Software Solutions, Inc.
16: */
17: public final class IntArray {
18:
19: private int[] buf;
20: private int size;
21:
22: public IntArray() {
23: this (64);
24: }
25:
26: public IntArray(int capacity) {
27: buf = new int[capacity];
28: }
29:
30: public int size() {
31: return size;
32: }
33:
34: private void ensureCapacity(int len) {
35: if (size + len > buf.length) {
36: int n = buf.length * 3 / 2 + 1;
37: if (size + len > n) {
38: n = size + len;
39: }
40: int[] a = new int[n];
41: System.arraycopy(buf, 0, a, 0, size);
42: buf = a;
43: }
44: }
45:
46: public void add(int v) {
47: ensureCapacity(size + 1);
48: buf[size++] = v;
49: }
50:
51: /**
52: * Add a value at a specified index in the array.
53: */
54: public void add(int index, int value) {
55: ensureCapacity(size + 1);
56: if (index == size) {
57: buf[size++] = value;
58: } else {
59: System.arraycopy(buf, index, buf, index + 1, size - index);
60: buf[index] = value;
61: }
62: }
63:
64: /**
65: * Constructs and returns a simple array containing the same data as held
66: * in this growable array.
67: */
68: public int[] toArray() {
69: int[] a = new int[size];
70: System.arraycopy(buf, 0, a, 0, size);
71: return a;
72: }
73:
74: public void clear() {
75: size = 0;
76: }
77:
78: /**
79: * Retrieve the value present at an index position in the array.
80: */
81: public int get(int index) {
82: return buf[index];
83: }
84:
85: }
|