001: /*
002: [The "BSD licence"]
003: Copyright (c) 2005-2006 Terence Parr
004: All rights reserved.
005:
006: Redistribution and use in source and binary forms, with or without
007: modification, are permitted provided that the following conditions
008: are met:
009: 1. Redistributions of source code must retain the above copyright
010: notice, this list of conditions and the following disclaimer.
011: 2. Redistributions in binary form must reproduce the above copyright
012: notice, this list of conditions and the following disclaimer in the
013: documentation and/or other materials provided with the distribution.
014: 3. The name of the author may not be used to endorse or promote products
015: derived from this software without specific prior written permission.
016:
017: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
018: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
019: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
020: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
021: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
022: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
023: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
024: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
025: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
026: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
027: */
028: package org.antlr.misc;
029:
030: import java.util.AbstractList;
031:
032: /** An ArrayList based upon int members. Not quite a real implementation of a
033: * modifiable list as I don't do, for example, add(index,element).
034: * TODO: unused?
035: */
036: public class IntArrayList extends AbstractList implements Cloneable {
037: private static final int DEFAULT_CAPACITY = 10;
038: protected int n = 0;
039: protected int[] elements = null;
040:
041: public IntArrayList() {
042: this (DEFAULT_CAPACITY);
043: }
044:
045: public IntArrayList(int initialCapacity) {
046: elements = new int[initialCapacity];
047: }
048:
049: /** Set the ith element. Like ArrayList, this does NOT affect size. */
050: public int set(int i, int newValue) {
051: if (i >= n) {
052: setSize(i); // unlike definition of set in ArrayList, set size
053: }
054: int v = elements[i];
055: elements[i] = newValue;
056: return v;
057: }
058:
059: public boolean add(int o) {
060: if (n >= elements.length) {
061: grow();
062: }
063: elements[n] = o;
064: n++;
065: return true;
066: }
067:
068: public void setSize(int newSize) {
069: if (newSize >= elements.length) {
070: ensureCapacity(newSize);
071: }
072: n = newSize;
073: }
074:
075: protected void grow() {
076: ensureCapacity((elements.length * 3) / 2 + 1);
077: }
078:
079: public boolean contains(int v) {
080: for (int i = 0; i < n; i++) {
081: int element = elements[i];
082: if (element == v) {
083: return true;
084: }
085: }
086: return false;
087: }
088:
089: public void ensureCapacity(int newCapacity) {
090: int oldCapacity = elements.length;
091: if (n >= oldCapacity) {
092: int oldData[] = elements;
093: elements = new int[newCapacity];
094: System.arraycopy(oldData, 0, elements, 0, n);
095: }
096: }
097:
098: public Object get(int i) {
099: return Utils.integer(element(i));
100: }
101:
102: public int element(int i) {
103: return elements[i];
104: }
105:
106: public int[] elements() {
107: int[] a = new int[n];
108: System.arraycopy(elements, 0, a, 0, n);
109: return a;
110: }
111:
112: public int size() {
113: return n;
114: }
115:
116: public int capacity() {
117: return elements.length;
118: }
119:
120: public boolean equals(Object o) {
121: if (o == null) {
122: return false;
123: }
124: IntArrayList other = (IntArrayList) o;
125: if (this .size() != other.size()) {
126: return false;
127: }
128: for (int i = 0; i < n; i++) {
129: if (elements[i] != other.elements[i]) {
130: return false;
131: }
132: }
133: return true;
134: }
135:
136: public Object clone() throws CloneNotSupportedException {
137: IntArrayList a = (IntArrayList) super .clone();
138: a.n = this .n;
139: System.arraycopy(this .elements, 0, a.elements, 0,
140: this .elements.length);
141: return a;
142: }
143:
144: public String toString() {
145: StringBuffer buf = new StringBuffer();
146: for (int i = 0; i < n; i++) {
147: if (i > 0) {
148: buf.append(", ");
149: }
150: buf.append(elements[i]);
151: }
152: return buf.toString();
153: }
154: }
|