]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/QueryIdentityHash.java
c9190193fe5587a26490be5350fc1fc553b57fef
[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 Object performFromCache(ReadGraphImpl graph, Object provider, Object procedure) {
122                 return null;
123         }
124
125         @Override
126         public void refute() {
127             // TODO Auto-generated method stub
128             
129         }
130
131         @Override
132         CacheEntry pruneFirstParents() {
133             // TODO Auto-generated method stub
134                 return null;
135         }
136         
137         @Override
138         public void removeParent(CacheEntry entry) {
139             // TODO Auto-generated method stub
140             
141         }
142
143         @Override
144         public void setPending() {
145             // TODO Auto-generated method stub
146             
147         }
148
149         @Override
150         public void setReady() {
151             // TODO Auto-generated method stub
152             
153         }
154
155         @Override
156         public void setResult(Object result) {
157             // TODO Auto-generated method stub
158             
159         }
160         
161         @Override
162         boolean isImmutable(ReadGraphImpl graph) {
163                 return true;
164         }
165
166                 @Override
167                 boolean shouldBeCollected() {
168                         // TODO Auto-generated method stub
169                         return false;
170                 }
171
172                 @Override
173                 CacheEntry getFirstParent(QueryProcessor processor) {
174                         // TODO Auto-generated method stub
175                         return null;
176                 }
177                 
178                 @Override
179                 boolean moreThanOneParent(QueryProcessor processor) {
180                         // TODO Auto-generated method stub
181                         return false;
182                 }
183
184                 @Override
185                 int parentCount(QueryProcessor processor) {
186                         // TODO Auto-generated method stub
187                         return 0;
188                 }
189                 
190                 @Override
191                 short getLevel() {
192                         // TODO Auto-generated method stub
193                         return 0;
194                 }
195
196                 @Override
197                 short setLevel(short level) {
198                         // TODO Auto-generated method stub
199                         return level;
200                 }
201                 
202                 @Override
203                 void prepareRecompute(QuerySupport querySupport) {
204                         // TODO Auto-generated method stub
205                 }
206
207                 @Override
208                 int getGCStatus() {
209                         // TODO Auto-generated method stub
210                         return 0;
211                 }
212
213                 @Override
214                 int setGCStatus(int status) {
215                         // TODO Auto-generated method stub
216                         return 0;
217                 }
218
219                 @Override
220                 void setGCStatusFlag(int flag, boolean value) {
221                         // TODO Auto-generated method stub
222                         
223                 }
224                 
225                 public Object getOriginalRequest() {
226                         throw new UnsupportedOperationException();
227                 }
228         
229     };
230
231     /**
232      * Creates a new <code>TObjectHash</code> instance with the
233      * default capacity and load factor.
234      */
235     public QueryIdentityHash() {
236         super();
237     }
238
239     /**
240      * Creates a new <code>TObjectHash</code> instance whose capacity
241      * is the next highest prime above <tt>initialCapacity + 1</tt>
242      * unless that value is already prime.
243      *
244      * @param initialCapacity an <code>int</code> value
245      */
246     public QueryIdentityHash(int initialCapacity) {
247         super(initialCapacity, 0.75f);
248     }
249
250     final public int capacity() {
251         return _set.length;
252     }
253
254     final protected void removeAt(int index) {
255         _set[index] = REMOVED;
256         super.removeAt(index);
257     }
258
259     /**
260      * initializes the Object set of this hash table.
261      *
262      * @param initialCapacity an <code>int</code> value
263      * @return an <code>int</code> value
264      */
265     final protected int setUp(int initialCapacity) {
266         int capacity;
267
268         capacity = super.setUp(initialCapacity);
269         _set = new CacheEntry[capacity];
270         return capacity;
271         
272     }
273
274     /**
275      * Searches the set for <tt>obj</tt>
276      *
277      * @param obj an <code>Object</code> value
278      * @return a <code>boolean</code> value
279      */
280     final public boolean contains(Object obj) {
281         return index(obj) >= 0;
282     }
283
284     /**
285      * Locates the index of <tt>obj</tt>.
286      *
287      * @param obj an <code>Object</code> value
288      * @return the index of <tt>obj</tt> or -1 if it isn't in the set.
289      */
290     final protected int index(Object obj) {
291
292         final Object[] set = _set;
293         final int length = set.length;
294         //final int hash = System.identityHashCode(obj) & 0x7fffffff;
295         final int hash = obj.hashCode() & 0x7fffffff;
296         int index = hash % length;
297         Object cur = set[index];
298
299         if ( cur == null ) return -1;
300
301         // NOTE: here it has to be REMOVED or FULL (some user-given value)
302         if ( cur == REMOVED || (cur != obj)) {
303             // see Knuth, p. 529
304             final int probe = 1 + (hash % (length - 2));
305
306             do {
307                 index -= probe;
308                 if (index < 0) {
309                     index += length;
310                 }
311                 cur = set[index];
312             } while (cur != null
313                  && (cur == REMOVED || (cur != obj)));
314         }
315
316         return cur == null ? -1 : index;
317     }
318     
319
320     /**
321      * Locates the index at which <tt>obj</tt> can be inserted.  if
322      * there is already a value equal()ing <tt>obj</tt> in the set,
323      * returns that value's index as <tt>-index - 1</tt>.
324      *
325      * @param obj an <code>Object</code> value
326      * @return the index of a FREE slot at which obj can be inserted
327      * or, if obj is already stored in the hash, the negative value of
328      * that index, minus 1: -index -1.
329      */
330     final protected int insertionIndex(Object obj) {
331
332         final Object[] set = _set;
333         final int length = set.length;
334         //final int hash = System.identityHashCode(obj) & 0x7fffffff;
335         final int hash = obj.hashCode() & 0x7fffffff;
336         int index = hash % length;
337         Object cur = set[index];
338
339         if (cur == null) {
340             return index;       // empty, all done
341         } else if (cur != REMOVED && (cur == obj)) {
342             return -index -1;   // already stored
343         } else {                // already FULL or REMOVED, must probe
344             // compute the double hash
345             final int probe = 1 + (hash % (length - 2));
346
347             // if the slot we landed on is FULL (but not removed), probe
348             // until we find an empty slot, a REMOVED slot, or an element
349             // equal to the one we are trying to insert.
350             // finding an empty slot means that the value is not present
351             // and that we should use that slot as the insertion point;
352             // finding a REMOVED slot means that we need to keep searching,
353             // however we want to remember the offset of that REMOVED slot
354             // so we can reuse it in case a "new" insertion (i.e. not an update)
355             // is possible.
356             // finding a matching value means that we've found that our desired
357             // key is already in the table
358             if (cur != REMOVED) {
359                 // starting at the natural offset, probe until we find an
360                 // offset that isn't full.
361                 do {
362                     index -= probe;
363                     if (index < 0) {
364                         index += length;
365                     }
366                     cur = set[index];
367                 } while (cur != null
368                          && cur != REMOVED
369                          && (cur != obj));
370             }
371
372             // if the index we found was removed: continue probing until we
373             // locate a free location or an element which equal()s the
374             // one we have.
375             if (cur == REMOVED) {
376                 int firstRemoved = index;
377                 while (cur != null
378                        && (cur == REMOVED || (cur != obj))) {
379                     index -= probe;
380                     if (index < 0) {
381                         index += length;
382                     }
383                     cur = set[index];
384                 }
385                 // NOTE: cur cannot == REMOVED in this block
386                 return (cur != null) ? -index -1 : firstRemoved;
387             }
388             // if it's full, the key is already stored
389             // NOTE: cur cannot equal REMOVE here (would have retuned already (see above)
390             return (cur != null) ? -index -1 : index;
391         }
392     }
393
394 } // TObjectHash