X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.db.impl%2Fsrc%2Forg%2Fsimantics%2Fdb%2Fimpl%2Fquery%2FDoubleKeyQueryHashMap.java;h=d59f5beb80a3e54e2246785fc997a0bb3d9ab105;hp=fccfd96b0bd4abc0c2ded4a78bcb2fc576cc0267;hb=HEAD;hpb=969bd23cab98a79ca9101af33334000879fb60c5 diff --git a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/DoubleKeyQueryHashMap.java b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/DoubleKeyQueryHashMap.java index fccfd96b0..d59f5beb8 100644 --- a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/DoubleKeyQueryHashMap.java +++ b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/DoubleKeyQueryHashMap.java @@ -1,227 +1,227 @@ -/******************************************************************************* - * Copyright (c) 2007, 2010 Association for Decentralized Information Management - * in Industry THTH ry. - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v1.0 - * which accompanies this distribution, and is available at - * http://www.eclipse.org/legal/epl-v10.html - * - * Contributors: - * VTT Technical Research Centre of Finland - initial API and implementation - *******************************************************************************/ -package org.simantics.db.impl.query; - -import java.lang.reflect.Array; -import java.util.ArrayList; - - - -/** - * An implementation of the Map interface which uses an open addressed - * hash table to store its contents. - * - * Created: Sun Nov 4 08:52:45 2001 - * - * @author Eric D. Friedman - * @version $Id: BinaryQueryHashMap.java,v 1.2 2008/03/14 11:38:53 tuoksk Exp $ - */ -public class DoubleKeyQueryHashMap extends DoubleKeyQueryHash { - - /** - * Creates a new THashMap instance with the default - * capacity and load factor. - */ - public DoubleKeyQueryHashMap() { - super(); - } - - /** - * initialize the value array of the map. - * - * @param initialCapacity an int value - * @return an int value - */ - protected int setUp(int initialCapacity) { - int capacity; - - capacity = super.setUp(initialCapacity); -// _values = (Object[]) new Object[capacity]; - return capacity; - } - - private int sizeInternal = 0; - - /** - * Inserts a key/value pair into the map. - * - * @param key an Object value - * @param value an Object value - * @return the previous value associated with key, - * or null if none was found. - */ - @SuppressWarnings("unchecked") - public DoubleKeyValueMap put(final int id, final DoubleKeyValueMap value) { - DoubleKeyValueMap previous = null; - Object oldKey; - int index = insertionIndex(id); - boolean isNewMapping = true; - if (index < 0) { - index = -index -1; - previous = _set[index]; - isNewMapping = false; - } - oldKey = _set[index]; - _set[index] = value; - if (isNewMapping) { - postInsertHook(oldKey == null); - } - - return previous; - } - - public CacheEntry put(final long id, BinaryQuery value) { - int r1 = r1(id); - DoubleKeyValueMap map = get(r1); - if(map == null) { - map = new DoubleKeyValueMap(r1); - put(r1, map); - } - CacheEntry old = map.put(id, value); - if(old == null) sizeInternal++; - return old; - } - - /** - * rehashes the map to the new capacity. - * - * @param newCapacity an int value - */ - protected void rehash(int newCapacity) { - - int oldCapacity = _set.length; - DoubleKeyValueMap oldKeys[] = _set; - - DoubleKeyValueMap newKeys[] = (DoubleKeyValueMap[])Array.newInstance(DoubleKeyValueMap.class, newCapacity); - - for (int i = oldCapacity; i-- > 0;) { - if(oldKeys[i] != null && oldKeys[i] != REMOVED) { - DoubleKeyValueMap o = oldKeys[i]; - int index = insertionIndex2(o.id, newKeys); - if (index < 0) { - throwObjectContractViolation(newKeys[(-index -1)], o); - } - newKeys[index] = o; - } - } - - _set = newKeys; - - } - - /** - * retrieves the value for key - * - * @param key an Object value - * @return the value of key or null if no such mapping exists. - */ - final public DoubleKeyValueMap get(final int id) { -// int index = index(id); -// return index < 0 ? null : _set[index]; - return index2(id); - } - - final protected static long id(long r1, long r2) { - long result = (r1<<32) | (r2 & 0xffffffffL); - return result; - } - - final public int r1(final long id) { - return (int)(id>>>32); - } - - final public int r2(final long id) { - return (int)id; - } - - final public BinaryQuery get(final int r1, final int r2) { - DoubleKeyValueMap map = get(r1); - if(map == null) return null; - return map.get(id(r1,r2)); - } - - final public BinaryQuery get(final long id) { - DoubleKeyValueMap map = get(r1(id)); - if(map == null) return null; - return map.get(id); - } - - /** - * Deletes a key/value pair from the map. - * - * @param key an Object value - * @return an Object value - */ - public Object remove(final int id) { - DoubleKeyValueMap prev = null; - int index = index(id); - if (index >= 0) { - prev = _set[index]; - removeAt(index); // clear key,state; adjust size - sizeInternal-=prev.size(); - } - return prev; - } - - public Object remove(final long id) { - int r1 = r1(id); - DoubleKeyValueMap map = get(r1); - if(map == null) return null; - Object removed = map.remove(id); - if(removed != null) sizeInternal--; - if(map.isEmpty()) remove(r1); - return removed; - } - - final public void values(int level, CacheCollectionResult result) { - - for (int i = _set.length; i-- > 0;) { - if(_set[i] != null && _set[i] != REMOVED) { - DoubleKeyValueMap map = _set[i]; - map.values(level, result); - } - } - - } - - final public ArrayList values() { - - ArrayList result = new ArrayList(); - - for (int i = _set.length; i-- > 0;) { - if(_set[i] != null && _set[i] != REMOVED) { - DoubleKeyValueMap map = _set[i]; - result.addAll(map.values()); - } - } - - return result; - - } - - @Override - public int size() { - return sizeInternal; - } - - final public static ArrayList NO_VALUES = new ArrayList(); - - final public ArrayList values(int r1) { - - DoubleKeyValueMap map = get(r1); - if(map == null) return (ArrayList)NO_VALUES; - - return map.values(); - - } - -} // THashMap +/******************************************************************************* + * Copyright (c) 2007, 2010 Association for Decentralized Information Management + * in Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.db.impl.query; + +import java.lang.reflect.Array; +import java.util.ArrayList; + + + +/** + * An implementation of the Map interface which uses an open addressed + * hash table to store its contents. + * + * Created: Sun Nov 4 08:52:45 2001 + * + * @author Eric D. Friedman + * @version $Id: BinaryQueryHashMap.java,v 1.2 2008/03/14 11:38:53 tuoksk Exp $ + */ +public class DoubleKeyQueryHashMap extends DoubleKeyQueryHash { + + /** + * Creates a new THashMap instance with the default + * capacity and load factor. + */ + public DoubleKeyQueryHashMap() { + super(); + } + + /** + * initialize the value array of the map. + * + * @param initialCapacity an int value + * @return an int value + */ + protected int setUp(int initialCapacity) { + int capacity; + + capacity = super.setUp(initialCapacity); +// _values = (Object[]) new Object[capacity]; + return capacity; + } + + private int sizeInternal = 0; + + /** + * Inserts a key/value pair into the map. + * + * @param key an Object value + * @param value an Object value + * @return the previous value associated with key, + * or null if none was found. + */ + @SuppressWarnings("unchecked") + public DoubleKeyValueMap put(final int id, final DoubleKeyValueMap value) { + DoubleKeyValueMap previous = null; + Object oldKey; + int index = insertionIndex(id); + boolean isNewMapping = true; + if (index < 0) { + index = -index -1; + previous = _set[index]; + isNewMapping = false; + } + oldKey = _set[index]; + _set[index] = value; + if (isNewMapping) { + postInsertHook(oldKey == null); + } + + return previous; + } + + public CacheEntry put(final long id, BinaryQuery value) { + int r1 = r1(id); + DoubleKeyValueMap map = get(r1); + if(map == null) { + map = new DoubleKeyValueMap(r1); + put(r1, map); + } + CacheEntry old = map.put(id, value); + if(old == null) sizeInternal++; + return old; + } + + /** + * rehashes the map to the new capacity. + * + * @param newCapacity an int value + */ + protected void rehash(int newCapacity) { + + int oldCapacity = _set.length; + DoubleKeyValueMap oldKeys[] = _set; + + DoubleKeyValueMap newKeys[] = (DoubleKeyValueMap[])Array.newInstance(DoubleKeyValueMap.class, newCapacity); + + for (int i = oldCapacity; i-- > 0;) { + if(oldKeys[i] != null && oldKeys[i] != REMOVED) { + DoubleKeyValueMap o = oldKeys[i]; + int index = insertionIndex2(o.id, newKeys); + if (index < 0) { + throwObjectContractViolation(newKeys[(-index -1)], o); + } + newKeys[index] = o; + } + } + + _set = newKeys; + + } + + /** + * retrieves the value for key + * + * @param key an Object value + * @return the value of key or null if no such mapping exists. + */ + final public DoubleKeyValueMap get(final int id) { +// int index = index(id); +// return index < 0 ? null : _set[index]; + return index2(id); + } + + final protected static long id(long r1, long r2) { + long result = (r1<<32) | (r2 & 0xffffffffL); + return result; + } + + final public int r1(final long id) { + return (int)(id>>>32); + } + + final public int r2(final long id) { + return (int)id; + } + + final public BinaryQuery get(final int r1, final int r2) { + DoubleKeyValueMap map = get(r1); + if(map == null) return null; + return map.get(id(r1,r2)); + } + + final public BinaryQuery get(final long id) { + DoubleKeyValueMap map = get(r1(id)); + if(map == null) return null; + return map.get(id); + } + + /** + * Deletes a key/value pair from the map. + * + * @param key an Object value + * @return an Object value + */ + public Object remove(final int id) { + DoubleKeyValueMap prev = null; + int index = index(id); + if (index >= 0) { + prev = _set[index]; + removeAt(index); // clear key,state; adjust size + sizeInternal-=prev.size(); + } + return prev; + } + + public Object remove(final long id) { + int r1 = r1(id); + DoubleKeyValueMap map = get(r1); + if(map == null) return null; + Object removed = map.remove(id); + if(removed != null) sizeInternal--; + if(map.isEmpty()) remove(r1); + return removed; + } + + final public void values(int level, CacheCollectionResult result) { + + for (int i = _set.length; i-- > 0;) { + if(_set[i] != null && _set[i] != REMOVED) { + DoubleKeyValueMap map = _set[i]; + map.values(level, result); + } + } + + } + + final public ArrayList values() { + + ArrayList result = new ArrayList(); + + for (int i = _set.length; i-- > 0;) { + if(_set[i] != null && _set[i] != REMOVED) { + DoubleKeyValueMap map = _set[i]; + result.addAll(map.values()); + } + } + + return result; + + } + + @Override + public int size() { + return sizeInternal; + } + + final public static ArrayList NO_VALUES = new ArrayList(); + + final public ArrayList values(int r1) { + + DoubleKeyValueMap map = get(r1); + if(map == null) return (ArrayList)NO_VALUES; + + return map.values(); + + } + +} // THashMap