improved PHP parser
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / core / TypeVector.java
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
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  *******************************************************************************/
11 package net.sourceforge.phpdt.internal.core;
12
13 import net.sourceforge.phpdt.core.IType;
14
15 public final class TypeVector {
16         static int INITIAL_SIZE = 10;
17         
18         public int size;
19         int maxSize;
20         IType[] elements;
21         
22         public final static IType[] NoElements = new IType[0];
23         
24 public TypeVector() {
25         maxSize = INITIAL_SIZE;
26         size = 0;
27         elements = new IType[maxSize];
28 }
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);     
34 }
35 public TypeVector(IType type) {
36         this.maxSize = INITIAL_SIZE;
37         this.size = 1;
38         elements = new IType[this.maxSize];
39         elements[0] = type;
40 }
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;
45 }
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);
50         }
51         System.arraycopy(newElements, 0, elements, size, newElements.length);
52         size += newElements.length;
53 }
54 public boolean contains(IType element) {
55         for (int i = size; --i >= 0;)
56                 if (element.equals(elements[i]))
57                         return true;
58         return false;
59 }
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;
66         return clone;
67 }
68 public IType elementAt(int index) {
69         return elements[index];
70 }
71 public IType[] elements() {
72         
73         // do not resize to 0 if empty since may add more elements later
74         if (this.size == 0) return NoElements;
75         
76         if (this.size < this.maxSize) {
77                 maxSize = size;
78                 System.arraycopy(this.elements, 0, (this.elements = new IType[maxSize]), 0, size);
79         }
80         return this.elements;
81 }
82 public IType find(IType element) {
83         for (int i = size; --i >= 0;)
84                 if (element == elements[i])
85                         return elements[i];
86         return null;
87 }
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;
95                         return element;
96                 }
97         return null;
98 }
99 public void removeAll() {
100         for (int i = size; --i >= 0;)
101                 elements[i] = null;
102         size = 0;
103 }
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]);
109         }
110         buffer.append("\n]"); //$NON-NLS-1$
111         return buffer.toString();
112 }
113 }