001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.compiler.util;
011:
012: import org.eclipse.jdt.core.compiler.CharOperation;
013:
014: public final class SimpleNameVector {
015:
016: static int INITIAL_SIZE = 10;
017:
018: public int size;
019: int maxSize;
020: char[][] elements;
021:
022: public SimpleNameVector() {
023:
024: this .maxSize = INITIAL_SIZE;
025: this .size = 0;
026: this .elements = new char[this .maxSize][];
027: }
028:
029: public void add(char[] newElement) {
030:
031: if (this .size == this .maxSize) // knows that size starts <= maxSize
032: System.arraycopy(this .elements, 0,
033: (this .elements = new char[this .maxSize *= 2][]), 0,
034: this .size);
035: this .elements[size++] = newElement;
036: }
037:
038: public void addAll(char[][] newElements) {
039:
040: if (this .size + newElements.length >= this .maxSize) {
041: this .maxSize = this .size + newElements.length; // assume no more elements will be added
042: System.arraycopy(this .elements, 0,
043: (this .elements = new char[this .maxSize][]), 0,
044: this .size);
045: }
046: System.arraycopy(newElements, 0, this .elements, this .size,
047: newElements.length);
048: this .size += newElements.length;
049: }
050:
051: public void copyInto(Object[] targetArray) {
052:
053: System.arraycopy(this .elements, 0, targetArray, 0, this .size);
054: }
055:
056: public boolean contains(char[] element) {
057:
058: for (int i = this .size; --i >= 0;)
059: if (CharOperation.equals(element, this .elements[i]))
060: return true;
061: return false;
062: }
063:
064: public char[] elementAt(int index) {
065: return this .elements[index];
066: }
067:
068: public char[] remove(char[] element) {
069:
070: // assumes only one occurrence of the element exists
071: for (int i = this .size; --i >= 0;)
072: if (element == this .elements[i]) {
073: // shift the remaining elements down one spot
074: System.arraycopy(this .elements, i + 1, this .elements,
075: i, --this .size - i);
076: this .elements[this .size] = null;
077: return element;
078: }
079: return null;
080: }
081:
082: public void removeAll() {
083:
084: for (int i = this .size; --i >= 0;)
085: this .elements[i] = null;
086: this .size = 0;
087: }
088:
089: public int size() {
090:
091: return this .size;
092: }
093:
094: public String toString() {
095: StringBuffer buffer = new StringBuffer();
096: for (int i = 0; i < this .size; i++) {
097: buffer.append(this .elements[i]).append("\n"); //$NON-NLS-1$
098: }
099: return buffer.toString();
100: }
101: }
|