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.core;
011:
012: import org.eclipse.jdt.core.IType;
013:
014: public final class TypeVector {
015: static int INITIAL_SIZE = 10;
016:
017: public int size;
018: int maxSize;
019: IType[] elements;
020:
021: public final static IType[] NoElements = new IType[0];
022:
023: public TypeVector() {
024: maxSize = INITIAL_SIZE;
025: size = 0;
026: elements = new IType[maxSize];
027: }
028:
029: public TypeVector(IType[] types) {
030: this .size = types.length;
031: this .maxSize = this .size + 1; // when an element is added, it assumes that the length is > 0
032: elements = new IType[this .maxSize];
033: System.arraycopy(types, 0, elements, 0, this .size);
034: }
035:
036: public TypeVector(IType type) {
037: this .maxSize = INITIAL_SIZE;
038: this .size = 1;
039: elements = new IType[this .maxSize];
040: elements[0] = type;
041: }
042:
043: public void add(IType newElement) {
044: if (size == maxSize) // knows that size starts <= maxSize
045: System.arraycopy(elements, 0,
046: (elements = new IType[maxSize *= 2]), 0, size);
047: elements[size++] = newElement;
048: }
049:
050: public void addAll(IType[] newElements) {
051: if (size + newElements.length >= maxSize) {
052: maxSize = size + newElements.length; // assume no more elements will be added
053: System.arraycopy(elements, 0,
054: (elements = new IType[maxSize]), 0, size);
055: }
056: System.arraycopy(newElements, 0, elements, size,
057: newElements.length);
058: size += newElements.length;
059: }
060:
061: public boolean contains(IType element) {
062: for (int i = size; --i >= 0;)
063: if (element.equals(elements[i]))
064: return true;
065: return false;
066: }
067:
068: public TypeVector copy() {
069: TypeVector clone = new TypeVector();
070: int length = this .elements.length;
071: System.arraycopy(this .elements, 0,
072: clone.elements = new IType[length], 0, length);
073: clone.size = this .size;
074: clone.maxSize = this .maxSize;
075: return clone;
076: }
077:
078: public IType elementAt(int index) {
079: return elements[index];
080: }
081:
082: public IType[] elements() {
083:
084: // do not resize to 0 if empty since may add more elements later
085: if (this .size == 0)
086: return NoElements;
087:
088: if (this .size < this .maxSize) {
089: maxSize = size;
090: System.arraycopy(this .elements, 0,
091: (this .elements = new IType[maxSize]), 0, size);
092: }
093: return this .elements;
094: }
095:
096: public IType find(IType element) {
097: for (int i = size; --i >= 0;)
098: if (element == elements[i])
099: return elements[i];
100: return null;
101: }
102:
103: public IType remove(IType element) {
104: // assumes only one occurrence of the element exists
105: for (int i = size; --i >= 0;)
106: if (element == elements[i]) {
107: // shift the remaining elements down one spot
108: System.arraycopy(elements, i + 1, elements, i, --size
109: - i);
110: elements[size] = null;
111: return element;
112: }
113: return null;
114: }
115:
116: public void removeAll() {
117: for (int i = size; --i >= 0;)
118: elements[i] = null;
119: size = 0;
120: }
121:
122: public String toString() {
123: StringBuffer buffer = new StringBuffer("["); //$NON-NLS-1$
124: for (int i = 0; i < size; i++) {
125: buffer.append("\n"); //$NON-NLS-1$
126: buffer.append(elements[i]);
127: }
128: buffer.append("\n]"); //$NON-NLS-1$
129: return buffer.toString();
130: }
131: }
|