diff options
Diffstat (limited to 'src/de/lmu/ifi/dbs/elki/database/datastore/memory')
10 files changed, 547 insertions, 17 deletions
diff --git a/src/de/lmu/ifi/dbs/elki/database/datastore/memory/ArrayDoubleStore.java b/src/de/lmu/ifi/dbs/elki/database/datastore/memory/ArrayDoubleStore.java new file mode 100644 index 00000000..433547a5 --- /dev/null +++ b/src/de/lmu/ifi/dbs/elki/database/datastore/memory/ArrayDoubleStore.java @@ -0,0 +1,121 @@ +package de.lmu.ifi.dbs.elki.database.datastore.memory; + +/* + 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.datastore.DataStoreIDMap; +import de.lmu.ifi.dbs.elki.database.datastore.WritableDoubleDataStore; +import de.lmu.ifi.dbs.elki.database.ids.DBID; + +/** + * A class to answer representation queries using the stored Array. + * + * @author Erich Schubert + * + * @apiviz.composedOf de.lmu.ifi.dbs.elki.database.datastore.DataStoreIDMap + */ +public class ArrayDoubleStore implements WritableDoubleDataStore { + /** + * Data array + */ + private double[] data; + + /** + * DBID to index map + */ + private DataStoreIDMap idmap; + + /** + * Constructor. + * + * @param size Size + * @param idmap ID map + */ + public ArrayDoubleStore(int size, DataStoreIDMap idmap) { + super(); + this.data = new double[size]; + this.idmap = idmap; + } + + @Override + @Deprecated + public Double get(DBID id) { + try { + return data[idmap.map(id)]; + } + catch(ArrayIndexOutOfBoundsException e) { + return null; + } + } + + @Override + @Deprecated + public Double put(DBID id, Double value) { + final int off = idmap.map(id); + double ret = data[off]; + data[off] = value; + return ret; + } + + @Override + public double doubleValue(DBID id) { + return data[idmap.map(id)]; + } + + @Override + public double putDouble(DBID id, double value) { + final int off = idmap.map(id); + final double ret = data[off]; + data[off] = value; + return ret; + } + + @Override + public double put(DBID id, double value) { + final int off = idmap.map(id); + final double ret = data[off]; + data[off] = value; + return ret; + } + + @Override + public void destroy() { + data = null; + idmap = null; + } + + @Override + public void delete(DBID id) { + throw new UnsupportedOperationException("Can't delete from a static array storage."); + } + + @Override + public String getLongName() { + return "raw"; + } + + @Override + public String getShortName() { + return "raw"; + } +}
\ No newline at end of file diff --git a/src/de/lmu/ifi/dbs/elki/database/datastore/memory/ArrayRecordStore.java b/src/de/lmu/ifi/dbs/elki/database/datastore/memory/ArrayRecordStore.java index 507fe15b..7be68c97 100644 --- a/src/de/lmu/ifi/dbs/elki/database/datastore/memory/ArrayRecordStore.java +++ b/src/de/lmu/ifi/dbs/elki/database/datastore/memory/ArrayRecordStore.java @@ -4,7 +4,7 @@ package de.lmu.ifi.dbs.elki.database.datastore.memory; This file is part of ELKI: Environment for Developing KDD-Applications Supported by Index-Structures - Copyright (C) 2011 + Copyright (C) 2012 Ludwig-Maximilians-Universität München Lehr- und Forschungseinheit für Datenbanksysteme ELKI Development Team @@ -33,8 +33,8 @@ import de.lmu.ifi.dbs.elki.database.ids.DBID; * * @author Erich Schubert * - * @apiviz.composedOf de.lmu.ifi.dbs.elki.database.datastore.DataStoreIDMap - * @apiviz.has de.lmu.ifi.dbs.elki.database.datastore.memory.ArrayRecordStore.StorageAccessor oneway - - projectsTo + * @apiviz.composedOf DataStoreIDMap + * @apiviz.has ArrayRecordStore.StorageAccessor oneway - - projectsTo */ public class ArrayRecordStore implements WritableRecordStore { /** diff --git a/src/de/lmu/ifi/dbs/elki/database/datastore/memory/ArrayStore.java b/src/de/lmu/ifi/dbs/elki/database/datastore/memory/ArrayStore.java index 96cadddf..a41a444d 100644 --- a/src/de/lmu/ifi/dbs/elki/database/datastore/memory/ArrayStore.java +++ b/src/de/lmu/ifi/dbs/elki/database/datastore/memory/ArrayStore.java @@ -4,7 +4,7 @@ package de.lmu.ifi.dbs.elki.database.datastore.memory; This file is part of ELKI: Environment for Developing KDD-Applications Supported by Index-Structures - Copyright (C) 2011 + Copyright (C) 2012 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/datastore/memory/MapIntegerDBIDDoubleStore.java b/src/de/lmu/ifi/dbs/elki/database/datastore/memory/MapIntegerDBIDDoubleStore.java new file mode 100644 index 00000000..ae06dc00 --- /dev/null +++ b/src/de/lmu/ifi/dbs/elki/database/datastore/memory/MapIntegerDBIDDoubleStore.java @@ -0,0 +1,98 @@ +package de.lmu.ifi.dbs.elki.database.datastore.memory; +/* + 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 gnu.trove.map.TIntDoubleMap; +import gnu.trove.map.hash.TIntDoubleHashMap; +import de.lmu.ifi.dbs.elki.database.datastore.WritableDoubleDataStore; +import de.lmu.ifi.dbs.elki.database.ids.DBID; + +/** + * Writable data store for double values. + * + * @author Erich Schubert + */ +public class MapIntegerDBIDDoubleStore implements WritableDoubleDataStore { + /** + * Data storage + */ + private TIntDoubleMap map; + + /** + * Constructor. + * + * @param size Expected size + */ + public MapIntegerDBIDDoubleStore(int size) { + super(); + map = new TIntDoubleHashMap(size, 0.5f, Integer.MIN_VALUE, Double.NaN); + } + + @Override + @Deprecated + public Double get(DBID id) { + return map.get(id.getIntegerID()); + } + + @Override + public double doubleValue(DBID id) { + return map.get(id.getIntegerID()); + } + + @Override + public String getLongName() { + return "raw"; + } + + @Override + public String getShortName() { + return "raw"; + } + + @Override + @Deprecated + public Double put(DBID id, Double value) { + return map.put(id.getIntegerID(), value); + } + + @Override + public void destroy() { + map.clear(); + map = null; + } + + @Override + public void delete(DBID id) { + map.remove(id.getIntegerID()); + } + + @Override + public double putDouble(DBID id, double value) { + return map.put(id.getIntegerID(), value); + } + + @Override + public double put(DBID id, double value) { + return map.put(id.getIntegerID(), value); + } +} diff --git a/src/de/lmu/ifi/dbs/elki/database/datastore/memory/MapIntegerDBIDRecordStore.java b/src/de/lmu/ifi/dbs/elki/database/datastore/memory/MapIntegerDBIDRecordStore.java new file mode 100644 index 00000000..8272fb2e --- /dev/null +++ b/src/de/lmu/ifi/dbs/elki/database/datastore/memory/MapIntegerDBIDRecordStore.java @@ -0,0 +1,191 @@ +package de.lmu.ifi.dbs.elki.database.datastore.memory; + +/* + 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 gnu.trove.map.TIntObjectMap; +import gnu.trove.map.hash.TIntObjectHashMap; +import de.lmu.ifi.dbs.elki.database.datastore.WritableDataStore; +import de.lmu.ifi.dbs.elki.database.datastore.WritableRecordStore; +import de.lmu.ifi.dbs.elki.database.ids.DBID; + +/** + * A class to answer representation queries using a map and an index within the + * record. + * + * @author Erich Schubert + * + * @apiviz.has MapIntegerDBIDRecordStore.StorageAccessor oneway - - projectsTo + */ +public class MapIntegerDBIDRecordStore implements WritableRecordStore { + /** + * Record length + */ + private final int rlen; + + /** + * Storage Map + */ + private final TIntObjectMap<Object[]> data; + + /** + * Constructor with existing data. + * + * @param rlen Number of columns (record length) + * @param data Existing data map + */ + public MapIntegerDBIDRecordStore(int rlen, TIntObjectMap<Object[]> data) { + super(); + this.rlen = rlen; + this.data = data; + } + + /** + * Constructor without existing data. + * + * @param rlen Number of columns (record length) + */ + public MapIntegerDBIDRecordStore(int rlen) { + this(rlen, new TIntObjectHashMap<Object[]>()); + } + + /** + * Constructor without existing data. + * + * @param size Expected size + * @param rlen Number of columns (record length) + */ + public MapIntegerDBIDRecordStore(int size, int rlen) { + this(rlen, new TIntObjectHashMap<Object[]>(size)); + } + + @Override + public <T> WritableDataStore<T> getStorage(int col, Class<? super T> datatype) { + // TODO: add type checking? + return new StorageAccessor<T>(col); + } + + /** + * Actual getter + * + * @param id Database ID + * @param index column index + * @return current value + */ + @SuppressWarnings("unchecked") + protected <T> T get(DBID id, int index) { + Object[] d = data.get(id.getIntegerID()); + if(d == null) { + return null; + } + try { + return (T) d[index]; + } + catch(ClassCastException e) { + return null; + } + catch(ArrayIndexOutOfBoundsException e) { + return null; + } + } + + /** + * Actual setter + * + * @param id Database ID + * @param index column index + * @param value new value + * @return previous value + */ + @SuppressWarnings("unchecked") + protected <T> T set(DBID id, int index, T value) { + Object[] d = data.get(id.getIntegerID()); + if(d == null) { + d = new Object[rlen]; + data.put(id.getIntegerID(), d); + } + T ret = (T) d[index]; + d[index] = value; + return ret; + } + + /** + * Access a single record in the given data. + * + * @author Erich Schubert + * + * @param <T> Object data type to access + */ + protected class StorageAccessor<T> implements WritableDataStore<T> { + /** + * Representation index. + */ + private final int index; + + /** + * Constructor. + * + * @param index In-record index + */ + protected StorageAccessor(int index) { + super(); + this.index = index; + } + + @SuppressWarnings("unchecked") + @Override + public T get(DBID id) { + return (T) MapIntegerDBIDRecordStore.this.get(id, index); + } + + @Override + public T put(DBID id, T value) { + return MapIntegerDBIDRecordStore.this.set(id, index, value); + } + + @Override + public void destroy() { + throw new UnsupportedOperationException("Record storage accessors cannot be destroyed."); + } + + @Override + public void delete(DBID id) { + throw new UnsupportedOperationException("Record storage values cannot be deleted."); + } + + @Override + public String getLongName() { + return "raw"; + } + + @Override + public String getShortName() { + return "raw"; + } + } + + @Override + public boolean remove(DBID id) { + return data.remove(id.getIntegerID()) != null; + } +}
\ No newline at end of file diff --git a/src/de/lmu/ifi/dbs/elki/database/datastore/memory/MapIntegerDBIDStore.java b/src/de/lmu/ifi/dbs/elki/database/datastore/memory/MapIntegerDBIDStore.java new file mode 100644 index 00000000..4deb929d --- /dev/null +++ b/src/de/lmu/ifi/dbs/elki/database/datastore/memory/MapIntegerDBIDStore.java @@ -0,0 +1,104 @@ +package de.lmu.ifi.dbs.elki.database.datastore.memory; + +/* + 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 gnu.trove.map.TIntObjectMap; +import gnu.trove.map.hash.TIntObjectHashMap; +import de.lmu.ifi.dbs.elki.database.datastore.WritableDataStore; +import de.lmu.ifi.dbs.elki.database.ids.DBID; + +/** + * A class to answer representation queries using a map. Basically, it is just a + * wrapper around a regular map. + * + * @author Erich Schubert + * + * @param <T> Representation object type + */ +public class MapIntegerDBIDStore<T> implements WritableDataStore<T> { + /** + * Storage Map + */ + private TIntObjectMap<T> data; + + /** + * Constructor. + * + * @param data Existing map + */ + public MapIntegerDBIDStore(TIntObjectMap<T> data) { + super(); + this.data = data; + } + + /** + * Constructor. + */ + public MapIntegerDBIDStore() { + super(); + this.data = new TIntObjectHashMap<T>(); + } + + /** + * Constructor. + * + * @param size Expected size + */ + public MapIntegerDBIDStore(int size) { + this.data = new TIntObjectHashMap<T>(size); + } + + @Override + public T get(DBID id) { + return data.get(id.getIntegerID()); + } + + @Override + public T put(DBID id, T value) { + if(value == null) { + return data.remove(id.getIntegerID()); + } + return data.put(id.getIntegerID(), value); + } + + @Override + public void destroy() { + data = null; + } + + @Override + public void delete(DBID id) { + data.remove(id.getIntegerID()); + } + + @Override + public String getLongName() { + return "raw"; + } + + @Override + public String getShortName() { + return "raw"; + } +}
\ No newline at end of file diff --git a/src/de/lmu/ifi/dbs/elki/database/datastore/memory/MapRecordStore.java b/src/de/lmu/ifi/dbs/elki/database/datastore/memory/MapRecordStore.java index 31efc613..5a98966f 100644 --- a/src/de/lmu/ifi/dbs/elki/database/datastore/memory/MapRecordStore.java +++ b/src/de/lmu/ifi/dbs/elki/database/datastore/memory/MapRecordStore.java @@ -4,7 +4,7 @@ package de.lmu.ifi.dbs.elki.database.datastore.memory; This file is part of ELKI: Environment for Developing KDD-Applications Supported by Index-Structures - Copyright (C) 2011 + Copyright (C) 2012 Ludwig-Maximilians-Universität München Lehr- und Forschungseinheit für Datenbanksysteme ELKI Development Team @@ -36,7 +36,7 @@ import de.lmu.ifi.dbs.elki.database.ids.DBID; * * @author Erich Schubert * - * @apiviz.has de.lmu.ifi.dbs.elki.database.datastore.memory.MapRecordStore.StorageAccessor oneway - - projectsTo + * @apiviz.has MapRecordStore.StorageAccessor oneway - - projectsTo */ public class MapRecordStore implements WritableRecordStore { /** diff --git a/src/de/lmu/ifi/dbs/elki/database/datastore/memory/MapStore.java b/src/de/lmu/ifi/dbs/elki/database/datastore/memory/MapStore.java index e8a806ec..27cd9f63 100644 --- a/src/de/lmu/ifi/dbs/elki/database/datastore/memory/MapStore.java +++ b/src/de/lmu/ifi/dbs/elki/database/datastore/memory/MapStore.java @@ -4,7 +4,7 @@ package de.lmu.ifi.dbs.elki.database.datastore.memory; This file is part of ELKI: Environment for Developing KDD-Applications Supported by Index-Structures - Copyright (C) 2011 + Copyright (C) 2012 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/datastore/memory/MemoryDataStoreFactory.java b/src/de/lmu/ifi/dbs/elki/database/datastore/memory/MemoryDataStoreFactory.java index 2c64ee55..3e3ce017 100644 --- a/src/de/lmu/ifi/dbs/elki/database/datastore/memory/MemoryDataStoreFactory.java +++ b/src/de/lmu/ifi/dbs/elki/database/datastore/memory/MemoryDataStoreFactory.java @@ -4,7 +4,7 @@ package de.lmu.ifi.dbs.elki.database.datastore.memory; This file is part of ELKI: Environment for Developing KDD-Applications Supported by Index-Structures - Copyright (C) 2011 + Copyright (C) 2012 Ludwig-Maximilians-Universität München Lehr- und Forschungseinheit für Datenbanksysteme ELKI Development Team @@ -26,14 +26,17 @@ package de.lmu.ifi.dbs.elki.database.datastore.memory; import de.lmu.ifi.dbs.elki.database.datastore.DataStoreFactory; import de.lmu.ifi.dbs.elki.database.datastore.RangeIDMap; import de.lmu.ifi.dbs.elki.database.datastore.WritableDataStore; +import de.lmu.ifi.dbs.elki.database.datastore.WritableDoubleDataStore; import de.lmu.ifi.dbs.elki.database.datastore.WritableRecordStore; import de.lmu.ifi.dbs.elki.database.ids.DBIDRange; import de.lmu.ifi.dbs.elki.database.ids.DBIDs; /** - * Simple factory class that will store all data in memory using object arrays or hashmaps. + * Simple factory class that will store all data in memory using object arrays + * or hashmaps. * - * Hints are currently not used by this implementation, since everything is in-memory. + * Hints are currently not used by this implementation, since everything is + * in-memory. * * @author Erich Schubert * @@ -46,23 +49,36 @@ import de.lmu.ifi.dbs.elki.database.ids.DBIDs; public class MemoryDataStoreFactory implements DataStoreFactory { @Override public <T> WritableDataStore<T> makeStorage(DBIDs ids, int hints, Class<? super T> dataclass) { - if (ids instanceof DBIDRange) { + if(ids instanceof DBIDRange) { DBIDRange range = (DBIDRange) ids; Object[] data = new Object[range.size()]; return new ArrayStore<T>(data, new RangeIDMap(range)); - } else { - return new MapStore<T>(); + } + else { + return new MapIntegerDBIDStore<T>(ids.size()); + } + } + + @Override + public WritableDoubleDataStore makeDoubleStorage(DBIDs ids, int hints) { + if(ids instanceof DBIDRange) { + DBIDRange range = (DBIDRange) ids; + return new ArrayDoubleStore(range.size(), new RangeIDMap(range)); + } + else { + return new MapIntegerDBIDDoubleStore(ids.size()); } } @Override public WritableRecordStore makeRecordStorage(DBIDs ids, int hints, Class<?>... dataclasses) { - if (ids instanceof DBIDRange) { + if(ids instanceof DBIDRange) { DBIDRange range = (DBIDRange) ids; Object[][] data = new Object[range.size()][dataclasses.length]; return new ArrayRecordStore(data, new RangeIDMap(range)); - } else { - return new MapRecordStore(dataclasses.length); + } + else { + return new MapIntegerDBIDRecordStore(ids.size(), dataclasses.length); } } }
\ No newline at end of file diff --git a/src/de/lmu/ifi/dbs/elki/database/datastore/memory/package-info.java b/src/de/lmu/ifi/dbs/elki/database/datastore/memory/package-info.java index c0f41618..6200bb46 100644 --- a/src/de/lmu/ifi/dbs/elki/database/datastore/memory/package-info.java +++ b/src/de/lmu/ifi/dbs/elki/database/datastore/memory/package-info.java @@ -5,7 +5,7 @@ This file is part of ELKI: Environment for Developing KDD-Applications Supported by Index-Structures -Copyright (C) 2011 +Copyright (C) 2012 Ludwig-Maximilians-Universität München Lehr- und Forschungseinheit für Datenbanksysteme ELKI Development Team |