Пример #1
0
static bool testElementSetIndex() {
	ASSERT_TRUE(spListElementGetIndex(NULL) == -1);
	ASSERT_TRUE(spListElementSetIndex(NULL,-1) ==SP_ELEMENT_INVALID_ARGUMENT);
	ASSERT_TRUE(spListElementSetIndex(NULL,1) ==SP_ELEMENT_INVALID_ARGUMENT);
	ASSERT_TRUE(spListElementSetIndex(NULL,-1) ==SP_ELEMENT_INVALID_ARGUMENT);
	SPListElement element = spListElementCreate(1, 0.0);
	ASSERT_TRUE(
			spListElementSetIndex(element, -1) == SP_ELEMENT_INVALID_ARGUMENT);
	ASSERT_TRUE(spListElementGetIndex(element) == 1);
	ASSERT_TRUE(spListElementSetIndex(element, 2) == SP_ELEMENT_SUCCESS);
	ASSERT_TRUE(spListElementGetIndex(element) == 2);
	spListElementDestroy(element);
	return true;
}
Пример #2
0
static bool testElementGetIndex() {
	SPListElement element1 = spListElementCreate(1, 0.0);
	SPListElement element2 = spListElementCreate(2, 0.0);
	SPListElement element3 = spListElementCreate(3, 1.0);
	SPListElement element4 = spListElementCreate(4, 0.0);
	ASSERT_TRUE(spListElementGetIndex(element1) == 1);
	ASSERT_TRUE(spListElementGetIndex(element2) == 2);
	ASSERT_TRUE(spListElementGetIndex(element3) == 3);
	ASSERT_TRUE(spListElementGetIndex(element4) == 4);
	ASSERT_TRUE(spListElementGetIndex(NULL) == -1);
	spListElementDestroy(element1);
	spListElementDestroy(element2);
	spListElementDestroy(element3);
	spListElementDestroy(element4);
	return true;
}
Пример #3
0
static bool testElementCopy() {
	ASSERT_TRUE(spListElementCopy(NULL) == NULL);
	SPListElement element = spListElementCreate(1, 0.0);
	SPListElement elementCopy = spListElementCopy(element);
	ASSERT_TRUE(elementCopy!=NULL);
	ASSERT_TRUE(spListElementCompare(element, elementCopy) == 0);
	ASSERT_TRUE(spListElementSetIndex(elementCopy, 2) == SP_ELEMENT_SUCCESS);
	ASSERT_TRUE(spListElementCompare(element, elementCopy) < 0);
	ASSERT_TRUE(spListElementGetIndex(elementCopy) == 2);
	ASSERT_TRUE(spListElementGetIndex(element) == 1);
	ASSERT_TRUE(spListElementSetValue(element, 1.0) == SP_ELEMENT_SUCCESS);
	ASSERT_TRUE(spListElementCompare(element, elementCopy) > 0);
	spListElementDestroy(element);
	spListElementDestroy(elementCopy);
	return true;
}
bool runKnnEdgeTestCase1(knnTestCaseData caseData){
	SPListElement curr_elem;
	bool successFlag;
	if (caseData == NULL){
		spLoggerSafePrintError(COULD_NOT_CREATE_CASE_DATA,
						__FILE__, __FUNCTION__,
								__LINE__);
		FAIL(COULD_NOT_CREATE_CASE_DATA);
		return false;
	}
	if (caseData->tree == NULL){
		spLoggerSafePrintError(COULD_NOT_CREATE_POINTS_ARRAY,
						__FILE__, __FUNCTION__,
								__LINE__);
		FAIL(COULD_NOT_CREATE_POINTS_ARRAY);
		return false;
	}
	if (caseData->points == NULL){
		spLoggerSafePrintError(COULD_NOT_INITIALIZE_TREE,
						__FILE__, __FUNCTION__,
								__LINE__);
		FAIL(COULD_NOT_INITIALIZE_TREE);
		return false;
	}
	if (caseData->queue == NULL){
		spLoggerSafePrintError(COULD_NOT_INITIALIZE_QUEUE,
						__FILE__, __FUNCTION__,
								__LINE__);
		FAIL(COULD_NOT_INITIALIZE_TREE);
		return false;
	}
	if (caseData->queryPoint == NULL){
		spLoggerSafePrintError(COULD_NOT_INITIALIZE_QUERY_POINT,
						__FILE__, __FUNCTION__,
								__LINE__);
		FAIL(COULD_NOT_INITIALIZE_TREE);
		return false;
	}

	successFlag = kNearestNeighbors(caseData->tree, caseData->queue, caseData->queryPoint);

	//general success
	ASSERT_TRUE(successFlag);
	ASSERT_TRUE(verifyKNN(caseData->queue, caseData->k, caseData->points, caseData->queryPoint,caseData->size));


	//elements should be 1
	curr_elem = spBPQueuePeek(caseData->queue);
	ASSERT_TRUE(spListElementGetIndex(curr_elem) == 1);
	spListElementDestroy(curr_elem);

	return true;
}
Пример #5
0
int* SPKDTreeKNN(SPKDTreeNode tree, SPPoint p, int k, SP_KDTREE_MSG* msg)
{
	int i;
	int* res;
	SPBPQueue bpq;
	SPListElement head;

	if(tree == NULL || k <= 0)
	{
		*msg = SP_KDTREE_INVALID_ARGUMENT;
		return NULL;
	}

	bpq = spBPQueueCreate(k);
	if(bpq == NULL)
	{
		*msg = SP_KDTREE_ALLOC_FAIL;
		return NULL;
	}

	res = (int*) malloc(k * sizeof(int));
	if(res == NULL)
	{
		*msg = SP_KDTREE_ALLOC_FAIL;
		spBPQueueDestroy(bpq);
		return NULL;
	}

	// Call SPKDTreeKNNRecursive to fill the bpq with the k nearest neighbors
	SPKDTreeKNNRecursive(tree, p, bpq, msg);
	if (*msg != SP_KDTREE_SUCCESS)
	{
		spBPQueueDestroy(bpq);
		return NULL;
	}

	// Cast the bpq to an array
	for(i = 0; i < k; i++)
	{
		head = spBPQueuePeek(bpq);
		res[i] = spListElementGetIndex(head);
		spListElementDestroy(head);
		spBPQueueDequeue(bpq);
	}

	spBPQueueDestroy(bpq);

	*msg = SP_KDTREE_SUCCESS;
	return res;
}
//general test
bool verifyKNN(SPBPQueue rsltQueue, int k,SPPoint* pointsArray,SPPoint queryPoint, int numOfPoints){
	SPBPQueue workingQueue = NULL;
	SPListElement tempElement = NULL;
	SP_BPQUEUE_MSG msg;
	bool emptyFlag;
	int i, queueSize;
	int* rsltsArray;

	if (rsltQueue == NULL || pointsArray == NULL || queryPoint == NULL)
		return false;

	workingQueue = spBPQueueCopy(rsltQueue);

	ASSERT_TRUE(workingQueue != NULL);


	rsltsArray = getRealRsltsArray(k,pointsArray,queryPoint,numOfPoints);

	if (rsltsArray == NULL){
		spBPQueueDestroy(workingQueue);
		return false;
	}

	queueSize = spBPQueueSize(workingQueue);
	for (i = 0; i< queueSize ; i++){
		tempElement = spBPQueuePeek(workingQueue);

		if (spListElementGetIndex(tempElement) != rsltsArray[i]){
			free(rsltsArray);
			spBPQueueDestroy(workingQueue);
			return false;
		}
		spListElementDestroy(tempElement);

		msg = spBPQueueDequeue(workingQueue);
		if (msg != SP_BPQUEUE_SUCCESS){
			free(rsltsArray);
			spBPQueueDestroy(workingQueue);
			return false;
		}
	}
	emptyFlag = spBPQueueIsEmpty(workingQueue);
	spBPQueueDestroy(workingQueue);
	free(rsltsArray);
	return emptyFlag;
}
Пример #7
0
int* spFindImages(SPPoint* queryFeatures, const int querySize,
		const SPKDTreeNode root, const SPConfig config, SP_CONFIG_MSG *msg){
    int i, knn, numOfSimilarImg, numOfImages;
    int *imageCounter, *res;
    SPListElement element;
    knn = spConfigGetKNN(config, msg);

    SPBPQueue q = spBPQueueCreate(knn);
    if(!q){
        spLoggerPrintError(ALLOC_FAIL, __FILE__, __FUNCTION__, __LINE__);
        return NULL;
    }
    numOfImages = spConfigGetNumOfImages(config, msg);
    numOfSimilarImg = spConfigGetNumOfSimilarImages(config, msg);
    imageCounter = (int*)calloc(numOfImages, sizeof(int));
    if(!imageCounter){
        spLoggerPrintError(ALLOC_FAIL, __FILE__, __FUNCTION__, __LINE__);
        spBPQueueDestroy(q);
        return NULL;
    }
    /* count the num of neighbours of every
     * image for all features of the query */
    for(i=0; i<querySize; i++){
        spBPQueueClear(q);
        spKNNSearch(queryFeatures[i], root, q);
        while((element = spBPQueuePeek(q))!=NULL){
            imageCounter[spListElementGetIndex(element)]+=1;
            spListElementDestroy(element);
            spBPQueueDequeue(q);
        }
    }
    spBPQueueClear(q);
    spBPQueueDestroy(q);
    res = getTopImagesFromArray(imageCounter, numOfImages, numOfSimilarImg);
    free(imageCounter);
    return res;
}
Пример #8
0
/**
 * Creates a separate copy of a node
 *
 * @param node to be copied
 * @return
 * NULL in case allocation failure occurred
 * Otherwise, the new node is returned
 */
 struct Qnode* copyNode(struct Qnode *n){
     return newNode(spListElementGetIndex(n->element), spListElementGetValue(n->element));
 }
Пример #9
0
SP_BPQUEUE_MSG spBPQueueEnqueue(SPBPQueue source, SPListElement element){
    int i;
    struct Qnode* new_node;
    struct Qnode* n;
    struct Qnode* oldNext;

    if (!source || !element){
        return SP_BPQUEUE_INVALID_ARGUMENT;
    }


    // create new node using the element's data
    new_node = newNode(spListElementGetIndex(element), spListElementGetValue(element));


    if (!new_node){
        return SP_BPQUEUE_OUT_OF_MEMORY;
    }

    n = source->first;

    // if queue is full
    if (source->size >= source->maxSize){
        // compare the new element to the last element
        if (spListElementCompare(new_node->element, source->last->element) == 1){
            spListElementDestroy(new_node->element);
            free(new_node);
            return SP_BPQUEUE_FULL;
        }
        // new element is smaller, therefore destroy current last node
        spListElementDestroy(source->last->element);
        free(source->last);

        // set the new last node pointer to NULL
        for(i=0;i<source->size-2;i++){
            n=n->next;
        }
        n->next=NULL;
    }

    n = source->first;

    // if queue is empty
    if (!source->first){
        source->first = new_node;
        source->last = new_node;
    } else {
        if (spListElementCompare(n->element, new_node->element) == 1){
            // in case new node is smallest
            source->first = new_node;
            new_node->next = n;
        } else {
            while (n->next && spListElementCompare(n->next->element, new_node->element) == -1){
                n = n->next;
            }
            oldNext = n->next;
            n->next = new_node;
            new_node->next = oldNext;
        }
    }

    n = source->first;
    while (n->next){
       n = n->next;
    }
    source->last = n;

    // if queue is not full, increment size
    if (source->size != source->maxSize){
        source->size += 1;
    }

	return SP_BPQUEUE_SUCCESS;
}