dbf31115fa281f01d1f603ee96dee53abb5725bc
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / compiler / util / SimpleNameVector.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2001, 2002 International Business Machines Corp. and others.
3  * All rights reserved. This program and the accompanying materials 
4  * are made available under the terms of the Common Public License v0.5 
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v05.html
7  * 
8  * Contributors:
9  *     IBM Corporation - initial API and implementation
10  ******************************************************************************/
11 package net.sourceforge.phpdt.internal.compiler.util;
12
13 public final class SimpleNameVector {
14         
15         static int INITIAL_SIZE = 10;
16
17         public int size;
18         int maxSize;
19         char[][] elements;
20         
21         public SimpleNameVector() {
22                 
23                 this.maxSize = INITIAL_SIZE;
24                 this.size = 0;
25                 this.elements = new char[this.maxSize][];
26         }
27         
28         public void add(char[] newElement) {
29                 
30                 if (this.size == this.maxSize) // knows that size starts <= maxSize
31                         System.arraycopy(this.elements, 0, (this.elements = new char[this.maxSize *= 2][]), 0, this.size);
32                 this.elements[size++] = newElement;
33         }
34         
35         public void addAll(char[][] newElements) {
36                 
37                 if (this.size + newElements.length >= this.maxSize) {
38                         this.maxSize = this.size + newElements.length; // assume no more elements will be added
39                         System.arraycopy(this.elements, 0, (this.elements = new char[this.maxSize][]), 0, this.size);
40                 }
41                 System.arraycopy(newElements, 0, this.elements, this.size, newElements.length);
42                 this.size += newElements.length;
43         }
44         
45         public void copyInto(Object[] targetArray){
46                 
47                 System.arraycopy(this.elements, 0, targetArray, 0, this.size);
48         }
49         
50         public boolean contains(char[] element) {
51                 
52                 for (int i = this.size; --i >= 0;)
53                         if (CharOperation.equals(element, this.elements[i]))
54                                 return true;
55                 return false;
56         }
57         
58         public char[] elementAt(int index) {
59                 return this.elements[index];
60         }
61         
62         public char[] remove(char[] element) {
63                 
64                 // assumes only one occurrence of the element exists
65                 for (int i = this.size; --i >= 0;)
66                         if (element == this.elements[i]) {
67                                 // shift the remaining elements down one spot
68                                 System.arraycopy(this.elements, i + 1, this.elements, i, --this.size - i);
69                                 this.elements[this.size] = null;
70                                 return element;
71                         }
72                 return null;
73         }
74         
75         public void removeAll() {
76                 
77                 for (int i = this.size; --i >= 0;)
78                         this.elements[i] = null;
79                 this.size = 0;
80         }
81         
82         public int size(){
83                 
84                 return this.size;
85         }
86         
87         public String toString() {
88                 StringBuffer buffer = new StringBuffer();
89                 for (int i = 0; i < this.size; i++) {
90                         buffer.append(this.elements[i]).append("\n"); //$NON-NLS-1$
91                 }
92                 return buffer.toString();
93         }
94 }