Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
tzaeschke committed May 28, 2018
1 parent 37a90be commit 9856f76
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 58 deletions.
24 changes: 9 additions & 15 deletions src/main/java/ch/ethz/globis/phtree/v16/PhQueryKnnHS.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

import ch.ethz.globis.phtree.PhDistance;
import ch.ethz.globis.phtree.PhEntryDist;
import ch.ethz.globis.phtree.PhFilterDistance;
import ch.ethz.globis.phtree.PhTree.PhKnnQuery;
import ch.ethz.globis.phtree.v16.Node.BSTEntry;
import ch.ethz.globis.phtree.v16.bst.BSTIteratorAll;
Expand All @@ -37,7 +36,6 @@ public class PhQueryKnnHS<T> implements PhKnnQuery<T> {
private PhTree16<T> pht;
private PhDistance distance;
private long[] center;
private final PhFilterDistance checker;
private final ArrayList<PhEntryDist<T>> results = new ArrayList<>();
private final ArrayList<PhEntryDist<Object>> pool = new ArrayList<>();
private final PriorityQueue<PhEntryDist<Object>> queue = new PriorityQueue<>(COMP);
Expand All @@ -52,7 +50,6 @@ public class PhQueryKnnHS<T> implements PhKnnQuery<T> {
public PhQueryKnnHS(PhTree16<T> pht) {
this.dims = pht.getDim();
this.pht = pht;
this.checker = new PhFilterDistance();
}

@Override
Expand All @@ -72,7 +69,7 @@ public PhEntryDist<T> nextEntry() {

@Override
public PhEntryDist<T> nextEntryReuse() {
//TODO
//Reusing happens only via pooling
return iterResult.next();
}

Expand Down Expand Up @@ -102,10 +99,8 @@ public PhKnnQuery<T> reset(int nMin, PhDistance dist, long... center) {
}

//Initialize queue
long[] rootKey = new long[dims];
double d = this.distance.dist(rootKey, center);
//TODO use d=0 (lies in Node!!!)
PhEntryDist<Object> rootE = new PhEntryDist<>(rootKey, pht.getRoot(), d);
//use d=0 (lies in Node!!!)
PhEntryDist<Object> rootE = createEntry(new long[dims], pht.getRoot(), 0);
this.queue.add(rootE);

search(nMin);
Expand All @@ -115,18 +110,17 @@ public PhKnnQuery<T> reset(int nMin, PhDistance dist, long... center) {
}


@SuppressWarnings("unchecked")
private void search(int k) {
while (!queue.isEmpty()) {
PhEntryDist<Object> candidate = queue.poll();
Object o = candidate.getValue();
if (!(o instanceof Node)) {
//data entry
//TODO if (checker == null || checker.isValid(candidate.getKey())) {
results.add((PhEntryDist<T>) candidate);
if (results.size() >= k) {
return;
}
// }
results.add((PhEntryDist<T>) candidate);
if (results.size() >= k) {
return;
}
} else {
//inner node
Node node = (Node)o;
Expand All @@ -142,7 +136,7 @@ private void search(int k) {
queue.add(createEntry(e2.getKdKey(), e2.getValue(), d));
}
}
//TODO pool.add(candidate);
pool.add(candidate);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/ch/ethz/globis/phtree/v16/PhQueryKnnHSZ.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public PhKnnQuery<T> reset(int nMin, PhDistance dist, long... center) {
}

//Initialize queue
PhEntryDist<Object> rootE = new PhEntryDist<>(new long[dims], pht.getRoot(), 0);
PhEntryDist<Object> rootE = createEntry(new long[dims], pht.getRoot(), 0);
this.queueLx.add(rootE);

search(nMin);
Expand Down
24 changes: 1 addition & 23 deletions src/main/java/ch/ethz/globis/phtree/v16/PhTree16.java
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ public int getBitDepth() {
}

/**
* Locate nearest neighbours for a given point in space.
* Locate nearest neighbors for a given point in space.
* @param nMin number of values to be returned. More values may or may not be returned when
* several have the same distance.
* @param v center point
Expand Down Expand Up @@ -543,28 +543,6 @@ public void clear() {
void adjustCounts(int deletedPosts) {
nEntries.addAndGet(-deletedPosts);
}


/**
* Best HC incrementer ever.
* @param v
* @param min
* @param max
* @return next valid value or min.
*/
static long inc(long v, long min, long max) {
//first, fill all 'invalid' bits with '1' (bits that can have only one value).
long r = v | (~max);
//increment. The '1's in the invalid bits will cause bitwise overflow to the next valid bit.
r++;
//remove invalid bits.
return (r & max) | min;

//return -1 if we exceed 'max' and cause an overflow or return the original value. The
//latter can happen if there is only one possible value (all filter bits are set).
//The <= is also owed to the bug tested in testBugDecrease()
//return (r <= v) ? -1 : r;
}

}

24 changes: 9 additions & 15 deletions src/main/java/ch/ethz/globis/phtree/v16hd/PhQueryKnnHS.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

import ch.ethz.globis.phtree.PhDistance;
import ch.ethz.globis.phtree.PhEntryDist;
import ch.ethz.globis.phtree.PhFilterDistance;
import ch.ethz.globis.phtree.PhTree.PhKnnQuery;
import ch.ethz.globis.phtree.v16hd.Node.BSTEntry;
import ch.ethz.globis.phtree.v16hd.bst.BSTIteratorAll;
Expand All @@ -37,7 +36,6 @@ public class PhQueryKnnHS<T> implements PhKnnQuery<T> {
private PhTree16HD<T> pht;
private PhDistance distance;
private long[] center;
private final PhFilterDistance checker;
private final ArrayList<PhEntryDist<T>> results = new ArrayList<>();
private final ArrayList<PhEntryDist<Object>> pool = new ArrayList<>();
private final PriorityQueue<PhEntryDist<Object>> queue = new PriorityQueue<>(COMP);
Expand All @@ -52,7 +50,6 @@ public class PhQueryKnnHS<T> implements PhKnnQuery<T> {
public PhQueryKnnHS(PhTree16HD<T> pht) {
this.dims = pht.getDim();
this.pht = pht;
this.checker = new PhFilterDistance();
}

@Override
Expand All @@ -72,7 +69,7 @@ public PhEntryDist<T> nextEntry() {

@Override
public PhEntryDist<T> nextEntryReuse() {
//TODO
//Reusing happens only via pooling
return iterResult.next();
}

Expand Down Expand Up @@ -102,10 +99,8 @@ public PhKnnQuery<T> reset(int nMin, PhDistance dist, long... center) {
}

//Initialize queue
long[] rootKey = new long[dims];
double d = this.distance.dist(rootKey, center);
//TODO use d=0 (lies in Node!!!)
PhEntryDist<Object> rootE = new PhEntryDist<>(rootKey, pht.getRoot(), d);
//use d=0 (lies in Node!!!)
PhEntryDist<Object> rootE = createEntry(new long[dims], pht.getRoot(), 0);
this.queue.add(rootE);

search(nMin);
Expand All @@ -115,18 +110,17 @@ public PhKnnQuery<T> reset(int nMin, PhDistance dist, long... center) {
}


@SuppressWarnings("unchecked")
private void search(int k) {
while (!queue.isEmpty()) {
PhEntryDist<Object> candidate = queue.poll();
Object o = candidate.getValue();
if (!(o instanceof Node)) {
//data entry
//TODO if (checker == null || checker.isValid(candidate.getKey())) {
results.add((PhEntryDist<T>) candidate);
if (results.size() >= k) {
return;
}
// }
results.add((PhEntryDist<T>) candidate);
if (results.size() >= k) {
return;
}
} else {
//inner node
Node node = (Node)o;
Expand All @@ -142,7 +136,7 @@ private void search(int k) {
queue.add(createEntry(e2.getKdKey(), e2.getValue(), d));
}
}
//TODO pool.add(candidate);
pool.add(candidate);
}
}
}
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/ch/ethz/globis/phtree/v16hd/PhQueryKnnHSZ.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public class PhQueryKnnHSZ<T> implements PhKnnQuery<T> {
private final PriorityQueue<PhEntryDist<Object>> queueLx = new PriorityQueue<>(COMP);
private final BSTIteratorAll iterNode = new BSTIteratorAll();
private Iterator<PhEntryDist<T>> iterResult;
//Field, to reduce garbage collection. Gets reset for every loop in the query.
private final long[] relativeQuadrantOfCenter;


/**
Expand All @@ -55,6 +57,7 @@ public class PhQueryKnnHSZ<T> implements PhKnnQuery<T> {
public PhQueryKnnHSZ(PhTree16HD<T> pht) {
this.dims = pht.getDim();
this.pht = pht;
this.relativeQuadrantOfCenter = BitsHD.newArray(dims);
}

@Override
Expand All @@ -74,7 +77,7 @@ public PhEntryDist<T> nextEntry() {

@Override
public PhEntryDist<T> nextEntryReuse() {
//TODO
//Reusing happens only via pooling
return iterResult.next();
}

Expand Down Expand Up @@ -105,7 +108,7 @@ public PhKnnQuery<T> reset(int nMin, PhDistance dist, long... center) {
}

//Initialize queue
PhEntryDist<Object> rootE = new PhEntryDist<>(new long[dims], pht.getRoot(), 0);
PhEntryDist<Object> rootE = createEntry(new long[dims], pht.getRoot(), 0);
this.queueLx.add(rootE);

search(nMin);
Expand All @@ -132,8 +135,6 @@ private void validateLxQueue(int k) {

@SuppressWarnings("unchecked")
private void search(int k) {
//TODO buffer
long[] relativeQuadrantOfCenter = BitsHD.newArray(dims);
while (!queueLx.isEmpty() || !queueEst.isEmpty()) {

//ensure that 1st LX entry is valid
Expand Down

0 comments on commit 9856f76

Please sign in to comment.