Search in sources :

Example 21 with DBIDRange

use of de.lmu.ifi.dbs.elki.database.ids.DBIDRange in project elki by elki-project.

the class WeightedQuickUnionRangeDBIDsTest method testBruteForce.

@Test
public void testBruteForce() {
    final Random r = new Random(0L);
    final int size = 100;
    DBIDRange range = DBIDUtil.generateStaticDBIDRange(size);
    UnionFind uf = new WeightedQuickUnionRangeDBIDs(range);
    DBIDArrayIter i1 = range.iter(), i2 = range.iter();
    int[] c = new int[size];
    for (int i = 0; i < size; i++) {
        c[i] = i;
    }
    int numc = size;
    while (numc > 1) {
        // Two randoms, with o1 < o2
        int o2 = r.nextInt(size - 1) + 1, o1 = r.nextInt(o2);
        final int c1 = c[o1], c2 = c[o2];
        final boolean ufc = uf.isConnected(i1.seek(o1), i2.seek(o2));
        assertEquals(c1 == c2, ufc);
        // always
        uf.union(i1, i2);
        if (c1 != c2) {
            for (int j = 0; j < size; j++) {
                if (c[j] == c1) {
                    c[j] = c2;
                }
            }
            --numc;
        }
        assertEquals(numc, uf.getRoots().size());
    }
}
Also used : Random(java.util.Random) DBIDRange(de.lmu.ifi.dbs.elki.database.ids.DBIDRange) DBIDArrayIter(de.lmu.ifi.dbs.elki.database.ids.DBIDArrayIter) Test(org.junit.Test)

Example 22 with DBIDRange

use of de.lmu.ifi.dbs.elki.database.ids.DBIDRange in project elki by elki-project.

the class WeightedQuickUnionStaticDBIDsTest method testTree.

@Test
public void testTree() {
    DBIDRange range = DBIDUtil.generateStaticDBIDRange(8);
    UnionFind uf = new WeightedQuickUnionStaticDBIDs(range);
    DBIDArrayIter i1 = range.iter(), i2 = range.iter();
    assertFalse(uf.isConnected(i1.seek(0), i2.seek(7)));
    uf.union(i1.seek(0), i2.seek(1));
    assertTrue(uf.isConnected(i1.seek(0), i2.seek(1)));
    uf.union(i1.seek(2), i2.seek(3));
    assertFalse(uf.isConnected(i1.seek(0), i2.seek(2)));
    uf.union(i1.seek(0), i2.seek(2));
    assertTrue(uf.isConnected(i1.seek(3), i2.seek(1)));
    uf.union(i1.seek(4), i2.seek(5));
    uf.union(i1.seek(6), i2.seek(7));
    uf.union(i1.seek(4), i2.seek(6));
    assertFalse(uf.isConnected(i1.seek(0), i2.seek(4)));
    uf.union(i1.seek(0), i2.seek(4));
    for (int i = 0; i < 8; i++) {
        for (int j = 0; j < 8; j++) {
            assertTrue(uf.isConnected(i1.seek(i), i2.seek(j)));
        }
    }
}
Also used : DBIDRange(de.lmu.ifi.dbs.elki.database.ids.DBIDRange) DBIDArrayIter(de.lmu.ifi.dbs.elki.database.ids.DBIDArrayIter) Test(org.junit.Test)

Example 23 with DBIDRange

use of de.lmu.ifi.dbs.elki.database.ids.DBIDRange in project elki by elki-project.

the class MemoryDataStoreFactory method makeRecordStorage.

@Override
public WritableRecordStore makeRecordStorage(DBIDs ids, int hints, Class<?>... dataclasses) {
    if (ids instanceof DBIDRange) {
        DBIDRange range = (DBIDRange) ids;
        Object[][] data = new Object[range.size()][dataclasses.length];
        return new ArrayRecordStore(data, range);
    } else {
        return new MapIntegerDBIDRecordStore(ids.size(), dataclasses.length);
    }
}
Also used : DBIDRange(de.lmu.ifi.dbs.elki.database.ids.DBIDRange)

Example 24 with DBIDRange

use of de.lmu.ifi.dbs.elki.database.ids.DBIDRange in project elki by elki-project.

the class TrivialDBIDFactory method generateStaticDBIDRange.

@Override
public DBIDRange generateStaticDBIDRange(int size) {
    final int start = next.getAndAdd(size);
    if (start > next.get()) {
        throw new AbortException("DBID range allocation error - too many objects allocated!");
    }
    DBIDRange alloc = new IntegerDBIDRange(start, size);
    return alloc;
}
Also used : DBIDRange(de.lmu.ifi.dbs.elki.database.ids.DBIDRange) AbortException(de.lmu.ifi.dbs.elki.utilities.exceptions.AbortException)

Aggregations

DBIDRange (de.lmu.ifi.dbs.elki.database.ids.DBIDRange)24 DBIDArrayIter (de.lmu.ifi.dbs.elki.database.ids.DBIDArrayIter)13 FiniteProgress (de.lmu.ifi.dbs.elki.logging.progress.FiniteProgress)8 AbortException (de.lmu.ifi.dbs.elki.utilities.exceptions.AbortException)8 Test (org.junit.Test)8 DBIDIter (de.lmu.ifi.dbs.elki.database.ids.DBIDIter)6 DBIDs (de.lmu.ifi.dbs.elki.database.ids.DBIDs)6 Random (java.util.Random)4 NumberVector (de.lmu.ifi.dbs.elki.data.NumberVector)3 TypeInformation (de.lmu.ifi.dbs.elki.data.type.TypeInformation)3 Database (de.lmu.ifi.dbs.elki.database.Database)3 KNNList (de.lmu.ifi.dbs.elki.database.ids.KNNList)3 MultipleObjectsBundle (de.lmu.ifi.dbs.elki.datasource.bundle.MultipleObjectsBundle)3 MeanVariance (de.lmu.ifi.dbs.elki.math.MeanVariance)3 DoubleVector (de.lmu.ifi.dbs.elki.data.DoubleVector)2 StaticArrayDatabase (de.lmu.ifi.dbs.elki.database.StaticArrayDatabase)2 Relation (de.lmu.ifi.dbs.elki.database.relation.Relation)2 ArrayAdapterDatabaseConnection (de.lmu.ifi.dbs.elki.datasource.ArrayAdapterDatabaseConnection)2 DatabaseConnection (de.lmu.ifi.dbs.elki.datasource.DatabaseConnection)2 OnDiskUpperTriangleMatrix (de.lmu.ifi.dbs.elki.persistent.OnDiskUpperTriangleMatrix)2