diff options
Diffstat (limited to 'src/de/lmu/ifi/dbs/elki/database/ids')
84 files changed, 4180 insertions, 398 deletions
diff --git a/src/de/lmu/ifi/dbs/elki/database/ids/ArrayDBIDs.java b/src/de/lmu/ifi/dbs/elki/database/ids/ArrayDBIDs.java index 7e9c55c0..865e0bb7 100644 --- a/src/de/lmu/ifi/dbs/elki/database/ids/ArrayDBIDs.java +++ b/src/de/lmu/ifi/dbs/elki/database/ids/ArrayDBIDs.java @@ -4,7 +4,7 @@ package de.lmu.ifi.dbs.elki.database.ids; This file is part of ELKI: Environment for Developing KDD-Applications Supported by Index-Structures - Copyright (C) 2012 + Copyright (C) 2013 Ludwig-Maximilians-Universität München Lehr- und Forschungseinheit für Datenbanksysteme ELKI Development Team @@ -47,7 +47,7 @@ public interface ArrayDBIDs extends DBIDs { * @param index Position * @param var Variable to assign the value to. */ - public void assign(int index, DBIDVar var); + public void assignVar(int index, DBIDVar var); /** * Iterable diff --git a/src/de/lmu/ifi/dbs/elki/database/ids/ArrayModifiableDBIDs.java b/src/de/lmu/ifi/dbs/elki/database/ids/ArrayModifiableDBIDs.java index ffac393b..3db40630 100644 --- a/src/de/lmu/ifi/dbs/elki/database/ids/ArrayModifiableDBIDs.java +++ b/src/de/lmu/ifi/dbs/elki/database/ids/ArrayModifiableDBIDs.java @@ -6,7 +6,7 @@ import java.util.Comparator; This file is part of ELKI: Environment for Developing KDD-Applications Supported by Index-Structures - Copyright (C) 2012 + Copyright (C) 2013 Ludwig-Maximilians-Universität München Lehr- und Forschungseinheit für Datenbanksysteme ELKI Development Team diff --git a/src/de/lmu/ifi/dbs/elki/database/ids/ArrayStaticDBIDs.java b/src/de/lmu/ifi/dbs/elki/database/ids/ArrayStaticDBIDs.java index a14eac0c..47cf295f 100644 --- a/src/de/lmu/ifi/dbs/elki/database/ids/ArrayStaticDBIDs.java +++ b/src/de/lmu/ifi/dbs/elki/database/ids/ArrayStaticDBIDs.java @@ -4,7 +4,7 @@ package de.lmu.ifi.dbs.elki.database.ids; This file is part of ELKI: Environment for Developing KDD-Applications Supported by Index-Structures - Copyright (C) 2012 + Copyright (C) 2013 Ludwig-Maximilians-Universität München Lehr- und Forschungseinheit für Datenbanksysteme ELKI Development Team diff --git a/src/de/lmu/ifi/dbs/elki/database/ids/DBID.java b/src/de/lmu/ifi/dbs/elki/database/ids/DBID.java index 5abf4377..a773ee27 100644 --- a/src/de/lmu/ifi/dbs/elki/database/ids/DBID.java +++ b/src/de/lmu/ifi/dbs/elki/database/ids/DBID.java @@ -4,7 +4,7 @@ package de.lmu.ifi.dbs.elki.database.ids; This file is part of ELKI: Environment for Developing KDD-Applications Supported by Index-Structures - Copyright (C) 2012 + Copyright (C) 2013 Ludwig-Maximilians-Universität München Lehr- und Forschungseinheit für Datenbanksysteme ELKI Development Team diff --git a/src/de/lmu/ifi/dbs/elki/database/ids/DBIDArrayIter.java b/src/de/lmu/ifi/dbs/elki/database/ids/DBIDArrayIter.java index fefe5ad1..ef1132b3 100644 --- a/src/de/lmu/ifi/dbs/elki/database/ids/DBIDArrayIter.java +++ b/src/de/lmu/ifi/dbs/elki/database/ids/DBIDArrayIter.java @@ -4,7 +4,7 @@ package de.lmu.ifi.dbs.elki.database.ids; This file is part of ELKI: Environment for Developing KDD-Applications Supported by Index-Structures - Copyright (C) 2012 + Copyright (C) 2013 Ludwig-Maximilians-Universität München Lehr- und Forschungseinheit für Datenbanksysteme ELKI Development Team @@ -22,7 +22,7 @@ package de.lmu.ifi.dbs.elki.database.ids; You should have received a copy of the GNU Affero General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */ -import de.lmu.ifi.dbs.elki.utilities.iterator.ArrayIter; +import de.lmu.ifi.dbs.elki.utilities.datastructures.iterator.ArrayIter; /** * Array iterators that can also go backwards and seek. diff --git a/src/de/lmu/ifi/dbs/elki/database/ids/DBIDArrayMIter.java b/src/de/lmu/ifi/dbs/elki/database/ids/DBIDArrayMIter.java index 1de202a2..1aaefc8e 100644 --- a/src/de/lmu/ifi/dbs/elki/database/ids/DBIDArrayMIter.java +++ b/src/de/lmu/ifi/dbs/elki/database/ids/DBIDArrayMIter.java @@ -4,7 +4,7 @@ package de.lmu.ifi.dbs.elki.database.ids; This file is part of ELKI: Environment for Developing KDD-Applications Supported by Index-Structures - Copyright (C) 2012 + Copyright (C) 2013 Ludwig-Maximilians-Universität München Lehr- und Forschungseinheit für Datenbanksysteme ELKI Development Team diff --git a/src/de/lmu/ifi/dbs/elki/database/ids/DBIDFactory.java b/src/de/lmu/ifi/dbs/elki/database/ids/DBIDFactory.java index 646e6e4d..4c6472a7 100644 --- a/src/de/lmu/ifi/dbs/elki/database/ids/DBIDFactory.java +++ b/src/de/lmu/ifi/dbs/elki/database/ids/DBIDFactory.java @@ -4,7 +4,7 @@ package de.lmu.ifi.dbs.elki.database.ids; This file is part of ELKI: Environment for Developing KDD-Applications Supported by Index-Structures - Copyright (C) 2012 + Copyright (C) 2013 Ludwig-Maximilians-Universität München Lehr- und Forschungseinheit für Datenbanksysteme ELKI Development Team @@ -23,6 +23,10 @@ package de.lmu.ifi.dbs.elki.database.ids; along with this program. If not, see <http://www.gnu.org/licenses/>. */ +import de.lmu.ifi.dbs.elki.database.ids.distance.DistanceDBIDPair; +import de.lmu.ifi.dbs.elki.database.ids.distance.DoubleDistanceDBIDPair; +import de.lmu.ifi.dbs.elki.database.ids.distance.KNNHeap; +import de.lmu.ifi.dbs.elki.database.ids.distance.KNNList; import de.lmu.ifi.dbs.elki.database.ids.integer.TrivialDBIDFactory; import de.lmu.ifi.dbs.elki.distance.distancevalue.Distance; import de.lmu.ifi.dbs.elki.persistent.ByteBufferSerializer; @@ -192,6 +196,27 @@ public interface DBIDFactory { HashSetModifiableDBIDs newHashSet(DBIDs existing); /** + * Create an appropriate heap for the distance function. + * + * This will use a double heap if appropriate. + * + * @param factory distance prototype + * @param k K value + * @param <D> distance type + * @return New heap of size k, appropriate for this distance type. + */ + <D extends Distance<D>> KNNHeap<D> newHeap(D factory, int k); + + /** + * Build a new heap from a given list. + * + * @param exist Existing result + * @param <D> Distance type + * @return New heap + */ + <D extends Distance<D>> KNNHeap<D> newHeap(KNNList<D> exist); + + /** * Get a serializer for DBIDs. * * @return DBID serializer diff --git a/src/de/lmu/ifi/dbs/elki/database/ids/DBIDIter.java b/src/de/lmu/ifi/dbs/elki/database/ids/DBIDIter.java index 268f4441..f051d51c 100644 --- a/src/de/lmu/ifi/dbs/elki/database/ids/DBIDIter.java +++ b/src/de/lmu/ifi/dbs/elki/database/ids/DBIDIter.java @@ -4,7 +4,7 @@ package de.lmu.ifi.dbs.elki.database.ids; This file is part of ELKI: Environment for Developing KDD-Applications Supported by Index-Structures - Copyright (C) 2012 + Copyright (C) 2013 Ludwig-Maximilians-Universität München Lehr- und Forschungseinheit für Datenbanksysteme ELKI Development Team @@ -23,7 +23,7 @@ package de.lmu.ifi.dbs.elki.database.ids; along with this program. If not, see <http://www.gnu.org/licenses/>. */ -import de.lmu.ifi.dbs.elki.utilities.iterator.Iter; +import de.lmu.ifi.dbs.elki.utilities.datastructures.iterator.Iter; /** * Iterator for DBIDs. diff --git a/src/de/lmu/ifi/dbs/elki/database/ids/DBIDMIter.java b/src/de/lmu/ifi/dbs/elki/database/ids/DBIDMIter.java index 9f42c5a0..0fbed7e0 100644 --- a/src/de/lmu/ifi/dbs/elki/database/ids/DBIDMIter.java +++ b/src/de/lmu/ifi/dbs/elki/database/ids/DBIDMIter.java @@ -4,7 +4,7 @@ package de.lmu.ifi.dbs.elki.database.ids; This file is part of ELKI: Environment for Developing KDD-Applications Supported by Index-Structures - Copyright (C) 2012 + Copyright (C) 2013 Ludwig-Maximilians-Universität München Lehr- und Forschungseinheit für Datenbanksysteme ELKI Development Team @@ -23,7 +23,7 @@ package de.lmu.ifi.dbs.elki.database.ids; along with this program. If not, see <http://www.gnu.org/licenses/>. */ -import de.lmu.ifi.dbs.elki.utilities.iterator.MIter; +import de.lmu.ifi.dbs.elki.utilities.datastructures.iterator.MIter; /** * Modifiable DBID iterator. diff --git a/src/de/lmu/ifi/dbs/elki/database/ids/DBIDPair.java b/src/de/lmu/ifi/dbs/elki/database/ids/DBIDPair.java index 8f03e279..bdbbe2da 100644 --- a/src/de/lmu/ifi/dbs/elki/database/ids/DBIDPair.java +++ b/src/de/lmu/ifi/dbs/elki/database/ids/DBIDPair.java @@ -4,7 +4,7 @@ package de.lmu.ifi.dbs.elki.database.ids; This file is part of ELKI: Environment for Developing KDD-Applications Supported by Index-Structures - Copyright (C) 2012 + Copyright (C) 2013 Ludwig-Maximilians-Universität München Lehr- und Forschungseinheit für Datenbanksysteme ELKI Development Team diff --git a/src/de/lmu/ifi/dbs/elki/database/ids/DBIDRange.java b/src/de/lmu/ifi/dbs/elki/database/ids/DBIDRange.java index 8f7e428d..588cfe6a 100644 --- a/src/de/lmu/ifi/dbs/elki/database/ids/DBIDRange.java +++ b/src/de/lmu/ifi/dbs/elki/database/ids/DBIDRange.java @@ -6,7 +6,7 @@ import de.lmu.ifi.dbs.elki.database.datastore.DataStoreIDMap; This file is part of ELKI: Environment for Developing KDD-Applications Supported by Index-Structures - Copyright (C) 2012 + Copyright (C) 2013 Ludwig-Maximilians-Universität München Lehr- und Forschungseinheit für Datenbanksysteme ELKI Development Team diff --git a/src/de/lmu/ifi/dbs/elki/database/ids/DBIDRef.java b/src/de/lmu/ifi/dbs/elki/database/ids/DBIDRef.java index fce87c31..77cc621e 100644 --- a/src/de/lmu/ifi/dbs/elki/database/ids/DBIDRef.java +++ b/src/de/lmu/ifi/dbs/elki/database/ids/DBIDRef.java @@ -4,7 +4,7 @@ package de.lmu.ifi.dbs.elki.database.ids; This file is part of ELKI: Environment for Developing KDD-Applications Supported by Index-Structures - Copyright (C) 2012 + Copyright (C) 2013 Ludwig-Maximilians-Universität München Lehr- und Forschungseinheit für Datenbanksysteme ELKI Development Team diff --git a/src/de/lmu/ifi/dbs/elki/database/ids/DBIDUtil.java b/src/de/lmu/ifi/dbs/elki/database/ids/DBIDUtil.java index 9cb4082a..78dba268 100644 --- a/src/de/lmu/ifi/dbs/elki/database/ids/DBIDUtil.java +++ b/src/de/lmu/ifi/dbs/elki/database/ids/DBIDUtil.java @@ -4,7 +4,7 @@ package de.lmu.ifi.dbs.elki.database.ids; This file is part of ELKI: Environment for Developing KDD-Applications Supported by Index-Structures - Copyright (C) 2012 + Copyright (C) 2013 Ludwig-Maximilians-Universität München Lehr- und Forschungseinheit für Datenbanksysteme ELKI Development Team @@ -25,10 +25,17 @@ package de.lmu.ifi.dbs.elki.database.ids; import java.util.Random; +import de.lmu.ifi.dbs.elki.database.ids.distance.DistanceDBIDPair; +import de.lmu.ifi.dbs.elki.database.ids.distance.DoubleDistanceDBIDPair; +import de.lmu.ifi.dbs.elki.database.ids.distance.DoubleDistanceKNNList; +import de.lmu.ifi.dbs.elki.database.ids.distance.KNNHeap; +import de.lmu.ifi.dbs.elki.database.ids.distance.KNNList; +import de.lmu.ifi.dbs.elki.database.ids.generic.DoubleDistanceKNNSubList; +import de.lmu.ifi.dbs.elki.database.ids.generic.KNNSubList; import de.lmu.ifi.dbs.elki.database.ids.generic.UnmodifiableArrayDBIDs; import de.lmu.ifi.dbs.elki.database.ids.generic.UnmodifiableDBIDs; +import de.lmu.ifi.dbs.elki.database.ids.integer.IntegerArrayDBIDs; import de.lmu.ifi.dbs.elki.database.ids.integer.IntegerDBIDs; -import de.lmu.ifi.dbs.elki.database.ids.integer.TroveArrayDBIDs; import de.lmu.ifi.dbs.elki.database.ids.integer.UnmodifiableIntegerArrayDBIDs; import de.lmu.ifi.dbs.elki.database.ids.integer.UnmodifiableIntegerDBIDs; import de.lmu.ifi.dbs.elki.distance.distancevalue.Distance; @@ -319,9 +326,33 @@ public final class DBIDUtil { * @return size */ public static int intersectionSize(DBIDs first, DBIDs second) { - if (first.size() > second.size()) { - return intersectionSize(second, first); + // If exactly one is a Set, use it as second parameter. + if (second instanceof SetDBIDs) { + if (!(first instanceof SetDBIDs)) { + return internalIntersectionSize(first, second); + } + } else { + if (first instanceof SetDBIDs) { + return internalIntersectionSize(second, first); + } } + // Both are the same type: both set or both non set. + // Smaller goes first. + if (first.size() <= second.size()) { + return internalIntersectionSize(first, second); + } else { + return internalIntersectionSize(second, first); + } + } + + /** + * Compute the set intersection size of two sets. + * + * @param first First set + * @param second Second set + * @return size + */ + private static int internalIntersectionSize(DBIDs first, DBIDs second) { int c = 0; for (DBIDIter it = first.iter(); it.valid(); it.advance()) { if (second.contains(it)) { @@ -398,8 +429,8 @@ public final class DBIDUtil { if (existing instanceof StaticDBIDs) { return (StaticDBIDs) existing; } - if (existing instanceof TroveArrayDBIDs) { - return new UnmodifiableIntegerArrayDBIDs((TroveArrayDBIDs) existing); + if (existing instanceof IntegerArrayDBIDs) { + return new UnmodifiableIntegerArrayDBIDs((IntegerArrayDBIDs) existing); } if (existing instanceof IntegerDBIDs) { return new UnmodifiableIntegerDBIDs((IntegerDBIDs) existing); @@ -504,6 +535,31 @@ public final class DBIDUtil { } /** + * Create an appropriate heap for the distance type. + * + * This will use a double heap if appropriate. + * + * @param distancetype distance prototype + * @param k K value + * @param <D> distance type + * @return New heap of size k, appropriate for this distance type. + */ + public static <D extends Distance<D>> KNNHeap<D> newHeap(D distancetype, int k) { + return DBIDFactory.FACTORY.newHeap(distancetype, k); + } + + /** + * Build a new heap from a given list. + * + * @param exist Existing result + * @param <D> Distance type + * @return New heap + */ + public static <D extends Distance<D>> KNNHeap<D> newHeap(KNNList<D> exist) { + return DBIDFactory.FACTORY.newHeap(exist); + } + + /** * Produce a random sample of the given DBIDs. * * @param source Original DBIDs @@ -614,4 +670,23 @@ public final class DBIDUtil { return sample; } } + + /** + * Get a subset of the KNN result. + * + * @param list Existing list + * @param k k + * @param <D> distance type + * @return Subset + */ + @SuppressWarnings("unchecked") + public static <D extends Distance<D>> KNNList<D> subList(KNNList<D> list, int k) { + if (k >= list.size()) { + return list; + } + if (list instanceof DoubleDistanceKNNList) { + return (KNNList<D>) new DoubleDistanceKNNSubList((DoubleDistanceKNNList) list, k); + } + return new KNNSubList<>(list, k); + } } diff --git a/src/de/lmu/ifi/dbs/elki/database/ids/DBIDVar.java b/src/de/lmu/ifi/dbs/elki/database/ids/DBIDVar.java index b66ae5f5..94480fe9 100644 --- a/src/de/lmu/ifi/dbs/elki/database/ids/DBIDVar.java +++ b/src/de/lmu/ifi/dbs/elki/database/ids/DBIDVar.java @@ -4,7 +4,7 @@ package de.lmu.ifi.dbs.elki.database.ids; This file is part of ELKI: Environment for Developing KDD-Applications Supported by Index-Structures - Copyright (C) 2012 + Copyright (C) 2013 Ludwig-Maximilians-Universität München Lehr- und Forschungseinheit für Datenbanksysteme ELKI Development Team diff --git a/src/de/lmu/ifi/dbs/elki/database/ids/DBIDs.java b/src/de/lmu/ifi/dbs/elki/database/ids/DBIDs.java index 0b9db136..2bf2b28d 100644 --- a/src/de/lmu/ifi/dbs/elki/database/ids/DBIDs.java +++ b/src/de/lmu/ifi/dbs/elki/database/ids/DBIDs.java @@ -4,7 +4,7 @@ package de.lmu.ifi.dbs.elki.database.ids; This file is part of ELKI: Environment for Developing KDD-Applications Supported by Index-Structures - Copyright (C) 2012 + Copyright (C) 2013 Ludwig-Maximilians-Universität München Lehr- und Forschungseinheit für Datenbanksysteme ELKI Development Team diff --git a/src/de/lmu/ifi/dbs/elki/database/ids/DoubleDBIDPair.java b/src/de/lmu/ifi/dbs/elki/database/ids/DoubleDBIDPair.java index 06210076..970092b0 100644 --- a/src/de/lmu/ifi/dbs/elki/database/ids/DoubleDBIDPair.java +++ b/src/de/lmu/ifi/dbs/elki/database/ids/DoubleDBIDPair.java @@ -4,7 +4,7 @@ package de.lmu.ifi.dbs.elki.database.ids; This file is part of ELKI: Environment for Developing KDD-Applications Supported by Index-Structures - Copyright (C) 2012 + Copyright (C) 2013 Ludwig-Maximilians-Universität München Lehr- und Forschungseinheit für Datenbanksysteme ELKI Development Team diff --git a/src/de/lmu/ifi/dbs/elki/database/ids/EmptyDBIDs.java b/src/de/lmu/ifi/dbs/elki/database/ids/EmptyDBIDs.java index 995f917c..8a7b2e28 100644 --- a/src/de/lmu/ifi/dbs/elki/database/ids/EmptyDBIDs.java +++ b/src/de/lmu/ifi/dbs/elki/database/ids/EmptyDBIDs.java @@ -4,7 +4,7 @@ package de.lmu.ifi.dbs.elki.database.ids; This file is part of ELKI: Environment for Developing KDD-Applications Supported by Index-Structures - Copyright (C) 2012 + Copyright (C) 2013 Ludwig-Maximilians-Universität München Lehr- und Forschungseinheit für Datenbanksysteme ELKI Development Team @@ -68,7 +68,7 @@ public class EmptyDBIDs implements ArrayStaticDBIDs, SetDBIDs { } @Override - public void assign(int index, DBIDVar var) { + public void assignVar(int index, DBIDVar var) { throw new ArrayIndexOutOfBoundsException(); } diff --git a/src/de/lmu/ifi/dbs/elki/database/ids/HashSetDBIDs.java b/src/de/lmu/ifi/dbs/elki/database/ids/HashSetDBIDs.java index 5ff0bc97..cabe9898 100644 --- a/src/de/lmu/ifi/dbs/elki/database/ids/HashSetDBIDs.java +++ b/src/de/lmu/ifi/dbs/elki/database/ids/HashSetDBIDs.java @@ -4,7 +4,7 @@ package de.lmu.ifi.dbs.elki.database.ids; This file is part of ELKI: Environment for Developing KDD-Applications Supported by Index-Structures - Copyright (C) 2012 + Copyright (C) 2013 Ludwig-Maximilians-Universität München Lehr- und Forschungseinheit für Datenbanksysteme ELKI Development Team diff --git a/src/de/lmu/ifi/dbs/elki/database/ids/HashSetModifiableDBIDs.java b/src/de/lmu/ifi/dbs/elki/database/ids/HashSetModifiableDBIDs.java index efb39bc8..6a57f5f0 100644 --- a/src/de/lmu/ifi/dbs/elki/database/ids/HashSetModifiableDBIDs.java +++ b/src/de/lmu/ifi/dbs/elki/database/ids/HashSetModifiableDBIDs.java @@ -4,7 +4,7 @@ package de.lmu.ifi.dbs.elki.database.ids; This file is part of ELKI: Environment for Developing KDD-Applications Supported by Index-Structures - Copyright (C) 2012 + Copyright (C) 2013 Ludwig-Maximilians-Universität München Lehr- und Forschungseinheit für Datenbanksysteme ELKI Development Team diff --git a/src/de/lmu/ifi/dbs/elki/database/ids/ModifiableDBIDs.java b/src/de/lmu/ifi/dbs/elki/database/ids/ModifiableDBIDs.java index 547f3297..1cd8c4e7 100644 --- a/src/de/lmu/ifi/dbs/elki/database/ids/ModifiableDBIDs.java +++ b/src/de/lmu/ifi/dbs/elki/database/ids/ModifiableDBIDs.java @@ -4,7 +4,7 @@ package de.lmu.ifi.dbs.elki.database.ids; This file is part of ELKI: Environment for Developing KDD-Applications Supported by Index-Structures - Copyright (C) 2012 + Copyright (C) 2013 Ludwig-Maximilians-Universität München Lehr- und Forschungseinheit für Datenbanksysteme ELKI Development Team diff --git a/src/de/lmu/ifi/dbs/elki/database/ids/SetDBIDs.java b/src/de/lmu/ifi/dbs/elki/database/ids/SetDBIDs.java index 7fd28326..3a92593e 100644 --- a/src/de/lmu/ifi/dbs/elki/database/ids/SetDBIDs.java +++ b/src/de/lmu/ifi/dbs/elki/database/ids/SetDBIDs.java @@ -4,7 +4,7 @@ package de.lmu.ifi.dbs.elki.database.ids; This file is part of ELKI: Environment for Developing KDD-Applications Supported by Index-Structures - Copyright (C) 2012 + Copyright (C) 2013 Ludwig-Maximilians-Universität München Lehr- und Forschungseinheit für Datenbanksysteme ELKI Development Team diff --git a/src/de/lmu/ifi/dbs/elki/database/ids/StaticDBIDs.java b/src/de/lmu/ifi/dbs/elki/database/ids/StaticDBIDs.java index ce616da3..2ba30d4b 100644 --- a/src/de/lmu/ifi/dbs/elki/database/ids/StaticDBIDs.java +++ b/src/de/lmu/ifi/dbs/elki/database/ids/StaticDBIDs.java @@ -4,7 +4,7 @@ package de.lmu.ifi.dbs.elki.database.ids; This file is part of ELKI: Environment for Developing KDD-Applications Supported by Index-Structures - Copyright (C) 2012 + Copyright (C) 2013 Ludwig-Maximilians-Universität München Lehr- und Forschungseinheit für Datenbanksysteme ELKI Development Team diff --git a/src/de/lmu/ifi/dbs/elki/database/ids/distance/DistanceDBIDList.java b/src/de/lmu/ifi/dbs/elki/database/ids/distance/DistanceDBIDList.java new file mode 100644 index 00000000..360dda12 --- /dev/null +++ b/src/de/lmu/ifi/dbs/elki/database/ids/distance/DistanceDBIDList.java @@ -0,0 +1,87 @@ +package de.lmu.ifi.dbs.elki.database.ids.distance; + +/* + This file is part of ELKI: + Environment for Developing KDD-Applications Supported by Index-Structures + + Copyright (C) 2013 + Ludwig-Maximilians-Universität München + Lehr- und Forschungseinheit für Datenbanksysteme + ELKI Development Team + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +import de.lmu.ifi.dbs.elki.database.ids.DBIDs; +import de.lmu.ifi.dbs.elki.distance.distancevalue.Distance; + +/** + * Collection of objects and their distances. + * + * To iterate over the results, use the following code: + * + * <pre> + * {@code + * for (DistanceDBIDResultIter<D> iter = result.iter(); iter.valid(); iter.advance()) { + * // You can get the distance via: iter.getDistance(); + * // Or use iter just like any other DBIDRef + * } + * } + * </pre> + * + * If you are only interested in the IDs of the objects, the following is also + * sufficient: + * + * <pre> + * {@code + * for (DBIDIter<D> iter = result.iter(); iter.valid(); iter.advance()) { + * // Use iter just like any other DBIDRef + * } + * } + * </pre> + * + * @author Erich Schubert + * + * @apiviz.landmark + * + * @apiviz.composedOf DistanceDBIDPair + * @apiviz.has DistanceDBIDListIter + * + * @param <D> Distance type + */ +public interface DistanceDBIDList<D extends Distance<D>> extends DBIDs { + /** + * Size of list. + * + * @return Size + */ + @Override + int size(); + + /** + * Access a single pair. + * + * @param off Offset + * @return Pair + */ + DistanceDBIDPair<D> get(int off); + + /** + * Get an iterator + * + * @return New iterator + */ + @Override + DistanceDBIDListIter<D> iter(); +} diff --git a/src/de/lmu/ifi/dbs/elki/database/ids/distance/DistanceDBIDListIter.java b/src/de/lmu/ifi/dbs/elki/database/ids/distance/DistanceDBIDListIter.java new file mode 100644 index 00000000..914f3676 --- /dev/null +++ b/src/de/lmu/ifi/dbs/elki/database/ids/distance/DistanceDBIDListIter.java @@ -0,0 +1,55 @@ +package de.lmu.ifi.dbs.elki.database.ids.distance; + +/* + This file is part of ELKI: + Environment for Developing KDD-Applications Supported by Index-Structures + + Copyright (C) 2013 + Ludwig-Maximilians-Universität München + Lehr- und Forschungseinheit für Datenbanksysteme + ELKI Development Team + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +import de.lmu.ifi.dbs.elki.database.ids.DBIDArrayIter; +import de.lmu.ifi.dbs.elki.distance.distancevalue.Distance; + +/** + * Iterator over distance-based query results. + * + * There is no getter for the DBID, as this implements + * {@link de.lmu.ifi.dbs.elki.database.ids.DBIDRef}. + * + * @author Erich Schubert + * + * @apiviz.landmark + * + * @apiviz.has DistanceDBIDPair - - iterator for + */ +public interface DistanceDBIDListIter<D extends Distance<D>> extends DBIDArrayIter { + /** + * Get the distance + * + * @return distance + */ + public D getDistance(); + + /** + * Get an object pair. + * + * @return object pair + */ + public DistanceDBIDPair<D> getDistancePair(); +}
\ No newline at end of file diff --git a/src/de/lmu/ifi/dbs/elki/database/ids/DistanceDBIDPair.java b/src/de/lmu/ifi/dbs/elki/database/ids/distance/DistanceDBIDPair.java index 01a1f407..a9d879d9 100644 --- a/src/de/lmu/ifi/dbs/elki/database/ids/DistanceDBIDPair.java +++ b/src/de/lmu/ifi/dbs/elki/database/ids/distance/DistanceDBIDPair.java @@ -1,10 +1,10 @@ -package de.lmu.ifi.dbs.elki.database.ids; +package de.lmu.ifi.dbs.elki.database.ids.distance; /* This file is part of ELKI: Environment for Developing KDD-Applications Supported by Index-Structures - Copyright (C) 2012 + Copyright (C) 2013 Ludwig-Maximilians-Universität München Lehr- und Forschungseinheit für Datenbanksysteme ELKI Development Team @@ -23,6 +23,7 @@ package de.lmu.ifi.dbs.elki.database.ids; along with this program. If not, see <http://www.gnu.org/licenses/>. */ +import de.lmu.ifi.dbs.elki.database.ids.DBIDRef; import de.lmu.ifi.dbs.elki.distance.distancevalue.Distance; /** diff --git a/src/de/lmu/ifi/dbs/elki/database/ids/distance/DoubleDistanceDBIDList.java b/src/de/lmu/ifi/dbs/elki/database/ids/distance/DoubleDistanceDBIDList.java new file mode 100644 index 00000000..85182313 --- /dev/null +++ b/src/de/lmu/ifi/dbs/elki/database/ids/distance/DoubleDistanceDBIDList.java @@ -0,0 +1,39 @@ +package de.lmu.ifi.dbs.elki.database.ids.distance; + +/* + This file is part of ELKI: + Environment for Developing KDD-Applications Supported by Index-Structures + + Copyright (C) 2013 + Ludwig-Maximilians-Universität München + Lehr- und Forschungseinheit für Datenbanksysteme + ELKI Development Team + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +import de.lmu.ifi.dbs.elki.distance.distancevalue.DoubleDistance; + +/** + * An object containing Double-DBID-Pairs. + * + * @author Erich Schubert + */ +public interface DoubleDistanceDBIDList extends DistanceDBIDList<DoubleDistance> { + @Override + DoubleDistanceDBIDListIter iter(); + + @Override + DoubleDistanceDBIDPair get(int off); +} diff --git a/src/de/lmu/ifi/dbs/elki/database/ids/distance/DoubleDistanceDBIDListIter.java b/src/de/lmu/ifi/dbs/elki/database/ids/distance/DoubleDistanceDBIDListIter.java new file mode 100644 index 00000000..68b2de1e --- /dev/null +++ b/src/de/lmu/ifi/dbs/elki/database/ids/distance/DoubleDistanceDBIDListIter.java @@ -0,0 +1,60 @@ +package de.lmu.ifi.dbs.elki.database.ids.distance; + +/* + This file is part of ELKI: + Environment for Developing KDD-Applications Supported by Index-Structures + + Copyright (C) 2013 + Ludwig-Maximilians-Universität München + Lehr- und Forschungseinheit für Datenbanksysteme + ELKI Development Team + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +import de.lmu.ifi.dbs.elki.distance.distancevalue.DoubleDistance; + +/** + * Iterator for double valued distance-based query results. + * + * @author Erich Schubert + */ +public interface DoubleDistanceDBIDListIter extends DistanceDBIDListIter<DoubleDistance> { + /** + * Get the distance + * + * @return distance + */ + public double doubleDistance(); + + /** + * Get an object pair. + * + * @return object pair + */ + @Override + public DoubleDistanceDBIDPair getDistancePair(); + + /** + * Get the distance + * + * @deprecated Use {@link #doubleDistance} to avoid creating unnecessary + * objects. + * + * @return distance + */ + @Deprecated + @Override + public DoubleDistance getDistance(); +} diff --git a/src/de/lmu/ifi/dbs/elki/database/ids/DoubleDistanceDBIDPair.java b/src/de/lmu/ifi/dbs/elki/database/ids/distance/DoubleDistanceDBIDPair.java index 72a9cfef..5286029b 100644 --- a/src/de/lmu/ifi/dbs/elki/database/ids/DoubleDistanceDBIDPair.java +++ b/src/de/lmu/ifi/dbs/elki/database/ids/distance/DoubleDistanceDBIDPair.java @@ -1,10 +1,10 @@ -package de.lmu.ifi.dbs.elki.database.ids; +package de.lmu.ifi.dbs.elki.database.ids.distance; /* This file is part of ELKI: Environment for Developing KDD-Applications Supported by Index-Structures - Copyright (C) 2012 + Copyright (C) 2013 Ludwig-Maximilians-Universität München Lehr- und Forschungseinheit für Datenbanksysteme ELKI Development Team @@ -23,6 +23,7 @@ package de.lmu.ifi.dbs.elki.database.ids; along with this program. If not, see <http://www.gnu.org/licenses/>. */ +import de.lmu.ifi.dbs.elki.database.ids.DBIDRef; import de.lmu.ifi.dbs.elki.distance.distancevalue.DoubleDistance; /** diff --git a/src/de/lmu/ifi/dbs/elki/database/ids/distance/DoubleDistanceDBIDPairList.java b/src/de/lmu/ifi/dbs/elki/database/ids/distance/DoubleDistanceDBIDPairList.java new file mode 100644 index 00000000..f9bfc20a --- /dev/null +++ b/src/de/lmu/ifi/dbs/elki/database/ids/distance/DoubleDistanceDBIDPairList.java @@ -0,0 +1,212 @@ +package de.lmu.ifi.dbs.elki.database.ids.distance; + +/* + This file is part of ELKI: + Environment for Developing KDD-Applications Supported by Index-Structures + + Copyright (C) 2013 + Ludwig-Maximilians-Universität München + Lehr- und Forschungseinheit für Datenbanksysteme + ELKI Development Team + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +import java.util.ArrayList; +import java.util.Collections; + +import de.lmu.ifi.dbs.elki.database.ids.DBIDFactory; +import de.lmu.ifi.dbs.elki.database.ids.DBIDIter; +import de.lmu.ifi.dbs.elki.database.ids.DBIDRef; +import de.lmu.ifi.dbs.elki.database.ids.DBIDUtil; +import de.lmu.ifi.dbs.elki.distance.distanceresultlist.DistanceDBIDResultUtil; +import de.lmu.ifi.dbs.elki.distance.distancevalue.DoubleDistance; + +/** + * Default class to keep a list of distance-object pairs. + * + * @author Erich Schubert + * + * @apiviz.composedOf DoubleDistanceDBIDPair + * @apiviz.has DoubleDistanceDBIDListIter + */ +public class DoubleDistanceDBIDPairList implements ModifiableDoubleDistanceDBIDList { + /** + * Actual storage. + */ + final ArrayList<DoubleDistanceDBIDPair> storage; + + /** + * Constructor. + */ + public DoubleDistanceDBIDPairList() { + super(); + storage = new ArrayList<>(); + } + + /** + * Constructor. + * + * @param initialCapacity Capacity + */ + public DoubleDistanceDBIDPairList(int initialCapacity) { + super(); + storage = new ArrayList<>(initialCapacity); + } + + /** + * Add an element. + * + * @deprecated Pass a double value instead. + * + * @param dist Distance + * @param id ID + */ + @Override + @Deprecated + public void add(DoubleDistance dist, DBIDRef id) { + storage.add(DBIDFactory.FACTORY.newDistancePair(dist.doubleValue(), id)); + } + + /** + * Add an element. + * + * @param dist Distance + * @param id ID + */ + @Override + public void add(double dist, DBIDRef id) { + storage.add(DBIDFactory.FACTORY.newDistancePair(dist, id)); + } + + /** + * Add an element. + * + * @param pair Pair to add + */ + @Override + public void add(DoubleDistanceDBIDPair pair) { + storage.add(pair); + } + + @Override + public void sort() { + Collections.sort(storage, DistanceDBIDResultUtil.distanceComparator()); + } + + @Override + public int size() { + return storage.size(); + } + + @Override + public DoubleDistanceDBIDPair get(int off) { + return storage.get(off); + } + + @Override + public DoubleDistanceDBIDListIter iter() { + return new Itr(); + } + + @Override + public boolean contains(DBIDRef o) { + for(DBIDIter iter = iter(); iter.valid(); iter.advance()) { + if(DBIDUtil.equal(iter, o)) { + return true; + } + } + return false; + } + + @Override + public boolean isEmpty() { + return size() == 0; + } + + @Override + public String toString() { + return DistanceDBIDResultUtil.toString(this); + } + + /** + * Iterator class. + * + * @author Erich Schubert + * + * @apiviz.exclude + */ + protected class Itr implements DoubleDistanceDBIDListIter { + /** + * Iterator position. + */ + int pos = 0; + + @Override + public int internalGetIndex() { + return get(pos).internalGetIndex(); + } + + @Override + public boolean valid() { + return pos < size(); + } + + @Override + public void advance() { + pos++; + } + + @Override + @Deprecated + public DoubleDistance getDistance() { + return get(pos).getDistance(); + } + + @Override + public double doubleDistance() { + return get(pos).doubleDistance(); + } + + @Override + public DoubleDistanceDBIDPair getDistancePair() { + return get(pos); + } + + @Override + public String toString() { + return valid() ? getDistancePair().toString() : "null"; + } + + @Override + public int getOffset() { + return pos; + } + + @Override + public void advance(int count) { + pos += count; + } + + @Override + public void retract() { + --pos; + } + + @Override + public void seek(int off) { + pos = off; + } + } +}
\ No newline at end of file diff --git a/src/de/lmu/ifi/dbs/elki/database/ids/distance/DoubleDistanceKNNHeap.java b/src/de/lmu/ifi/dbs/elki/database/ids/distance/DoubleDistanceKNNHeap.java new file mode 100644 index 00000000..1e75f120 --- /dev/null +++ b/src/de/lmu/ifi/dbs/elki/database/ids/distance/DoubleDistanceKNNHeap.java @@ -0,0 +1,100 @@ +package de.lmu.ifi.dbs.elki.database.ids.distance; + +/* + This file is part of ELKI: + Environment for Developing KDD-Applications Supported by Index-Structures + + Copyright (C) 2013 + Ludwig-Maximilians-Universität München + Lehr- und Forschungseinheit für Datenbanksysteme + ELKI Development Team + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +import de.lmu.ifi.dbs.elki.database.ids.DBIDRef; +import de.lmu.ifi.dbs.elki.distance.distancevalue.DoubleDistance; + +/** + * Interface for kNN heaps storing double distances and DBIDs. + * + * @author Erich Schubert + */ +public interface DoubleDistanceKNNHeap extends KNNHeap<DoubleDistance> { + /** + * Add a distance-id pair to the heap unless the distance is too large. + * + * Compared to the super.add() method, this often saves the pair construction. + * + * @param distance Distance value + * @param id ID number + */ + void add(double distance, DBIDRef id); + + /** + * Add a distance-id pair to the heap unless the distance is too large. + * + * Compared to the super.add() method, this often saves the pair construction. + * + * @param distance Distance value + * @param id ID number + */ + @Deprecated + void add(Double distance, DBIDRef id); + + /** + * Add a distance-id pair to the heap unless the distance is too large. + * + * Use for existing pairs. + * + * @param e Existing distance pair + */ + void add(DoubleDistanceDBIDPair e); + + /** + * {@inheritDoc} + * + * @deprecated if you know your distances are double-valued, you should be + * using the primitive type. + */ + @Override + @Deprecated + void add(DoubleDistance dist, DBIDRef id); + + /** + * Get the distance to the k nearest neighbor, or maxdist otherwise. + * + * @return Maximum distance + */ + double doubleKNNDistance(); + + /** + * {@inheritDoc} + * + * @deprecated if you know your distances are double-valued, you should be + * using the primitive type. + */ + @Override + @Deprecated + DoubleDistance getKNNDistance(); + + @Override + DoubleDistanceDBIDPair poll(); + + @Override + DoubleDistanceDBIDPair peek(); + + @Override + DoubleDistanceKNNList toKNNList(); +} diff --git a/src/de/lmu/ifi/dbs/elki/database/ids/distance/DoubleDistanceKNNList.java b/src/de/lmu/ifi/dbs/elki/database/ids/distance/DoubleDistanceKNNList.java new file mode 100644 index 00000000..c54110ab --- /dev/null +++ b/src/de/lmu/ifi/dbs/elki/database/ids/distance/DoubleDistanceKNNList.java @@ -0,0 +1,55 @@ +package de.lmu.ifi.dbs.elki.database.ids.distance; + +/* + This file is part of ELKI: + Environment for Developing KDD-Applications Supported by Index-Structures + + Copyright (C) 2013 + Ludwig-Maximilians-Universität München + Lehr- und Forschungseinheit für Datenbanksysteme + ELKI Development Team + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +import de.lmu.ifi.dbs.elki.distance.distancevalue.DoubleDistance; + +/** + * Double-valued KNN result. + * + * @author Erich Schubert + */ +public interface DoubleDistanceKNNList extends KNNList<DoubleDistance> { + /** + * {@inheritDoc} + * + * @deprecated use doubleKNNDistance()! + */ + @Override + @Deprecated + DoubleDistance getKNNDistance(); + + /** + * Get the kNN distance as double value. + * + * @return Distance + */ + double doubleKNNDistance(); + + @Override + DoubleDistanceDBIDListIter iter(); + + @Override + DoubleDistanceDBIDPair get(int off); +} diff --git a/src/de/lmu/ifi/dbs/elki/database/ids/distance/KNNHeap.java b/src/de/lmu/ifi/dbs/elki/database/ids/distance/KNNHeap.java new file mode 100644 index 00000000..c02071e7 --- /dev/null +++ b/src/de/lmu/ifi/dbs/elki/database/ids/distance/KNNHeap.java @@ -0,0 +1,111 @@ +package de.lmu.ifi.dbs.elki.database.ids.distance; + +/* + This file is part of ELKI: + Environment for Developing KDD-Applications Supported by Index-Structures + + Copyright (C) 2013 + Ludwig-Maximilians-Universität München + Lehr- und Forschungseinheit für Datenbanksysteme + ELKI Development Team + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +import de.lmu.ifi.dbs.elki.database.ids.DBIDRef; +import de.lmu.ifi.dbs.elki.distance.distancevalue.Distance; + +/** + * Interface for kNN heaps. + * + * To instantiate, use: {@link de.lmu.ifi.dbs.elki.database.ids.DBIDUtil#newHeap}! + * + * @author Erich Schubert + * + * @apiviz.landmark + * + * @apiviz.uses KNNList - - «serializes to» + * @apiviz.composedOf DistanceDBIDPair + * + * @param <D> Distance function + */ +public interface KNNHeap<D extends Distance<D>> { + /** + * Serialize to a {@link KNNList}. This empties the heap! + * + * @return KNNList with the heaps contents. + */ + KNNList<D> toKNNList(); + + /** + * Get the K parameter ("maxsize" internally). + * + * @return K + */ + int getK(); + + /** + * Get the distance to the k nearest neighbor, or maxdist otherwise. + * + * @return Maximum distance + */ + D getKNNDistance(); + + /** + * Add a distance-id pair to the heap unless the distance is too large. + * + * Compared to the super.add() method, this often saves the pair construction. + * + * @param distance Distance value + * @param id ID number + */ + void add(D distance, DBIDRef id); + + /** + * Current size of heap. + * + * @return Heap size + */ + int size(); + + /** + * Test if the heap is empty. + * + * @return true when empty. + */ + boolean isEmpty(); + + /** + * Clear the heap. + */ + void clear(); + + /** + * Poll the <em>largest</em> element from the heap. + * + * This is in descending order because of the heap structure. For a convenient + * way to serialize the heap into a list that you can iterate in ascending + * order, see {@link #toKNNList()}. + * + * @return largest element + */ + DistanceDBIDPair<D> poll(); + + /** + * Peek at the <em>largest</em> element in the heap. + * + * @return The current largest element. + */ + DistanceDBIDPair<D> peek(); +}
\ No newline at end of file diff --git a/src/de/lmu/ifi/dbs/elki/database/ids/distance/KNNList.java b/src/de/lmu/ifi/dbs/elki/database/ids/distance/KNNList.java new file mode 100644 index 00000000..61b75ba8 --- /dev/null +++ b/src/de/lmu/ifi/dbs/elki/database/ids/distance/KNNList.java @@ -0,0 +1,89 @@ +package de.lmu.ifi.dbs.elki.database.ids.distance; + +/* + This file is part of ELKI: + Environment for Developing KDD-Applications Supported by Index-Structures + + Copyright (C) 2013 + Ludwig-Maximilians-Universität München + Lehr- und Forschungseinheit für Datenbanksysteme + ELKI Development Team + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +import de.lmu.ifi.dbs.elki.distance.distancevalue.Distance; + +/** + * Interface for kNN results. + * + * To iterate over the results, use the following code: + * + * <pre> + * {@code + * for (DistanceDBIDResultIter<D> iter = result.iter(); iter.valid(); iter.advance()) { + * // You can get the distance via: iter.getDistance(); + * // Or use iter just like any other DBIDRef + * } + * } + * </pre> + * + * If you are only interested in the IDs of the objects, the following is also + * sufficient: + * + * <pre> + * {@code + * for (DBIDIter<D> iter = result.iter(); iter.valid(); iter.advance()) { + * // Use iter just like any other DBIDRef + * } + * } + * </pre> + * + * @author Erich Schubert + * + * @apiviz.landmark + * + * @apiviz.composedOf DistanceDBIDPair + * + * @param <D> Distance type + */ +public interface KNNList<D extends Distance<D>> extends DistanceDBIDList<D> { + /** + * Size + */ + @Override + public int size(); + + /** + * Get the K parameter (note: this may be less than the size of the list!) + * + * @return K + */ + public int getK(); + + /** + * Direct object access. + * + * @param index + */ + @Override + public DistanceDBIDPair<D> get(int index); + + /** + * Get the distance to the k nearest neighbor, or maxdist otherwise. + * + * @return Maximum distance + */ + public D getKNNDistance(); +}
\ No newline at end of file diff --git a/src/de/lmu/ifi/dbs/elki/database/ids/distance/ModifiableDistanceDBIDList.java b/src/de/lmu/ifi/dbs/elki/database/ids/distance/ModifiableDistanceDBIDList.java new file mode 100644 index 00000000..afb15f93 --- /dev/null +++ b/src/de/lmu/ifi/dbs/elki/database/ids/distance/ModifiableDistanceDBIDList.java @@ -0,0 +1,48 @@ +package de.lmu.ifi.dbs.elki.database.ids.distance; + +/* + This file is part of ELKI: + Environment for Developing KDD-Applications Supported by Index-Structures + + Copyright (C) 2013 + Ludwig-Maximilians-Universität München + Lehr- und Forschungseinheit für Datenbanksysteme + ELKI Development Team + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +import de.lmu.ifi.dbs.elki.database.ids.DBIDRef; +import de.lmu.ifi.dbs.elki.distance.distancevalue.Distance; +/** + * Modifiable API for Distance-DBID results + * + * @author Erich Schubert + * + * @param <D> Distance type + */ +public interface ModifiableDistanceDBIDList<D extends Distance<D>> extends DistanceDBIDList<D> { + /** + * Add an object to this result. + * + * @param distance Distance to add + * @param id DBID to add + */ + public void add(D distance, DBIDRef id); + + /** + * Sort the result in ascending order + */ + public void sort(); +}
\ No newline at end of file diff --git a/src/de/lmu/ifi/dbs/elki/database/ids/distance/ModifiableDoubleDistanceDBIDList.java b/src/de/lmu/ifi/dbs/elki/database/ids/distance/ModifiableDoubleDistanceDBIDList.java new file mode 100644 index 00000000..12cdaf69 --- /dev/null +++ b/src/de/lmu/ifi/dbs/elki/database/ids/distance/ModifiableDoubleDistanceDBIDList.java @@ -0,0 +1,63 @@ +package de.lmu.ifi.dbs.elki.database.ids.distance; + +/* + This file is part of ELKI: + Environment for Developing KDD-Applications Supported by Index-Structures + + Copyright (C) 2013 + Ludwig-Maximilians-Universität München + Lehr- und Forschungseinheit für Datenbanksysteme + ELKI Development Team + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +import de.lmu.ifi.dbs.elki.database.ids.DBIDRef; +import de.lmu.ifi.dbs.elki.database.ids.distance.DoubleDistanceDBIDList; +import de.lmu.ifi.dbs.elki.database.ids.distance.ModifiableDistanceDBIDList; +import de.lmu.ifi.dbs.elki.distance.distancevalue.DoubleDistance; + +/** + * An object containing Double-DBID-Pairs. + * + * @author Erich Schubert + */ +public interface ModifiableDoubleDistanceDBIDList extends DoubleDistanceDBIDList, ModifiableDistanceDBIDList<DoubleDistance> { + /** + * Add an element. + * + * @deprecated Pass a double value instead. + * + * @param dist Distance + * @param id ID + */ + @Override + @Deprecated + void add(DoubleDistance dist, DBIDRef id); + + /** + * Add an element. + * + * @param dist Distance + * @param id ID + */ + void add(double dist, DBIDRef id); + + /** + * Add an element. + * + * @param pair Pair to add + */ + void add(DoubleDistanceDBIDPair pair); +} diff --git a/src/de/lmu/ifi/dbs/elki/database/ids/distance/package-info.java b/src/de/lmu/ifi/dbs/elki/database/ids/distance/package-info.java new file mode 100644 index 00000000..7fefbedd --- /dev/null +++ b/src/de/lmu/ifi/dbs/elki/database/ids/distance/package-info.java @@ -0,0 +1,26 @@ +/** + * Distance-DBID pairs, lists and heaps. + */ +/* + This file is part of ELKI: + Environment for Developing KDD-Applications Supported by Index-Structures + + Copyright (C) 2013 + Ludwig-Maximilians-Universität München + Lehr- und Forschungseinheit für Datenbanksysteme + ELKI Development Team + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. + */ +package de.lmu.ifi.dbs.elki.database.ids.distance; diff --git a/src/de/lmu/ifi/dbs/elki/database/ids/generic/AbstractKNNHeap.java b/src/de/lmu/ifi/dbs/elki/database/ids/generic/AbstractKNNHeap.java new file mode 100644 index 00000000..2c2e60b0 --- /dev/null +++ b/src/de/lmu/ifi/dbs/elki/database/ids/generic/AbstractKNNHeap.java @@ -0,0 +1,93 @@ +package de.lmu.ifi.dbs.elki.database.ids.generic; + +/* + This file is part of ELKI: + Environment for Developing KDD-Applications Supported by Index-Structures + + Copyright (C) 2013 + Ludwig-Maximilians-Universität München + Lehr- und Forschungseinheit für Datenbanksysteme + ELKI Development Team + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + + +import de.lmu.ifi.dbs.elki.database.ids.distance.DistanceDBIDPair; +import de.lmu.ifi.dbs.elki.database.ids.distance.KNNHeap; +import de.lmu.ifi.dbs.elki.distance.distanceresultlist.DistanceDBIDResultUtil; +import de.lmu.ifi.dbs.elki.distance.distancevalue.Distance; +import de.lmu.ifi.dbs.elki.utilities.datastructures.heap.TiedTopBoundedHeap; + +/** + * Heap used for KNN management. + * + * @author Erich Schubert + * + * @param <P> pair type + * @param <D> distance type + */ +abstract class AbstractKNNHeap<P extends DistanceDBIDPair<D>, D extends Distance<D>> implements KNNHeap<D> { + /** + * The actual heap. + */ + protected final TiedTopBoundedHeap<P> heap; + + /** + * Constructor. + * + * @param k Maximum heap size (unless tied) + */ + public AbstractKNNHeap(int k) { + super(); + heap = new TiedTopBoundedHeap<>(k, DistanceDBIDResultUtil.BY_REVERSE_DISTANCE); + } + + /** + * Add a pair to the heap. + * + * @param pair Pair to add. + */ + public abstract void add(P pair); + + @Override + public final int getK() { + return heap.getMaxSize(); + } + + @Override + public int size() { + return heap.size(); + } + + @Override + public P peek() { + return heap.peek(); + } + + @Override + public boolean isEmpty() { + return heap.isEmpty(); + } + + @Override + public void clear() { + heap.clear(); + } + + @Override + public P poll() { + return heap.poll(); + } +}
\ No newline at end of file diff --git a/src/de/lmu/ifi/dbs/elki/database/ids/generic/DBIDIterAdapter.java b/src/de/lmu/ifi/dbs/elki/database/ids/generic/DBIDIterAdapter.java index 124d1b28..85fdcffd 100644 --- a/src/de/lmu/ifi/dbs/elki/database/ids/generic/DBIDIterAdapter.java +++ b/src/de/lmu/ifi/dbs/elki/database/ids/generic/DBIDIterAdapter.java @@ -4,7 +4,7 @@ package de.lmu.ifi.dbs.elki.database.ids.generic; This file is part of ELKI: Environment for Developing KDD-Applications Supported by Index-Structures - Copyright (C) 2012 + Copyright (C) 2013 Ludwig-Maximilians-Universität München Lehr- und Forschungseinheit für Datenbanksysteme ELKI Development Team diff --git a/src/de/lmu/ifi/dbs/elki/database/ids/generic/DistanceDBIDPairKNNHeap.java b/src/de/lmu/ifi/dbs/elki/database/ids/generic/DistanceDBIDPairKNNHeap.java new file mode 100644 index 00000000..88459077 --- /dev/null +++ b/src/de/lmu/ifi/dbs/elki/database/ids/generic/DistanceDBIDPairKNNHeap.java @@ -0,0 +1,106 @@ +package de.lmu.ifi.dbs.elki.database.ids.generic; + +/* + This file is part of ELKI: + Environment for Developing KDD-Applications Supported by Index-Structures + + Copyright (C) 2013 + Ludwig-Maximilians-Universität München + Lehr- und Forschungseinheit für Datenbanksysteme + ELKI Development Team + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +import de.lmu.ifi.dbs.elki.database.ids.DBIDFactory; +import de.lmu.ifi.dbs.elki.database.ids.DBIDRef; +import de.lmu.ifi.dbs.elki.database.ids.distance.DistanceDBIDPair; +import de.lmu.ifi.dbs.elki.distance.distancevalue.Distance; + +/** + * Heap for collecting kNN candidates with arbitrary distance types. + * + * For double distances, see {@link DoubleDistanceDBIDPairKNNHeap} + * + * <b>To instantiate, use {@link de.lmu.ifi.dbs.elki.database.ids.DBIDUtil#newHeap} instead!</b> + * + * @author Erich Schubert + * + * @param <D> Distance type + */ +public class DistanceDBIDPairKNNHeap<D extends Distance<D>> extends AbstractKNNHeap<DistanceDBIDPair<D>, D> { + /** + * Cached distance to k nearest neighbor (to avoid going through {@link #peek} + * each time). + */ + protected D knndistance = null; + + /** + * Constructor. + * + * <b>To instantiate, use {@link de.lmu.ifi.dbs.elki.database.ids.DBIDUtil#newHeap} instead!</b> + * + * @param k Heap size + */ + public DistanceDBIDPairKNNHeap(int k) { + super(k); + } + + /** + * Serialize to a {@link DistanceDBIDPairKNNList}. This empties the heap! + * + * @return KNNList with the heaps contents. + */ + @Override + public DistanceDBIDPairKNNList<D> toKNNList() { + return new DistanceDBIDPairKNNList<>(this); + } + + @Override + public void add(D distance, DBIDRef id) { + if (size() < getK()) { + heap.add(DBIDFactory.FACTORY.newDistancePair(distance, id)); + heapModified(); + return; + } + // size >= maxsize. Insert only when necessary. + if (knndistance.compareTo(distance) >= 0) { + // Replace worst element. + heap.add(DBIDFactory.FACTORY.newDistancePair(distance, id)); + heapModified(); + } + } + + @Override + public void add(DistanceDBIDPair<D> pair) { + if (size() < getK() || knndistance.compareTo(pair.getDistance()) >= 0) { + heap.add(pair); + heapModified(); + } + } + + // @Override + protected void heapModified() { + // super.heapModified(); + // Update threshold. + if (size() >= getK()) { + knndistance = heap.peek().getDistance(); + } + } + + @Override + public D getKNNDistance() { + return knndistance; + } +} diff --git a/src/de/lmu/ifi/dbs/elki/database/ids/generic/DistanceDBIDPairKNNList.java b/src/de/lmu/ifi/dbs/elki/database/ids/generic/DistanceDBIDPairKNNList.java new file mode 100644 index 00000000..bc5392d6 --- /dev/null +++ b/src/de/lmu/ifi/dbs/elki/database/ids/generic/DistanceDBIDPairKNNList.java @@ -0,0 +1,211 @@ +package de.lmu.ifi.dbs.elki.database.ids.generic; + +/* + This file is part of ELKI: + Environment for Developing KDD-Applications Supported by Index-Structures + + Copyright (C) 2013 + Ludwig-Maximilians-Universität München + Lehr- und Forschungseinheit für Datenbanksysteme + ELKI Development Team + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +import de.lmu.ifi.dbs.elki.database.ids.DBIDIter; +import de.lmu.ifi.dbs.elki.database.ids.DBIDRef; +import de.lmu.ifi.dbs.elki.database.ids.DBIDUtil; +import de.lmu.ifi.dbs.elki.database.ids.distance.DistanceDBIDPair; +import de.lmu.ifi.dbs.elki.database.ids.distance.DistanceDBIDListIter; +import de.lmu.ifi.dbs.elki.database.ids.distance.KNNHeap; +import de.lmu.ifi.dbs.elki.database.ids.distance.KNNList; +import de.lmu.ifi.dbs.elki.distance.distancevalue.Distance; +import de.lmu.ifi.dbs.elki.utilities.datastructures.heap.Heap; + +/** + * Finalized KNN List. + * + * @author Erich Schubert + * + * @param <D> Distance type + */ +public class DistanceDBIDPairKNNList<D extends Distance<D>> implements KNNList<D> { + /** + * The value of k this was materialized for. + */ + private final int k; + + /** + * The actual data array. + */ + private final Object[] data; + + /** + * Constructor, to be called from KNNHeap only. Use {@link KNNHeap#toKNNList} + * instead! + * + * @param heap Calling heap + */ + protected DistanceDBIDPairKNNList(KNNHeap<D> heap) { + super(); + this.data = new Object[heap.size()]; + this.k = heap.getK(); + // Get sorted data from heap; but in reverse. + int i = heap.size(); + while (heap.size() > 0) { + i--; + assert (i >= 0); + data[i] = heap.poll(); + } + assert (data.length == 0 || data[0] != null); + assert (heap.size() == 0); + } + + /** + * Constructor. With a KNNHeap, use {@link KNNHeap#toKNNList} instead! + * + * @param heap Calling heap + * @param k K value + */ + public DistanceDBIDPairKNNList(Heap<? extends DistanceDBIDPair<D>> heap, int k) { + super(); + this.data = new Object[heap.size()]; + this.k = k; + assert (heap.size() >= this.k) : "Heap doesn't contain enough objects!"; + // Get sorted data from heap; but in reverse. + int i = heap.size(); + while (!heap.isEmpty()) { + i--; + assert (i >= 0); + data[i] = heap.poll(); + } + assert (data.length == 0 || data[0] != null); + assert (heap.size() == 0); + } + + @Override + public int getK() { + return k; + } + + @Override + public D getKNNDistance() { + return get(getK() - 1).getDistance(); + } + + @Override + public String toString() { + StringBuilder buf = new StringBuilder(); + buf.append("kNNList["); + for (DistanceDBIDListIter<D> iter = this.iter(); iter.valid();) { + buf.append(iter.getDistance()).append(':').append(DBIDUtil.toString(iter)); + iter.advance(); + if (iter.valid()) { + buf.append(','); + } + } + buf.append(']'); + return buf.toString(); + } + + @SuppressWarnings("unchecked") + @Override + public DistanceDBIDPair<D> get(int index) { + return (DistanceDBIDPair<D>) data[index]; + } + + @Override + public DistanceDBIDListIter<D> iter() { + return new Itr(); + } + + @Override + public int size() { + return data.length; + } + + @Override + public boolean contains(DBIDRef o) { + for (DBIDIter iter = iter(); iter.valid(); iter.advance()) { + if (DBIDUtil.equal(iter, o)) { + return true; + } + } + return false; + } + + @Override + public boolean isEmpty() { + return size() == 0; + } + + /** + * Iterator. + * + * @author Erich Schubert + * + * @apiviz.exclude + */ + private class Itr implements DistanceDBIDListIter<D> { + /** + * Cursor position. + */ + private int pos = 0; + + @Override + public int internalGetIndex() { + return get(pos).internalGetIndex(); + } + + @Override + public boolean valid() { + return pos < data.length; + } + + @Override + public void advance() { + pos++; + } + + @Override + public D getDistance() { + return get(pos).getDistance(); + } + + @Override + public DistanceDBIDPair<D> getDistancePair() { + return get(pos); + } + + @Override + public int getOffset() { + return pos; + } + + @Override + public void advance(int count) { + pos += count; + } + + @Override + public void retract() { + --pos; + } + + @Override + public void seek(int off) { + pos += off; + } + } +} diff --git a/src/de/lmu/ifi/dbs/elki/database/ids/generic/DoubleDistanceDBIDPairKNNHeap.java b/src/de/lmu/ifi/dbs/elki/database/ids/generic/DoubleDistanceDBIDPairKNNHeap.java new file mode 100644 index 00000000..829bb00c --- /dev/null +++ b/src/de/lmu/ifi/dbs/elki/database/ids/generic/DoubleDistanceDBIDPairKNNHeap.java @@ -0,0 +1,196 @@ +package de.lmu.ifi.dbs.elki.database.ids.generic; + +/* + This file is part of ELKI: + Environment for Developing KDD-Applications Supported by Index-Structures + + Copyright (C) 2013 + Ludwig-Maximilians-Universität München + Lehr- und Forschungseinheit für Datenbanksysteme + ELKI Development Team + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +import java.util.Comparator; + +import de.lmu.ifi.dbs.elki.database.ids.DBIDFactory; +import de.lmu.ifi.dbs.elki.database.ids.DBIDRef; +import de.lmu.ifi.dbs.elki.database.ids.distance.DoubleDistanceDBIDPair; +import de.lmu.ifi.dbs.elki.database.ids.distance.DoubleDistanceKNNHeap; +import de.lmu.ifi.dbs.elki.distance.distancevalue.DoubleDistance; + +/** + * Heap for collecting double-valued KNN instances. + * + * See also: {@link de.lmu.ifi.dbs.elki.database.ids.DBIDUtil#newHeap}! + * + * Experiments have shown that it <em>can</em> be much more performant to track the + * knndistance <em>outside</em> of the heap, and do comparisons on the stack: + * <blockquote> + * + * <pre> + * {@code + * double knndist = Double.POSITIVE_INFINITY; + * DoubleDistanceDBIDPairKNNHeap heap = new DoubleDistanceDBIDPairKNNHeap(k); + * for (DBIDIter iditer = relation.iterDBIDs(); iditer.valid(); iditer.advance()) { + * double dist = computeDistance(iditer, ...); + * if (dist < knndist) { + * heap.add(dist, iditer); + * if (heap.size() >= k) { + * max = heap.doubleKNNDistance(); + * } + * } + * } + * } + * </pre> + * + * </blockquote> + * + * The reason probably is that {@code knndist} resides on the stack and can be + * better optimized by the hotspot compiler. + * + * @author Erich Schubert + */ +public class DoubleDistanceDBIDPairKNNHeap extends AbstractKNNHeap<DoubleDistanceDBIDPair, DoubleDistance> implements DoubleDistanceKNNHeap { + /** + * Comparator class. + */ + public static final Comparator<DoubleDistanceDBIDPair> COMPARATOR = new Comp(); + + /** + * Cached distance to k nearest neighbor (to avoid going through {@link #peek} + * too often). + */ + protected double knndistance = Double.POSITIVE_INFINITY; + + /** + * Constructor. + * + * See also: {@link de.lmu.ifi.dbs.elki.database.ids.DBIDUtil#newHeap}! + * + * @param k Heap size + */ + public DoubleDistanceDBIDPairKNNHeap(int k) { + super(k); + } + + /** + * Serialize to a {@link DoubleDistanceDBIDPairKNNList}. This empties the heap! + * + * @return KNNList with the heaps contents. + */ + @Override + public DoubleDistanceDBIDPairKNNList toKNNList() { + return new DoubleDistanceDBIDPairKNNList(this); + } + + /** + * Add a distance-id pair to the heap unless the distance is too large. + * + * Compared to the super.add() method, this often saves the pair construction. + * + * @param distance Distance value + * @param id ID number + */ + @Override + public final void add(final double distance, final DBIDRef id) { + if (size() < getK() || knndistance >= distance) { + heap.add(DBIDFactory.FACTORY.newDistancePair(distance, id)); + heapModified(); + } + } + + /** + * Add a distance-id pair to the heap unless the distance is too large. + * + * Compared to the super.add() method, this often saves the pair construction. + * + * @param distance Distance value + * @param id ID number + */ + @Override + @Deprecated + public final void add(final Double distance, final DBIDRef id) { + if (size() < getK() || knndistance >= distance) { + heap.add(DBIDFactory.FACTORY.newDistancePair(distance, id)); + heapModified(); + } + } + + // @Override + protected void heapModified() { + // super.heapModified(); + if (size() >= getK()) { + knndistance = heap.peek().doubleDistance(); + } + } + + @Override + public void add(final DoubleDistanceDBIDPair e) { + if (size() < getK() || knndistance >= e.doubleDistance()) { + heap.add(e); + heapModified(); + } + } + + /** + * {@inheritDoc} + * + * @deprecated if you know your distances are double-valued, you should be + * using the primitive type. + * + */ + @Override + @Deprecated + public void add(DoubleDistance dist, DBIDRef id) { + add(dist.doubleValue(), id); + } + + /** + * Get the distance to the k nearest neighbor, or maxdist otherwise. + * + * @return Maximum distance + */ + @Override + public double doubleKNNDistance() { + return knndistance; + } + + /** + * {@inheritDoc} + * + * @deprecated if you know your distances are double-valued, you should be + * using the primitive type. + */ + @Override + @Deprecated + public DoubleDistance getKNNDistance() { + return new DoubleDistance(knndistance); + } + + /** + * Comparator to use. + * + * @author Erich Schubert + * + * @apiviz.exclude + */ + protected static class Comp implements Comparator<DoubleDistanceDBIDPair> { + @Override + public int compare(DoubleDistanceDBIDPair o1, DoubleDistanceDBIDPair o2) { + return -Double.compare(o1.doubleDistance(), o2.doubleDistance()); + } + } +} diff --git a/src/de/lmu/ifi/dbs/elki/database/ids/generic/DoubleDistanceDBIDPairKNNList.java b/src/de/lmu/ifi/dbs/elki/database/ids/generic/DoubleDistanceDBIDPairKNNList.java new file mode 100644 index 00000000..c72529ad --- /dev/null +++ b/src/de/lmu/ifi/dbs/elki/database/ids/generic/DoubleDistanceDBIDPairKNNList.java @@ -0,0 +1,257 @@ +package de.lmu.ifi.dbs.elki.database.ids.generic; + +/* + This file is part of ELKI: + Environment for Developing KDD-Applications Supported by Index-Structures + + Copyright (C) 2013 + Ludwig-Maximilians-Universität München + Lehr- und Forschungseinheit für Datenbanksysteme + ELKI Development Team + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +import java.util.Collection; +import java.util.Iterator; + +import de.lmu.ifi.dbs.elki.database.ids.DBIDIter; +import de.lmu.ifi.dbs.elki.database.ids.DBIDRef; +import de.lmu.ifi.dbs.elki.database.ids.DBIDUtil; +import de.lmu.ifi.dbs.elki.database.ids.distance.DoubleDistanceDBIDPair; +import de.lmu.ifi.dbs.elki.database.ids.distance.DoubleDistanceDBIDListIter; +import de.lmu.ifi.dbs.elki.database.ids.distance.DoubleDistanceKNNList; +import de.lmu.ifi.dbs.elki.database.ids.distance.KNNHeap; +import de.lmu.ifi.dbs.elki.distance.distancevalue.DoubleDistance; +import de.lmu.ifi.dbs.elki.utilities.datastructures.heap.Heap; + +/** + * Finalized KNN List. + * + * @author Erich Schubert + * + * @apiviz.composedOf DoubleDistanceDBIDPair + * @apiviz.has DoubleDistanceDBIDListIter + */ +public class DoubleDistanceDBIDPairKNNList implements DoubleDistanceKNNList { + /** + * The value of k this was materialized for. + */ + private final int k; + + /** + * The actual data array. + */ + private final DoubleDistanceDBIDPair[] data; + + /** + * Constructor. This will <em>clone</em> the given collection! + * + * @param col Existing collection + * @param k K parameter + */ + public DoubleDistanceDBIDPairKNNList(Collection<DoubleDistanceDBIDPair> col, int k) { + super(); + this.data = new DoubleDistanceDBIDPair[col.size()]; + this.k = k; + assert (col.size() >= this.k) : "Collection doesn't contain enough objects!"; + // Get sorted data from heap; but in reverse. + Iterator<DoubleDistanceDBIDPair> it = col.iterator(); + for (int i = 0; it.hasNext(); i++) { + data[i] = it.next(); + } + assert (data.length == 0 || data[0] != null); + } + + /** + * Constructor, to be called from KNNHeap only. Use {@link KNNHeap#toKNNList} + * instead! + * + * @param heap Calling heap + */ + protected DoubleDistanceDBIDPairKNNList(DoubleDistanceDBIDPairKNNHeap heap) { + super(); + this.data = new DoubleDistanceDBIDPair[heap.size()]; + this.k = heap.getK(); + // Get sorted data from heap; but in reverse. + int i = heap.size(); + while (heap.size() > 0) { + i--; + assert (i >= 0); + data[i] = heap.poll(); + } + assert (data.length == 0 || data[0] != null); + assert (heap.size() == 0); + } + + /** + * Constructor, to be called from KNNHeap only. Use {@link KNNHeap#toKNNList} + * instead! + * + * @param heap Calling heap + * @param k Target number of neighbors (before ties) + */ + public DoubleDistanceDBIDPairKNNList(Heap<DoubleDistanceDBIDPair> heap, int k) { + super(); + this.data = new DoubleDistanceDBIDPair[heap.size()]; + this.k = k; + assert (heap.size() >= this.k) : "Heap doesn't contain enough objects!"; + // Get sorted data from heap; but in reverse. + int i = heap.size(); + while (heap.size() > 0) { + i--; + assert (i >= 0); + data[i] = heap.poll(); + } + assert (data.length == 0 || data[0] != null); + assert (heap.size() == 0); + } + + @Override + public int getK() { + return k; + } + + @Override + @Deprecated + public DoubleDistance getKNNDistance() { + if (size() < k) { + return DoubleDistance.INFINITE_DISTANCE; + } + return get(k - 1).getDistance(); + } + + @Override + public double doubleKNNDistance() { + if (size() < k) { + return Double.POSITIVE_INFINITY; + } + return get(k - 1).doubleDistance(); + } + + @Override + public String toString() { + StringBuilder buf = new StringBuilder(); + buf.append("kNNList["); + for (DoubleDistanceDBIDListIter iter = this.iter(); iter.valid();) { + buf.append(iter.doubleDistance()).append(':').append(DBIDUtil.toString(iter)); + iter.advance(); + if (iter.valid()) { + buf.append(','); + } + } + buf.append(']'); + return buf.toString(); + } + + @Override + public DoubleDistanceDBIDPair get(int index) { + return data[index]; + } + + @Override + public DoubleDistanceDBIDListIter iter() { + return new Itr(); + } + + @Override + public int size() { + return data.length; + } + + @Override + public boolean contains(DBIDRef o) { + for (DBIDIter iter = iter(); iter.valid(); iter.advance()) { + if (DBIDUtil.equal(iter, o)) { + return true; + } + } + return false; + } + + @Override + public boolean isEmpty() { + return size() == 0; + } + + /** + * Iterator. + * + * @author Erich Schubert + * + * @apiviz.exclude + */ + private class Itr implements DoubleDistanceDBIDListIter { + /** + * Cursor position. + */ + private int pos = 0; + + @Override + public int internalGetIndex() { + return get(pos).internalGetIndex(); + } + + @Override + public boolean valid() { + return pos < data.length; + } + + @Override + public void advance() { + pos++; + } + + /** + * {@inheritDoc} + * + * @deprecated use {@link #doubleDistance}! + */ + @Override + @Deprecated + public DoubleDistance getDistance() { + return get(pos).getDistance(); + } + + @Override + public double doubleDistance() { + return get(pos).doubleDistance(); + } + + @Override + public DoubleDistanceDBIDPair getDistancePair() { + return get(pos); + } + + @Override + public int getOffset() { + return pos; + } + + @Override + public void advance(int count) { + pos += count; + } + + @Override + public void retract() { + --pos; + } + + @Override + public void seek(int off) { + pos = off; + } + } +} diff --git a/src/de/lmu/ifi/dbs/elki/database/ids/generic/DoubleDistanceKNNSubList.java b/src/de/lmu/ifi/dbs/elki/database/ids/generic/DoubleDistanceKNNSubList.java new file mode 100644 index 00000000..c2854a54 --- /dev/null +++ b/src/de/lmu/ifi/dbs/elki/database/ids/generic/DoubleDistanceKNNSubList.java @@ -0,0 +1,190 @@ +package de.lmu.ifi.dbs.elki.database.ids.generic; + +/* + This file is part of ELKI: + Environment for Developing KDD-Applications Supported by Index-Structures + + Copyright (C) 2013 + Ludwig-Maximilians-Universität München + Lehr- und Forschungseinheit für Datenbanksysteme + ELKI Development Team + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. + */ +import de.lmu.ifi.dbs.elki.database.ids.DBIDIter; +import de.lmu.ifi.dbs.elki.database.ids.DBIDRef; +import de.lmu.ifi.dbs.elki.database.ids.DBIDUtil; +import de.lmu.ifi.dbs.elki.database.ids.distance.DoubleDistanceDBIDPair; +import de.lmu.ifi.dbs.elki.database.ids.distance.DoubleDistanceDBIDListIter; +import de.lmu.ifi.dbs.elki.database.ids.distance.DoubleDistanceKNNList; +import de.lmu.ifi.dbs.elki.distance.distancevalue.DoubleDistance; + +/** + * Sublist of an existing result to contain only the first k elements. + * + * TOOD: can be optimized slightly better. + * + * @author Erich Schubert + */ +public class DoubleDistanceKNNSubList implements DoubleDistanceKNNList { + /** + * Parameter k. + */ + private final int k; + + /** + * Actual size, including ties. + */ + private final int size; + + /** + * Wrapped inner result. + */ + private final DoubleDistanceKNNList inner; + + /** + * Constructor. + * + * @param inner Inner instance + * @param k k value + */ + public DoubleDistanceKNNSubList(DoubleDistanceKNNList inner, int k) { + this.inner = inner; + this.k = k; + // Compute list size + { + DoubleDistanceDBIDPair dist = inner.get(k); + int i = k; + while (i + 1 < inner.size()) { + if (dist.compareByDistance(inner.get(i + 1)) < 0) { + break; + } + i++; + } + size = i; + } + } + + @Override + public int getK() { + return k; + } + + @Override + public DoubleDistanceDBIDPair get(int index) { + assert (index < size) : "Access beyond design size of list."; + return inner.get(index); + } + + @Override + @Deprecated + public DoubleDistance getKNNDistance() { + return inner.get(k).getDistance(); + } + + @Override + public double doubleKNNDistance() { + return inner.get(k).doubleDistance(); + } + + @Override + public DoubleDistanceDBIDListIter iter() { + return new Itr(); + } + + @Override + public boolean contains(DBIDRef o) { + for (DBIDIter iter = iter(); iter.valid(); iter.advance()) { + if (DBIDUtil.equal(iter, o)) { + return true; + } + } + return false; + } + + @Override + public boolean isEmpty() { + return size == 0; + } + + @Override + public int size() { + return size; + } + + /** + * Iterator for the sublist. + * + * @author Erich Schubert + * + * @apiviz.exclude + */ + private class Itr implements DoubleDistanceDBIDListIter { + /** + * Current position. + */ + private int pos = 0; + + @Override + public boolean valid() { + return pos < size; + } + + @Override + public void advance() { + pos++; + } + + @Override + @Deprecated + public DoubleDistance getDistance() { + return inner.get(pos).getDistance(); + } + + @Override + public double doubleDistance() { + return inner.get(pos).doubleDistance(); + } + + @Override + public DoubleDistanceDBIDPair getDistancePair() { + return inner.get(pos); + } + + @Override + public int internalGetIndex() { + return inner.get(pos).internalGetIndex(); + } + + @Override + public int getOffset() { + return pos; + } + + @Override + public void advance(int count) { + pos += count; + } + + @Override + public void retract() { + --pos; + } + + @Override + public void seek(int off) { + pos = off; + } + } +} diff --git a/src/de/lmu/ifi/dbs/elki/database/ids/generic/GenericDistanceDBIDList.java b/src/de/lmu/ifi/dbs/elki/database/ids/generic/GenericDistanceDBIDList.java new file mode 100644 index 00000000..9b6c188b --- /dev/null +++ b/src/de/lmu/ifi/dbs/elki/database/ids/generic/GenericDistanceDBIDList.java @@ -0,0 +1,186 @@ +package de.lmu.ifi.dbs.elki.database.ids.generic; + +/* + This file is part of ELKI: + Environment for Developing KDD-Applications Supported by Index-Structures + + Copyright (C) 2013 + Ludwig-Maximilians-Universität München + Lehr- und Forschungseinheit für Datenbanksysteme + ELKI Development Team + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +import java.util.ArrayList; + +import de.lmu.ifi.dbs.elki.database.ids.DBIDFactory; +import de.lmu.ifi.dbs.elki.database.ids.DBIDIter; +import de.lmu.ifi.dbs.elki.database.ids.DBIDRef; +import de.lmu.ifi.dbs.elki.database.ids.DBIDUtil; +import de.lmu.ifi.dbs.elki.database.ids.distance.DistanceDBIDListIter; +import de.lmu.ifi.dbs.elki.database.ids.distance.DistanceDBIDPair; +import de.lmu.ifi.dbs.elki.database.ids.distance.ModifiableDistanceDBIDList; +import de.lmu.ifi.dbs.elki.distance.distanceresultlist.DistanceDBIDResultUtil; +import de.lmu.ifi.dbs.elki.distance.distancevalue.Distance; + +/** + * Default class to keep a list of distance-object pairs. + * + * @author Erich Schubert + * + * @param <D> Distance type + */ +public class GenericDistanceDBIDList<D extends Distance<D>> implements ModifiableDistanceDBIDList<D> { + /** + * Actual storage. + */ + final ArrayList<DistanceDBIDPair<D>> storage; + + /** + * Constructor. + */ + public GenericDistanceDBIDList() { + super(); + storage = new ArrayList<>(); + } + + /** + * Constructor. + * + * @param initialCapacity Capacity + */ + public GenericDistanceDBIDList(int initialCapacity) { + super(); + storage = new ArrayList<>(initialCapacity); + } + + @Override + public void add(D dist, DBIDRef id) { + storage.add(DBIDFactory.FACTORY.newDistancePair(dist, id)); + } + + /** + * Add a prepared pair. + * + * @param pair Pair to add + */ + public void add(DistanceDBIDPair<D> pair) { + storage.add(pair); + } + + @Override + public void sort() { + DistanceDBIDResultUtil.sortByDistance(storage); + } + + @Override + public int size() { + return storage.size(); + } + + @Override + public DistanceDBIDPair<D> get(int off) { + return storage.get(off); + } + + @Override + public DistanceDBIDListIter<D> iter() { + return new Itr(); + } + + @Override + public boolean contains(DBIDRef o) { + for(DBIDIter iter = iter(); iter.valid(); iter.advance()) { + if(DBIDUtil.equal(iter, o)) { + return true; + } + } + return false; + } + + @Override + public boolean isEmpty() { + return size() == 0; + } + + @Override + public String toString() { + return DistanceDBIDResultUtil.toString(this); + } + + /** + * Iterator class. + * + * @author Erich Schubert + * + * @apiviz.exclude + */ + protected class Itr implements DistanceDBIDListIter<D> { + /** + * Iterator position. + */ + int pos = 0; + + @Override + public int internalGetIndex() { + return get(pos).internalGetIndex(); + } + + @Override + public boolean valid() { + return pos < size(); + } + + @Override + public void advance() { + pos++; + } + + @Override + public D getDistance() { + return get(pos).getDistance(); + } + + @Override + public DistanceDBIDPair<D> getDistancePair() { + return get(pos); + } + + @Override + public String toString() { + return valid() ? getDistancePair().toString() : "null"; + } + + @Override + public int getOffset() { + return pos; + } + + @Override + public void advance(int count) { + pos += count; + } + + @Override + public void retract() { + --pos; + } + + @Override + public void seek(int off) { + pos = off; + } + } +}
\ No newline at end of file diff --git a/src/de/lmu/ifi/dbs/elki/database/ids/generic/KNNSubList.java b/src/de/lmu/ifi/dbs/elki/database/ids/generic/KNNSubList.java new file mode 100644 index 00000000..3d7863fd --- /dev/null +++ b/src/de/lmu/ifi/dbs/elki/database/ids/generic/KNNSubList.java @@ -0,0 +1,180 @@ +package de.lmu.ifi.dbs.elki.database.ids.generic; + +import de.lmu.ifi.dbs.elki.database.ids.DBIDIter; +import de.lmu.ifi.dbs.elki.database.ids.DBIDRef; +import de.lmu.ifi.dbs.elki.database.ids.DBIDUtil; +import de.lmu.ifi.dbs.elki.database.ids.distance.DistanceDBIDListIter; +import de.lmu.ifi.dbs.elki.database.ids.distance.DistanceDBIDPair; +import de.lmu.ifi.dbs.elki.database.ids.distance.KNNList; +import de.lmu.ifi.dbs.elki.distance.distancevalue.Distance; + +/* + This file is part of ELKI: + Environment for Developing KDD-Applications Supported by Index-Structures + + Copyright (C) 2013 + Ludwig-Maximilians-Universität München + Lehr- und Forschungseinheit für Datenbanksysteme + ELKI Development Team + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +/** + * Sublist of an existing result to contain only the first k elements. + * + * @author Erich Schubert + * + * @param <D> Distance + */ +public class KNNSubList<D extends Distance<D>> implements KNNList<D> { + /** + * Parameter k. + */ + private final int k; + + /** + * Actual size, including ties. + */ + private final int size; + + /** + * Wrapped inner result. + */ + private final KNNList<D> inner; + + /** + * Constructor. + * + * @param inner Inner instance + * @param k k value + */ + public KNNSubList(KNNList<D> inner, int k) { + this.inner = inner; + this.k = k; + // Compute list size + // TODO: optimize for double distances. + { + DistanceDBIDPair<D> dist = inner.get(k); + int i = k; + while (i + 1 < inner.size()) { + if (dist.compareByDistance(inner.get(i + 1)) < 0) { + break; + } + i++; + } + size = i; + } + } + + @Override + public int getK() { + return k; + } + + @Override + public DistanceDBIDPair<D> get(int index) { + assert (index < size) : "Access beyond design size of list."; + return inner.get(index); + } + + @Override + public D getKNNDistance() { + return inner.get(k).getDistance(); + } + + @Override + public DistanceDBIDListIter<D> iter() { + return new Itr(); + } + + @Override + public boolean contains(DBIDRef o) { + for (DBIDIter iter = iter(); iter.valid(); iter.advance()) { + if (DBIDUtil.equal(iter, o)) { + return true; + } + } + return false; + } + + @Override + public boolean isEmpty() { + return size == 0; + } + + @Override + public int size() { + return size; + } + + /** + * Iterator for the sublist. + * + * @author Erich Schubert + * + * @apiviz.exclude + */ + private class Itr implements DistanceDBIDListIter<D> { + /** + * Current position. + */ + private int pos = 0; + + @Override + public boolean valid() { + return pos < size; + } + + @Override + public void advance() { + pos++; + } + + @Override + public D getDistance() { + return inner.get(pos).getDistance(); + } + + @Override + public DistanceDBIDPair<D> getDistancePair() { + return inner.get(pos); + } + + @Override + public int internalGetIndex() { + return inner.get(pos).internalGetIndex(); + } + + @Override + public int getOffset() { + return pos; + } + + @Override + public void advance(int count) { + pos -= count; + } + + @Override + public void retract() { + --pos; + } + + @Override + public void seek(int off) { + pos = off; + } + } +} diff --git a/src/de/lmu/ifi/dbs/elki/database/ids/generic/MaskedDBIDs.java b/src/de/lmu/ifi/dbs/elki/database/ids/generic/MaskedDBIDs.java index 668ac0d8..2b481fca 100644 --- a/src/de/lmu/ifi/dbs/elki/database/ids/generic/MaskedDBIDs.java +++ b/src/de/lmu/ifi/dbs/elki/database/ids/generic/MaskedDBIDs.java @@ -4,7 +4,7 @@ package de.lmu.ifi.dbs.elki.database.ids.generic; This file is part of ELKI: Environment for Developing KDD-Applications Supported by Index-Structures - Copyright (C) 2012 + Copyright (C) 2013 Ludwig-Maximilians-Universität München Lehr- und Forschungseinheit für Datenbanksysteme ELKI Development Team diff --git a/src/de/lmu/ifi/dbs/elki/database/ids/generic/MergedDBIDs.java b/src/de/lmu/ifi/dbs/elki/database/ids/generic/MergedDBIDs.java index 9d2583e3..7df6975c 100644 --- a/src/de/lmu/ifi/dbs/elki/database/ids/generic/MergedDBIDs.java +++ b/src/de/lmu/ifi/dbs/elki/database/ids/generic/MergedDBIDs.java @@ -4,7 +4,7 @@ package de.lmu.ifi.dbs.elki.database.ids.generic; This file is part of ELKI: Environment for Developing KDD-Applications Supported by Index-Structures - Copyright (C) 2012 + Copyright (C) 2013 Ludwig-Maximilians-Universität München Lehr- und Forschungseinheit für Datenbanksysteme ELKI Development Team diff --git a/src/de/lmu/ifi/dbs/elki/database/ids/generic/UnmodifiableArrayDBIDs.java b/src/de/lmu/ifi/dbs/elki/database/ids/generic/UnmodifiableArrayDBIDs.java index abcdef54..318e4e79 100644 --- a/src/de/lmu/ifi/dbs/elki/database/ids/generic/UnmodifiableArrayDBIDs.java +++ b/src/de/lmu/ifi/dbs/elki/database/ids/generic/UnmodifiableArrayDBIDs.java @@ -4,7 +4,7 @@ package de.lmu.ifi.dbs.elki.database.ids.generic; This file is part of ELKI: Environment for Developing KDD-Applications Supported by Index-Structures - Copyright (C) 2012 + Copyright (C) 2013 Ludwig-Maximilians-Universität München Lehr- und Forschungseinheit für Datenbanksysteme ELKI Development Team @@ -90,8 +90,8 @@ public class UnmodifiableArrayDBIDs implements ArrayStaticDBIDs { } @Override - public void assign(int index, DBIDVar var) { - inner.assign(index, var); + public void assignVar(int index, DBIDVar var) { + inner.assignVar(index, var); } @Override diff --git a/src/de/lmu/ifi/dbs/elki/database/ids/generic/UnmodifiableDBIDs.java b/src/de/lmu/ifi/dbs/elki/database/ids/generic/UnmodifiableDBIDs.java index fea35692..458dab3f 100644 --- a/src/de/lmu/ifi/dbs/elki/database/ids/generic/UnmodifiableDBIDs.java +++ b/src/de/lmu/ifi/dbs/elki/database/ids/generic/UnmodifiableDBIDs.java @@ -4,7 +4,7 @@ package de.lmu.ifi.dbs.elki.database.ids.generic; This file is part of ELKI: Environment for Developing KDD-Applications Supported by Index-Structures - Copyright (C) 2012 + Copyright (C) 2013 Ludwig-Maximilians-Universität München Lehr- und Forschungseinheit für Datenbanksysteme ELKI Development Team diff --git a/src/de/lmu/ifi/dbs/elki/database/ids/generic/package-info.java b/src/de/lmu/ifi/dbs/elki/database/ids/generic/package-info.java index 890b9c0a..9920cac7 100644 --- a/src/de/lmu/ifi/dbs/elki/database/ids/generic/package-info.java +++ b/src/de/lmu/ifi/dbs/elki/database/ids/generic/package-info.java @@ -7,7 +7,7 @@ This file is part of ELKI: Environment for Developing KDD-Applications Supported by Index-Structures -Copyright (C) 2012 +Copyright (C) 2013 Ludwig-Maximilians-Universität München Lehr- und Forschungseinheit für Datenbanksysteme ELKI Development Team diff --git a/src/de/lmu/ifi/dbs/elki/database/ids/integer/AbstractIntegerDBIDFactory.java b/src/de/lmu/ifi/dbs/elki/database/ids/integer/AbstractIntegerDBIDFactory.java new file mode 100644 index 00000000..061deb08 --- /dev/null +++ b/src/de/lmu/ifi/dbs/elki/database/ids/integer/AbstractIntegerDBIDFactory.java @@ -0,0 +1,201 @@ +package de.lmu.ifi.dbs.elki.database.ids.integer; + +/* + This file is part of ELKI: + Environment for Developing KDD-Applications Supported by Index-Structures + + Copyright (C) 2013 + Ludwig-Maximilians-Universität München + Lehr- und Forschungseinheit für Datenbanksysteme + ELKI Development Team + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. + */ +import de.lmu.ifi.dbs.elki.database.ids.ArrayModifiableDBIDs; +import de.lmu.ifi.dbs.elki.database.ids.DBID; +import de.lmu.ifi.dbs.elki.database.ids.DBIDFactory; +import de.lmu.ifi.dbs.elki.database.ids.DBIDPair; +import de.lmu.ifi.dbs.elki.database.ids.DBIDRef; +import de.lmu.ifi.dbs.elki.database.ids.DBIDVar; +import de.lmu.ifi.dbs.elki.database.ids.DBIDs; +import de.lmu.ifi.dbs.elki.database.ids.DoubleDBIDPair; +import de.lmu.ifi.dbs.elki.database.ids.HashSetModifiableDBIDs; +import de.lmu.ifi.dbs.elki.database.ids.distance.DistanceDBIDPair; +import de.lmu.ifi.dbs.elki.database.ids.distance.DoubleDistanceDBIDPair; +import de.lmu.ifi.dbs.elki.database.ids.distance.DoubleDistanceKNNHeap; +import de.lmu.ifi.dbs.elki.database.ids.distance.DoubleDistanceKNNList; +import de.lmu.ifi.dbs.elki.database.ids.distance.KNNHeap; +import de.lmu.ifi.dbs.elki.database.ids.distance.KNNList; +import de.lmu.ifi.dbs.elki.database.ids.generic.DistanceDBIDPairKNNHeap; +import de.lmu.ifi.dbs.elki.distance.distancevalue.Distance; +import de.lmu.ifi.dbs.elki.distance.distancevalue.DoubleDistance; +import de.lmu.ifi.dbs.elki.persistent.ByteBufferSerializer; +import de.lmu.ifi.dbs.elki.persistent.FixedSizeByteBufferSerializer; + +/** + * Abstract base class for DBID factories. + * + * @author Erich Schubert + * + * @apiviz.uses IntegerDBID oneway - - «create» + * @apiviz.uses IntegerDBIDPair oneway - - «create» + * @apiviz.uses IntegerDBIDRange oneway - - «create» + * @apiviz.uses TroveArrayModifiableDBIDs oneway - - «create» + * @apiviz.uses TroveHashSetModifiableDBIDs oneway - - «create» + */ +abstract class AbstractIntegerDBIDFactory implements DBIDFactory { + /** + * Invalid ID. + */ + DBID invalid = new IntegerDBID(Integer.MIN_VALUE); + + @Override + public DBID importInteger(int id) { + return new IntegerDBID(id); + } + + @Override + public void assignVar(DBIDVar var, int val) { + if (var instanceof IntegerDBIDVar) { + ((IntegerDBIDVar)var).internalSetIndex(val); + } else { + var.set(new IntegerDBID(val)); + } + } + + @Override + public int compare(DBIDRef a, DBIDRef b) { + final int inta = a.internalGetIndex(); + final int intb = b.internalGetIndex(); + return (inta < intb ? -1 : (inta == intb ? 0 : 1)); + } + + @Override + public boolean equal(DBIDRef a, DBIDRef b) { + return a.internalGetIndex() == b.internalGetIndex(); + } + + @Override + public String toString(DBIDRef id) { + return Integer.toString(id.internalGetIndex()); + } + + @Override + public DBIDVar newVar(DBIDRef val) { + return new IntegerDBIDVar(val); + } + + @Override + public ArrayModifiableDBIDs newArray() { + return new ArrayModifiableIntegerDBIDs(); + } + + @Override + public HashSetModifiableDBIDs newHashSet() { + return new TroveHashSetModifiableDBIDs(); + } + + @Override + public ArrayModifiableDBIDs newArray(int size) { + return new ArrayModifiableIntegerDBIDs(size); + } + + @Override + public HashSetModifiableDBIDs newHashSet(int size) { + return new TroveHashSetModifiableDBIDs(size); + } + + @Override + public ArrayModifiableDBIDs newArray(DBIDs existing) { + return new ArrayModifiableIntegerDBIDs(existing); + } + + @Override + public HashSetModifiableDBIDs newHashSet(DBIDs existing) { + return new TroveHashSetModifiableDBIDs(existing); + } + + @Override + public DBIDPair newPair(DBIDRef first, DBIDRef second) { + return new IntegerDBIDPair(first.internalGetIndex(), second.internalGetIndex()); + } + + @Override + public DoubleDBIDPair newPair(double val, DBIDRef id) { + return new IntegerDoubleDBIDPair(val, id.internalGetIndex()); + } + + @SuppressWarnings("unchecked") + @Override + public <D extends Distance<D>> DistanceDBIDPair<D> newDistancePair(D val, DBIDRef id) { + if (val instanceof DoubleDistance) { + return (DistanceDBIDPair<D>) new DoubleDistanceIntegerDBIDPair(((DoubleDistance) val).doubleValue(), id.internalGetIndex()); + } + return new DistanceIntegerDBIDPair<>(val, id.internalGetIndex()); + } + + @Override + public DoubleDistanceDBIDPair newDistancePair(double val, DBIDRef id) { + return new DoubleDistanceIntegerDBIDPair(val, id.internalGetIndex()); + } + + @SuppressWarnings("unchecked") + @Override + public <D extends Distance<D>> KNNHeap<D> newHeap(D factory, int k) { + if (factory instanceof DoubleDistance) { + return (KNNHeap<D>) new DoubleDistanceIntegerDBIDKNNListHeap(k); + } + return new DistanceDBIDPairKNNHeap<>(k); + } + + @SuppressWarnings("unchecked") + @Override + public <D extends Distance<D>> KNNHeap<D> newHeap(KNNList<D> exist) { + if (exist instanceof DoubleDistanceKNNList) { + DoubleDistanceKNNHeap heap = new DoubleDistanceIntegerDBIDKNNListHeap(exist.getK()); + // Insert backwards, as this will produce a proper heap + for (int i = exist.size() - 1; i >= 0; i--) { + heap.add((DoubleDistanceDBIDPair) exist.get(i)); + } + return (KNNHeap<D>) heap; + } else { + DistanceDBIDPairKNNHeap<D> heap = new DistanceDBIDPairKNNHeap<>(exist.getK()); + // Insert backwards, as this will produce a proper heap + for (int i = exist.size() - 1; i >= 0; i--) { + heap.add(exist.get(i)); + } + return heap; + } + } + + @Override + public ByteBufferSerializer<DBID> getDBIDSerializer() { + return IntegerDBID.DYNAMIC_SERIALIZER; + } + + @Override + public FixedSizeByteBufferSerializer<DBID> getDBIDSerializerStatic() { + return IntegerDBID.STATIC_SERIALIZER; + } + + @Override + public Class<? extends DBID> getTypeRestriction() { + return IntegerDBID.class; + } + + @Override + public DBIDRef invalid() { + return invalid; + } +} diff --git a/src/de/lmu/ifi/dbs/elki/database/ids/integer/ArrayModifiableIntegerDBIDs.java b/src/de/lmu/ifi/dbs/elki/database/ids/integer/ArrayModifiableIntegerDBIDs.java new file mode 100644 index 00000000..dfff45b4 --- /dev/null +++ b/src/de/lmu/ifi/dbs/elki/database/ids/integer/ArrayModifiableIntegerDBIDs.java @@ -0,0 +1,306 @@ +package de.lmu.ifi.dbs.elki.database.ids.integer; + +/* + This file is part of ELKI: + Environment for Developing KDD-Applications Supported by Index-Structures + + Copyright (C) 2013 + Ludwig-Maximilians-Universität München + Lehr- und Forschungseinheit für Datenbanksysteme + ELKI Development Team + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +import java.util.Arrays; +import java.util.Comparator; + +import de.lmu.ifi.dbs.elki.database.ids.ArrayModifiableDBIDs; +import de.lmu.ifi.dbs.elki.database.ids.DBID; +import de.lmu.ifi.dbs.elki.database.ids.DBIDIter; +import de.lmu.ifi.dbs.elki.database.ids.DBIDRef; +import de.lmu.ifi.dbs.elki.database.ids.DBIDVar; +import de.lmu.ifi.dbs.elki.database.ids.DBIDs; + +/** + * Class using a primitive int[] array as storage. + * + * @author Erich Schubert + */ +public class ArrayModifiableIntegerDBIDs implements ArrayModifiableDBIDs, IntegerArrayDBIDs { + /** + * The actual Trove array list. + */ + private int[] store; + + /** + * Occupied size. + */ + private int size = 0; + + /** + * Initial size. + */ + public static final int INITIAL_SIZE = 21; + + /** + * Constructor. + * + * @param size Initial size + */ + protected ArrayModifiableIntegerDBIDs(int size) { + super(); + this.store = new int[size]; + } + + /** + * Constructor. + */ + protected ArrayModifiableIntegerDBIDs() { + super(); + this.store = new int[INITIAL_SIZE]; + } + + /** + * Constructor. + * + * @param existing Existing ids + */ + protected ArrayModifiableIntegerDBIDs(DBIDs existing) { + this(existing.size()); + this.addDBIDs(existing); + } + + @Override + public int size() { + return size; + } + + @Override + public boolean isEmpty() { + return size == 0; + } + + @Override + public DBID get(int i) { + return new IntegerDBID(store[i]); + } + + @Override + public void assignVar(int index, DBIDVar var) { + if(var instanceof IntegerDBIDVar) { + ((IntegerDBIDVar) var).internalSetIndex(store[index]); + } + else { + // less efficient, involves object creation. + var.set(get(index)); + } + } + + /** + * Resize as desired. + * + * @param minsize Desired size + */ + private void ensureSize(int minsize) { + int asize = store.length; + // Ensure a minimum size, to not run into an infinite loop below! + if (asize < 2) { + asize = 2; + } + while(asize < minsize) { + asize = (asize >> 1) + asize; + } + if(asize > store.length) { + store = Arrays.copyOf(store, asize); + } + } + + @Override + public boolean addDBIDs(DBIDs ids) { + ensureSize(size + ids.size()); + for(DBIDIter iter = ids.iter(); iter.valid(); iter.advance()) { + store[size] = iter.internalGetIndex(); + ++size; + } + return true; + } + + @Override + public boolean removeDBIDs(DBIDs ids) { + boolean success = false; + for(DBIDIter id = ids.iter(); id.valid(); id.advance()) { + int rm = id.internalGetIndex(); + // TODO: when sorted, use binary search! + for(int i = 0; i < size; i++) { + if(store[i] == rm) { + --size; + store[i] = store[size]; + success = true; + break; + } + } + } + return success; + } + + @Override + public boolean add(DBIDRef e) { + if(size == store.length) { + ensureSize(size + 1); + } + store[size] = e.internalGetIndex(); + ++size; + return true; + } + + @Override + public boolean remove(DBIDRef o) { + int rm = o.internalGetIndex(); + // TODO: when sorted, use binary search! + for(int i = 0; i < size; i++) { + if(store[i] == rm) { + --size; + store[i] = store[size]; + return true; + } + } + return false; + } + + @Override + public DBID set(int index, DBIDRef element) { + int prev = store[index]; + store[index] = element.internalGetIndex(); + return new IntegerDBID(prev); + } + + @Override + public DBID remove(int index) { + DBID ret = new IntegerDBID(store[index]); + --size; + if(size > 0) { + store[index] = store[size]; + } + return ret; + } + + @Override + public void clear() { + size = 0; + } + + @Override + public int binarySearch(DBIDRef key) { + return Arrays.binarySearch(store, 0, size, key.internalGetIndex()); + } + + @Override + public boolean contains(DBIDRef o) { + // TODO: recognize sorted arrays, then use binary search? + int oid = o.internalGetIndex(); + for(int i = 0; i < size; i++) { + if(store[i] == oid) { + return true; + } + } + return false; + } + + @Override + public void sort() { + Arrays.sort(store, 0, size); + } + + @Override + public void sort(Comparator<? super DBIDRef> comparator) { + IntegerDBIDArrayQuickSort.sort(store, 0, size, comparator); + } + + @Override + public void sort(int start, int end, Comparator<? super DBIDRef> comparator) { + IntegerDBIDArrayQuickSort.sort(store, start, end, comparator); + } + + @Override + public void swap(int a, int b) { + int tmp = store[b]; + store[b] = store[a]; + store[a] = tmp; + } + + @Override + public IntegerDBIDArrayMIter iter() { + return new Itr(); + } + + /** + * Iterator class. + * + * @author Erich Schubert + * + * @apiviz.exclude + */ + private class Itr implements IntegerDBIDArrayMIter { + /** + * Iterator position. + */ + int pos = 0; + + @Override + public int internalGetIndex() { + return store[pos]; + } + + @Override + public boolean valid() { + return pos < size && pos >= 0; + } + + @Override + public void advance() { + ++pos; + } + + @Override + public int getOffset() { + return pos; + } + + @Override + public void advance(int count) { + pos += count; + } + + @Override + public void retract() { + --pos; + } + + @Override + public void seek(int off) { + pos = off; + } + + @Override + public void remove() { + ArrayModifiableIntegerDBIDs.this.remove(pos); + } + + @Override + public String toString() { + return Integer.toString(internalGetIndex()) + "@" + pos; + } + } +} diff --git a/src/de/lmu/ifi/dbs/elki/database/ids/integer/IntArrayStaticDBIDs.java b/src/de/lmu/ifi/dbs/elki/database/ids/integer/ArrayStaticIntegerDBIDs.java index aa3b3cc0..4b4b5a42 100644 --- a/src/de/lmu/ifi/dbs/elki/database/ids/integer/IntArrayStaticDBIDs.java +++ b/src/de/lmu/ifi/dbs/elki/database/ids/integer/ArrayStaticIntegerDBIDs.java @@ -4,7 +4,7 @@ package de.lmu.ifi.dbs.elki.database.ids.integer; This file is part of ELKI: Environment for Developing KDD-Applications Supported by Index-Structures - Copyright (C) 2012 + Copyright (C) 2013 Ludwig-Maximilians-Universität München Lehr- und Forschungseinheit für Datenbanksysteme ELKI Development Team @@ -39,7 +39,7 @@ import de.lmu.ifi.dbs.elki.logging.LoggingUtil; * * @apiviz.has IntegerDBID */ -public class IntArrayStaticDBIDs implements IntegerArrayStaticDBIDs { +public class ArrayStaticIntegerDBIDs implements IntegerArrayStaticDBIDs { /** * The actual storage. */ @@ -50,7 +50,7 @@ public class IntArrayStaticDBIDs implements IntegerArrayStaticDBIDs { * * @param ids Array of ids. */ - public IntArrayStaticDBIDs(int... ids) { + public ArrayStaticIntegerDBIDs(int... ids) { super(); this.ids = ids; } @@ -118,7 +118,7 @@ public class IntArrayStaticDBIDs implements IntegerArrayStaticDBIDs { @Override public String toString() { - return Integer.toString(internalGetIndex()); + return Integer.toString(internalGetIndex()) + "@" + pos; } } @@ -149,7 +149,7 @@ public class IntArrayStaticDBIDs implements IntegerArrayStaticDBIDs { } @Override - public void assign(int i, DBIDVar var) { + public void assignVar(int i, DBIDVar var) { if (var instanceof IntegerDBIDVar) { ((IntegerDBIDVar)var).internalSetIndex(ids[i]); } else { diff --git a/src/de/lmu/ifi/dbs/elki/database/ids/integer/DistanceIntegerDBIDPair.java b/src/de/lmu/ifi/dbs/elki/database/ids/integer/DistanceIntegerDBIDPair.java index 01eec02e..a8930b87 100644 --- a/src/de/lmu/ifi/dbs/elki/database/ids/integer/DistanceIntegerDBIDPair.java +++ b/src/de/lmu/ifi/dbs/elki/database/ids/integer/DistanceIntegerDBIDPair.java @@ -4,7 +4,7 @@ package de.lmu.ifi.dbs.elki.database.ids.integer; This file is part of ELKI: Environment for Developing KDD-Applications Supported by Index-Structures - Copyright (C) 2012 + Copyright (C) 2013 Ludwig-Maximilians-Universität München Lehr- und Forschungseinheit für Datenbanksysteme ELKI Development Team @@ -23,9 +23,10 @@ package de.lmu.ifi.dbs.elki.database.ids.integer; along with this program. If not, see <http://www.gnu.org/licenses/>. */ -import de.lmu.ifi.dbs.elki.database.ids.DistanceDBIDPair; +import de.lmu.ifi.dbs.elki.database.ids.distance.DistanceDBIDPair; import de.lmu.ifi.dbs.elki.distance.distancevalue.Distance; import de.lmu.ifi.dbs.elki.distance.distancevalue.DoubleDistance; +import de.lmu.ifi.dbs.elki.utilities.Util; /** * Class storing a double distance a DBID. @@ -88,8 +89,13 @@ class DistanceIntegerDBIDPair<D extends Distance<D>> implements DistanceDBIDPair } if (o instanceof DoubleDistanceIntegerDBIDPair && distance instanceof DoubleDistance) { DoubleDistanceIntegerDBIDPair p = (DoubleDistanceIntegerDBIDPair) o; - return (this.id == p.id) && (((DoubleDistance) this.distance).doubleValue() == p.distance); + return (this.id == p.id) && (Double.compare(((DoubleDistance) this.distance).doubleValue(), p.distance) == 0); } return false; } + + @Override + public int hashCode() { + return Util.mixHashCodes(distance.hashCode(), id); + } } diff --git a/src/de/lmu/ifi/dbs/elki/database/ids/integer/DoubleDistanceIntegerDBIDKNNHeap.java b/src/de/lmu/ifi/dbs/elki/database/ids/integer/DoubleDistanceIntegerDBIDKNNHeap.java new file mode 100644 index 00000000..6c88c2d8 --- /dev/null +++ b/src/de/lmu/ifi/dbs/elki/database/ids/integer/DoubleDistanceIntegerDBIDKNNHeap.java @@ -0,0 +1,252 @@ +package de.lmu.ifi.dbs.elki.database.ids.integer; + +/* + This file is part of ELKI: + Environment for Developing KDD-Applications Supported by Index-Structures + + Copyright (C) 2013 + Ludwig-Maximilians-Universität München + Lehr- und Forschungseinheit für Datenbanksysteme + ELKI Development Team + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +import java.util.Arrays; + +import de.lmu.ifi.dbs.elki.database.ids.DBIDRef; +import de.lmu.ifi.dbs.elki.database.ids.distance.DoubleDistanceDBIDPair; +import de.lmu.ifi.dbs.elki.database.ids.distance.DoubleDistanceKNNHeap; +import de.lmu.ifi.dbs.elki.distance.distancevalue.DoubleDistance; +import de.lmu.ifi.dbs.elki.utilities.datastructures.heap.DoubleIntegerMaxHeap; + +/** + * Class to efficiently manage a kNN heap. + * + * @author Erich Schubert + * + * @apiviz.has DoubleDistanceIntegerDBIDKNNList + * @apiviz.composedOf DoubleIntegerMaxHeap + */ +public class DoubleDistanceIntegerDBIDKNNHeap implements DoubleDistanceKNNHeap { + /** + * k for this heap. + */ + private final int k; + + /** + * The main heap. + */ + private final DoubleIntegerMaxHeap heap; + + /** + * List to track ties. + */ + private int[] ties; + + /** + * Number of element in ties list. + */ + private int numties = 0; + + /** + * Current maximum value. + */ + private double kdist = Double.POSITIVE_INFINITY; + + /** + * Initial size of ties array. + */ + private static final int INITIAL_TIES_SIZE = 11; + + /** + * Constructor. + * + * @param k Size of knn. + */ + public DoubleDistanceIntegerDBIDKNNHeap(int k) { + super(); + this.k = k; + this.heap = new DoubleIntegerMaxHeap(k); + this.ties = new int[INITIAL_TIES_SIZE]; + } + + @Override + public int getK() { + return k; + } + + @Override + @Deprecated + public DoubleDistance getKNNDistance() { + if (heap.size() < k) { + return DoubleDistance.INFINITE_DISTANCE; + } + return new DoubleDistance(kdist); + } + + @Override + public double doubleKNNDistance() { + return kdist; + } + + @Override + @Deprecated + public void add(DoubleDistance distance, DBIDRef id) { + add(distance.doubleValue(), id); + } + + @Override + @Deprecated + public void add(Double distance, DBIDRef id) { + add(distance.doubleValue(), id); + } + + @Override + public final void add(final double distance, final DBIDRef id) { + if (distance > kdist) { + return; + } + final int iid = id.internalGetIndex(); + if (heap.size() < k) { + heap.add(distance, iid); + if (heap.size() >= k) { + kdist = heap.peekKey(); + } + return; + } + // Tied with top: + if (distance >= kdist) { + addToTies(iid); + return; + } + // Old top element: (kdist, previd) + updateHeap(distance, iid); + } + + @Override + public void add(DoubleDistanceDBIDPair e) { + add(e.doubleDistance(), e); + } + + /** + * Do a full update for the heap. + * + * @param distance Distance + * @param iid Object id + */ + private final void updateHeap(final double distance, final int iid) { + final double prevdist = kdist; + final int previd = heap.peekValue(); + heap.replaceTopElement(distance, iid); + kdist = heap.peekKey(); + // If the kdist improved, zap ties. + if (kdist < prevdist) { + numties = 0; + } else { + addToTies(previd); + } + } + + /** + * Ensure the ties array has capacity for at least one more element. + * + * @param id Id to add + */ + private final void addToTies(int id) { + if (ties.length == numties) { + ties = Arrays.copyOf(ties, (ties.length << 1) + 1); // grow. + } + ties[numties] = id; + ++numties; + } + + @Override + public DoubleDistanceIntegerDBIDPair poll() { + final DoubleDistanceIntegerDBIDPair ret; + if (numties > 0) { + ret = new DoubleDistanceIntegerDBIDPair(kdist, ties[numties - 1]); + --numties; + } else { + ret = new DoubleDistanceIntegerDBIDPair(heap.peekKey(), heap.peekValue()); + heap.poll(); + } + return ret; + } + + /** + * Pop the topmost element. + */ + protected void pop() { + if (numties > 0) { + --numties; + } else { + heap.poll(); + } + } + + @Override + public DoubleDistanceIntegerDBIDPair peek() { + if (numties > 0) { + return new DoubleDistanceIntegerDBIDPair(kdist, ties[numties - 1]); + } + return new DoubleDistanceIntegerDBIDPair(heap.peekKey(), heap.peekValue()); + } + + @Override + public int size() { + return heap.size() + numties; + } + + @Override + public boolean isEmpty() { + return heap.size() == 0; + } + + @Override + public void clear() { + heap.clear(); + numties = 0; + } + + @Override + public DoubleDistanceIntegerDBIDKNNList toKNNList() { + return new DoubleDistanceIntegerDBIDKNNList(this); + } + + /** + * Peek the topmost distance. + * + * @return distance + */ + protected double peekDistance() { + if (numties > 0) { + return kdist; + } else { + return heap.peekKey(); + } + } + + /** + * Peek the topmost internal ID. + * + * @return internal id + */ + protected int peekInternalDBID() { + if (numties > 0) { + return ties[numties - 1]; + } + return heap.peekValue(); + } +} diff --git a/src/de/lmu/ifi/dbs/elki/database/ids/integer/DoubleDistanceIntegerDBIDKNNList.java b/src/de/lmu/ifi/dbs/elki/database/ids/integer/DoubleDistanceIntegerDBIDKNNList.java new file mode 100644 index 00000000..a74497e8 --- /dev/null +++ b/src/de/lmu/ifi/dbs/elki/database/ids/integer/DoubleDistanceIntegerDBIDKNNList.java @@ -0,0 +1,298 @@ +package de.lmu.ifi.dbs.elki.database.ids.integer; + +/* + This file is part of ELKI: + Environment for Developing KDD-Applications Supported by Index-Structures + + Copyright (C) 2013 + Ludwig-Maximilians-Universität München + Lehr- und Forschungseinheit für Datenbanksysteme + ELKI Development Team + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. + */ +import java.util.Arrays; + +import de.lmu.ifi.dbs.elki.database.ids.DBIDRef; +import de.lmu.ifi.dbs.elki.database.ids.distance.DoubleDistanceDBIDPair; +import de.lmu.ifi.dbs.elki.database.ids.distance.DoubleDistanceDBIDListIter; +import de.lmu.ifi.dbs.elki.database.ids.distance.DoubleDistanceKNNList; +import de.lmu.ifi.dbs.elki.database.ids.distance.ModifiableDoubleDistanceDBIDList; +import de.lmu.ifi.dbs.elki.distance.distancevalue.DoubleDistance; + +/** + * Class to store double distance, integer DBID results. + * + * @author Erich Schubert + * + * @apiviz.uses DoubleIntegerArrayQuickSort + */ +public class DoubleDistanceIntegerDBIDKNNList implements ModifiableDoubleDistanceDBIDList, DoubleDistanceKNNList, IntegerDBIDs { + /** + * Initial size allocation. + */ + private static final int INITIAL_SIZE = 21; + + /** + * The k value this list was generated for. + */ + int k; + + /** + * The size + */ + int size; + + /** + * Distance values + */ + double[] dists; + + /** + * DBIDs + */ + int[] ids; + + /** + * Constructor. + */ + public DoubleDistanceIntegerDBIDKNNList() { + super(); + this.k = -1; + this.dists = new double[INITIAL_SIZE]; + this.ids = new int[INITIAL_SIZE]; + } + + /** + * Constructor. + * + * @param k K parameter + * @param size Actual size + */ + public DoubleDistanceIntegerDBIDKNNList(int k, int size) { + super(); + this.k = k; + if (size > 0) { + this.dists = new double[size]; + this.ids = new int[size]; + } else { + this.dists = new double[INITIAL_SIZE]; + this.ids = new int[INITIAL_SIZE]; + } + } + + /** + * Constructor from heap. + * + * @param heap KNN heap. + */ + public DoubleDistanceIntegerDBIDKNNList(DoubleDistanceIntegerDBIDKNNHeap heap) { + super(); + this.k = heap.getK(); + this.size = heap.size(); + this.dists = new double[size]; + this.ids = new int[size]; + for (int i = size - 1; i >= 0; i--) { + dists[i] = heap.peekDistance(); + ids[i] = heap.peekInternalDBID(); + heap.pop(); + } + } + + @Override + public DoubleDistanceIntegerDBIDListIter iter() { + return new Itr(); + } + + @Override + public boolean contains(DBIDRef o) { + final int q = o.internalGetIndex(); + for (int i = 0; i < size; i++) { + if (q == ids[i]) { + return true; + } + } + return false; + } + + @Override + public boolean isEmpty() { + return size == 0; + } + + @Override + public int size() { + return size; + } + + @Override + public int getK() { + if (k <= 0) { + return size - 1; + } + return k; + } + + @Override + public DoubleDistanceIntegerDBIDPair get(int index) { + return new DoubleDistanceIntegerDBIDPair(dists[index], ids[index]); + } + + @Override + @Deprecated + public DoubleDistance getKNNDistance() { + return new DoubleDistance(doubleKNNDistance()); + } + + @Override + public double doubleKNNDistance() { + if (k <= 0) { + return dists[size - 1]; + } + if (size < k) { + return Double.POSITIVE_INFINITY; + } + return dists[k - 1]; + } + + /** + * Add an entry, consisting of distance and internal index. + * + * @param dist Distance + * @param id Internal index + */ + protected void add(double dist, int id) { + if (size == dists.length) { + final int newlength = (dists.length << 1) + 1; + dists = Arrays.copyOf(dists, newlength); + ids = Arrays.copyOf(ids, newlength); + } + dists[size] = dist; + ids[size] = id; + ++size; + } + + @Override + @Deprecated + public void add(DoubleDistance dist, DBIDRef id) { + add(dist.doubleValue(), id); + } + + @Override + public void add(double dist, DBIDRef id) { + add(dist, id.internalGetIndex()); + } + + @Override + public void add(DoubleDistanceDBIDPair pair) { + add(pair.doubleDistance(), pair.internalGetIndex()); + } + + @Override + public void sort() { + DoubleIntegerArrayQuickSort.sort(dists, ids, 0, size); + } + + /** + * Reverse the list. + */ + protected void reverse() { + for (int i = 0, j = size - 1; i < j; i++, j--) { + double tmpd = dists[j]; + dists[j] = dists[i]; + dists[i] = tmpd; + int tmpi = ids[j]; + ids[j] = ids[i]; + ids[i] = tmpi; + } + } + + @Override + public String toString() { + StringBuilder buf = new StringBuilder(); + buf.append("kNNList["); + for (DoubleDistanceDBIDListIter iter = this.iter(); iter.valid();) { + buf.append(iter.doubleDistance()).append(':').append(iter.internalGetIndex()); + iter.advance(); + if (iter.valid()) { + buf.append(','); + } + } + buf.append(']'); + return buf.toString(); + } + + /** + * List iterator. + * + * @author Erich Schubert + * + * @apiviz.exclude + */ + private class Itr implements DoubleDistanceIntegerDBIDListIter { + int offset = 0; + + @Override + public boolean valid() { + return offset < size; + } + + @Override + public void advance() { + ++offset; + } + + @Override + public int getOffset() { + return offset; + } + + @Override + public void advance(int count) { + offset += count; + } + + @Override + public void retract() { + offset--; + } + + @Override + public void seek(int off) { + offset = off; + } + + @Override + public int internalGetIndex() { + return ids[offset]; + } + + @Override + public double doubleDistance() { + return dists[offset]; + } + + @Override + public DoubleDistanceDBIDPair getDistancePair() { + return new DoubleDistanceIntegerDBIDPair(dists[offset], ids[offset]); + } + + @Override + @Deprecated + public DoubleDistance getDistance() { + return new DoubleDistance(dists[offset]); + } + + } +} diff --git a/src/de/lmu/ifi/dbs/elki/database/ids/integer/DoubleDistanceIntegerDBIDKNNListHeap.java b/src/de/lmu/ifi/dbs/elki/database/ids/integer/DoubleDistanceIntegerDBIDKNNListHeap.java new file mode 100644 index 00000000..ffc2266e --- /dev/null +++ b/src/de/lmu/ifi/dbs/elki/database/ids/integer/DoubleDistanceIntegerDBIDKNNListHeap.java @@ -0,0 +1,315 @@ +package de.lmu.ifi.dbs.elki.database.ids.integer; + +/* + This file is part of ELKI: + Environment for Developing KDD-Applications Supported by Index-Structures + + Copyright (C) 2013 + Ludwig-Maximilians-Universität München + Lehr- und Forschungseinheit für Datenbanksysteme + ELKI Development Team + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. + */ +import java.util.Arrays; + +import de.lmu.ifi.dbs.elki.database.ids.DBIDRef; +import de.lmu.ifi.dbs.elki.database.ids.distance.DoubleDistanceDBIDListIter; +import de.lmu.ifi.dbs.elki.database.ids.distance.DoubleDistanceDBIDPair; +import de.lmu.ifi.dbs.elki.database.ids.distance.DoubleDistanceKNNHeap; +import de.lmu.ifi.dbs.elki.database.ids.distance.DoubleDistanceKNNList; +import de.lmu.ifi.dbs.elki.distance.distancevalue.DoubleDistance; + +/** + * Class to store double distance, integer DBID results. + * + * @author Erich Schubert + * + * @apiviz.uses DoubleIntegerArrayQuickSort + */ +public class DoubleDistanceIntegerDBIDKNNListHeap implements DoubleDistanceKNNHeap, DoubleDistanceKNNList, IntegerDBIDs { + /** + * The k value this list was generated for. + */ + int k; + + /** + * The size + */ + int size; + + /** + * Distance values + */ + double[] dists; + + /** + * DBIDs + */ + int[] ids; + + /** + * Constructor. + * + * @param k K parameter + */ + public DoubleDistanceIntegerDBIDKNNListHeap(int k) { + super(); + this.k = k; + this.size = 0; + this.dists = new double[k + 1]; + this.ids = new int[k + 1]; + } + + @Override + public DoubleDistanceIntegerDBIDListIter iter() { + return new Itr(); + } + + @Override + public boolean contains(DBIDRef o) { + final int q = o.internalGetIndex(); + for(int i = 0; i < size; i++) { + if(q == ids[i]) { + return true; + } + } + return false; + } + + @Override + public boolean isEmpty() { + return size == 0; + } + + @Override + public int size() { + return size; + } + + @Override + public int getK() { + return k; + } + + @Override + public DoubleDistanceIntegerDBIDPair get(int index) { + return new DoubleDistanceIntegerDBIDPair(dists[index], ids[index]); + } + + @Override + @Deprecated + public DoubleDistance getKNNDistance() { + return new DoubleDistance(doubleKNNDistance()); + } + + @Override + public double doubleKNNDistance() { + if(size < k) { + return Double.POSITIVE_INFINITY; + } + return dists[k - 1]; + } + + /** + * Add an entry, consisting of distance and internal index. + * + * @param dist Distance + * @param id Internal index + */ + protected void append(double dist, int id) { + ensureSize(size + 1); + dists[size] = dist; + ids[size] = id; + ++size; + } + + /** + * Add a new element to the heap/list. + * + * @param dist Distance + * @param id Object ID + */ + protected void add(double dist, int id) { + if(size < k) { + dists[size] = dist; + ids[size] = id; + ++size; + if(size == k) { + sort(); + } + return; + } + if (dist > dists[size - 1]) { + return; + } + // Ensure we have enough space. + ensureSize(size + 1); + // Insertion sort: + int pos = size; + while(pos > 0 && dists[pos - 1] > dist) { + dists[pos] = dists[pos - 1]; + ids[pos] = ids[pos - 1]; + --pos; + } + dists[pos] = dist; + ids[pos] = id; + ++size; + // Truncate if necessary: + if(dists[k] > dists[k - 1]) { + size = k; + } + } + + /** + * Ensure we have enough space. + * + * @param size Desired size + */ + private void ensureSize(int size) { + if(size > dists.length) { + final int newlength = Math.max(size, (dists.length << 1) + 1); + dists = Arrays.copyOf(dists, newlength); + ids = Arrays.copyOf(ids, newlength); + } + } + + @Override + @Deprecated + public void add(DoubleDistance dist, DBIDRef id) { + add(dist.doubleValue(), id); + } + + @Override + @Deprecated + public void add(Double dist, DBIDRef id) { + add(dist.doubleValue(), id); + } + + @Override + public void add(double dist, DBIDRef id) { + add(dist, id.internalGetIndex()); + } + + @Override + public void add(DoubleDistanceDBIDPair pair) { + add(pair.doubleDistance(), pair.internalGetIndex()); + } + + /** + * Sort the current contents of the list. + */ + protected void sort() { + DoubleIntegerArrayQuickSort.sort(dists, ids, 0, size); + } + + @Override + public void clear() { + size = 0; + Arrays.fill(dists, Double.NaN); + Arrays.fill(ids, -1); + } + + @Override + public DoubleDistanceIntegerDBIDPair poll() { + return new DoubleDistanceIntegerDBIDPair(dists[k], ids[k]); + } + + @Override + public DoubleDistanceIntegerDBIDPair peek() { + return new DoubleDistanceIntegerDBIDPair(dists[k], ids[k]); + } + + @Override + public DoubleDistanceKNNList toKNNList() { + return this; + } + + @Override + public String toString() { + StringBuilder buf = new StringBuilder(); + buf.append("kNNListHeap["); + for(DoubleDistanceDBIDListIter iter = this.iter(); iter.valid();) { + buf.append(iter.doubleDistance()).append(':').append(iter.internalGetIndex()); + iter.advance(); + if(iter.valid()) { + buf.append(','); + } + } + buf.append(']'); + return buf.toString(); + } + + /** + * List iterator. + * + * @author Erich Schubert + * + * @apiviz.exclude + */ + private class Itr implements DoubleDistanceIntegerDBIDListIter { + int offset = 0; + + @Override + public boolean valid() { + return offset < size; + } + + @Override + public void advance() { + ++offset; + } + + @Override + public int getOffset() { + return offset; + } + + @Override + public void advance(int count) { + offset += count; + } + + @Override + public void retract() { + offset--; + } + + @Override + public void seek(int off) { + offset = off; + } + + @Override + public int internalGetIndex() { + return ids[offset]; + } + + @Override + public double doubleDistance() { + return dists[offset]; + } + + @Override + public DoubleDistanceDBIDPair getDistancePair() { + return new DoubleDistanceIntegerDBIDPair(dists[offset], ids[offset]); + } + + @Override + @Deprecated + public DoubleDistance getDistance() { + return new DoubleDistance(dists[offset]); + } + } +} diff --git a/src/de/lmu/ifi/dbs/elki/database/ids/integer/DoubleDistanceIntegerDBIDListIter.java b/src/de/lmu/ifi/dbs/elki/database/ids/integer/DoubleDistanceIntegerDBIDListIter.java new file mode 100644 index 00000000..0df81929 --- /dev/null +++ b/src/de/lmu/ifi/dbs/elki/database/ids/integer/DoubleDistanceIntegerDBIDListIter.java @@ -0,0 +1,34 @@ +package de.lmu.ifi.dbs.elki.database.ids.integer; + +/* + This file is part of ELKI: + Environment for Developing KDD-Applications Supported by Index-Structures + + Copyright (C) 2013 + Ludwig-Maximilians-Universität München + Lehr- und Forschungseinheit für Datenbanksysteme + ELKI Development Team + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. + */ +import de.lmu.ifi.dbs.elki.database.ids.distance.DoubleDistanceDBIDListIter; + +/** + * Combination interface. + * + * @author Erich Schubert + */ +public interface DoubleDistanceIntegerDBIDListIter extends DoubleDistanceDBIDListIter, IntegerDBIDArrayIter { + // Yet another painful combination interface. +} diff --git a/src/de/lmu/ifi/dbs/elki/database/ids/integer/DoubleDistanceIntegerDBIDPair.java b/src/de/lmu/ifi/dbs/elki/database/ids/integer/DoubleDistanceIntegerDBIDPair.java index 8334d2e3..1f3b2a45 100644 --- a/src/de/lmu/ifi/dbs/elki/database/ids/integer/DoubleDistanceIntegerDBIDPair.java +++ b/src/de/lmu/ifi/dbs/elki/database/ids/integer/DoubleDistanceIntegerDBIDPair.java @@ -4,7 +4,7 @@ package de.lmu.ifi.dbs.elki.database.ids.integer; This file is part of ELKI: Environment for Developing KDD-Applications Supported by Index-Structures - Copyright (C) 2012 + Copyright (C) 2013 Ludwig-Maximilians-Universität München Lehr- und Forschungseinheit für Datenbanksysteme ELKI Development Team @@ -22,9 +22,10 @@ package de.lmu.ifi.dbs.elki.database.ids.integer; You should have received a copy of the GNU Affero General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */ -import de.lmu.ifi.dbs.elki.database.ids.DistanceDBIDPair; -import de.lmu.ifi.dbs.elki.database.ids.DoubleDistanceDBIDPair; +import de.lmu.ifi.dbs.elki.database.ids.distance.DistanceDBIDPair; +import de.lmu.ifi.dbs.elki.database.ids.distance.DoubleDistanceDBIDPair; import de.lmu.ifi.dbs.elki.distance.distancevalue.DoubleDistance; +import de.lmu.ifi.dbs.elki.utilities.Util; /** * Class storing a double distance a DBID. @@ -90,14 +91,20 @@ class DoubleDistanceIntegerDBIDPair implements DoubleDistanceDBIDPair, IntegerDB } if (o instanceof DoubleDistanceIntegerDBIDPair) { DoubleDistanceIntegerDBIDPair p = (DoubleDistanceIntegerDBIDPair) o; - return (this.id == p.id) && (this.distance == p.distance); + return (this.id == p.id) && (Double.compare(this.distance, p.distance) == 0); } if (o instanceof DistanceIntegerDBIDPair) { DistanceIntegerDBIDPair<?> p = (DistanceIntegerDBIDPair<?>) o; if (p.distance instanceof DoubleDistance) { - return (this.id == p.id) && (this.distance == ((DoubleDistance) p.distance).doubleValue()); + return (this.id == p.id) && (Double.compare(this.distance, ((DoubleDistance) p.distance).doubleValue()) == 0); } } return false; } + + @Override + public int hashCode() { + long bits = Double.doubleToLongBits(distance); + return Util.mixHashCodes((int) (bits ^ (bits >>> 32)), id); + } } diff --git a/src/de/lmu/ifi/dbs/elki/database/ids/integer/DoubleIntegerArrayQuickSort.java b/src/de/lmu/ifi/dbs/elki/database/ids/integer/DoubleIntegerArrayQuickSort.java new file mode 100644 index 00000000..8f1c58d6 --- /dev/null +++ b/src/de/lmu/ifi/dbs/elki/database/ids/integer/DoubleIntegerArrayQuickSort.java @@ -0,0 +1,181 @@ +package de.lmu.ifi.dbs.elki.database.ids.integer; + +/* + This file is part of ELKI: + Environment for Developing KDD-Applications Supported by Index-Structures + + Copyright (C) 2013 + Ludwig-Maximilians-Universität München + Lehr- und Forschungseinheit für Datenbanksysteme + ELKI Development Team + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +/** + * Class to sort a double and an integer DBID array, using a quicksort with a + * best of 5 heuristic. + * + * @author Erich Schubert + */ +class DoubleIntegerArrayQuickSort { + /** + * Threshold for using insertion sort. + */ + private static final int INSERTION_THRESHOLD = 22; + + /** + * Sort the full array using the given comparator. + * + * @param keys Keys for sorting + * @param values Values for sorting + * @param len Length to sort. + */ + public static void sort(double[] keys, int[] values, int len) { + sort(keys, values, 0, len); + } + + /** + * Sort the array using the given comparator. + * + * @param keys Keys for sorting + * @param values Values for sorting + * @param start First index + * @param end Last index (exclusive) + */ + public static void sort(double[] keys, int[] values, int start, int end) { + quickSort(keys, values, start, end); + } + + /** + * Actual recursive sort function. + * + * @param keys Keys for sorting + * @param vals Values for sorting + * @param start First index + * @param end Last index (exclusive!) + */ + private static void quickSort(double[] keys, int[] vals, final int start, final int end) { + final int len = end - start; + if (len < INSERTION_THRESHOLD) { + // Classic insertion sort. + for (int i = start + 1; i < end; i++) { + for (int j = i; j > start; j--) { + if (keys[j] < keys[j - 1]) { + swap(keys, vals, j, j - 1); + } else { + break; + } + } + } + return; + } + + // Choose pivots by looking at five candidates. + final int seventh = (len >> 3) + (len >> 6) + 1; + final int m3 = (start + end) >> 1; // middle + final int m2 = m3 - seventh; + final int m1 = m2 - seventh; + final int m4 = m3 + seventh; + final int m5 = m4 + seventh; + + // Mixture of insertion and merge sort: + if (keys[m1] > keys[m2]) { + swap(keys, vals, m1, m2); + } + if (keys[m3] > keys[m4]) { + swap(keys, vals, m3, m4); + } + // Merge 1+2 and 3+4 + if (keys[m2] > keys[m4]) { + swap(keys, vals, m2, m4); + } + if (keys[m1] > keys[m3]) { + swap(keys, vals, m1, m3); + } + if (keys[m2] > keys[m3]) { + swap(keys, vals, m2, m3); + } + // Insertion sort m5: + if (keys[m4] > keys[m5]) { + swap(keys, vals, m4, m5); + if (keys[m3] > keys[m4]) { + swap(keys, vals, m3, m4); + if (keys[m2] > keys[m3]) { + swap(keys, vals, m2, m3); + if (keys[m1] > keys[m1]) { + swap(keys, vals, m1, m2); + } + } + } + } + + // Move pivot to the front. + double pivotkey = keys[m3]; + int pivotval = vals[m3]; + keys[m3] = keys[start]; + vals[m3] = vals[start]; + + // The interval to pivotize + int left = start + 1; + int right = end - 1; + + // This is the classic QuickSort loop: + while (true) { + while (left <= right && keys[left] <= pivotkey) { + left++; + } + while (left <= right && pivotkey <= keys[right]) { + right--; + } + if (right <= left) { + break; + } + swap(keys, vals, left, right); + left++; + right--; + } + + // Move pivot back into the appropriate place + keys[start] = keys[right]; + vals[start] = vals[right]; + keys[right] = pivotkey; + vals[right] = pivotval; + + // Recursion: + if (start + 1 < right) { + quickSort(keys, vals, start, right); + } + if (right + 2 < end) { + quickSort(keys, vals, right + 1, end); + } + } + + /** + * Swap two entries. + * + * @param keys Keys + * @param vals Values + * @param j First index + * @param i Second index + */ + private static void swap(double[] keys, int[] vals, int j, int i) { + double td = keys[j]; + keys[j] = keys[i]; + keys[i] = td; + int ti = vals[j]; + vals[j] = vals[i]; + vals[i] = ti; + } +} diff --git a/src/de/lmu/ifi/dbs/elki/database/ids/integer/TroveArrayStaticDBIDs.java b/src/de/lmu/ifi/dbs/elki/database/ids/integer/IntegerArrayDBIDs.java index 60dd50eb..61a12b3f 100644 --- a/src/de/lmu/ifi/dbs/elki/database/ids/integer/TroveArrayStaticDBIDs.java +++ b/src/de/lmu/ifi/dbs/elki/database/ids/integer/IntegerArrayDBIDs.java @@ -1,9 +1,10 @@ package de.lmu.ifi.dbs.elki.database.ids.integer; + /* This file is part of ELKI: Environment for Developing KDD-Applications Supported by Index-Structures - Copyright (C) 2012 + Copyright (C) 2013 Ludwig-Maximilians-Universität München Lehr- und Forschungseinheit für Datenbanksysteme ELKI Development Team @@ -22,31 +23,14 @@ package de.lmu.ifi.dbs.elki.database.ids.integer; along with this program. If not, see <http://www.gnu.org/licenses/>. */ -import gnu.trove.list.TIntList; +import de.lmu.ifi.dbs.elki.database.ids.ArrayDBIDs; /** - * Class accessing a trove int array. + * Trivial combination interface. * * @author Erich Schubert */ -class TroveArrayStaticDBIDs extends TroveArrayDBIDs implements IntegerArrayStaticDBIDs { - /** - * Actual trove store. - */ - private final TIntList store; - - /** - * Constructor. - * - * @param store Actual trove store. - */ - protected TroveArrayStaticDBIDs(TIntList store) { - super(); - this.store = store; - } - +public interface IntegerArrayDBIDs extends IntegerDBIDs, ArrayDBIDs { @Override - protected TIntList getStore() { - return store; - } + IntegerDBIDArrayIter iter(); } diff --git a/src/de/lmu/ifi/dbs/elki/database/ids/integer/IntegerArrayStaticDBIDs.java b/src/de/lmu/ifi/dbs/elki/database/ids/integer/IntegerArrayStaticDBIDs.java index 4bafd343..9e18631b 100644 --- a/src/de/lmu/ifi/dbs/elki/database/ids/integer/IntegerArrayStaticDBIDs.java +++ b/src/de/lmu/ifi/dbs/elki/database/ids/integer/IntegerArrayStaticDBIDs.java @@ -4,7 +4,7 @@ package de.lmu.ifi.dbs.elki.database.ids.integer; This file is part of ELKI: Environment for Developing KDD-Applications Supported by Index-Structures - Copyright (C) 2012 + Copyright (C) 2013 Ludwig-Maximilians-Universität München Lehr- und Forschungseinheit für Datenbanksysteme ELKI Development Team @@ -29,9 +29,8 @@ import de.lmu.ifi.dbs.elki.database.ids.ArrayStaticDBIDs; * Combination of {@link ArrayStaticDBIDs} and {@link IntegerDBIDs}. * * @author Erich Schubert - * */ -public interface IntegerArrayStaticDBIDs extends ArrayStaticDBIDs, IntegerDBIDs { +public interface IntegerArrayStaticDBIDs extends ArrayStaticDBIDs, IntegerArrayDBIDs { @Override IntegerDBIDArrayIter iter(); }
\ No newline at end of file diff --git a/src/de/lmu/ifi/dbs/elki/database/ids/integer/IntegerDBID.java b/src/de/lmu/ifi/dbs/elki/database/ids/integer/IntegerDBID.java index 91c00939..40243695 100644 --- a/src/de/lmu/ifi/dbs/elki/database/ids/integer/IntegerDBID.java +++ b/src/de/lmu/ifi/dbs/elki/database/ids/integer/IntegerDBID.java @@ -4,7 +4,7 @@ package de.lmu.ifi.dbs.elki.database.ids.integer; This file is part of ELKI: Environment for Developing KDD-Applications Supported by Index-Structures - Copyright (C) 2012 + Copyright (C) 2013 Ludwig-Maximilians-Universität München Lehr- und Forschungseinheit für Datenbanksysteme ELKI Development Team @@ -133,7 +133,7 @@ final class IntegerDBID implements DBID, IntegerDBIDRef { } @Override - public void assign(int index, DBIDVar var) { + public void assignVar(int index, DBIDVar var) { if (index != 0) { throw new ArrayIndexOutOfBoundsException(); } diff --git a/src/de/lmu/ifi/dbs/elki/database/ids/integer/IntegerDBIDArrayIter.java b/src/de/lmu/ifi/dbs/elki/database/ids/integer/IntegerDBIDArrayIter.java index b0e2d339..c604ac71 100644 --- a/src/de/lmu/ifi/dbs/elki/database/ids/integer/IntegerDBIDArrayIter.java +++ b/src/de/lmu/ifi/dbs/elki/database/ids/integer/IntegerDBIDArrayIter.java @@ -4,7 +4,7 @@ package de.lmu.ifi.dbs.elki.database.ids.integer; This file is part of ELKI: Environment for Developing KDD-Applications Supported by Index-Structures - Copyright (C) 2012 + Copyright (C) 2013 Ludwig-Maximilians-Universität München Lehr- und Forschungseinheit für Datenbanksysteme ELKI Development Team diff --git a/src/de/lmu/ifi/dbs/elki/database/ids/integer/IntegerDBIDArrayMIter.java b/src/de/lmu/ifi/dbs/elki/database/ids/integer/IntegerDBIDArrayMIter.java index 21616f75..dcead6bd 100644 --- a/src/de/lmu/ifi/dbs/elki/database/ids/integer/IntegerDBIDArrayMIter.java +++ b/src/de/lmu/ifi/dbs/elki/database/ids/integer/IntegerDBIDArrayMIter.java @@ -4,7 +4,7 @@ package de.lmu.ifi.dbs.elki.database.ids.integer; This file is part of ELKI: Environment for Developing KDD-Applications Supported by Index-Structures - Copyright (C) 2012 + Copyright (C) 2013 Ludwig-Maximilians-Universität München Lehr- und Forschungseinheit für Datenbanksysteme ELKI Development Team diff --git a/src/de/lmu/ifi/dbs/elki/database/ids/integer/IntegerDBIDArrayQuickSort.java b/src/de/lmu/ifi/dbs/elki/database/ids/integer/IntegerDBIDArrayQuickSort.java index 2c096ab9..90a97609 100644 --- a/src/de/lmu/ifi/dbs/elki/database/ids/integer/IntegerDBIDArrayQuickSort.java +++ b/src/de/lmu/ifi/dbs/elki/database/ids/integer/IntegerDBIDArrayQuickSort.java @@ -4,7 +4,7 @@ package de.lmu.ifi.dbs.elki.database.ids.integer; This file is part of ELKI: Environment for Developing KDD-Applications Supported by Index-Structures - Copyright (C) 2012 + Copyright (C) 2013 Ludwig-Maximilians-Universität München Lehr- und Forschungseinheit für Datenbanksysteme ELKI Development Team diff --git a/src/de/lmu/ifi/dbs/elki/database/ids/integer/IntegerDBIDIter.java b/src/de/lmu/ifi/dbs/elki/database/ids/integer/IntegerDBIDIter.java index 9b50d544..cc241475 100644 --- a/src/de/lmu/ifi/dbs/elki/database/ids/integer/IntegerDBIDIter.java +++ b/src/de/lmu/ifi/dbs/elki/database/ids/integer/IntegerDBIDIter.java @@ -4,7 +4,7 @@ package de.lmu.ifi.dbs.elki.database.ids.integer; This file is part of ELKI: Environment for Developing KDD-Applications Supported by Index-Structures - Copyright (C) 2012 + Copyright (C) 2013 Ludwig-Maximilians-Universität München Lehr- und Forschungseinheit für Datenbanksysteme ELKI Development Team diff --git a/src/de/lmu/ifi/dbs/elki/database/ids/integer/IntegerDBIDMIter.java b/src/de/lmu/ifi/dbs/elki/database/ids/integer/IntegerDBIDMIter.java index 2e339dbc..c0291d5b 100644 --- a/src/de/lmu/ifi/dbs/elki/database/ids/integer/IntegerDBIDMIter.java +++ b/src/de/lmu/ifi/dbs/elki/database/ids/integer/IntegerDBIDMIter.java @@ -4,7 +4,7 @@ package de.lmu.ifi.dbs.elki.database.ids.integer; This file is part of ELKI: Environment for Developing KDD-Applications Supported by Index-Structures - Copyright (C) 2012 + Copyright (C) 2013 Ludwig-Maximilians-Universität München Lehr- und Forschungseinheit für Datenbanksysteme ELKI Development Team diff --git a/src/de/lmu/ifi/dbs/elki/database/ids/integer/IntegerDBIDPair.java b/src/de/lmu/ifi/dbs/elki/database/ids/integer/IntegerDBIDPair.java index e716f5b5..1b1ab154 100644 --- a/src/de/lmu/ifi/dbs/elki/database/ids/integer/IntegerDBIDPair.java +++ b/src/de/lmu/ifi/dbs/elki/database/ids/integer/IntegerDBIDPair.java @@ -4,7 +4,7 @@ package de.lmu.ifi.dbs.elki.database.ids.integer; This file is part of ELKI: Environment for Developing KDD-Applications Supported by Index-Structures - Copyright (C) 2012 + Copyright (C) 2013 Ludwig-Maximilians-Universität München Lehr- und Forschungseinheit für Datenbanksysteme ELKI Development Team diff --git a/src/de/lmu/ifi/dbs/elki/database/ids/integer/IntegerDBIDRange.java b/src/de/lmu/ifi/dbs/elki/database/ids/integer/IntegerDBIDRange.java index 85c47f43..3ceb163d 100644 --- a/src/de/lmu/ifi/dbs/elki/database/ids/integer/IntegerDBIDRange.java +++ b/src/de/lmu/ifi/dbs/elki/database/ids/integer/IntegerDBIDRange.java @@ -4,7 +4,7 @@ package de.lmu.ifi.dbs.elki.database.ids.integer; This file is part of ELKI: Environment for Developing KDD-Applications Supported by Index-Structures - Copyright (C) 2012 + Copyright (C) 2013 Ludwig-Maximilians-Universität München Lehr- und Forschungseinheit für Datenbanksysteme ELKI Development Team @@ -189,7 +189,7 @@ class IntegerDBIDRange implements DBIDRange { } @Override - public void assign(int index, DBIDVar var) { + public void assignVar(int index, DBIDVar var) { if (var instanceof IntegerDBIDVar) { ((IntegerDBIDVar)var).internalSetIndex(start + index); } else { diff --git a/src/de/lmu/ifi/dbs/elki/database/ids/integer/IntegerDBIDRef.java b/src/de/lmu/ifi/dbs/elki/database/ids/integer/IntegerDBIDRef.java index 45854440..dc63b117 100644 --- a/src/de/lmu/ifi/dbs/elki/database/ids/integer/IntegerDBIDRef.java +++ b/src/de/lmu/ifi/dbs/elki/database/ids/integer/IntegerDBIDRef.java @@ -4,7 +4,7 @@ package de.lmu.ifi.dbs.elki.database.ids.integer; This file is part of ELKI: Environment for Developing KDD-Applications Supported by Index-Structures - Copyright (C) 2012 + Copyright (C) 2013 Ludwig-Maximilians-Universität München Lehr- und Forschungseinheit für Datenbanksysteme ELKI Development Team diff --git a/src/de/lmu/ifi/dbs/elki/database/ids/integer/IntegerDBIDVar.java b/src/de/lmu/ifi/dbs/elki/database/ids/integer/IntegerDBIDVar.java index 73d164e0..57d400df 100644 --- a/src/de/lmu/ifi/dbs/elki/database/ids/integer/IntegerDBIDVar.java +++ b/src/de/lmu/ifi/dbs/elki/database/ids/integer/IntegerDBIDVar.java @@ -4,7 +4,7 @@ package de.lmu.ifi.dbs.elki.database.ids.integer; This file is part of ELKI: Environment for Developing KDD-Applications Supported by Index-Structures - Copyright (C) 2012 + Copyright (C) 2013 Ludwig-Maximilians-Universität München Lehr- und Forschungseinheit für Datenbanksysteme ELKI Development Team @@ -24,7 +24,6 @@ package de.lmu.ifi.dbs.elki.database.ids.integer; */ import de.lmu.ifi.dbs.elki.database.ids.DBID; -import de.lmu.ifi.dbs.elki.database.ids.DBIDArrayIter; import de.lmu.ifi.dbs.elki.database.ids.DBIDRef; import de.lmu.ifi.dbs.elki.database.ids.DBIDVar; import de.lmu.ifi.dbs.elki.logging.LoggingUtil; @@ -38,7 +37,7 @@ import de.lmu.ifi.dbs.elki.logging.LoggingUtil; * * @author Erich Schubert */ -class IntegerDBIDVar implements DBIDVar { +class IntegerDBIDVar implements DBIDVar, IntegerDBIDs { /** * The actual value. */ @@ -88,7 +87,7 @@ class IntegerDBIDVar implements DBIDVar { } @Override - public DBIDArrayIter iter() { + public IntegerDBIDArrayIter iter() { return new DBIDItr(); } @@ -115,7 +114,7 @@ class IntegerDBIDVar implements DBIDVar { * * @apiviz.exclude */ - protected class DBIDItr implements DBIDArrayIter, IntegerDBIDRef { + protected class DBIDItr implements IntegerDBIDArrayIter, IntegerDBIDRef { /** * Iterator position: We use an integer so we can support retract(). */ @@ -182,15 +181,15 @@ class IntegerDBIDVar implements DBIDVar { } @Override - public void assign(int i, DBIDVar var) { + public void assignVar(int i, DBIDVar var) { if (var instanceof IntegerDBIDVar) { - ((IntegerDBIDVar)var).internalSetIndex(i); + ((IntegerDBIDVar) var).internalSetIndex(i); } else { // Much less efficient: var.set(get(i)); } } - + @Override public String toString() { return Integer.toString(id); diff --git a/src/de/lmu/ifi/dbs/elki/database/ids/integer/IntegerDBIDs.java b/src/de/lmu/ifi/dbs/elki/database/ids/integer/IntegerDBIDs.java index 8ffb9c6b..4acf91e6 100644 --- a/src/de/lmu/ifi/dbs/elki/database/ids/integer/IntegerDBIDs.java +++ b/src/de/lmu/ifi/dbs/elki/database/ids/integer/IntegerDBIDs.java @@ -4,7 +4,7 @@ package de.lmu.ifi.dbs.elki.database.ids.integer; This file is part of ELKI: Environment for Developing KDD-Applications Supported by Index-Structures - Copyright (C) 2012 + Copyright (C) 2013 Ludwig-Maximilians-Universität München Lehr- und Forschungseinheit für Datenbanksysteme ELKI Development Team diff --git a/src/de/lmu/ifi/dbs/elki/database/ids/integer/IntegerDoubleDBIDPair.java b/src/de/lmu/ifi/dbs/elki/database/ids/integer/IntegerDoubleDBIDPair.java index fe8c6a60..b522ffb2 100644 --- a/src/de/lmu/ifi/dbs/elki/database/ids/integer/IntegerDoubleDBIDPair.java +++ b/src/de/lmu/ifi/dbs/elki/database/ids/integer/IntegerDoubleDBIDPair.java @@ -4,7 +4,7 @@ package de.lmu.ifi.dbs.elki.database.ids.integer; This file is part of ELKI: Environment for Developing KDD-Applications Supported by Index-Structures - Copyright (C) 2012 + Copyright (C) 2013 Ludwig-Maximilians-Universität München Lehr- und Forschungseinheit für Datenbanksysteme ELKI Development Team diff --git a/src/de/lmu/ifi/dbs/elki/database/ids/integer/ReusingDBIDFactory.java b/src/de/lmu/ifi/dbs/elki/database/ids/integer/ReusingDBIDFactory.java index f6df2e0d..ebb36f22 100644 --- a/src/de/lmu/ifi/dbs/elki/database/ids/integer/ReusingDBIDFactory.java +++ b/src/de/lmu/ifi/dbs/elki/database/ids/integer/ReusingDBIDFactory.java @@ -4,7 +4,7 @@ package de.lmu.ifi.dbs.elki.database.ids.integer; This file is part of ELKI: Environment for Developing KDD-Applications Supported by Index-Structures - Copyright (C) 2012 + Copyright (C) 2013 Ludwig-Maximilians-Universität München Lehr- und Forschungseinheit für Datenbanksysteme ELKI Development Team @@ -33,14 +33,15 @@ import de.lmu.ifi.dbs.elki.database.ids.DBIDRef; import de.lmu.ifi.dbs.elki.logging.Logging; /** - * Slightly more advanced DBID management, that allows reuse of DBIDs. + * Slightly more complex DBID management, that allows reuse of DBIDs. + * + * NOT tested a lot yet. Not reusing is much simpler! + * + * TODO: manage fragmentation of ranges? * * @author Erich Schubert * * @apiviz.stereotype factory - * @apiviz.uses IntegerDBID oneway - - «create» - * @apiviz.uses IntegerDBIDPair oneway - - «create» - * @apiviz.uses IntegerDBIDRange oneway - - «create» */ public class ReusingDBIDFactory extends SimpleDBIDFactory { /** @@ -64,7 +65,7 @@ public class ReusingDBIDFactory extends SimpleDBIDFactory { /** * Returned range allocations */ - ArrayList<IntegerDBIDRange> returnedAllocations = new ArrayList<IntegerDBIDRange>(); + ArrayList<IntegerDBIDRange> returnedAllocations = new ArrayList<>(); /** * Constructor @@ -115,7 +116,6 @@ public class ReusingDBIDFactory extends SimpleDBIDFactory { @Override public synchronized void deallocateDBIDRange(DBIDRange range) { - // TODO: catch an eventual cast exception? returnedAllocations.add((IntegerDBIDRange) range); } } diff --git a/src/de/lmu/ifi/dbs/elki/database/ids/integer/SimpleDBIDFactory.java b/src/de/lmu/ifi/dbs/elki/database/ids/integer/SimpleDBIDFactory.java index 77c1a91b..c3abd43a 100644 --- a/src/de/lmu/ifi/dbs/elki/database/ids/integer/SimpleDBIDFactory.java +++ b/src/de/lmu/ifi/dbs/elki/database/ids/integer/SimpleDBIDFactory.java @@ -4,7 +4,7 @@ package de.lmu.ifi.dbs.elki.database.ids.integer; This file is part of ELKI: Environment for Developing KDD-Applications Supported by Index-Structures - Copyright (C) 2012 + Copyright (C) 2013 Ludwig-Maximilians-Universität München Lehr- und Forschungseinheit für Datenbanksysteme ELKI Development Team @@ -23,22 +23,9 @@ package de.lmu.ifi.dbs.elki.database.ids.integer; along with this program. If not, see <http://www.gnu.org/licenses/>. */ -import de.lmu.ifi.dbs.elki.database.ids.ArrayModifiableDBIDs; import de.lmu.ifi.dbs.elki.database.ids.DBID; -import de.lmu.ifi.dbs.elki.database.ids.DBIDFactory; -import de.lmu.ifi.dbs.elki.database.ids.DBIDPair; import de.lmu.ifi.dbs.elki.database.ids.DBIDRange; import de.lmu.ifi.dbs.elki.database.ids.DBIDRef; -import de.lmu.ifi.dbs.elki.database.ids.DBIDVar; -import de.lmu.ifi.dbs.elki.database.ids.DBIDs; -import de.lmu.ifi.dbs.elki.database.ids.DistanceDBIDPair; -import de.lmu.ifi.dbs.elki.database.ids.DoubleDBIDPair; -import de.lmu.ifi.dbs.elki.database.ids.DoubleDistanceDBIDPair; -import de.lmu.ifi.dbs.elki.database.ids.HashSetModifiableDBIDs; -import de.lmu.ifi.dbs.elki.distance.distancevalue.Distance; -import de.lmu.ifi.dbs.elki.distance.distancevalue.DoubleDistance; -import de.lmu.ifi.dbs.elki.persistent.ByteBufferSerializer; -import de.lmu.ifi.dbs.elki.persistent.FixedSizeByteBufferSerializer; import de.lmu.ifi.dbs.elki.utilities.exceptions.AbortException; /** @@ -50,13 +37,8 @@ import de.lmu.ifi.dbs.elki.utilities.exceptions.AbortException; * * @apiviz.landmark * @apiviz.stereotype factory - * @apiviz.uses IntegerDBID oneway - - «create» - * @apiviz.uses IntegerDBIDPair oneway - - «create» - * @apiviz.uses IntegerDBIDRange oneway - - «create» - * @apiviz.uses TroveArrayModifiableDBIDs oneway - - «create» - * @apiviz.uses TroveHashSetModifiableDBIDs oneway - - «create» */ -public class SimpleDBIDFactory implements DBIDFactory { +public class SimpleDBIDFactory extends AbstractIntegerDBIDFactory { /** * Keep track of the smallest dynamic DBID offset not used. */ @@ -68,11 +50,6 @@ public class SimpleDBIDFactory implements DBIDFactory { int rangestart = 0; /** - * Invalid ID. - */ - DBID invalid = new IntegerDBID(Integer.MIN_VALUE); - - /** * Constructor. */ public SimpleDBIDFactory() { @@ -81,10 +58,10 @@ public class SimpleDBIDFactory implements DBIDFactory { @Override public synchronized DBID generateSingleDBID() { + dynamicids--; if(dynamicids == Integer.MIN_VALUE) { throw new AbortException("DBID range allocation error - too many objects allocated!"); } - dynamicids--; return new IntegerDBID(dynamicids); } @@ -107,114 +84,4 @@ public class SimpleDBIDFactory implements DBIDFactory { public void deallocateDBIDRange(DBIDRange range) { // ignore. } - - @Override - public DBID importInteger(int id) { - return new IntegerDBID(id); - } - - @Override - public void assignVar(DBIDVar var, int val) { - if (var instanceof IntegerDBIDVar) { - ((IntegerDBIDVar)var).internalSetIndex(val); - } else { - var.set(new IntegerDBID(val)); - } - } - - @Override - public int compare(DBIDRef a, DBIDRef b) { - final int inta = a.internalGetIndex(); - final int intb = b.internalGetIndex(); - return (inta < intb ? -1 : (inta == intb ? 0 : 1)); - } - - @Override - public boolean equal(DBIDRef a, DBIDRef b) { - return a.internalGetIndex() == b.internalGetIndex(); - } - - @Override - public String toString(DBIDRef id) { - return Integer.toString(id.internalGetIndex()); - } - - @Override - public DBIDVar newVar(DBIDRef val) { - return new IntegerDBIDVar(val); - } - - @Override - public ArrayModifiableDBIDs newArray() { - return new TroveArrayModifiableDBIDs(); - } - - @Override - public HashSetModifiableDBIDs newHashSet() { - return new TroveHashSetModifiableDBIDs(); - } - - @Override - public ArrayModifiableDBIDs newArray(int size) { - return new TroveArrayModifiableDBIDs(size); - } - - @Override - public HashSetModifiableDBIDs newHashSet(int size) { - return new TroveHashSetModifiableDBIDs(size); - } - - @Override - public ArrayModifiableDBIDs newArray(DBIDs existing) { - return new TroveArrayModifiableDBIDs(existing); - } - - @Override - public HashSetModifiableDBIDs newHashSet(DBIDs existing) { - return new TroveHashSetModifiableDBIDs(existing); - } - - @Override - public DBIDPair newPair(DBIDRef first, DBIDRef second) { - return new IntegerDBIDPair(first.internalGetIndex(), second.internalGetIndex()); - } - - @Override - public DoubleDBIDPair newPair(double val, DBIDRef id) { - return new IntegerDoubleDBIDPair(val, id.internalGetIndex()); - } - - @SuppressWarnings("unchecked") - @Override - public <D extends Distance<D>> DistanceDBIDPair<D> newDistancePair(D val, DBIDRef id) { - if (val instanceof DoubleDistance) { - return (DistanceDBIDPair<D>) new DoubleDistanceIntegerDBIDPair(((DoubleDistance) val).doubleValue(), id.internalGetIndex()); - } - return new DistanceIntegerDBIDPair<D>(val, id.internalGetIndex()); - } - - @Override - public DoubleDistanceDBIDPair newDistancePair(double val, DBIDRef id) { - return new DoubleDistanceIntegerDBIDPair(val, id.internalGetIndex()); - } - - @Override - public ByteBufferSerializer<DBID> getDBIDSerializer() { - return IntegerDBID.DYNAMIC_SERIALIZER; - } - - @Override - public FixedSizeByteBufferSerializer<DBID> getDBIDSerializerStatic() { - return IntegerDBID.STATIC_SERIALIZER; - } - - @Override - public Class<? extends DBID> getTypeRestriction() { - return IntegerDBID.class; - } - - @Override - public DBIDRef invalid() { - return invalid; - } }
\ No newline at end of file diff --git a/src/de/lmu/ifi/dbs/elki/database/ids/integer/TrivialDBIDFactory.java b/src/de/lmu/ifi/dbs/elki/database/ids/integer/TrivialDBIDFactory.java index a9d8aa90..577c29ac 100644 --- a/src/de/lmu/ifi/dbs/elki/database/ids/integer/TrivialDBIDFactory.java +++ b/src/de/lmu/ifi/dbs/elki/database/ids/integer/TrivialDBIDFactory.java @@ -4,7 +4,7 @@ package de.lmu.ifi.dbs.elki.database.ids.integer; This file is part of ELKI: Environment for Developing KDD-Applications Supported by Index-Structures - Copyright (C) 2012 + Copyright (C) 2013 Ludwig-Maximilians-Universität München Lehr- und Forschungseinheit für Datenbanksysteme ELKI Development Team @@ -25,51 +25,27 @@ package de.lmu.ifi.dbs.elki.database.ids.integer; import java.util.concurrent.atomic.AtomicInteger; -import de.lmu.ifi.dbs.elki.database.ids.ArrayModifiableDBIDs; import de.lmu.ifi.dbs.elki.database.ids.DBID; -import de.lmu.ifi.dbs.elki.database.ids.DBIDFactory; -import de.lmu.ifi.dbs.elki.database.ids.DBIDPair; import de.lmu.ifi.dbs.elki.database.ids.DBIDRange; import de.lmu.ifi.dbs.elki.database.ids.DBIDRef; -import de.lmu.ifi.dbs.elki.database.ids.DBIDVar; -import de.lmu.ifi.dbs.elki.database.ids.DBIDs; -import de.lmu.ifi.dbs.elki.database.ids.DistanceDBIDPair; -import de.lmu.ifi.dbs.elki.database.ids.DoubleDBIDPair; -import de.lmu.ifi.dbs.elki.database.ids.DoubleDistanceDBIDPair; -import de.lmu.ifi.dbs.elki.database.ids.HashSetModifiableDBIDs; -import de.lmu.ifi.dbs.elki.distance.distancevalue.Distance; -import de.lmu.ifi.dbs.elki.distance.distancevalue.DoubleDistance; -import de.lmu.ifi.dbs.elki.persistent.ByteBufferSerializer; -import de.lmu.ifi.dbs.elki.persistent.FixedSizeByteBufferSerializer; import de.lmu.ifi.dbs.elki.utilities.exceptions.AbortException; /** * Trivial DBID management, that never reuses IDs and just gives them out in - * sequence. Statically allocated DBID ranges are given positive values, - * Dynamically allocated DBIDs are given negative values. + * sequence. All IDs will be positive. * * @author Erich Schubert * * @apiviz.landmark * @apiviz.stereotype factory - * @apiviz.uses IntegerDBID oneway - - «create» - * @apiviz.uses IntegerDBIDPair oneway - - «create» - * @apiviz.uses IntegerDBIDRange oneway - - «create» - * @apiviz.uses TroveArrayModifiableDBIDs oneway - - «create» - * @apiviz.uses TroveHashSetModifiableDBIDs oneway - - «create» */ -final public class TrivialDBIDFactory implements DBIDFactory { +final public class TrivialDBIDFactory extends AbstractIntegerDBIDFactory { /** * Keep track of the smallest dynamic DBID offset not used. */ AtomicInteger next = new AtomicInteger(1); /** - * Invalid ID. - */ - DBID invalid = new IntegerDBID(Integer.MIN_VALUE); - - /** * Constructor. */ public TrivialDBIDFactory() { @@ -105,114 +81,4 @@ final public class TrivialDBIDFactory implements DBIDFactory { public void deallocateDBIDRange(DBIDRange range) { // ignore. } - - @Override - public DBID importInteger(int id) { - return new IntegerDBID(id); - } - - @Override - public void assignVar(DBIDVar var, int val) { - if (var instanceof IntegerDBIDVar) { - ((IntegerDBIDVar)var).internalSetIndex(val); - } else { - var.set(new IntegerDBID(val)); - } - } - - @Override - public int compare(DBIDRef a, DBIDRef b) { - final int inta = a.internalGetIndex(); - final int intb = b.internalGetIndex(); - return (inta < intb ? -1 : (inta == intb ? 0 : 1)); - } - - @Override - public boolean equal(DBIDRef a, DBIDRef b) { - return a.internalGetIndex() == b.internalGetIndex(); - } - - @Override - public String toString(DBIDRef id) { - return Integer.toString(id.internalGetIndex()); - } - - @Override - public DBIDVar newVar(DBIDRef val) { - return new IntegerDBIDVar(val); - } - - @Override - public ArrayModifiableDBIDs newArray() { - return new TroveArrayModifiableDBIDs(); - } - - @Override - public HashSetModifiableDBIDs newHashSet() { - return new TroveHashSetModifiableDBIDs(); - } - - @Override - public ArrayModifiableDBIDs newArray(int size) { - return new TroveArrayModifiableDBIDs(size); - } - - @Override - public HashSetModifiableDBIDs newHashSet(int size) { - return new TroveHashSetModifiableDBIDs(size); - } - - @Override - public ArrayModifiableDBIDs newArray(DBIDs existing) { - return new TroveArrayModifiableDBIDs(existing); - } - - @Override - public HashSetModifiableDBIDs newHashSet(DBIDs existing) { - return new TroveHashSetModifiableDBIDs(existing); - } - - @Override - public DBIDPair newPair(DBIDRef first, DBIDRef second) { - return new IntegerDBIDPair(first.internalGetIndex(), second.internalGetIndex()); - } - - @Override - public DoubleDBIDPair newPair(double val, DBIDRef id) { - return new IntegerDoubleDBIDPair(val, id.internalGetIndex()); - } - - @SuppressWarnings("unchecked") - @Override - public <D extends Distance<D>> DistanceDBIDPair<D> newDistancePair(D val, DBIDRef id) { - if (val instanceof DoubleDistance) { - return (DistanceDBIDPair<D>) new DoubleDistanceIntegerDBIDPair(((DoubleDistance) val).doubleValue(), id.internalGetIndex()); - } - return new DistanceIntegerDBIDPair<D>(val, id.internalGetIndex()); - } - - @Override - public DoubleDistanceDBIDPair newDistancePair(double val, DBIDRef id) { - return new DoubleDistanceIntegerDBIDPair(val, id.internalGetIndex()); - } - - @Override - public ByteBufferSerializer<DBID> getDBIDSerializer() { - return IntegerDBID.DYNAMIC_SERIALIZER; - } - - @Override - public FixedSizeByteBufferSerializer<DBID> getDBIDSerializerStatic() { - return IntegerDBID.STATIC_SERIALIZER; - } - - @Override - public Class<? extends DBID> getTypeRestriction() { - return IntegerDBID.class; - } - - @Override - public DBIDRef invalid() { - return invalid; - } } diff --git a/src/de/lmu/ifi/dbs/elki/database/ids/integer/TroveArrayDBIDs.java b/src/de/lmu/ifi/dbs/elki/database/ids/integer/TroveArrayDBIDs.java index 313a0f3b..6980176a 100644 --- a/src/de/lmu/ifi/dbs/elki/database/ids/integer/TroveArrayDBIDs.java +++ b/src/de/lmu/ifi/dbs/elki/database/ids/integer/TroveArrayDBIDs.java @@ -4,7 +4,7 @@ package de.lmu.ifi.dbs.elki.database.ids.integer; This file is part of ELKI: Environment for Developing KDD-Applications Supported by Index-Structures - Copyright (C) 2012 + Copyright (C) 2013 Ludwig-Maximilians-Universität München Lehr- und Forschungseinheit für Datenbanksysteme ELKI Development Team @@ -24,7 +24,6 @@ package de.lmu.ifi.dbs.elki.database.ids.integer; */ import gnu.trove.list.TIntList; -import de.lmu.ifi.dbs.elki.database.ids.ArrayDBIDs; import de.lmu.ifi.dbs.elki.database.ids.DBID; import de.lmu.ifi.dbs.elki.database.ids.DBIDIter; import de.lmu.ifi.dbs.elki.database.ids.DBIDRef; @@ -40,7 +39,7 @@ import de.lmu.ifi.dbs.elki.logging.LoggingUtil; * @apiviz.has IntegerDBID * @apiviz.has DBIDItr */ -public abstract class TroveArrayDBIDs implements ArrayDBIDs, IntegerDBIDs { +public abstract class TroveArrayDBIDs implements IntegerArrayDBIDs { /** * Get the array store. * @@ -59,7 +58,7 @@ public abstract class TroveArrayDBIDs implements ArrayDBIDs, IntegerDBIDs { } @Override - public void assign(int index, DBIDVar var) { + public void assignVar(int index, DBIDVar var) { if (var instanceof IntegerDBIDVar) { ((IntegerDBIDVar)var).internalSetIndex(getStore().get(index)); } else { diff --git a/src/de/lmu/ifi/dbs/elki/database/ids/integer/TroveArrayModifiableDBIDs.java b/src/de/lmu/ifi/dbs/elki/database/ids/integer/TroveArrayModifiableDBIDs.java index 7e84eb56..41191b10 100644 --- a/src/de/lmu/ifi/dbs/elki/database/ids/integer/TroveArrayModifiableDBIDs.java +++ b/src/de/lmu/ifi/dbs/elki/database/ids/integer/TroveArrayModifiableDBIDs.java @@ -4,7 +4,7 @@ package de.lmu.ifi.dbs.elki.database.ids.integer; This file is part of ELKI: Environment for Developing KDD-Applications Supported by Index-Structures - Copyright (C) 2012 + Copyright (C) 2013 Ludwig-Maximilians-Universität München Lehr- und Forschungseinheit für Datenbanksysteme ELKI Development Team @@ -81,6 +81,7 @@ class TroveArrayModifiableDBIDs extends TroveArrayDBIDs implements ArrayModifiab @Override public boolean addDBIDs(DBIDs ids) { boolean success = false; + store.ensureCapacity(ids.size()); for (DBIDIter iter = ids.iter(); iter.valid(); iter.advance()) { success |= store.add(DBIDUtil.asInteger(iter)); } diff --git a/src/de/lmu/ifi/dbs/elki/database/ids/integer/TroveHashSetModifiableDBIDs.java b/src/de/lmu/ifi/dbs/elki/database/ids/integer/TroveHashSetModifiableDBIDs.java index 9e65b3ae..9ebdccea 100644 --- a/src/de/lmu/ifi/dbs/elki/database/ids/integer/TroveHashSetModifiableDBIDs.java +++ b/src/de/lmu/ifi/dbs/elki/database/ids/integer/TroveHashSetModifiableDBIDs.java @@ -9,13 +9,13 @@ import de.lmu.ifi.dbs.elki.database.ids.DBIDRef; import de.lmu.ifi.dbs.elki.database.ids.DBIDUtil; import de.lmu.ifi.dbs.elki.database.ids.DBIDs; import de.lmu.ifi.dbs.elki.database.ids.HashSetModifiableDBIDs; -import de.lmu.ifi.dbs.elki.utilities.iterator.Iter; +import de.lmu.ifi.dbs.elki.utilities.datastructures.iterator.Iter; /* This file is part of ELKI: Environment for Developing KDD-Applications Supported by Index-Structures - Copyright (C) 2012 + Copyright (C) 2013 Ludwig-Maximilians-Universität München Lehr- und Forschungseinheit für Datenbanksysteme ELKI Development Team @@ -83,6 +83,7 @@ class TroveHashSetModifiableDBIDs implements HashSetModifiableDBIDs, IntegerDBID @Override public boolean addDBIDs(DBIDs ids) { + store.ensureCapacity(ids.size()); boolean success = false; for (DBIDIter iter = ids.iter(); iter.valid(); iter.advance()) { success |= store.add(DBIDUtil.asInteger(iter)); diff --git a/src/de/lmu/ifi/dbs/elki/database/ids/integer/UnmodifiableIntegerArrayDBIDs.java b/src/de/lmu/ifi/dbs/elki/database/ids/integer/UnmodifiableIntegerArrayDBIDs.java index 14e26748..ba30f54f 100644 --- a/src/de/lmu/ifi/dbs/elki/database/ids/integer/UnmodifiableIntegerArrayDBIDs.java +++ b/src/de/lmu/ifi/dbs/elki/database/ids/integer/UnmodifiableIntegerArrayDBIDs.java @@ -4,7 +4,7 @@ package de.lmu.ifi.dbs.elki.database.ids.integer; This file is part of ELKI: Environment for Developing KDD-Applications Supported by Index-Structures - Copyright (C) 2012 + Copyright (C) 2013 Ludwig-Maximilians-Universität München Lehr- und Forschungseinheit für Datenbanksysteme ELKI Development Team @@ -40,14 +40,14 @@ public class UnmodifiableIntegerArrayDBIDs implements IntegerArrayStaticDBIDs { /** * The DBIDs we wrap. */ - private final TroveArrayDBIDs inner; + private final IntegerArrayDBIDs inner; /** * Constructor. * * @param inner Inner DBID collection. */ - public UnmodifiableIntegerArrayDBIDs(TroveArrayDBIDs inner) { + public UnmodifiableIntegerArrayDBIDs(IntegerArrayDBIDs inner) { super(); this.inner = inner; } @@ -87,8 +87,8 @@ public class UnmodifiableIntegerArrayDBIDs implements IntegerArrayStaticDBIDs { } @Override - public void assign(int index, DBIDVar var) { - inner.assign(index, var); + public void assignVar(int index, DBIDVar var) { + inner.assignVar(index, var); } @Override diff --git a/src/de/lmu/ifi/dbs/elki/database/ids/integer/UnmodifiableIntegerDBIDs.java b/src/de/lmu/ifi/dbs/elki/database/ids/integer/UnmodifiableIntegerDBIDs.java index 1d27f530..a3f03bd9 100644 --- a/src/de/lmu/ifi/dbs/elki/database/ids/integer/UnmodifiableIntegerDBIDs.java +++ b/src/de/lmu/ifi/dbs/elki/database/ids/integer/UnmodifiableIntegerDBIDs.java @@ -4,7 +4,7 @@ package de.lmu.ifi.dbs.elki.database.ids.integer; This file is part of ELKI: Environment for Developing KDD-Applications Supported by Index-Structures - Copyright (C) 2012 + Copyright (C) 2013 Ludwig-Maximilians-Universität München Lehr- und Forschungseinheit für Datenbanksysteme ELKI Development Team diff --git a/src/de/lmu/ifi/dbs/elki/database/ids/integer/package-info.java b/src/de/lmu/ifi/dbs/elki/database/ids/integer/package-info.java index 2508c930..74a39fb9 100644 --- a/src/de/lmu/ifi/dbs/elki/database/ids/integer/package-info.java +++ b/src/de/lmu/ifi/dbs/elki/database/ids/integer/package-info.java @@ -10,7 +10,7 @@ This file is part of ELKI: Environment for Developing KDD-Applications Supported by Index-Structures -Copyright (C) 2012 +Copyright (C) 2013 Ludwig-Maximilians-Universität München Lehr- und Forschungseinheit für Datenbanksysteme ELKI Development Team diff --git a/src/de/lmu/ifi/dbs/elki/database/ids/package-info.java b/src/de/lmu/ifi/dbs/elki/database/ids/package-info.java index 71af955c..eb0a6733 100644 --- a/src/de/lmu/ifi/dbs/elki/database/ids/package-info.java +++ b/src/de/lmu/ifi/dbs/elki/database/ids/package-info.java @@ -83,7 +83,7 @@ This file is part of ELKI: Environment for Developing KDD-Applications Supported by Index-Structures - Copyright (C) 2012 + Copyright (C) 2013 Ludwig-Maximilians-Universität München Lehr- und Forschungseinheit für Datenbanksysteme ELKI Development Team |