KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gov > nasa > jpf > util > HashPool


1 //
2
// Copyright (C) 2005 United States Government as represented by the
3
// Administrator of the National Aeronautics and Space Administration
4
// (NASA). All Rights Reserved.
5
//
6
// This software is distributed under the NASA Open Source Agreement
7
// (NOSA), version 1.3. The NOSA has been approved by the Open Source
8
// Initiative. See the file NOSA-1.3-JPF at the top of the distribution
9
// directory tree for the complete NOSA document.
10
//
11
// THE SUBJECT SOFTWARE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY OF ANY
12
// KIND, EITHER EXPRESSED, IMPLIED, OR STATUTORY, INCLUDING, BUT NOT
13
// LIMITED TO, ANY WARRANTY THAT THE SUBJECT SOFTWARE WILL CONFORM TO
14
// SPECIFICATIONS, ANY IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR
15
// A PARTICULAR PURPOSE, OR FREEDOM FROM INFRINGEMENT, ANY WARRANTY THAT
16
// THE SUBJECT SOFTWARE WILL BE ERROR FREE, OR ANY WARRANTY THAT
17
// DOCUMENTATION, IF PROVIDED, WILL CONFORM TO THE SUBJECT SOFTWARE.
18
//
19
package gov.nasa.jpf.util;
20
21 import java.util.Hashtable JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.Set JavaDoc;
24 import java.util.TreeSet JavaDoc;
25
26
27 /**
28  * data structure used to do hash collapsing. All the major state components
29  * (fields, Monitors, StackFrames, uThreadData) are stored in pools to
30  * determine if they are new. Only the pool index values are used to
31  * compute hash values
32  *
33  * <2do> pcm - check if an open hashing scheme wouldn't be more efficient. This
34  * looks like a major memory hotspot
35  */

36 public class HashPool {
37   private static final int DELTA = 10;
38   private Hashtable JavaDoc pool;
39   private int size;
40   private int length;
41   private Object JavaDoc[] objects;
42
43   public HashPool () {
44     pool = new Hashtable JavaDoc();
45     objects = new Object JavaDoc[length = DELTA];
46     objects[0] = null;
47     size = 1;
48   }
49
50   public PoolEntry getEntry (Object JavaDoc o) {
51     if (o == null) {
52       return null;
53     }
54
55     PoolEntry e = (PoolEntry) pool.get(o);
56
57     if (e != null) {
58       return e;
59     }
60
61     return put(o);
62   }
63
64   public int getIndex (Object JavaDoc o) {
65     if (o == null) {
66       return -1;
67     }
68
69     PoolEntry e = (PoolEntry) pool.get(o);
70
71     if (e != null) {
72       return e.idx;
73     }
74
75     return put(o).idx;
76   }
77
78   public Object JavaDoc getObject (int idx) {
79     return objects[idx];
80   }
81
82   public Object JavaDoc get (Object JavaDoc o) {
83     if (o == null) {
84       return null;
85     }
86
87     PoolEntry e = (PoolEntry) pool.get(o);
88
89     if (e != null) {
90       return e.obj;
91     }
92
93     return put(o).obj;
94   }
95
96   public synchronized void print () {
97     System.out.println("{");
98
99     Set JavaDoc s = new TreeSet JavaDoc(pool.values());
100
101     for (Iterator JavaDoc i = s.iterator(); i.hasNext();) {
102       Object JavaDoc entry = i.next();
103       System.out.println("\t" + entry);
104     }
105
106     System.out.println("}");
107   }
108
109   public synchronized PoolEntry put (Object JavaDoc o) {
110     PoolEntry e = (PoolEntry) pool.get(o);
111
112     if (e != null) {
113       return e;
114     }
115
116     if (length < (size + 1)) {
117       Object JavaDoc[] newObjects = new Object JavaDoc[length + DELTA];
118       System.arraycopy(objects, 0, newObjects, 0, length);
119       objects = newObjects;
120       length += DELTA;
121     }
122
123     objects[size] = o;
124     pool.put(o, e = new PoolEntry(o, size++));
125
126     return e;
127   }
128
129   public synchronized int size () {
130     return size;
131   }
132
133   /**
134    * DOCUMENT ME!
135    */

136   public class PoolEntry implements Comparable JavaDoc {
137     private Object JavaDoc obj;
138     private int idx;
139
140     public PoolEntry (Object JavaDoc o, int i) {
141       obj = o;
142       idx = i;
143     }
144
145     public int getIndex () {
146       return idx;
147     }
148
149     public Object JavaDoc getObject () {
150       return obj;
151     }
152
153     public int compareTo (Object JavaDoc o) {
154       PoolEntry other = (PoolEntry) o;
155
156       return (idx - other.idx);
157     }
158
159     public boolean equals (Object JavaDoc obj) {
160       PoolEntry other = (PoolEntry) obj;
161
162       return (other.idx == idx);
163     }
164
165     public int hashCode () {
166       return idx;
167     }
168
169     public String JavaDoc toString () {
170       return idx + " => " + obj;
171     }
172   }
173 }
Popular Tags