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;
24 public final static IType[] NoElements = new IType[0];
27 maxSize = INITIAL_SIZE;
29 elements = new IType[maxSize];
32 public TypeVector(IType[] types) {
33 this.size = types.length;
34 this.maxSize = this.size + 1; // when an element is added, it assumes
35 // that the length is > 0
36 elements = new IType[this.maxSize];
37 System.arraycopy(types, 0, elements, 0, this.size);
40 public TypeVector(IType type) {
41 this.maxSize = INITIAL_SIZE;
43 elements = new IType[this.maxSize];
47 public void add(IType newElement) {
48 if (size == maxSize) // knows that size starts <= maxSize
49 System.arraycopy(elements, 0, (elements = new IType[maxSize *= 2]),
51 elements[size++] = newElement;
54 public void addAll(IType[] newElements) {
55 if (size + newElements.length >= maxSize) {
56 maxSize = size + newElements.length; // assume no more elements
58 System.arraycopy(elements, 0, (elements = new IType[maxSize]), 0,
61 System.arraycopy(newElements, 0, elements, size, newElements.length);
62 size += newElements.length;
65 public boolean contains(IType element) {
66 for (int i = size; --i >= 0;)
67 if (element.equals(elements[i]))
72 public TypeVector copy() {
73 TypeVector clone = new TypeVector();
74 int length = this.elements.length;
75 System.arraycopy(this.elements, 0, clone.elements = new IType[length],
77 clone.size = this.size;
78 clone.maxSize = this.maxSize;
82 public IType elementAt(int index) {
83 return elements[index];
86 public IType[] elements() {
88 // do not resize to 0 if empty since may add more elements later
92 if (this.size < this.maxSize) {
94 System.arraycopy(this.elements, 0,
95 (this.elements = new IType[maxSize]), 0, size);
100 public IType find(IType element) {
101 for (int i = size; --i >= 0;)
102 if (element == elements[i])
107 public IType remove(IType element) {
108 // assumes only one occurrence of the element exists
109 for (int i = size; --i >= 0;)
110 if (element == elements[i]) {
111 // shift the remaining elements down one spot
112 System.arraycopy(elements, i + 1, elements, i, --size - i);
113 elements[size] = null;
119 public void removeAll() {
120 for (int i = size; --i >= 0;)
125 public String toString() {
126 StringBuffer buffer = new StringBuffer("["); //$NON-NLS-1$
127 for (int i = 0; i < size; i++) {
128 buffer.append("\n"); //$NON-NLS-1$
129 buffer.append(elements[i]);
131 buffer.append("\n]"); //$NON-NLS-1$
132 return buffer.toString();