]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/DoubleKeyQueryHashMap.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.db.impl / src / org / simantics / db / impl / query / DoubleKeyQueryHashMap.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 DoubleKeyQueryHashMap<Procedure> extends DoubleKeyQueryHash<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 DoubleKeyQueryHashMap() {\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     private int sizeInternal = 0;\r
53     \r
54     /**\r
55      * Inserts a key/value pair into the map.\r
56      *\r
57      * @param key an <code>Object</code> value\r
58      * @param value an <code>Object</code> value\r
59      * @return the previous value associated with <tt>key</tt>,\r
60      * or null if none was found.\r
61      */\r
62     @SuppressWarnings("unchecked")\r
63         public DoubleKeyValueMap put(final int id, final DoubleKeyValueMap value) {\r
64         DoubleKeyValueMap previous = null;\r
65         Object oldKey;\r
66         int index = insertionIndex(id);\r
67         boolean isNewMapping = true;\r
68         if (index < 0) {\r
69             index = -index -1;\r
70             previous = _set[index];\r
71             isNewMapping = false;\r
72         }\r
73         oldKey = _set[index];\r
74         _set[index] = value;\r
75         if (isNewMapping) {\r
76             postInsertHook(oldKey == null);\r
77         }\r
78 \r
79         return previous;\r
80     }\r
81     \r
82     public CacheEntry put(final long id, BinaryQuery value) {\r
83         int r1 = r1(id);\r
84         DoubleKeyValueMap map = get(r1);\r
85         if(map == null) {\r
86                 map = new DoubleKeyValueMap(r1);\r
87                 put(r1, map);\r
88         }\r
89         CacheEntry old = map.put(id, value);\r
90         if(old == null) sizeInternal++;\r
91         return old;\r
92     }\r
93 \r
94     /**\r
95      * rehashes the map to the new capacity.\r
96      *\r
97      * @param newCapacity an <code>int</code> value\r
98      */\r
99         protected void rehash(int newCapacity) {\r
100         \r
101         int oldCapacity = _set.length;\r
102         DoubleKeyValueMap<Procedure> oldKeys[] = _set;\r
103 \r
104         DoubleKeyValueMap<Procedure> newKeys[]  = (DoubleKeyValueMap<Procedure>[])Array.newInstance(DoubleKeyValueMap.class, newCapacity);\r
105 \r
106         for (int i = oldCapacity; i-- > 0;) {\r
107             if(oldKeys[i] != null && oldKeys[i] != REMOVED) {\r
108                 DoubleKeyValueMap<Procedure> o = oldKeys[i];\r
109                 int index = insertionIndex2(o.id, newKeys);\r
110                 if (index < 0) {\r
111                     throwObjectContractViolation(newKeys[(-index -1)], o);\r
112                 }\r
113                 newKeys[index] = o;\r
114             }\r
115         }\r
116         \r
117         _set = newKeys;\r
118         \r
119     }\r
120 \r
121     /**\r
122      * retrieves the value for <tt>key</tt>\r
123      *\r
124      * @param key an <code>Object</code> value\r
125      * @return the value of <tt>key</tt> or null if no such mapping exists.\r
126      */\r
127     final public DoubleKeyValueMap<Procedure> get(final int id) {\r
128 //        int index = index(id);\r
129 //        return index < 0 ? null : _set[index];\r
130         return index2(id);\r
131     }\r
132     \r
133     final protected static long id(long r1, long r2) {\r
134         long result = (r1<<32) | (r2 & 0xffffffffL); \r
135         return result;\r
136     }\r
137 \r
138     final public int r1(final long id) {\r
139         return (int)(id>>>32);\r
140     }\r
141     \r
142     final public int r2(final long id) {\r
143         return (int)id;\r
144     }\r
145     \r
146     final public BinaryQuery get(final int r1, final int r2) {\r
147         DoubleKeyValueMap<Procedure> map = get(r1);\r
148         if(map == null) return null;\r
149         return map.get(id(r1,r2));\r
150     }\r
151 \r
152     final public BinaryQuery get(final long id) {\r
153         DoubleKeyValueMap<Procedure> map = get(r1(id));\r
154         if(map == null) return null;\r
155         return map.get(id);\r
156     }\r
157 \r
158     /**\r
159      * Deletes a key/value pair from the map.\r
160      *\r
161      * @param key an <code>Object</code> value\r
162      * @return an <code>Object</code> value\r
163      */\r
164     public Object remove(final int id) {\r
165         DoubleKeyValueMap<Procedure> prev = null;\r
166         int index = index(id);\r
167         if (index >= 0) {\r
168             prev = _set[index];\r
169             removeAt(index);    // clear key,state; adjust size\r
170             sizeInternal-=prev.size();\r
171         }\r
172         return prev;\r
173     }\r
174 \r
175     public Object remove(final long id) {\r
176         int r1 = r1(id);\r
177         DoubleKeyValueMap<Procedure> map = get(r1);\r
178         if(map == null) return null;\r
179         Object removed = map.remove(id);\r
180         if(removed != null) sizeInternal--;\r
181         if(map.isEmpty()) remove(r1);\r
182         return removed;\r
183     }\r
184 \r
185     final public void values(int level, CacheCollectionResult result) {\r
186         \r
187         for (int i = _set.length; i-- > 0;) {\r
188             if(_set[i] != null && _set[i] != REMOVED) {\r
189                 DoubleKeyValueMap map = _set[i];\r
190                 map.values(level, result);\r
191             }\r
192         }\r
193         \r
194     }\r
195 \r
196     final public ArrayList<CacheEntry> values() {\r
197 \r
198         ArrayList<CacheEntry> result = new ArrayList<CacheEntry>();\r
199         \r
200         for (int i = _set.length; i-- > 0;) {\r
201             if(_set[i] != null && _set[i] != REMOVED) {\r
202                 DoubleKeyValueMap map = _set[i];\r
203                 result.addAll(map.values());\r
204             }\r
205         }\r
206     \r
207         return result;\r
208         \r
209     }\r
210     \r
211     @Override\r
212     public int size() {\r
213         return sizeInternal;\r
214     }\r
215     \r
216     final public static ArrayList<BinaryQuery> NO_VALUES = new ArrayList<BinaryQuery>();\r
217 \r
218     final public <T extends BinaryQuery> ArrayList<T> values(int r1) {\r
219 \r
220         DoubleKeyValueMap map = get(r1);\r
221         if(map == null) return (ArrayList<T>)NO_VALUES;\r
222         \r
223         return map.values();\r
224         \r
225     }\r
226     \r
227 } // THashMap\r