1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.db.impl.query;
14 import java.lang.reflect.Array;
15 import java.util.ArrayList;
20 * An implementation of the Map interface which uses an open addressed
21 * hash table to store its contents.
23 * Created: Sun Nov 4 08:52:45 2001
25 * @author Eric D. Friedman
26 * @version $Id: BinaryQueryHashMap.java,v 1.2 2008/03/14 11:38:53 tuoksk Exp $
28 public class DoubleKeyQueryHashMap<Procedure> extends DoubleKeyQueryHash<Procedure> {
31 * Creates a new <code>THashMap</code> instance with the default
32 * capacity and load factor.
34 public DoubleKeyQueryHashMap() {
39 * initialize the value array of the map.
41 * @param initialCapacity an <code>int</code> value
42 * @return an <code>int</code> value
44 protected int setUp(int initialCapacity) {
47 capacity = super.setUp(initialCapacity);
48 // _values = (Object[]) new Object[capacity];
52 private int sizeInternal = 0;
55 * Inserts a key/value pair into the map.
57 * @param key an <code>Object</code> value
58 * @param value an <code>Object</code> value
59 * @return the previous value associated with <tt>key</tt>,
60 * or null if none was found.
62 @SuppressWarnings("unchecked")
63 public DoubleKeyValueMap put(final int id, final DoubleKeyValueMap value) {
64 DoubleKeyValueMap previous = null;
66 int index = insertionIndex(id);
67 boolean isNewMapping = true;
70 previous = _set[index];
76 postInsertHook(oldKey == null);
82 public CacheEntry put(final long id, BinaryQuery value) {
84 DoubleKeyValueMap map = get(r1);
86 map = new DoubleKeyValueMap(r1);
89 CacheEntry old = map.put(id, value);
90 if(old == null) sizeInternal++;
95 * rehashes the map to the new capacity.
97 * @param newCapacity an <code>int</code> value
99 protected void rehash(int newCapacity) {
101 int oldCapacity = _set.length;
102 DoubleKeyValueMap<Procedure> oldKeys[] = _set;
104 DoubleKeyValueMap<Procedure> newKeys[] = (DoubleKeyValueMap<Procedure>[])Array.newInstance(DoubleKeyValueMap.class, newCapacity);
106 for (int i = oldCapacity; i-- > 0;) {
107 if(oldKeys[i] != null && oldKeys[i] != REMOVED) {
108 DoubleKeyValueMap<Procedure> o = oldKeys[i];
109 int index = insertionIndex2(o.id, newKeys);
111 throwObjectContractViolation(newKeys[(-index -1)], o);
122 * retrieves the value for <tt>key</tt>
124 * @param key an <code>Object</code> value
125 * @return the value of <tt>key</tt> or null if no such mapping exists.
127 final public DoubleKeyValueMap<Procedure> get(final int id) {
128 // int index = index(id);
129 // return index < 0 ? null : _set[index];
133 final protected static long id(long r1, long r2) {
134 long result = (r1<<32) | (r2 & 0xffffffffL);
138 final public int r1(final long id) {
139 return (int)(id>>>32);
142 final public int r2(final long id) {
146 final public BinaryQuery get(final int r1, final int r2) {
147 DoubleKeyValueMap<Procedure> map = get(r1);
148 if(map == null) return null;
149 return map.get(id(r1,r2));
152 final public BinaryQuery get(final long id) {
153 DoubleKeyValueMap<Procedure> map = get(r1(id));
154 if(map == null) return null;
159 * Deletes a key/value pair from the map.
161 * @param key an <code>Object</code> value
162 * @return an <code>Object</code> value
164 public Object remove(final int id) {
165 DoubleKeyValueMap<Procedure> prev = null;
166 int index = index(id);
169 removeAt(index); // clear key,state; adjust size
170 sizeInternal-=prev.size();
175 public Object remove(final long id) {
177 DoubleKeyValueMap<Procedure> map = get(r1);
178 if(map == null) return null;
179 Object removed = map.remove(id);
180 if(removed != null) sizeInternal--;
181 if(map.isEmpty()) remove(r1);
185 final public void values(int level, CacheCollectionResult result) {
187 for (int i = _set.length; i-- > 0;) {
188 if(_set[i] != null && _set[i] != REMOVED) {
189 DoubleKeyValueMap map = _set[i];
190 map.values(level, result);
196 final public ArrayList<CacheEntry> values() {
198 ArrayList<CacheEntry> result = new ArrayList<CacheEntry>();
200 for (int i = _set.length; i-- > 0;) {
201 if(_set[i] != null && _set[i] != REMOVED) {
202 DoubleKeyValueMap map = _set[i];
203 result.addAll(map.values());
216 final public static ArrayList<BinaryQuery> NO_VALUES = new ArrayList<BinaryQuery>();
218 final public <T extends BinaryQuery> ArrayList<T> values(int r1) {
220 DoubleKeyValueMap map = get(r1);
221 if(map == null) return (ArrayList<T>)NO_VALUES;