X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.db.impl%2Fsrc%2Forg%2Fsimantics%2Fdb%2Fimpl%2Fquery%2FBinaryQueryHashMap.java;h=6556daf382a21878bcd6535f057282468c6a5b1f;hb=a88c02c2d0f4250caf887a130b9f4314c6564722;hp=a7a4411b8a127b17355d0b1c0cdb596d26691f25;hpb=969bd23cab98a79ca9101af33334000879fb60c5;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/BinaryQueryHashMap.java b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/BinaryQueryHashMap.java index a7a4411b8..6556daf38 100644 --- a/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/BinaryQueryHashMap.java +++ b/bundles/org.simantics.db.impl/src/org/simantics/db/impl/query/BinaryQueryHashMap.java @@ -1,176 +1,176 @@ -/******************************************************************************* - * 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 BinaryQueryHashMap extends BinaryQueryHash { - - /** - * Creates a new THashMap instance with the default - * capacity and load factor. - */ - public BinaryQueryHashMap() { - 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; - } - - /** - * 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. - */ - public BinaryQuery put(final long id, final BinaryQuery value) { - BinaryQuery previous = null; - Object oldKey; - int index = insertionIndex(id); - boolean isNewMapping = true; - if (index < 0) { - index = -index -1; - previous = _set[index]; - isNewMapping = false; - new Exception().printStackTrace(); - throw new Error("Duplicate entry in BinaryQueryHashMap2 " + value); - } - oldKey = _set[index]; - _set[index] = value; - if (isNewMapping) { - postInsertHook(oldKey == null); - } - - return previous; - - } - - /** - * rehashes the map to the new capacity. - * - * @param newCapacity an int value - */ - @SuppressWarnings("unchecked") - protected void rehash(int newCapacity) { - - int oldCapacity = _set.length; - BinaryQuery oldKeys[] = _set; - - _set = (BinaryQuery[])Array.newInstance(BinaryQuery.class, newCapacity); - - for (int i = oldCapacity; i-- > 0;) { - if(oldKeys[i] != null && oldKeys[i] != REMOVED) { - BinaryQuery o = oldKeys[i]; - int index = insertionIndex(o.id); - if (index < 0) { - throwObjectContractViolation(_set[(-index -1)], o); - } - _set[index] = o; - } - } - } - - /** - * retrieves the value for key - * - * @param key an Object value - * @return the value of key or null if no such mapping exists. - */ - public BinaryQuery get(final long id) { - int index = index(id); - return index < 0 ? null : _set[index]; - } - - /** - * Deletes a key/value pair from the map. - * - * @param key an Object value - * @return an Object value - */ - public Object remove(final long id) { - Object prev = null; - int index = index(id); - if (index >= 0) { - prev = _set[index]; - removeAt(index); // clear key,state; adjust size - } - return prev; - } - - final public void values(int level, CacheCollectionResult result) { - - for (int i = _set.length; i-- > 0;) { - CacheEntryBase entry = _set[i]; - if(entry != null && entry != REMOVED) { - if(entry.getLevel() <= level) - result.add(entry); - } - } - - } - - final public ArrayList values() { - - ArrayList result = new ArrayList(); - - for (int i = _set.length; i-- > 0;) { - if(_set[i] != null && _set[i] != REMOVED) { - result.add(_set[i]); - } - } - - return result; - - } - - final public ArrayList values(final int r1) { - - ArrayList result = new ArrayList(); - - for (int i = _set.length; i-- > 0;) { - if(_set[i] != null && _set[i] != REMOVED) { - BinaryQuery e = _set[i]; - if(e.r1() == r1) - result.add((T)e); - } - } - - return result; - - } - -} // 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 BinaryQueryHashMap extends BinaryQueryHash { + + /** + * Creates a new THashMap instance with the default + * capacity and load factor. + */ + public BinaryQueryHashMap() { + 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; + } + + /** + * 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. + */ + public BinaryQuery put(final long id, final BinaryQuery value) { + BinaryQuery previous = null; + Object oldKey; + int index = insertionIndex(id); + boolean isNewMapping = true; + if (index < 0) { + index = -index -1; + previous = _set[index]; + isNewMapping = false; + new Exception().printStackTrace(); + throw new Error("Duplicate entry in BinaryQueryHashMap2 " + value); + } + oldKey = _set[index]; + _set[index] = value; + if (isNewMapping) { + postInsertHook(oldKey == null); + } + + return previous; + + } + + /** + * rehashes the map to the new capacity. + * + * @param newCapacity an int value + */ + @SuppressWarnings("unchecked") + protected void rehash(int newCapacity) { + + int oldCapacity = _set.length; + BinaryQuery oldKeys[] = _set; + + _set = (BinaryQuery[])Array.newInstance(BinaryQuery.class, newCapacity); + + for (int i = oldCapacity; i-- > 0;) { + if(oldKeys[i] != null && oldKeys[i] != REMOVED) { + BinaryQuery o = oldKeys[i]; + int index = insertionIndex(o.id); + if (index < 0) { + throwObjectContractViolation(_set[(-index -1)], o); + } + _set[index] = o; + } + } + } + + /** + * retrieves the value for key + * + * @param key an Object value + * @return the value of key or null if no such mapping exists. + */ + public BinaryQuery get(final long id) { + int index = index(id); + return index < 0 ? null : _set[index]; + } + + /** + * Deletes a key/value pair from the map. + * + * @param key an Object value + * @return an Object value + */ + public Object remove(final long id) { + Object prev = null; + int index = index(id); + if (index >= 0) { + prev = _set[index]; + removeAt(index); // clear key,state; adjust size + } + return prev; + } + + final public void values(int level, CacheCollectionResult result) { + + for (int i = _set.length; i-- > 0;) { + CacheEntryBase entry = _set[i]; + if(entry != null && entry != REMOVED) { + if(entry.getLevel() <= level) + result.add(entry); + } + } + + } + + final public ArrayList values() { + + ArrayList result = new ArrayList(); + + for (int i = _set.length; i-- > 0;) { + if(_set[i] != null && _set[i] != REMOVED) { + result.add(_set[i]); + } + } + + return result; + + } + + final public ArrayList values(final int r1) { + + ArrayList result = new ArrayList(); + + for (int i = _set.length; i-- > 0;) { + if(_set[i] != null && _set[i] != REMOVED) { + BinaryQuery e = _set[i]; + if(e.r1() == r1) + result.add((T)e); + } + } + + return result; + + } + +} // THashMap