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
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.db.impl.query;
\r
14 import java.lang.reflect.Array;
\r
15 import java.util.ArrayList;
\r
20 * An implementation of the Map interface which uses an open addressed
\r
21 * hash table to store its contents.
\r
23 * Created: Sun Nov 4 08:52:45 2001
\r
25 * @author Eric D. Friedman
\r
26 * @version $Id: BinaryQueryHashMap.java,v 1.2 2008/03/14 11:38:53 tuoksk Exp $
\r
28 public class DoubleKeyQueryHashMap<Procedure> extends DoubleKeyQueryHash<Procedure> {
\r
31 * Creates a new <code>THashMap</code> instance with the default
\r
32 * capacity and load factor.
\r
34 public DoubleKeyQueryHashMap() {
\r
39 * initialize the value array of the map.
\r
41 * @param initialCapacity an <code>int</code> value
\r
42 * @return an <code>int</code> value
\r
44 protected int setUp(int initialCapacity) {
\r
47 capacity = super.setUp(initialCapacity);
\r
48 // _values = (Object[]) new Object[capacity];
\r
52 private int sizeInternal = 0;
\r
55 * Inserts a key/value pair into the map.
\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
62 @SuppressWarnings("unchecked")
\r
63 public DoubleKeyValueMap put(final int id, final DoubleKeyValueMap value) {
\r
64 DoubleKeyValueMap previous = null;
\r
66 int index = insertionIndex(id);
\r
67 boolean isNewMapping = true;
\r
70 previous = _set[index];
\r
71 isNewMapping = false;
\r
73 oldKey = _set[index];
\r
74 _set[index] = value;
\r
76 postInsertHook(oldKey == null);
\r
82 public CacheEntry put(final long id, BinaryQuery value) {
\r
84 DoubleKeyValueMap map = get(r1);
\r
86 map = new DoubleKeyValueMap(r1);
\r
89 CacheEntry old = map.put(id, value);
\r
90 if(old == null) sizeInternal++;
\r
95 * rehashes the map to the new capacity.
\r
97 * @param newCapacity an <code>int</code> value
\r
99 protected void rehash(int newCapacity) {
\r
101 int oldCapacity = _set.length;
\r
102 DoubleKeyValueMap<Procedure> oldKeys[] = _set;
\r
104 DoubleKeyValueMap<Procedure> newKeys[] = (DoubleKeyValueMap<Procedure>[])Array.newInstance(DoubleKeyValueMap.class, newCapacity);
\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
111 throwObjectContractViolation(newKeys[(-index -1)], o);
\r
113 newKeys[index] = o;
\r
122 * retrieves the value for <tt>key</tt>
\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
127 final public DoubleKeyValueMap<Procedure> get(final int id) {
\r
128 // int index = index(id);
\r
129 // return index < 0 ? null : _set[index];
\r
133 final protected static long id(long r1, long r2) {
\r
134 long result = (r1<<32) | (r2 & 0xffffffffL);
\r
138 final public int r1(final long id) {
\r
139 return (int)(id>>>32);
\r
142 final public int r2(final long id) {
\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
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
159 * Deletes a key/value pair from the map.
\r
161 * @param key an <code>Object</code> value
\r
162 * @return an <code>Object</code> value
\r
164 public Object remove(final int id) {
\r
165 DoubleKeyValueMap<Procedure> prev = null;
\r
166 int index = index(id);
\r
168 prev = _set[index];
\r
169 removeAt(index); // clear key,state; adjust size
\r
170 sizeInternal-=prev.size();
\r
175 public Object remove(final long 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
185 final public void values(int level, CacheCollectionResult result) {
\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
196 final public ArrayList<CacheEntry> values() {
\r
198 ArrayList<CacheEntry> result = new ArrayList<CacheEntry>();
\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
212 public int size() {
\r
213 return sizeInternal;
\r
216 final public static ArrayList<BinaryQuery> NO_VALUES = new ArrayList<BinaryQuery>();
\r
218 final public <T extends BinaryQuery> ArrayList<T> values(int r1) {
\r
220 DoubleKeyValueMap map = get(r1);
\r
221 if(map == null) return (ArrayList<T>)NO_VALUES;
\r
223 return map.values();
\r