1/* 2 * [The "BSD license"] 3 * Copyright (c) 2010 Terence Parr 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. The name of the author may not be used to endorse or promote products 15 * derived from this software without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28package org.antlr.misc; 29 30import java.util.AbstractList; 31 32/** An ArrayList based upon int members. Not quite a real implementation of a 33 * modifiable list as I don't do, for example, add(index,element). 34 * TODO: unused? 35 */ 36public class IntArrayList extends AbstractList implements Cloneable { 37 private static final int DEFAULT_CAPACITY = 10; 38 protected int n = 0; 39 protected int[] elements = null; 40 41 public IntArrayList() { 42 this(DEFAULT_CAPACITY); 43 } 44 45 public IntArrayList(int initialCapacity) { 46 elements = new int[initialCapacity]; 47 } 48 49 /** Set the ith element. Like ArrayList, this does NOT affect size. */ 50 public int set(int i, int newValue) { 51 if ( i>=n ) { 52 setSize(i); // unlike definition of set in ArrayList, set size 53 } 54 int v = elements[i]; 55 elements[i] = newValue; 56 return v; 57 } 58 59 public boolean add(int o) { 60 if ( n>=elements.length ) { 61 grow(); 62 } 63 elements[n] = o; 64 n++; 65 return true; 66 } 67 68 public void setSize(int newSize) { 69 if ( newSize>=elements.length ) { 70 ensureCapacity(newSize); 71 } 72 n = newSize; 73 } 74 75 protected void grow() { 76 ensureCapacity((elements.length * 3)/2 + 1); 77 } 78 79 public boolean contains(int v) { 80 for (int i = 0; i < n; i++) { 81 int element = elements[i]; 82 if ( element == v ) { 83 return true; 84 } 85 } 86 return false; 87 } 88 89 public void ensureCapacity(int newCapacity) { 90 int oldCapacity = elements.length; 91 if (n>=oldCapacity) { 92 int oldData[] = elements; 93 elements = new int[newCapacity]; 94 System.arraycopy(oldData, 0, elements, 0, n); 95 } 96 } 97 98 public Object get(int i) { 99 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, this.elements.length); 140 return a; 141 } 142 143 public String toString() { 144 StringBuffer buf = new StringBuffer(); 145 for (int i = 0; i < n; i++) { 146 if ( i>0 ) { 147 buf.append(", "); 148 } 149 buf.append(elements[i]); 150 } 151 return buf.toString(); 152 } 153} 154