diff options
Diffstat (limited to 'src/de/lmu/ifi/dbs/elki/utilities/datastructures/arraylike')
16 files changed, 1294 insertions, 0 deletions
diff --git a/src/de/lmu/ifi/dbs/elki/utilities/datastructures/arraylike/ArrayAdapter.java b/src/de/lmu/ifi/dbs/elki/utilities/datastructures/arraylike/ArrayAdapter.java new file mode 100644 index 00000000..969d068d --- /dev/null +++ b/src/de/lmu/ifi/dbs/elki/utilities/datastructures/arraylike/ArrayAdapter.java @@ -0,0 +1,52 @@ +package de.lmu.ifi.dbs.elki.utilities.datastructures.arraylike; + +/* + This file is part of ELKI: + Environment for Developing KDD-Applications Supported by Index-Structures + + Copyright (C) 2012 + 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/>. + */ + +/** + * Adapter for array-like things. For example, arrays and lists. + * + * @author Erich Schubert + * + * @param <T> Item type + * @param <A> Array object type + */ +public interface ArrayAdapter<T, A> { + /** + * Get the size of the array. + * + * @param array Array-like thing + * @return Size + */ + public int size(A array); + + /** + * Get the off'th item from the array. + * + * @param array Array to get from + * @param off Offset + * @return Item at offset off + * @throws IndexOutOfBoundsException for an invalid index. + */ + public T get(A array, int off) throws IndexOutOfBoundsException; +}
\ No newline at end of file diff --git a/src/de/lmu/ifi/dbs/elki/utilities/datastructures/arraylike/ArrayDBIDsAdapter.java b/src/de/lmu/ifi/dbs/elki/utilities/datastructures/arraylike/ArrayDBIDsAdapter.java new file mode 100644 index 00000000..6e6e8122 --- /dev/null +++ b/src/de/lmu/ifi/dbs/elki/utilities/datastructures/arraylike/ArrayDBIDsAdapter.java @@ -0,0 +1,53 @@ +package de.lmu.ifi.dbs.elki.utilities.datastructures.arraylike; + +/* + This file is part of ELKI: + Environment for Developing KDD-Applications Supported by Index-Structures + + Copyright (C) 2012 + 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.ArrayDBIDs; +import de.lmu.ifi.dbs.elki.database.ids.DBID; + +/** + * Use a DBID array in a generic array-like context. + * + * @author Erich Schubert + */ +public class ArrayDBIDsAdapter implements ArrayAdapter<DBID, ArrayDBIDs> { + /** + * Constructor. + * + * Protected - use the static instance from {@link ArrayLikeUtil}! + */ + protected ArrayDBIDsAdapter() { + super(); + } + + @Override + public int size(ArrayDBIDs array) { + return array.size(); + } + + @Override + public DBID get(ArrayDBIDs array, int off) throws IndexOutOfBoundsException { + return array.get(off); + } +}
\ No newline at end of file diff --git a/src/de/lmu/ifi/dbs/elki/utilities/datastructures/arraylike/ArrayLikeUtil.java b/src/de/lmu/ifi/dbs/elki/utilities/datastructures/arraylike/ArrayLikeUtil.java new file mode 100644 index 00000000..2d29f40c --- /dev/null +++ b/src/de/lmu/ifi/dbs/elki/utilities/datastructures/arraylike/ArrayLikeUtil.java @@ -0,0 +1,253 @@ +package de.lmu.ifi.dbs.elki.utilities.datastructures.arraylike; + +/* + This file is part of ELKI: + Environment for Developing KDD-Applications Supported by Index-Structures + + Copyright (C) 2012 + 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.List; + +import de.lmu.ifi.dbs.elki.data.FeatureVector; +import de.lmu.ifi.dbs.elki.data.NumberVector; + +/** + * Utility class that allows plug-in use of various "array-like" types such as + * lists in APIs that can take any kind of array to safe the cost of + * reorganizing the objects into a real array. + * + * @author Erich Schubert + */ +public final class ArrayLikeUtil { + /** + * Static instance for lists + */ + private static final ListArrayAdapter<Object> LISTADAPTER = new ListArrayAdapter<Object>(); + + /** + * Static instance for lists of numbers + */ + private static final NumberListArrayAdapter<Number> NUMBERLISTADAPTER = new NumberListArrayAdapter<Number>(); + + /** + * Static instance + */ + private final static IdentityArrayAdapter<?> IDENTITYADAPTER = new IdentityArrayAdapter<Object>(); + + /** + * Static instance + */ + private static final FeatureVectorAdapter<?> FEATUREVECTORADAPTER = new FeatureVectorAdapter<Number>(); + + /** + * Use a number vector in the array API. + */ + private static final NumberVectorAdapter<?> NUMBERVECTORADAPTER = new NumberVectorAdapter<Double>(); + + /** + * Use a double array in the array API. + */ + public static final NumberArrayAdapter<Double, double[]> DOUBLEARRAYADAPTER = new DoubleArrayAdapter(); + + /** + * Use a float array in the array API. + */ + public static final NumberArrayAdapter<Float, float[]> FLOATARRAYADAPTER = new FloatArrayAdapter(); + + /** + * Use a Trove double list as array. + */ + public static final TDoubleListAdapter TDOUBLELISTADAPTER = new TDoubleListAdapter(); + + /** + * Use ArrayDBIDs as array. + */ + public static final ArrayDBIDsAdapter ARRAYDBIDADAPTER = new ArrayDBIDsAdapter(); + + /** + * Cast the static instance. + * + * @param dummy Dummy variable, for type inference + * @return Static instance + */ + @SuppressWarnings("unchecked") + public static <T> ArrayAdapter<T, List<? extends T>> listAdapter(List<? extends T> dummy) { + return (ListArrayAdapter<T>) LISTADAPTER; + } + + /** + * Cast the static instance. + * + * @param dummy Dummy variable, for type inference + * @return Static instance + */ + @SuppressWarnings("unchecked") + public static <T extends Number> NumberArrayAdapter<T, List<? extends T>> numberListAdapter(List<? extends T> dummy) { + return (NumberListArrayAdapter<T>) NUMBERLISTADAPTER; + } + + /** + * Get the static instance. + * + * @param dummy Dummy object for type inference + * @return Static instance + */ + @SuppressWarnings("unchecked") + public static <T> IdentityArrayAdapter<T> identityAdapter(T dummy) { + return (IdentityArrayAdapter<T>) IDENTITYADAPTER; + } + + /** + * Get the static instance. + * + * @param prototype Prototype value, for type inference + * @return Instance + */ + @SuppressWarnings("unchecked") + public static <F> FeatureVectorAdapter<F> featureVectorAdapter(FeatureVector<?, F> prototype) { + return (FeatureVectorAdapter<F>) FEATUREVECTORADAPTER; + } + + /** + * Get the static instance. + * + * @param prototype Prototype value, for type inference + * @return Instance + */ + @SuppressWarnings("unchecked") + public static <N extends Number> NumberVectorAdapter<N> numberVectorAdapter(NumberVector<?, N> prototype) { + return (NumberVectorAdapter<N>) NUMBERVECTORADAPTER; + } + + /** + * Get the adapter for double arrays. + * + * @return double array adapter + */ + public static NumberArrayAdapter<Double, double[]> doubleArrayAdapter() { + return DOUBLEARRAYADAPTER; + } + + /** + * Returns the index of the maximum of the given values. If no value is bigger + * than the first, the index of the first entry is returned. + * + * @param <A> array type + * @param array Array to inspect + * @param adapter API adapter class + * @return the index of the maximum in the given values + * @throws IndexOutOfBoundsException if the length of the array is 0. + */ + public static <A> int getIndexOfMaximum(A array, NumberArrayAdapter<?, A> adapter) throws IndexOutOfBoundsException { + final int size = adapter.size(array); + int index = 0; + double max = adapter.getDouble(array, 0); + for(int i = 1; i < size; i++) { + double val = adapter.getDouble(array, i); + if(val > max) { + max = val; + index = i; + } + } + return index; + } + + /** + * Returns the index of the maximum of the given values. If no value is bigger + * than the first, the index of the first entry is returned. + * + * @param array Array to inspect + * @return the index of the maximum in the given values + * @throws IndexOutOfBoundsException if the length of the array is 0. + */ + public static int getIndexOfMaximum(double[] array) throws IndexOutOfBoundsException { + return getIndexOfMaximum(array, DOUBLEARRAYADAPTER); + } + + /** + * Convert a numeric array-like to a <code>double[]</code> + * + * @param array Array-like + * @param adapter Adapter + * @return primitive double array + */ + public static <A> double[] toPrimitiveDoubleArray(A array, NumberArrayAdapter<?, ? super A> adapter) { + double[] ret = new double[adapter.size(array)]; + for(int i = 0; i < ret.length; i++) { + ret[i] = adapter.getDouble(array, i); + } + return ret; + } + + /** + * Convert a list of numbers to <code>double[]</code>. + * + * @param array List of numbers + * @return double array + */ + public static double[] toPrimitiveDoubleArray(List<? extends Number> array) { + return toPrimitiveDoubleArray(array, NUMBERLISTADAPTER); + } + + /** + * Convert a number vector to <code>double[]</code>. + * + * @param obj Object to convert + * @return primitive double array + */ + public static <N extends Number> double[] toPrimitiveDoubleArray(NumberVector<?, N> obj) { + return toPrimitiveDoubleArray(obj, numberVectorAdapter(obj)); + } + + /** + * Convert a numeric array-like to a <code>float[]</code> + * + * @param array Array-like + * @param adapter Adapter + * @return primitive float array + */ + public static <A> float[] toPrimitiveFloatArray(A array, NumberArrayAdapter<?, ? super A> adapter) { + float[] ret = new float[adapter.size(array)]; + for(int i = 0; i < ret.length; i++) { + ret[i] = adapter.getFloat(array, i); + } + return ret; + } + + /** + * Convert a list of numbers to <code>float[]</code>. + * + * @param array List of numbers + * @return float array + */ + public static float[] toPrimitiveFloatArray(List<? extends Number> array) { + return toPrimitiveFloatArray(array, NUMBERLISTADAPTER); + } + + /** + * Convert a number vector to <code>float[]</code>. + * + * @param obj Object to convert + * @return primitive float array + */ + public static <N extends Number> float[] toPrimitiveFloatArray(NumberVector<?, N> obj) { + return toPrimitiveFloatArray(obj, numberVectorAdapter(obj)); + } +}
\ No newline at end of file diff --git a/src/de/lmu/ifi/dbs/elki/utilities/datastructures/arraylike/DoubleArrayAdapter.java b/src/de/lmu/ifi/dbs/elki/utilities/datastructures/arraylike/DoubleArrayAdapter.java new file mode 100644 index 00000000..43419103 --- /dev/null +++ b/src/de/lmu/ifi/dbs/elki/utilities/datastructures/arraylike/DoubleArrayAdapter.java @@ -0,0 +1,81 @@ +package de.lmu.ifi.dbs.elki.utilities.datastructures.arraylike; +/* + This file is part of ELKI: + Environment for Developing KDD-Applications Supported by Index-Structures + + Copyright (C) 2012 + 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/>. + */ + +/** + * Use a double array as, well, double array in the ArrayAdapter API. + * + * @author Erich Schubert + * + * @apiviz.exclude + */ +class DoubleArrayAdapter implements NumberArrayAdapter<Double, double[]> { + /** + * Constructor. + * + * Use the static instance from {@link ArrayLikeUtil}! + */ + protected DoubleArrayAdapter() { + super(); + } + + @Override + public int size(double[] array) { + return array.length; + } + + @Override + public Double get(double[] array, int off) throws IndexOutOfBoundsException { + return array[off]; + } + + @Override + public double getDouble(double[] array, int off) throws IndexOutOfBoundsException { + return array[off]; + } + + @Override + public float getFloat(double[] array, int off) throws IndexOutOfBoundsException { + return (float) array[off]; + } + + @Override + public int getInteger(double[] array, int off) throws IndexOutOfBoundsException { + return (int) array[off]; + } + + @Override + public short getShort(double[] array, int off) throws IndexOutOfBoundsException { + return (short) array[off]; + } + + @Override + public long getLong(double[] array, int off) throws IndexOutOfBoundsException { + return (long) array[off]; + } + + @Override + public byte getByte(double[] array, int off) throws IndexOutOfBoundsException { + return (byte) array[off]; + } +}
\ No newline at end of file diff --git a/src/de/lmu/ifi/dbs/elki/utilities/datastructures/arraylike/ExtendedArray.java b/src/de/lmu/ifi/dbs/elki/utilities/datastructures/arraylike/ExtendedArray.java new file mode 100644 index 00000000..a195360b --- /dev/null +++ b/src/de/lmu/ifi/dbs/elki/utilities/datastructures/arraylike/ExtendedArray.java @@ -0,0 +1,96 @@ +package de.lmu.ifi.dbs.elki.utilities.datastructures.arraylike; + +/* + This file is part of ELKI: + Environment for Developing KDD-Applications Supported by Index-Structures + + Copyright (C) 2012 + 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 extend an array with a single element virtually. + * + * @author Erich Schubert + * + * @param <T> Object type + */ +public class ExtendedArray<T> implements ArrayAdapter<T, ExtendedArray<T>> { + /** + * The array + */ + final Object array; + + /** + * The array adapter + */ + final ArrayAdapter<T, Object> getter; + + /** + * The extra element + */ + final T extra; + + /** + * Our size + */ + final int size; + + /** + * Constructor. + * + * @param array Original array + * @param getter Adapter for array + * @param extra Extra element + */ + protected ExtendedArray(Object array, ArrayAdapter<T, Object> getter, T extra) { + super(); + this.array = array; + this.getter = getter; + this.extra = extra; + this.size = getter.size(array) + 1; + } + + @Override + public int size(ExtendedArray<T> array) { + assert (array == this); + return size; + } + + @Override + public T get(ExtendedArray<T> array, int off) throws IndexOutOfBoundsException { + assert (array == this); + if(off == size - 1) { + return extra; + } + return getter.get(this.array, off); + } + + /** + * Static wrapper that has a nicer generics signature. + * + * @param array Array to extend + * @param getter Getter for array + * @param extra Extra element + * @return Extended array + */ + @SuppressWarnings("unchecked") + public static <T, A> ExtendedArray<T> extend(A array, ArrayAdapter<T, A> getter, T extra) { + return new ExtendedArray<T>(array, (ArrayAdapter<T, Object>) getter, extra); + } +}
\ No newline at end of file diff --git a/src/de/lmu/ifi/dbs/elki/utilities/datastructures/arraylike/FeatureVectorAdapter.java b/src/de/lmu/ifi/dbs/elki/utilities/datastructures/arraylike/FeatureVectorAdapter.java new file mode 100644 index 00000000..19c2ec19 --- /dev/null +++ b/src/de/lmu/ifi/dbs/elki/utilities/datastructures/arraylike/FeatureVectorAdapter.java @@ -0,0 +1,56 @@ +package de.lmu.ifi.dbs.elki.utilities.datastructures.arraylike; + +import de.lmu.ifi.dbs.elki.data.FeatureVector; + +/* + This file is part of ELKI: + Environment for Developing KDD-Applications Supported by Index-Structures + + Copyright (C) 2012 + 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/>. + */ + +/** + * Adapter to use a feature vector as an array of features. + * + * Use the static instance from {@link ArrayLikeUtil}! + * + * @author Erich Schubert + * + * @param <F> Feature type + */ +public class FeatureVectorAdapter<F> implements ArrayAdapter<F, FeatureVector<?, F>> { + /** + * Constructor. + * + * Use the static instance from {@link ArrayLikeUtil}! + */ + protected FeatureVectorAdapter() { + super(); + } + + @Override + public int size(FeatureVector<?, F> array) { + return array.getDimensionality(); + } + + @Override + public F get(FeatureVector<?, F> array, int off) throws IndexOutOfBoundsException { + return array.getValue(off); + } +}
\ No newline at end of file diff --git a/src/de/lmu/ifi/dbs/elki/utilities/datastructures/arraylike/FloatArrayAdapter.java b/src/de/lmu/ifi/dbs/elki/utilities/datastructures/arraylike/FloatArrayAdapter.java new file mode 100644 index 00000000..14d42dc5 --- /dev/null +++ b/src/de/lmu/ifi/dbs/elki/utilities/datastructures/arraylike/FloatArrayAdapter.java @@ -0,0 +1,82 @@ +package de.lmu.ifi.dbs.elki.utilities.datastructures.arraylike; + +/* + This file is part of ELKI: + Environment for Developing KDD-Applications Supported by Index-Structures + + Copyright (C) 2012 + 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/>. + */ + +/** + * Use a double array as, well, double array in the ArrayAdapter API. + * + * @author Erich Schubert + * + * @apiviz.exclude + */ +class FloatArrayAdapter implements NumberArrayAdapter<Float, float[]> { + /** + * Constructor. + * + * Use the static instance from {@link ArrayLikeUtil}! + */ + protected FloatArrayAdapter() { + super(); + } + + @Override + public int size(float[] array) { + return array.length; + } + + @Override + public Float get(float[] array, int off) throws IndexOutOfBoundsException { + return array[off]; + } + + @Override + public double getDouble(float[] array, int off) throws IndexOutOfBoundsException { + return (double) array[off]; + } + + @Override + public float getFloat(float[] array, int off) throws IndexOutOfBoundsException { + return array[off]; + } + + @Override + public int getInteger(float[] array, int off) throws IndexOutOfBoundsException { + return (int) array[off]; + } + + @Override + public short getShort(float[] array, int off) throws IndexOutOfBoundsException { + return (short) array[off]; + } + + @Override + public long getLong(float[] array, int off) throws IndexOutOfBoundsException { + return (long) array[off]; + } + + @Override + public byte getByte(float[] array, int off) throws IndexOutOfBoundsException { + return (byte) array[off]; + } +}
\ No newline at end of file diff --git a/src/de/lmu/ifi/dbs/elki/utilities/datastructures/arraylike/IdentityArrayAdapter.java b/src/de/lmu/ifi/dbs/elki/utilities/datastructures/arraylike/IdentityArrayAdapter.java new file mode 100644 index 00000000..0c6e03dd --- /dev/null +++ b/src/de/lmu/ifi/dbs/elki/utilities/datastructures/arraylike/IdentityArrayAdapter.java @@ -0,0 +1,55 @@ +package de.lmu.ifi.dbs.elki.utilities.datastructures.arraylike; + +/* + This file is part of ELKI: + Environment for Developing KDD-Applications Supported by Index-Structures + + Copyright (C) 2012 + 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/>. + */ + +/** + * Single-item subset adapter + * + * Use the static instance from {@link ArrayLikeUtil}! + * + * @author Erich Schubert + * + * @param <T> Item type + */ +public class IdentityArrayAdapter<T> implements ArrayAdapter<T, T> { + /** + * Constructor. + * + * Use the static instance from {@link ArrayLikeUtil}! + */ + protected IdentityArrayAdapter() { + super(); + } + + @Override + public int size(T array) { + return 1; + } + + @Override + public T get(T array, int off) throws IndexOutOfBoundsException { + assert (off == 0) : "Invalid get()"; + return array; + } +}
\ No newline at end of file diff --git a/src/de/lmu/ifi/dbs/elki/utilities/datastructures/arraylike/ListArrayAdapter.java b/src/de/lmu/ifi/dbs/elki/utilities/datastructures/arraylike/ListArrayAdapter.java new file mode 100644 index 00000000..37875ab7 --- /dev/null +++ b/src/de/lmu/ifi/dbs/elki/utilities/datastructures/arraylike/ListArrayAdapter.java @@ -0,0 +1,55 @@ +package de.lmu.ifi.dbs.elki.utilities.datastructures.arraylike; + +import java.util.List; +/* + This file is part of ELKI: + Environment for Developing KDD-Applications Supported by Index-Structures + + Copyright (C) 2012 + 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/>. + */ + +/** + * Static adapter class to use a {@link java.util.List} in an array API. + * + * Use the static instance from {@link ArrayLikeUtil}! + * + * @author Erich Schubert + * + * @param <T> Data object type. + */ +public class ListArrayAdapter<T> implements ArrayAdapter<T, List<? extends T>> { + /** + * Constructor. + * + * Use the static instance from {@link ArrayLikeUtil}! + */ + protected ListArrayAdapter() { + super(); + } + + @Override + public int size(List<? extends T> array) { + return array.size(); + } + + @Override + public T get(List<? extends T> array, int off) throws IndexOutOfBoundsException { + return array.get(off); + } +}
\ No newline at end of file diff --git a/src/de/lmu/ifi/dbs/elki/utilities/datastructures/arraylike/NumberArrayAdapter.java b/src/de/lmu/ifi/dbs/elki/utilities/datastructures/arraylike/NumberArrayAdapter.java new file mode 100644 index 00000000..1dc823b1 --- /dev/null +++ b/src/de/lmu/ifi/dbs/elki/utilities/datastructures/arraylike/NumberArrayAdapter.java @@ -0,0 +1,99 @@ +package de.lmu.ifi.dbs.elki.utilities.datastructures.arraylike; +/* + This file is part of ELKI: + Environment for Developing KDD-Applications Supported by Index-Structures + + Copyright (C) 2012 + 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/>. + */ + +/** + * Adapter for arrays of numbers, to avoid boxing. + * + * @author Erich Schubert + * + * @param <N> Number type + * @param <A> Array type + */ +public interface NumberArrayAdapter<N extends Number, A> extends ArrayAdapter<N, A> { + @Override + public int size(A array); + + @Override + public N get(A array, int off) throws IndexOutOfBoundsException; + + /** + * Get the off'th item from the array as double. + * + * @param array Array to get from + * @param off Offset + * @return Item at offset off + * @throws IndexOutOfBoundsException for an invalid index. + */ + public double getDouble(A array, int off) throws IndexOutOfBoundsException; + + /** + * Get the off'th item from the array as float. + * + * @param array Array to get from + * @param off Offset + * @return Item at offset off + * @throws IndexOutOfBoundsException for an invalid index. + */ + public float getFloat(A array, int off) throws IndexOutOfBoundsException; + + /** + * Get the off'th item from the array as integer. + * + * @param array Array to get from + * @param off Offset + * @return Item at offset off + * @throws IndexOutOfBoundsException for an invalid index. + */ + public int getInteger(A array, int off) throws IndexOutOfBoundsException; + + /** + * Get the off'th item from the array as short. + * + * @param array Array to get from + * @param off Offset + * @return Item at offset off + * @throws IndexOutOfBoundsException for an invalid index. + */ + public short getShort(A array, int off) throws IndexOutOfBoundsException; + + /** + * Get the off'th item from the array as long. + * + * @param array Array to get from + * @param off Offset + * @return Item at offset off + * @throws IndexOutOfBoundsException for an invalid index. + */ + public long getLong(A array, int off) throws IndexOutOfBoundsException; + + /** + * Get the off'th item from the array as byte. + * + * @param array Array to get from + * @param off Offset + * @return Item at offset off + * @throws IndexOutOfBoundsException for an invalid index. + */ + public byte getByte(A array, int off) throws IndexOutOfBoundsException; +}
\ No newline at end of file diff --git a/src/de/lmu/ifi/dbs/elki/utilities/datastructures/arraylike/NumberListArrayAdapter.java b/src/de/lmu/ifi/dbs/elki/utilities/datastructures/arraylike/NumberListArrayAdapter.java new file mode 100644 index 00000000..89a4e3d6 --- /dev/null +++ b/src/de/lmu/ifi/dbs/elki/utilities/datastructures/arraylike/NumberListArrayAdapter.java @@ -0,0 +1,87 @@ +package de.lmu.ifi.dbs.elki.utilities.datastructures.arraylike; + +import java.util.List; + +/* + This file is part of ELKI: + Environment for Developing KDD-Applications Supported by Index-Structures + + Copyright (C) 2012 + 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/>. + */ + +/** + * Static adapter class to use a {@link java.util.List} in an array of number + * API. + * + * Use the static instance from {@link ArrayLikeUtil}! + * + * @author Erich Schubert + * + * @param <T> Data object type. + */ +public class NumberListArrayAdapter<T extends Number> implements NumberArrayAdapter<T, List<? extends T>> { + /** + * Constructor. + * + * Use the static instance from {@link ArrayLikeUtil}! + */ + protected NumberListArrayAdapter() { + super(); + } + + @Override + public int size(List<? extends T> array) { + return array.size(); + } + + @Override + public T get(List<? extends T> array, int off) throws IndexOutOfBoundsException { + return array.get(off); + } + + @Override + public double getDouble(List<? extends T> array, int off) throws IndexOutOfBoundsException { + return array.get(off).doubleValue(); + } + + @Override + public float getFloat(List<? extends T> array, int off) throws IndexOutOfBoundsException { + return array.get(off).floatValue(); + } + + @Override + public int getInteger(List<? extends T> array, int off) throws IndexOutOfBoundsException { + return array.get(off).intValue(); + } + + @Override + public short getShort(List<? extends T> array, int off) throws IndexOutOfBoundsException { + return array.get(off).shortValue(); + } + + @Override + public long getLong(List<? extends T> array, int off) throws IndexOutOfBoundsException { + return array.get(off).longValue(); + } + + @Override + public byte getByte(List<? extends T> array, int off) throws IndexOutOfBoundsException { + return array.get(off).byteValue(); + } +}
\ No newline at end of file diff --git a/src/de/lmu/ifi/dbs/elki/utilities/datastructures/arraylike/NumberVectorAdapter.java b/src/de/lmu/ifi/dbs/elki/utilities/datastructures/arraylike/NumberVectorAdapter.java new file mode 100644 index 00000000..c75acd64 --- /dev/null +++ b/src/de/lmu/ifi/dbs/elki/utilities/datastructures/arraylike/NumberVectorAdapter.java @@ -0,0 +1,86 @@ +package de.lmu.ifi.dbs.elki.utilities.datastructures.arraylike; + +import de.lmu.ifi.dbs.elki.data.NumberVector; + +/* + This file is part of ELKI: + Environment for Developing KDD-Applications Supported by Index-Structures + + Copyright (C) 2012 + 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/>. + */ + +/** + * Adapter to use a feature vector as an array of features. + * + * Use the static instance from {@link ArrayLikeUtil}! + * + * @author Erich Schubert + * + * @param <N> Number type + */ +public class NumberVectorAdapter<N extends Number> implements NumberArrayAdapter<N, NumberVector<?, N>> { + /** + * Constructor. + * + * Use the static instance from {@link ArrayLikeUtil}! + */ + protected NumberVectorAdapter() { + super(); + } + + @Override + public int size(NumberVector<?, N> array) { + return array.getDimensionality(); + } + + @Override + public N get(NumberVector<?, N> array, int off) throws IndexOutOfBoundsException { + return array.getValue(off + 1); + } + + @Override + public double getDouble(NumberVector<?, N> array, int off) throws IndexOutOfBoundsException { + return array.doubleValue(off + 1); + } + + @Override + public float getFloat(NumberVector<?, N> array, int off) throws IndexOutOfBoundsException { + return array.floatValue(off + 1); + } + + @Override + public int getInteger(NumberVector<?, N> array, int off) throws IndexOutOfBoundsException { + return array.intValue(off + 1); + } + + @Override + public short getShort(NumberVector<?, N> array, int off) throws IndexOutOfBoundsException { + return array.shortValue(off + 1); + } + + @Override + public long getLong(NumberVector<?, N> array, int off) throws IndexOutOfBoundsException { + return array.longValue(off + 1); + } + + @Override + public byte getByte(NumberVector<?, N> array, int off) throws IndexOutOfBoundsException { + return array.byteValue(off + 1); + } +}
\ No newline at end of file diff --git a/src/de/lmu/ifi/dbs/elki/utilities/datastructures/arraylike/SingleSubsetArrayAdapter.java b/src/de/lmu/ifi/dbs/elki/utilities/datastructures/arraylike/SingleSubsetArrayAdapter.java new file mode 100644 index 00000000..d7483e4d --- /dev/null +++ b/src/de/lmu/ifi/dbs/elki/utilities/datastructures/arraylike/SingleSubsetArrayAdapter.java @@ -0,0 +1,67 @@ +package de.lmu.ifi.dbs.elki.utilities.datastructures.arraylike; + +/* + This file is part of ELKI: + Environment for Developing KDD-Applications Supported by Index-Structures + + Copyright (C) 2012 + 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/>. + */ + +/** + * Single-item subset adapter + * + * @author Erich Schubert + * + * @param <T> Entry type + * @param <A> Array type + */ +public class SingleSubsetArrayAdapter<T, A> implements ArrayAdapter<T, A> { + /** + * Wrapped adapter + */ + ArrayAdapter<T, ? super A> wrapped; + + /** + * Offset to return + */ + int off; + + /** + * Constructor. + * + * @param wrapped Wrapped adapter + * @param off Offset + */ + public SingleSubsetArrayAdapter(ArrayAdapter<T, ? super A> wrapped, int off) { + super(); + this.wrapped = wrapped; + this.off = off; + } + + @Override + public int size(A array) { + return 1; + } + + @Override + public T get(A array, int off) throws IndexOutOfBoundsException { + assert (off == 0) : "Invalid get()"; + return wrapped.get(array, off); + } +}
\ No newline at end of file diff --git a/src/de/lmu/ifi/dbs/elki/utilities/datastructures/arraylike/SubsetArrayAdapter.java b/src/de/lmu/ifi/dbs/elki/utilities/datastructures/arraylike/SubsetArrayAdapter.java new file mode 100644 index 00000000..b2958358 --- /dev/null +++ b/src/de/lmu/ifi/dbs/elki/utilities/datastructures/arraylike/SubsetArrayAdapter.java @@ -0,0 +1,65 @@ +package de.lmu.ifi.dbs.elki.utilities.datastructures.arraylike; +/* + This file is part of ELKI: + Environment for Developing KDD-Applications Supported by Index-Structures + + Copyright (C) 2012 + 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/>. + */ + +/** + * Subset array adapter (allows reordering and projection) + * + * @author Erich Schubert + * + * @param <T> Entry type + * @param <A> Array type + */ +public class SubsetArrayAdapter<T, A> implements ArrayAdapter<T, A> { + /** + * Wrapped adapter + */ + ArrayAdapter<T, ? super A> wrapped; + + /** + * Offsets to return + */ + int[] offs; + + /** + * Constructor. + * + * @param wrapped Wrapped adapter + * @param offs Offsets + */ + public SubsetArrayAdapter(ArrayAdapter<T, ? super A> wrapped, int[] offs) { + super(); + this.wrapped = wrapped; + this.offs = offs; + } + + @Override + public int size(A array) { + return offs.length; + } + + @Override + public T get(A array, int off) throws IndexOutOfBoundsException { + return wrapped.get(array, offs[off]); + } +}
\ No newline at end of file diff --git a/src/de/lmu/ifi/dbs/elki/utilities/datastructures/arraylike/TDoubleListAdapter.java b/src/de/lmu/ifi/dbs/elki/utilities/datastructures/arraylike/TDoubleListAdapter.java new file mode 100644 index 00000000..889c64e8 --- /dev/null +++ b/src/de/lmu/ifi/dbs/elki/utilities/datastructures/arraylike/TDoubleListAdapter.java @@ -0,0 +1,81 @@ +package de.lmu.ifi.dbs.elki.utilities.datastructures.arraylike; + +import gnu.trove.list.TDoubleList; + +/* + This file is part of ELKI: + Environment for Developing KDD-Applications Supported by Index-Structures + + Copyright (C) 2012 + 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/>. + */ +/** + * Adapter for using Trove TDoubleLists as array-like. + * + * @author Erich Schubert + * + * @apiviz.uses TDoubleList + */ +public class TDoubleListAdapter implements NumberArrayAdapter<Double, TDoubleList> { + /** + * Constructor. + */ + protected TDoubleListAdapter() { + super(); + } + + @Override + public int size(TDoubleList array) { + return array.size(); + } + + @Override + public Double get(TDoubleList array, int off) throws IndexOutOfBoundsException { + return array.get(off); + } + + @Override + public double getDouble(TDoubleList array, int off) throws IndexOutOfBoundsException { + return array.get(off); + } + + @Override + public float getFloat(TDoubleList array, int off) throws IndexOutOfBoundsException { + return (float) array.get(off); + } + + @Override + public int getInteger(TDoubleList array, int off) throws IndexOutOfBoundsException { + return (int) array.get(off); + } + + @Override + public short getShort(TDoubleList array, int off) throws IndexOutOfBoundsException { + return (short) array.get(off); + } + + @Override + public long getLong(TDoubleList array, int off) throws IndexOutOfBoundsException { + return (long) array.get(off); + } + + @Override + public byte getByte(TDoubleList array, int off) throws IndexOutOfBoundsException { + return (byte) array.get(off); + } +}
\ No newline at end of file diff --git a/src/de/lmu/ifi/dbs/elki/utilities/datastructures/arraylike/package-info.java b/src/de/lmu/ifi/dbs/elki/utilities/datastructures/arraylike/package-info.java new file mode 100644 index 00000000..55627df4 --- /dev/null +++ b/src/de/lmu/ifi/dbs/elki/utilities/datastructures/arraylike/package-info.java @@ -0,0 +1,26 @@ +/** + * <p>Common API for accessing objects that are "array-like", including lists, numerical vectors, database vectors and arrays.</p> + */ +/* + This file is part of ELKI: + Environment for Developing KDD-Applications Supported by Index-Structures + + Copyright (C) 2012 + 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.utilities.datastructures.arraylike;
\ No newline at end of file |