01: package antlr.collections.impl;
02:
03: /* ANTLR Translator Generator
04: * Project led by Terence Parr at http://www.cs.usfca.edu
05: * Software rights: http://www.antlr.org/license.html
06: */
07:
08: import java.util.Hashtable;
09: import java.util.Enumeration;
10:
11: import antlr.collections.impl.Vector;
12:
13: /**
14: * A simple indexed vector: a normal vector except that you must
15: * specify a key when adding an element. This allows fast lookup
16: * and allows the order of specification to be preserved.
17: */
18: public class IndexedVector {
19: protected Vector elements;
20: protected Hashtable index;
21:
22: /**
23: * IndexedVector constructor comment.
24: */
25: public IndexedVector() {
26: elements = new Vector(10);
27: index = new Hashtable(10);
28: }
29:
30: /**
31: * IndexedVector constructor comment.
32: * @param size int
33: */
34: public IndexedVector(int size) {
35: elements = new Vector(size);
36: index = new Hashtable(size);
37: }
38:
39: public synchronized void appendElement(Object key, Object value) {
40: elements.appendElement(value);
41: index.put(key, value);
42: }
43:
44: /**
45: * Returns the element at the specified index.
46: * @param index the index of the desired element
47: * @exception ArrayIndexOutOfBoundsException If an invalid
48: * index was given.
49: */
50: public Object elementAt(int i) {
51: return elements.elementAt(i);
52: }
53:
54: public Enumeration elements() {
55: return elements.elements();
56: }
57:
58: public Object getElement(Object key) {
59: Object o = index.get(key);
60: return o;
61: }
62:
63: /** remove element referred to by key NOT value; return false if not found. */
64: public synchronized boolean removeElement(Object key) {
65: Object value = index.get(key);
66: if (value == null) {
67: return false;
68: }
69: index.remove(key);
70: elements.removeElement(value);
71: return false;
72: }
73:
74: public int size() {
75: return elements.size();
76: }
77: }
|