1 /*******************************************************************************
2 * Copyright (c) 2000, 2004 IBM Corporation and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Common Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/cpl-v10.html
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.core;
13 import net.sourceforge.phpdt.core.IType;
15 public final class TypeVector {
16 static int INITIAL_SIZE = 10;
22 public final static IType[] NoElements = new IType[0];
25 maxSize = INITIAL_SIZE;
27 elements = new IType[maxSize];
29 public TypeVector(IType[] types) {
30 this.size = types.length;
31 this.maxSize = this.size + 1; // when an element is added, it assumes that the length is > 0
32 elements = new IType[this.maxSize];
33 System.arraycopy(types, 0, elements, 0, this.size);
35 public TypeVector(IType type) {
36 this.maxSize = INITIAL_SIZE;
38 elements = new IType[this.maxSize];
41 public void add(IType newElement) {
42 if (size == maxSize) // knows that size starts <= maxSize
43 System.arraycopy(elements, 0, (elements = new IType[maxSize *= 2]), 0, size);
44 elements[size++] = newElement;
46 public void addAll(IType[] newElements) {
47 if (size + newElements.length >= maxSize) {
48 maxSize = size + newElements.length; // assume no more elements will be added
49 System.arraycopy(elements, 0, (elements = new IType[maxSize]), 0, size);
51 System.arraycopy(newElements, 0, elements, size, newElements.length);
52 size += newElements.length;
54 public boolean contains(IType element) {
55 for (int i = size; --i >= 0;)
56 if (element.equals(elements[i]))
60 public TypeVector copy() {
61 TypeVector clone = new TypeVector();
62 int length = this.elements.length;
63 System.arraycopy(this.elements, 0, clone.elements = new IType[length], 0, length);
64 clone.size = this.size;
65 clone.maxSize = this.maxSize;
68 public IType elementAt(int index) {
69 return elements[index];
71 public IType[] elements() {
73 // do not resize to 0 if empty since may add more elements later
74 if (this.size == 0) return NoElements;
76 if (this.size < this.maxSize) {
78 System.arraycopy(this.elements, 0, (this.elements = new IType[maxSize]), 0, size);
82 public IType find(IType element) {
83 for (int i = size; --i >= 0;)
84 if (element == elements[i])
88 public IType remove(IType element) {
89 // assumes only one occurrence of the element exists
90 for (int i = size; --i >= 0;)
91 if (element == elements[i]) {
92 // shift the remaining elements down one spot
93 System.arraycopy(elements, i + 1, elements, i, --size - i);
94 elements[size] = null;
99 public void removeAll() {
100 for (int i = size; --i >= 0;)
104 public String toString() {
105 StringBuffer buffer = new StringBuffer("["); //$NON-NLS-1$
106 for (int i = 0; i < size; i++) {
107 buffer.append("\n"); //$NON-NLS-1$
108 buffer.append(elements[i]);
110 buffer.append("\n]"); //$NON-NLS-1$
111 return buffer.toString();