if(getLogger().isVerbose()) {
getLogger().verbose("Approximating nearest neighbor lists to database objects");
}
List<E> leaves = index.getLeaves();
FiniteProgress progress = getLogger().isVerbose() ? new FiniteProgress("Processing leaf nodes.", leaves.size(), getLogger()) : null;
for(E leaf : leaves) {
N node = index.getNode(leaf);
int size = node.getNumEntries();
pagesize.put(size);
if(getLogger().isDebuggingFinest()) {
getLogger().debugFinest("NumEntires = " + size);
}
// Collect the ids in this node.
DBID[] ids = new DBID[size];
for(int i = 0; i < size; i++) {
ids[i] = ((LeafEntry) node.getEntry(i)).getDBID();
}
HashMap<DBIDPair, D> cache = new HashMap<DBIDPair, D>(size * size * 3 / 8);
for(DBID id : ids) {
KNNHeap<D> kNN = new KNNHeap<D>(k, distanceQuery.infiniteDistance());
for(DBID id2 : ids) {
DBIDPair key = DBIDUtil.newPair(id, id2);
D d = cache.remove(key);
if(d != null) {
// consume the previous result.
kNN.add(d, id2);
}
else {
// compute new and store the previous result.
d = distanceQuery.distance(id, id2);
kNN.add(d, id2);
// put it into the cache, but with the keys reversed
key = DBIDUtil.newPair(id2, id);
cache.put(key, d);
}
}
ksize.put(kNN.size());
storage.put(id, kNN.toSortedArrayList());
}
if(getLogger().isDebugging()) {
if(cache.size() > 0) {
getLogger().warning("Cache should be empty after each run, but still has " + cache.size() + " elements.");
}
}
if(progress != null) {
progress.incrementProcessed(getLogger());
}
}
if(progress != null) {
progress.ensureCompleted(getLogger());
}
if(getLogger().isVerbose()) {
getLogger().verbose("Average page size = " + pagesize.getMean() + " +- " + pagesize.getSampleStddev());
getLogger().verbose("On average, " + ksize.getMean() + " +- " + ksize.getSampleStddev() + " neighbors returned.");
}