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