]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/QueryIdentityHash.java
Fixed all line endings of the repository
[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     };
231
232     /**
233      * Creates a new <code>TObjectHash</code> instance with the
234      * default capacity and load factor.
235      */
236     public QueryIdentityHash() {
237         super();
238     }
239
240     /**
241      * Creates a new <code>TObjectHash</code> instance whose capacity
242      * is the next highest prime above <tt>initialCapacity + 1</tt>
243      * unless that value is already prime.
244      *
245      * @param initialCapacity an <code>int</code> value
246      */
247     public QueryIdentityHash(int initialCapacity) {
248         super(initialCapacity, 0.75f);
249     }
250
251     final public int capacity() {
252         return _set.length;
253     }
254
255     final protected void removeAt(int index) {
256         _set[index] = REMOVED;
257         super.removeAt(index);
258     }
259
260     /**
261      * initializes the Object set of this hash table.
262      *
263      * @param initialCapacity an <code>int</code> value
264      * @return an <code>int</code> value
265      */
266     final protected int setUp(int initialCapacity) {
267         int capacity;
268
269         capacity = super.setUp(initialCapacity);
270         _set = new CacheEntry[capacity];
271         return capacity;
272         
273     }
274
275     /**
276      * Searches the set for <tt>obj</tt>
277      *
278      * @param obj an <code>Object</code> value
279      * @return a <code>boolean</code> value
280      */
281     final public boolean contains(Object obj) {
282         return index(obj) >= 0;
283     }
284
285     /**
286      * Locates the index of <tt>obj</tt>.
287      *
288      * @param obj an <code>Object</code> value
289      * @return the index of <tt>obj</tt> or -1 if it isn't in the set.
290      */
291     final protected int index(Object obj) {
292
293         final Object[] set = _set;
294         final int length = set.length;
295         //final int hash = System.identityHashCode(obj) & 0x7fffffff;
296         final int hash = obj.hashCode() & 0x7fffffff;
297         int index = hash % length;
298         Object cur = set[index];
299
300         if ( cur == null ) return -1;
301
302         // NOTE: here it has to be REMOVED or FULL (some user-given value)
303         if ( cur == REMOVED || (cur != obj)) {
304             // see Knuth, p. 529
305             final int probe = 1 + (hash % (length - 2));
306
307             do {
308                 index -= probe;
309                 if (index < 0) {
310                     index += length;
311                 }
312                 cur = set[index];
313             } while (cur != null
314                  && (cur == REMOVED || (cur != obj)));
315         }
316
317         return cur == null ? -1 : index;
318     }
319     
320
321     /**
322      * Locates the index at which <tt>obj</tt> can be inserted.  if
323      * there is already a value equal()ing <tt>obj</tt> in the set,
324      * returns that value's index as <tt>-index - 1</tt>.
325      *
326      * @param obj an <code>Object</code> value
327      * @return the index of a FREE slot at which obj can be inserted
328      * or, if obj is already stored in the hash, the negative value of
329      * that index, minus 1: -index -1.
330      */
331     final protected int insertionIndex(Object obj) {
332
333         final Object[] set = _set;
334         final int length = set.length;
335         //final int hash = System.identityHashCode(obj) & 0x7fffffff;
336         final int hash = obj.hashCode() & 0x7fffffff;
337         int index = hash % length;
338         Object cur = set[index];
339
340         if (cur == null) {
341             return index;       // empty, all done
342         } else if (cur != REMOVED && (cur == obj)) {
343             return -index -1;   // already stored
344         } else {                // already FULL or REMOVED, must probe
345             // compute the double hash
346             final int probe = 1 + (hash % (length - 2));
347
348             // if the slot we landed on is FULL (but not removed), probe
349             // until we find an empty slot, a REMOVED slot, or an element
350             // equal to the one we are trying to insert.
351             // finding an empty slot means that the value is not present
352             // and that we should use that slot as the insertion point;
353             // finding a REMOVED slot means that we need to keep searching,
354             // however we want to remember the offset of that REMOVED slot
355             // so we can reuse it in case a "new" insertion (i.e. not an update)
356             // is possible.
357             // finding a matching value means that we've found that our desired
358             // key is already in the table
359             if (cur != REMOVED) {
360                 // starting at the natural offset, probe until we find an
361                 // offset that isn't full.
362                 do {
363                     index -= probe;
364                     if (index < 0) {
365                         index += length;
366                     }
367                     cur = set[index];
368                 } while (cur != null
369                          && cur != REMOVED
370                          && (cur != obj));
371             }
372
373             // if the index we found was removed: continue probing until we
374             // locate a free location or an element which equal()s the
375             // one we have.
376             if (cur == REMOVED) {
377                 int firstRemoved = index;
378                 while (cur != null
379                        && (cur == REMOVED || (cur != obj))) {
380                     index -= probe;
381                     if (index < 0) {
382                         index += length;
383                     }
384                     cur = set[index];
385                 }
386                 // NOTE: cur cannot == REMOVED in this block
387                 return (cur != null) ? -index -1 : firstRemoved;
388             }
389             // if it's full, the key is already stored
390             // NOTE: cur cannot equal REMOVE here (would have retuned already (see above)
391             return (cur != null) ? -index -1 : index;
392         }
393     }
394
395 } // TObjectHash