01: /*
02: *
03: *
04: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
05: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
06: *
07: * This program is free software; you can redistribute it and/or
08: * modify it under the terms of the GNU General Public License version
09: * 2 only, as published by the Free Software Foundation.
10: *
11: * This program is distributed in the hope that it will be useful, but
12: * WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14: * General Public License version 2 for more details (a copy is
15: * included at /legal/license.txt).
16: *
17: * You should have received a copy of the GNU General Public License
18: * version 2 along with this work; if not, write to the Free Software
19: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20: * 02110-1301 USA
21: *
22: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
23: * Clara, CA 95054 or visit www.sun.com if you need additional
24: * information or have any questions.
25: */
26:
27: /*
28: * Set-of-objects
29: * Add, delete, query, enumerate.
30: * Dead simple.
31: * This set implementation is best for small, often
32: * empty sets, as used by the dependenceAnalysis classes.
33: * It appears that a good fraction ( 1/3 to 1/2 ) of those
34: * sets are empty, and most of the rest are pretty small.
35: * That is the target use of this implementation.
36: *
37: */
38: package util;
39:
40: import java.util.Enumeration;
41: import java.util.Vector;
42:
43: public class Set {
44:
45: static private final int defaultInitial = 10;
46:
47: int ninitial; // initial allocation for this one.
48: Vector setData;
49:
50: public Set(int nin) {
51: ninitial = nin;
52: }
53:
54: public Set() {
55: ninitial = defaultInitial;
56: }
57:
58: public boolean isIn(Object o) {
59: return (setData == null) ? false : setData.contains(o);
60: }
61:
62: public void addElement(Object o) {
63: // see if its already in. If not,
64: // add at end.
65: if (isIn(o))
66: return;
67:
68: // now add, extending if necessary.
69: if (setData == null) {
70: setData = new Vector(ninitial);
71: }
72: setData.addElement(o);
73: }
74:
75: public void add(Object o) {
76: addElement(o);
77: }
78:
79: public Enumeration elements() {
80: return (setData == null) ? EmptyEnumeration.instance : setData
81: .elements();
82: }
83:
84: public void deleteAllElements() {
85: setData = null;
86: }
87:
88: public int size() {
89: return (setData == null) ? 0 : setData.size();
90: }
91:
92: }
|