]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/BinaryQueryHashMap.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / BinaryQueryHashMap.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.db.impl.query;\r
13 \r
14 import java.lang.reflect.Array;\r
15 import java.util.ArrayList;\r
16 \r
17 \r
18 \r
19 /**\r
20  * An implementation of the Map interface which uses an open addressed\r
21  * hash table to store its contents.\r
22  *\r
23  * Created: Sun Nov  4 08:52:45 2001\r
24  *\r
25  * @author Eric D. Friedman\r
26  * @version $Id: BinaryQueryHashMap.java,v 1.2 2008/03/14 11:38:53 tuoksk Exp $\r
27  */\r
28 public class BinaryQueryHashMap<Procedure> extends BinaryQueryHash<Procedure> {\r
29 \r
30     /**\r
31      * Creates a new <code>THashMap</code> instance with the default\r
32      * capacity and load factor.\r
33      */\r
34     public BinaryQueryHashMap() {\r
35         super();\r
36     }\r
37 \r
38     /**\r
39      * initialize the value array of the map.\r
40      *\r
41      * @param initialCapacity an <code>int</code> value\r
42      * @return an <code>int</code> value\r
43      */\r
44     protected int setUp(int initialCapacity) {\r
45         int capacity;\r
46         \r
47         capacity = super.setUp(initialCapacity);\r
48 //        _values = (Object[]) new Object[capacity];\r
49         return capacity;\r
50     }\r
51     \r
52     /**\r
53      * Inserts a key/value pair into the map.\r
54      *\r
55      * @param key an <code>Object</code> value\r
56      * @param value an <code>Object</code> value\r
57      * @return the previous value associated with <tt>key</tt>,\r
58      * or null if none was found.\r
59      */\r
60         public BinaryQuery<Procedure> put(final long id, final BinaryQuery<Procedure> value) {\r
61         BinaryQuery<Procedure> previous = null;\r
62         Object oldKey;\r
63         int index = insertionIndex(id);\r
64         boolean isNewMapping = true;\r
65         if (index < 0) {\r
66             index = -index -1;\r
67             previous = _set[index];\r
68             isNewMapping = false;\r
69             new Exception().printStackTrace();\r
70             throw new Error("Duplicate entry in BinaryQueryHashMap2 " + value);\r
71         }\r
72         oldKey = _set[index];\r
73         _set[index] = value;\r
74         if (isNewMapping) {\r
75             postInsertHook(oldKey == null);\r
76         }\r
77 \r
78         return previous;\r
79         \r
80     }\r
81 \r
82     /**\r
83      * rehashes the map to the new capacity.\r
84      *\r
85      * @param newCapacity an <code>int</code> value\r
86      */\r
87     @SuppressWarnings("unchecked")\r
88         protected void rehash(int newCapacity) {\r
89         \r
90         int oldCapacity = _set.length;\r
91         BinaryQuery oldKeys[] = _set;\r
92 \r
93         _set = (BinaryQuery[])Array.newInstance(BinaryQuery.class, newCapacity);\r
94 \r
95         for (int i = oldCapacity; i-- > 0;) {\r
96             if(oldKeys[i] != null && oldKeys[i] != REMOVED) {\r
97                 BinaryQuery o = oldKeys[i];\r
98                 int index = insertionIndex(o.id);\r
99                 if (index < 0) {\r
100                     throwObjectContractViolation(_set[(-index -1)], o);\r
101                 }\r
102                 _set[index] = o;\r
103             }\r
104         }\r
105     }\r
106 \r
107     /**\r
108      * retrieves the value for <tt>key</tt>\r
109      *\r
110      * @param key an <code>Object</code> value\r
111      * @return the value of <tt>key</tt> or null if no such mapping exists.\r
112      */\r
113     public BinaryQuery<Procedure> get(final long id) {\r
114         int index = index(id);\r
115         return index < 0 ? null : _set[index];\r
116     }\r
117 \r
118     /**\r
119      * Deletes a key/value pair from the map.\r
120      *\r
121      * @param key an <code>Object</code> value\r
122      * @return an <code>Object</code> value\r
123      */\r
124     public Object remove(final long id) {\r
125         Object prev = null;\r
126         int index = index(id);\r
127         if (index >= 0) {\r
128             prev = _set[index];\r
129             removeAt(index);    // clear key,state; adjust size\r
130         }\r
131         return prev;\r
132     }\r
133     \r
134     final public void values(int level, CacheCollectionResult result) {\r
135 \r
136         for (int i = _set.length; i-- > 0;) {\r
137                 CacheEntryBase entry = _set[i];\r
138             if(entry != null && entry != REMOVED) {\r
139                 if(entry.getLevel() <= level)\r
140                         result.add(entry);\r
141             }\r
142         }\r
143 \r
144     }\r
145 \r
146     final public ArrayList<CacheEntry> values() {\r
147 \r
148         ArrayList<CacheEntry> result = new ArrayList<CacheEntry>();\r
149         \r
150         for (int i = _set.length; i-- > 0;) {\r
151             if(_set[i] != null && _set[i] != REMOVED) {\r
152                 result.add(_set[i]);\r
153             }\r
154         }\r
155     \r
156         return result;\r
157         \r
158     }\r
159 \r
160     final public <T extends BinaryQuery> ArrayList<T> values(final int r1) {\r
161 \r
162         ArrayList<T> result = new ArrayList<T>();\r
163         \r
164         for (int i = _set.length; i-- > 0;) {\r
165             if(_set[i] != null && _set[i] != REMOVED) {\r
166                 BinaryQuery<Procedure> e = _set[i];\r
167                 if(e.r1() == r1)\r
168                         result.add((T)e);\r
169             }\r
170         }\r
171     \r
172         return result;\r
173         \r
174     }\r
175     \r
176 } // THashMap\r