Debian Codegraph @

debian/additionalSrc/trove4j/util/src/gnu/trove/generate/TPHash.template

raw
1 ///////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2001, Eric D. Friedman All Rights Reserved.
3 //
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License, or (at your option) any later version.
8 //
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 ///////////////////////////////////////////////////////////////////////////////
18 // THIS FILE IS AUTOGENERATED, PLEASE DO NOT EDIT OR ELSE
19 package gnu.trove;
20
21 /**
22 * An open addressed hashing implementation for #e# primitives.
23 *
24 * Created: Sun Nov 4 08:56:06 2001
25 *
26 * @author Eric D. Friedman
27 */
28
29 public abstract class T#E#Hash extends TPrimitiveHash
30 implements T#E#HashingStrategy {
31
32 /** the set of #e#s */
33 protected transient #e#[] _set;
34
35 /** strategy used to hash values in this collection */
36 protected final T#E#HashingStrategy _hashingStrategy;
37
38 /**
39 * Creates a new <code>T#E#Hash</code> instance with the default
40 * capacity and load factor.
41 */
42 public T#E#Hash() {
43 _hashingStrategy = this;
44 }
45
46 /**
47 * Creates a new <code>T#E#Hash</code> instance whose capacity
48 * is the next highest prime above <tt>initialCapacity + 1</tt>
49 * unless that value is already prime.
50 *
51 * @param initialCapacity an <code>int</code> value
52 */
53 public T#E#Hash(int initialCapacity) {
54 super(initialCapacity);
55 _hashingStrategy = this;
56 }
57
58 /**
59 * Creates a new <code>T#E#Hash</code> instance with a prime
60 * value at or near the specified capacity and load factor.
61 *
62 * @param initialCapacity used to find a prime capacity for the table.
63 * @param loadFactor used to calculate the threshold over which
64 * rehashing takes place.
65 */
66 public T#E#Hash(int initialCapacity, float loadFactor) {
67 super(initialCapacity, loadFactor);
68 _hashingStrategy = this;
69 }
70
71 /**
72 * Creates a new <code>T#E#Hash</code> instance with the default
73 * capacity and load factor.
74 * @param strategy used to compute hash codes and to compare keys.
75 */
76 public T#E#Hash(T#E#HashingStrategy strategy) {
77 _hashingStrategy = strategy;
78 }
79
80 /**
81 * Creates a new <code>T#E#Hash</code> instance whose capacity
82 * is the next highest prime above <tt>initialCapacity + 1</tt>
83 * unless that value is already prime.
84 *
85 * @param initialCapacity an <code>int</code> value
86 * @param strategy used to compute hash codes and to compare keys.
87 */
88 public T#E#Hash(int initialCapacity, T#E#HashingStrategy strategy) {
89 super(initialCapacity);
90 _hashingStrategy = strategy;
91 }
92
93 /**
94 * Creates a new <code>T#E#Hash</code> instance with a prime
95 * value at or near the specified capacity and load factor.
96 *
97 * @param initialCapacity used to find a prime capacity for the table.
98 * @param loadFactor used to calculate the threshold over which
99 * rehashing takes place.
100 * @param strategy used to compute hash codes and to compare keys.
101 */
102 public T#E#Hash(int initialCapacity, float loadFactor, T#E#HashingStrategy strategy) {
103 super(initialCapacity, loadFactor);
104 _hashingStrategy = strategy;
105 }
106
107 /**
108 * @return a deep clone of this collection
109 */
110 @Override
111 public Object clone() {
112 T#E#Hash h = (T#E#Hash)super.clone();
113 h._set = _set == null ? null : _set.clone();
114 return h;
115 }
116
117 /**
118 * initializes the hash table to a prime capacity which is at least
119 * <tt>initialCapacity + 1</tt>.
120 *
121 * @param initialCapacity an <code>int</code> value
122 * @return the actual capacity chosen
123 */
124 @Override
125 protected int setUp(int initialCapacity) {
126 int capacity = super.setUp(initialCapacity);
127 _set = initialCapacity == JUST_CREATED_CAPACITY ? null : new #e#[capacity];
128 return capacity;
129 }
130
131 /**
132 * Searches the set for <tt>val</tt>
133 *
134 * @param val an <code>#e#</code> value
135 * @return a <code>boolean</code> value
136 */
137 public boolean contains(#e# val) {
138 return index(val) >= 0;
139 }
140
141 /**
142 * Executes <tt>procedure</tt> for each element in the set.
143 *
144 * @param procedure a <code>TObjectProcedure</code> value
145 * @return false if the loop over the set terminated because
146 * the procedure returned false for some value.
147 */
148 public boolean forEach(T#E#Procedure procedure) {
149 byte[] states = _states;
150 #e#[] set = _set;
151 if (states != null) {
152 for (int i = states.length; i-- > 0;) {
153 if (states[i] == FULL && ! procedure.execute(set[i])) {
154 return false;
155 }
156 }
157 }
158 return true;
159 }
160
161 /**
162 * Releases the element currently stored at <tt>index</tt>.
163 *
164 * @param index an <code>int</code> value
165 */
166 @Override
167 protected void removeAt(int index) {
168 _set[index] = (#e#)0;
169 super.removeAt(index);
170 }
171
172 /**
173 * Locates the index of <tt>val</tt>.
174 *
175 * @param val an <code>#e#</code> value
176 * @return the index of <tt>val</tt> or -1 if it isn't in the set.
177 */
178 protected int index(#e# val) {
179 byte[] states = _states;
180 if (states == null) return -1;
181 #e#[] set = _set;
182 int length = states.length;
183 int hash = _hashingStrategy.computeHashCode(val) & 0x7fffffff;
184 int index = hash % length;
185
186 if (states[index] != FREE &&
187 (states[index] == REMOVED || set[index] != val)) {
188 // see Knuth, p. 529
189 int probe = 1 + (hash % (length - 2));
190
191 do {
192 index -= probe;
193 if (index < 0) {
194 index += length;
195 }
196 } while (states[index] != FREE &&
197 (states[index] == REMOVED || set[index] != val));
198 }
199
200 return states[index] == FREE ? -1 : index;
201 }
202
203 /**
204 * Locates the index at which <tt>val</tt> can be inserted. if
205 * there is already a value equal()ing <tt>val</tt> in the set,
206 * returns that value as a negative integer.
207 *
208 * @param val an <code>#e#</code> value
209 * @return an <code>int</code> value
210 */
211 protected int insertionIndex(#e# val) {
212 if (_set == null) {
213 setUp((int) (DEFAULT_INITIAL_CAPACITY / DEFAULT_LOAD_FACTOR + 1));
214 }
215 byte[] states = _states;
216 #e#[] set = _set;
217 int length = states.length;
218 int hash = _hashingStrategy.computeHashCode(val) & 0x7fffffff;
219 int index = hash % length;
220
221 if (states[index] == FREE) {
222 return index; // empty, all done
223 } else if (states[index] == FULL && set[index] == val) {
224 return -index -1; // already stored
225 } else { // already FULL or REMOVED, must probe
226 // compute the double hash
227 int probe = 1 + (hash % (length - 2));
228 // starting at the natural offset, probe until we find an
229 // offset that isn't full.
230 do {
231 index -= probe;
232 if (index < 0) {
233 index += length;
234 }
235 } while (states[index] == FULL && set[index] != val);
236
237 // if the index we found was removed: continue probing until we
238 // locate a free location or an element which equal()s the
239 // one we have.
240 if (states[index] == REMOVED) {
241 int firstRemoved = index;
242 while (states[index] != FREE &&
243 (states[index] == REMOVED || set[index] != val)) {
244 index -= probe;
245 if (index < 0) {
246 index += length;
247 }
248 }
249 return states[index] == FULL ? -index -1 : firstRemoved;
250 }
251 // if it's full, the key is already stored
252 return states[index] == FULL ? -index -1 : index;
253 }
254 }
255
256 /**
257 * Default implementation of T#E#HashingStrategy:
258 * delegates hashing to HashFunctions.hash(#e#).
259 *
260 * @param val the value to hash
261 * @return the hash code.
262 */
263 public final int computeHashCode(#e# val) {
264 return HashFunctions.hash(val);
265 }
266 } // T#E#Hash