]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/QueryIdentityHash.java
396f1ba07790de3f597895a5a8536ff02fed6e90
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / QueryIdentityHash.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.db.impl.query;
13
14 import gnu.trove.impl.hash.THash;
15
16 import org.simantics.db.impl.graph.ReadGraphImpl;
17
18
19 /**
20  * An open addressed hashing implementation for Object types.
21  *
22  * Created: Sun Nov  4 08:56:06 2001
23  *
24  * @author Eric D. Friedman
25  * @version $Id: QueryIdentityHash.java,v 1.1 2008/03/14 11:32:01 tuoksk Exp $
26  */
27 abstract public class QueryIdentityHash extends THash {
28
29         static final long serialVersionUID = -3461112548087185871L;
30
31     /** the set of Objects */
32     protected transient CacheEntry[] _set;
33
34     protected static final CacheEntry REMOVED = new CacheEntry() {
35
36         @Override
37         public void addParent(CacheEntry entry) {
38             // TODO Auto-generated method stub
39             
40         }
41
42         @Override
43         public void clearResult(QuerySupport support) {
44             // TODO Auto-generated method stub
45             
46         }
47
48         @Override
49         public void discard() {
50             // TODO Auto-generated method stub
51             
52         }
53
54         @Override
55         public void except(Throwable t) {
56             // TODO Auto-generated method stub
57             
58         }
59
60         @Override
61         public Iterable<CacheEntry> getParents(QueryProcessor processor) {
62             // TODO Auto-generated method stub
63             return null;
64         }
65
66         @Override
67         public Query getQuery() {
68             // TODO Auto-generated method stub
69             return null;
70         }
71
72         @Override
73         public <T> T getResult() {
74             // TODO Auto-generated method stub
75             return null;
76         }
77
78         @Override
79         public boolean hasParents() {
80             // TODO Auto-generated method stub
81             return false;
82         }
83
84         @Override
85         public boolean isDiscarded() {
86             // TODO Auto-generated method stub
87             return false;
88         }
89
90         @Override
91         public boolean isExcepted() {
92             // TODO Auto-generated method stub
93             return false;
94         }
95
96         @Override
97         public boolean isFresh() {
98             // TODO Auto-generated method stub
99             return false;
100         }
101
102         @Override
103         public boolean isPending() {
104             // TODO Auto-generated method stub
105             return false;
106         }
107
108         @Override
109         public boolean isReady() {
110             // TODO Auto-generated method stub
111             return false;
112         }
113
114         @Override
115         public boolean isRefuted() {
116             // TODO Auto-generated method stub
117             return false;
118         }
119
120         @Override
121         public void performFromCache(ReadGraphImpl graph, Object provider, Object procedure) {
122             // TODO Auto-generated method stub
123             
124         }
125
126         @Override
127         public void refute() {
128             // TODO Auto-generated method stub
129             
130         }
131
132         @Override
133         CacheEntry pruneFirstParents() {
134             // TODO Auto-generated method stub
135                 return null;
136         }
137         
138         @Override
139         public void removeParent(CacheEntry entry) {
140             // TODO Auto-generated method stub
141             
142         }
143
144         @Override
145         public void setPending() {
146             // TODO Auto-generated method stub
147             
148         }
149
150         @Override
151         public void setReady() {
152             // TODO Auto-generated method stub
153             
154         }
155
156         @Override
157         public void setResult(Object result) {
158             // TODO Auto-generated method stub
159             
160         }
161         
162         @Override
163         boolean isImmutable(ReadGraphImpl graph) {
164                 return true;
165         }
166
167                 @Override
168                 boolean shouldBeCollected() {
169                         // TODO Auto-generated method stub
170                         return false;
171                 }
172
173                 @Override
174                 CacheEntry getFirstParent(QueryProcessor processor) {
175                         // TODO Auto-generated method stub
176                         return null;
177                 }
178                 
179                 @Override
180                 boolean moreThanOneParent(QueryProcessor processor) {
181                         // TODO Auto-generated method stub
182                         return false;
183                 }
184
185                 @Override
186                 int parentCount(QueryProcessor processor) {
187                         // TODO Auto-generated method stub
188                         return 0;
189                 }
190                 
191                 @Override
192                 short getLevel() {
193                         // TODO Auto-generated method stub
194                         return 0;
195                 }
196
197                 @Override
198                 short setLevel(short level) {
199                         // TODO Auto-generated method stub
200                         return level;
201                 }
202                 
203                 @Override
204                 void prepareRecompute(QuerySupport querySupport) {
205                         // TODO Auto-generated method stub
206                 }
207
208                 @Override
209                 int getGCStatus() {
210                         // TODO Auto-generated method stub
211                         return 0;
212                 }
213
214                 @Override
215                 int setGCStatus(int status) {
216                         // TODO Auto-generated method stub
217                         return 0;
218                 }
219
220                 @Override
221                 void setGCStatusFlag(int flag, boolean value) {
222                         // TODO Auto-generated method stub
223                         
224                 }
225                 
226                 public Object getOriginalRequest() {
227                         throw new UnsupportedOperationException();
228                 }
229
230         @Override
231         void pruneParentSet() {
232         }
233
234     };
235
236     /**
237      * Creates a new <code>TObjectHash</code> instance with the
238      * default capacity and load factor.
239      */
240     public QueryIdentityHash() {
241         super();
242     }
243
244     /**
245      * Creates a new <code>TObjectHash</code> instance whose capacity
246      * is the next highest prime above <tt>initialCapacity + 1</tt>
247      * unless that value is already prime.
248      *
249      * @param initialCapacity an <code>int</code> value
250      */
251     public QueryIdentityHash(int initialCapacity) {
252         super(initialCapacity, 0.75f);
253     }
254
255     final public int capacity() {
256         return _set.length;
257     }
258
259     final protected void removeAt(int index) {
260         _set[index] = REMOVED;
261         super.removeAt(index);
262     }
263
264     /**
265      * initializes the Object set of this hash table.
266      *
267      * @param initialCapacity an <code>int</code> value
268      * @return an <code>int</code> value
269      */
270     final protected int setUp(int initialCapacity) {
271         int capacity;
272
273         capacity = super.setUp(initialCapacity);
274         _set = new CacheEntry[capacity];
275         return capacity;
276         
277     }
278
279     /**
280      * Searches the set for <tt>obj</tt>
281      *
282      * @param obj an <code>Object</code> value
283      * @return a <code>boolean</code> value
284      */
285     final public boolean contains(Object obj) {
286         return index(obj) >= 0;
287     }
288
289     /**
290      * Locates the index of <tt>obj</tt>.
291      *
292      * @param obj an <code>Object</code> value
293      * @return the index of <tt>obj</tt> or -1 if it isn't in the set.
294      */
295     final protected int index(Object obj) {
296
297         final Object[] set = _set;
298         final int length = set.length;
299         //final int hash = System.identityHashCode(obj) & 0x7fffffff;
300         final int hash = obj.hashCode() & 0x7fffffff;
301         int index = hash % length;
302         Object cur = set[index];
303
304         if ( cur == null ) return -1;
305
306         // NOTE: here it has to be REMOVED or FULL (some user-given value)
307         if ( cur == REMOVED || (cur != obj)) {
308             // see Knuth, p. 529
309             final int probe = 1 + (hash % (length - 2));
310
311             do {
312                 index -= probe;
313                 if (index < 0) {
314                     index += length;
315                 }
316                 cur = set[index];
317             } while (cur != null
318                  && (cur == REMOVED || (cur != obj)));
319         }
320
321         return cur == null ? -1 : index;
322     }
323     
324
325     /**
326      * Locates the index at which <tt>obj</tt> can be inserted.  if
327      * there is already a value equal()ing <tt>obj</tt> in the set,
328      * returns that value's index as <tt>-index - 1</tt>.
329      *
330      * @param obj an <code>Object</code> value
331      * @return the index of a FREE slot at which obj can be inserted
332      * or, if obj is already stored in the hash, the negative value of
333      * that index, minus 1: -index -1.
334      */
335     final protected int insertionIndex(Object obj) {
336
337         final Object[] set = _set;
338         final int length = set.length;
339         //final int hash = System.identityHashCode(obj) & 0x7fffffff;
340         final int hash = obj.hashCode() & 0x7fffffff;
341         int index = hash % length;
342         Object cur = set[index];
343
344         if (cur == null) {
345             return index;       // empty, all done
346         } else if (cur != REMOVED && (cur == obj)) {
347             return -index -1;   // already stored
348         } else {                // already FULL or REMOVED, must probe
349             // compute the double hash
350             final int probe = 1 + (hash % (length - 2));
351
352             // if the slot we landed on is FULL (but not removed), probe
353             // until we find an empty slot, a REMOVED slot, or an element
354             // equal to the one we are trying to insert.
355             // finding an empty slot means that the value is not present
356             // and that we should use that slot as the insertion point;
357             // finding a REMOVED slot means that we need to keep searching,
358             // however we want to remember the offset of that REMOVED slot
359             // so we can reuse it in case a "new" insertion (i.e. not an update)
360             // is possible.
361             // finding a matching value means that we've found that our desired
362             // key is already in the table
363             if (cur != REMOVED) {
364                 // starting at the natural offset, probe until we find an
365                 // offset that isn't full.
366                 do {
367                     index -= probe;
368                     if (index < 0) {
369                         index += length;
370                     }
371                     cur = set[index];
372                 } while (cur != null
373                          && cur != REMOVED
374                          && (cur != obj));
375             }
376
377             // if the index we found was removed: continue probing until we
378             // locate a free location or an element which equal()s the
379             // one we have.
380             if (cur == REMOVED) {
381                 int firstRemoved = index;
382                 while (cur != null
383                        && (cur == REMOVED || (cur != obj))) {
384                     index -= probe;
385                     if (index < 0) {
386                         index += length;
387                     }
388                     cur = set[index];
389                 }
390                 // NOTE: cur cannot == REMOVED in this block
391                 return (cur != null) ? -index -1 : firstRemoved;
392             }
393             // if it's full, the key is already stored
394             // NOTE: cur cannot equal REMOVE here (would have retuned already (see above)
395             return (cur != null) ? -index -1 : index;
396         }
397     }
398
399 } // TObjectHash