static bool bpqGetMaxSizeTest() { SPBPQueue source = NULL; SPListElement e1; ASSERT_TRUE(-1 == spBPQueueGetMaxSize(source)); //check edge case // check that max size is always maxSize source = quickQ(0); ASSERT_TRUE(maxSize == spBPQueueGetMaxSize(source)); // insert a new element and check max size e1 = spListElementCreate(1, 1.0); spBPQueueEnqueue(source, e1); ASSERT_TRUE(maxSize == spBPQueueGetMaxSize(source)); // insert another element and check max size spListElementSetIndex(e1, 2); spListElementSetValue(e1, 2.0); spBPQueueEnqueue(source, e1); ASSERT_TRUE(maxSize == spBPQueueGetMaxSize(source)); // remove an element and check max size spBPQueueDequeue(source); ASSERT_TRUE(maxSize == spBPQueueGetMaxSize(source)); // free memory spBPQueueDestroy(source); spListElementDestroy(e1); return true; }
/** * internal help method to find the k nearest neighbors */ void spKNNSearch(SPPoint queryFeature, const SPKDTreeNode node, SPBPQueue q){ SPListElement element; int index, distance; bool distanceFlag = false; if(!node){ return; } if(node->dim==INVALID){ //** this is a leaf **// index = spPointGetIndex(node->data); distance = spPointL2SquaredDistance(queryFeature, node->data); element = spListElementCreate(index, distance); spBPQueueEnqueue(q, element); spListElementDestroy(element); return; } //** go to the left sub tree **// if(spPointGetAxisCoor(queryFeature, node->dim)<= node->val){ spKNNSearch(queryFeature, node->left, q); distance = pow((spPointGetAxisCoor(queryFeature, node->dim) - node->val),2); distanceFlag = distance < spBPQueueMaxValue(q); if(!spBPQueueIsFull(q) || distanceFlag){ spKNNSearch(queryFeature, node->right, q); } }else{ spKNNSearch(queryFeature, node->right, q); distance = pow((spPointGetAxisCoor(queryFeature, node->dim) - node->val),2); distanceFlag = distance < spBPQueueMaxValue(q); if(!spBPQueueIsFull(q) || distanceFlag){ spKNNSearch(queryFeature, node->left, q); } } return; }
// tester for spBPQueueCopy static bool bpqCopyTest() { SPBPQueue source, source2, copy, copy2; SPListElement e1, e2, e3, e4, epeek; ASSERT_TRUE(spBPQueueCopy(NULL) == NULL); // check edge case source = spBPQueueCreate(10); copy = spBPQueueCopy(source); ASSERT_TRUE(copy != NULL); ASSERT_TRUE(0 == spBPQueueSize(copy)); // e1, e2, e3, e4 CREATE_4_ELEMENTS() spBPQueueEnqueue(source, e1); ASSERT_TRUE(0 == spBPQueueSize(copy)); // ensure the copy is a NEW COPY source2 = quickQ(4, e1, e2, e3, e4); copy2 = spBPQueueCopy(source2); ASSERT_TRUE(4 == spBPQueueSize(copy2)); // check that size of copy is correct // check that all elements copied correctly epeek = spBPQueuePeek(copy2); ASSERT_TRUE(spListElementCompare(e1, epeek) == 0); spBPQueueDequeue(copy2); spListElementDestroy(epeek); // free the element // repeat epeek = spBPQueuePeek(copy2); ASSERT_TRUE(spListElementCompare(e2, epeek) == 0); spBPQueueDequeue(copy2); spListElementDestroy(epeek); epeek = spBPQueuePeek(copy2); ASSERT_TRUE(spListElementCompare(e3, epeek) == 0); spBPQueueDequeue(copy2); spListElementDestroy(epeek); epeek = spBPQueuePeek(copy2); ASSERT_TRUE(spListElementCompare(e4, epeek) == 0); spBPQueueDequeue(copy2); spListElementDestroy(epeek); epeek = spBPQueuePeek(copy2); ASSERT_TRUE(epeek == NULL); // free all remaining memory spListElementDestroy(epeek); spBPQueueDestroy(source); spBPQueueDestroy(source2); spBPQueueDestroy(copy); spBPQueueDestroy(copy2); // e1, e2, e3, e4 DESTROY_4_ELEMENTS() return true; }
void SPKDTreeKNNRecursive(SPKDTreeNode treeNode, SPPoint p, SPBPQueue bpq, SP_KDTREE_MSG* msg) { SPListElement listElement; SPPoint treePoint; bool searchedLeft; double dist; if(bpq == NULL || treeNode == NULL) { *msg = SP_KDTREE_INVALID_ARGUMENT; return; } // If treeNode is a leaf if(treeNode->left == NULL && treeNode->right == NULL) { treePoint = *(treeNode->data); listElement = spListElementCreate(spPointGetIndex(treePoint), spPointL2SquaredDistance(p, treePoint)); spBPQueueEnqueue(bpq, listElement); spListElementDestroy(listElement); *msg = SP_KDTREE_SUCCESS; return; } // Turn to search the tree that would've contain the point p (if it was in the tree) if(spPointGetAxisCoor(p, treeNode->dim) <= treeNode->val) { searchedLeft = true; SPKDTreeKNNRecursive(treeNode->left, p, bpq, msg); if (*msg != SP_KDTREE_SUCCESS) return; } else { searchedLeft = false; SPKDTreeKNNRecursive(treeNode->right, p, bpq, msg); if (*msg != SP_KDTREE_SUCCESS) return; } // dist = |treeNode.val - p[treeNode.dim]| dist = treeNode->val - spPointGetAxisCoor(p, treeNode->dim); if(dist < 0) dist *= -1; //dist *= dist; if(!spBPQueueIsFull(bpq) || dist < spBPQueueMaxValue(bpq)) { if(searchedLeft) SPKDTreeKNNRecursive(treeNode->right, p, bpq, msg); else SPKDTreeKNNRecursive(treeNode->left, p, bpq, msg); } }
// create queue of given size, from given elements static SPBPQueue quickQ(int size, ...) { SPBPQueue source; va_list items; source = spBPQueueCreate(maxSize); va_start(items, size); for (int i = 0; i < size; i++) { spBPQueueEnqueue(source, va_arg(items, SPListElement)); } va_end(items); return source; }
static bool bpqGetSizeTest() { SPBPQueue source = NULL; SPListElement e1, e; ASSERT_TRUE(-1 == spBPQueueSize(source)); //check edge case source = quickQ(0); ASSERT_TRUE(0 == spBPQueueSize(source)); // insert a new element and check size e1 = spListElementCreate(1, 1.0); spBPQueueEnqueue(source, e1); ASSERT_TRUE(1 == spBPQueueSize(source)); // make sure that inserting same element twice works spListElementSetIndex(e1, 2); spListElementSetValue(e1, 2.0); spBPQueueEnqueue(source, e1); ASSERT_TRUE(2 == spBPQueueSize(source)); // remove an element and check size spBPQueueDequeue(source); ASSERT_TRUE(1 == spBPQueueSize(source)); // insert more then maxSize elements and check that size is always less then maxSize for (int i = 0; i < 2 * maxSize; i++) { ASSERT_TRUE(spBPQueueSize(source) <= maxSize); e = spListElementCreate(i, 1.0); spBPQueueEnqueue(source, e); spListElementDestroy(e); } // free memory spBPQueueDestroy(source); spListElementDestroy(e1); return true; }
/** * Given a kd-tree and a point p, the function stores the nearest neighbors of p to bpq * * @param curr - the kd-tree containing the points * @param bpq - the bounded priority queue to store the nearest neighbors in * @param p - the point to find the nearest neighbors to * * does nothing if curr == NULL or bpq == NULL or p == NULL */ void nearestNeighbors(KDTreeNode curr, SPBPQueue bpq, SPPoint p) { SPListElement node; SPPoint q; bool isLeft; double coorDis; if (curr == NULL || bpq == NULL || p == NULL ) return; q = curr->data; /* Add the current point to the BPQ. Note that this is a no-op if the * point is not as good as the points we've seen so far.*/ if (curr->dim == -1) { int index; double dis; index = spPointGetIndex(q); dis = spPointL2SquaredDistance(p, curr->data); node = spListElementCreate(index, dis); spBPQueueEnqueue(bpq, node); spListElementDestroy(node); return; } /* Recursively search the half of the tree that contains the test point. */ if (spPointGetAxisCoor(p, curr->dim) <= curr->val) { nearestNeighbors(curr->left, bpq, p); isLeft = true; } else { nearestNeighbors(curr->right, bpq, p); isLeft = false; } /* If the candidate hypersphere crosses this splitting plane, look on the * other side of the plane by examining the other subtree*/ coorDis = abs(spPointGetAxisCoor(p, curr->dim) - curr->val); if (!spBPQueueIsFull(bpq) || coorDis*coorDis < spBPQueueMaxValue(bpq)) { if (isLeft) nearestNeighbors(curr->right, bpq, p); else nearestNeighbors(curr->left, bpq, p); } }
static bool bpqFullTest() { SPBPQueue source; SPListElement e1; source = NULL; ASSERT_TRUE(spBPQueueIsFull(source) == false); // check edge case source = spBPQueueCreate(maxSize); // insert maxSize element and check that full at the end while (spBPQueueSize(source) < maxSize) { ASSERT_TRUE(spBPQueueIsFull(source) == false); // check that not full in the process e1 = spListElementCreate(1, 1.0); spBPQueueEnqueue(source, e1); spListElementDestroy(e1); } ASSERT_TRUE(spBPQueueIsFull(source) == true); // free memory spBPQueueDestroy(source); return true; }
static bool bpqDequeueTest() { SPBPQueue source; SPListElement e; ASSERT_TRUE(SP_BPQUEUE_INVALID_ARGUMENT == spBPQueueDequeue(NULL)); // check edge case source = spBPQueueCreate(maxSize); ASSERT_TRUE(SP_BPQUEUE_INVALID_ARGUMENT == spBPQueueDequeue(source)); // check edge case // insert maxSize elements, then remove them for (int i = 0; i < maxSize; i++) { e = spListElementCreate(i, (double) i); spBPQueueEnqueue(source, e); spListElementDestroy(e); } for (int i = 0; i < maxSize; i++) { ASSERT_TRUE((int )spBPQueueMinValue(source) == i); // check that the minimum was removed on the last dequeue ASSERT_TRUE(spBPQueueDequeue(source) == SP_BPQUEUE_SUCCESS); // check that dequeue succeeded } // free memory spBPQueueDestroy(source); return true; }
static bool bpqEnqueueTest() { SPBPQueue source, source2; SPListElement e, e1, e2, e3, e4, e5, peek, peekLast; ASSERT_TRUE(SP_BPQUEUE_INVALID_ARGUMENT == spBPQueueEnqueue(NULL, NULL)); // check edge case CREATE_4_ELEMENTS() // e1, e2, e3, e4 e5 = spListElementCreate(5, 4.0); source = quickQ(3, e2, e1, e4); ASSERT_TRUE(SP_BPQUEUE_SUCCESS == spBPQueueEnqueue(source, e3)); // check that enqueue succeeded ASSERT_TRUE(SP_BPQUEUE_SUCCESS == spBPQueueEnqueue(source, e5)); ASSERT_TRUE(5 == spBPQueueSize(source)); // check that enqueue inserts in order peek = spBPQueuePeek(source); peekLast = spBPQueuePeekLast(source); ASSERT_TRUE(spListElementCompare(e1, peek) == 0); // make sure queue sorts by value and then by index ASSERT_TRUE(spListElementCompare(e5, peekLast) == 0); // e1, e2, e3, e4 DESTROY_4_ELEMENTS() spListElementDestroy(e5); // create new queue with maxSize source2 = spBPQueueCreate(maxSize); // insert 2*maxSize elements from lowest to highest value and check that min and max are correct for (int i = 0; i < maxSize; i++) { e = spListElementCreate(i, (double) i); ASSERT_TRUE(SP_BPQUEUE_SUCCESS == spBPQueueEnqueue(source2, e)); spListElementDestroy(e); } for (int i = maxSize; i < 2 * maxSize; i++) { e = spListElementCreate(i, (double) i); ASSERT_TRUE(SP_BPQUEUE_FULL == spBPQueueEnqueue(source2, e)); // check full when inserting more then maxSize elements spListElementDestroy(e); } ASSERT_TRUE(spBPQueueMinValue(source2) == 0.0); // check that all elements with value too high are not in the queue ASSERT_TRUE((int )spBPQueueMaxValue(source2) == maxSize - 1); spBPQueueClear(source2); // insert 2*maxSize elements from highest to lowest value and check that min and max are correct and same as before for (int i = 2 * maxSize - 1; i >= 0; i--) { e = spListElementCreate(i, (double) i); spBPQueueEnqueue(source2, e); spListElementDestroy(e); } // check min value is correct ASSERT_TRUE(spBPQueueMinValue(source2) == 0.0); ASSERT_TRUE((int )spBPQueueMaxValue(source2) == maxSize - 1); // free memory spBPQueueDestroy(source); spBPQueueDestroy(source2); spListElementDestroy(peek); spListElementDestroy(peekLast); return true; }