//edge test case 1
knnTestCaseData initializeKnnEdgeTestCase1(){
	//edge case 1 data
	SPKDTreeNode kNNedgeTestCase1Tree = NULL;
	SPBPQueue kNNedgeTestCase1Queue = NULL;
	SPPoint kNNedgeTestCase1QueryPoint = NULL;
	SPPoint* kNNedgeTestCase1Points = NULL;
	int kNNedgeTestCase1MaxDim = 1;
	int kNNedgeTestCase1Size = 1;
	SP_KDTREE_SPLIT_METHOD kNNedgeTestCase1SplitMethod 	= INCREMENTAL;
	int kNNedgeTestCase1K = 1;
	knnTestCaseData caseData = NULL;
	double data1[1] = {2}, queryData[1] = {0};
	SPPoint p1;
	SPKDArray kdArr = NULL;
	spCalloc(caseData, knn_test_case_data, 1);

	kNNedgeTestCase1Points = (SPPoint*)calloc(sizeof(SPPoint),kNNedgeTestCase1Size);
	if (kNNedgeTestCase1Points == NULL){
		spLoggerSafePrintError(ERROR_ALLOCATING_MEMORY,__FILE__,__FUNCTION__,__LINE__);
		return NULL;
	}
	caseData->points = kNNedgeTestCase1Points;
	caseData->size = kNNedgeTestCase1Size;

	p1 = spPointCreate(data1,kNNedgeTestCase1MaxDim,1);
	if (p1 == NULL){
		spLoggerSafePrintError(ERROR_ALLOCATING_MEMORY,__FILE__,__FUNCTION__,__LINE__);
		return NULL;
	}
	kNNedgeTestCase1Points[0] = p1;


	kNNedgeTestCase1QueryPoint = spPointCreate(queryData,kNNedgeTestCase1MaxDim,2);
	if (kNNedgeTestCase1QueryPoint == NULL){
		spPointDestroy(p1);
		spLoggerSafePrintError(ERROR_ALLOCATING_MEMORY,__FILE__,__FUNCTION__,__LINE__);
		return NULL;
	}
	caseData->queryPoint = kNNedgeTestCase1QueryPoint;

	kNNedgeTestCase1Queue = spBPQueueCreate(kNNedgeTestCase1K);
	if (kNNedgeTestCase1Queue == NULL){
		spPointDestroy(p1);
		spLoggerSafePrintError(ERROR_ALLOCATING_MEMORY,__FILE__,__FUNCTION__,__LINE__);
		return NULL;
	}
	caseData->queue = kNNedgeTestCase1Queue;


	kdArr = Init(kNNedgeTestCase1Points,kNNedgeTestCase1Size);
	kNNedgeTestCase1Tree = InitKDTree(kdArr, kNNedgeTestCase1SplitMethod);
	spKDArrayDestroy(kdArr);

	caseData->k = kNNedgeTestCase1K;
	caseData->split_method = kNNedgeTestCase1SplitMethod;
	caseData->tree = kNNedgeTestCase1Tree;

	return caseData;
}
void destroyCaseData(SPKDTreeNode tree,SPKDArray kdArr,SPPoint* pointsArray,int size,
		SPBPQueue queue,SPPoint queryPoint){
	if (tree) spKDTreeDestroy(tree, false);
	if (kdArr) spKDArrayDestroy(kdArr);
	if (pointsArray) destroyPointsArray(pointsArray,size);
	if (queue) spBPQueueDestroy(queue);
	if (queryPoint) spPointDestroy(queryPoint);
}
Exemplo n.º 3
0
Arquivo: KDTree.c Projeto: orrbarkat/c
SPKDTreeNode spKDTreeCreateFromArray(SPKDArray kdArr, int prevDim,
					SPConfig config, SP_CONFIG_MSG *msg, int* problem ){
    SPKDTreeNode res = NULL;
    SPKDArray *children;
    if (spKDArrayGetNumOfFeatures(kdArr)==1){
        res = spKDTreeCreateLeaf(spKDArrayGetPoint(kdArr, 0));
        if(!res){
        	spLoggerPrintError(ALLOC_FAIL, __FILE__, __FUNCTION__, __LINE__);
        	spKDArrayDestroy(kdArr, ALL_ROWS);
        	*problem = true;
        	return NULL;
        }
        spKDArrayDestroy(kdArr, ALL_ROWS);
        return res;
    }
    else{			//** KD Array is larger than 1 point **//
        res = spKDTreeCreateNode();
        if(!res){
        	spLoggerPrintError(ALLOC_FAIL, __FILE__, __FUNCTION__, __LINE__);
        	spKDArrayDestroy(kdArr, ALL_ROWS);
        	*problem = true;
        	return NULL;
        }
        res->dim = spKDArrayFindSplitDim(kdArr, prevDim, config, msg);
        children = spKDArraySplit(kdArr, res->dim);
        if(!children){
        	spKDTreeDestroy(res);
        	spKDArrayDestroy(kdArr, ALL_ROWS);
        	*problem = true;
        	return NULL;
        }
        res->val = spKDArrayGetMedianVal(children[LEFT], res->dim);
        spKDArrayDestroy(kdArr, ALL_ROWS);
        res->left = spKDTreeCreateFromArray(children[LEFT],
        		res->dim, config, msg, problem);
        if(!(res->left)){
        	spKDTreeDestroy(res);
        	spKDArrayDestroy(kdArr, ALL_ROWS);
			spKDMultiArrayDestroy(children, true, ALL_ROWS, true, ALL_ROWS);
			*problem = true;
			return NULL;
        }
        res->right  = spKDTreeCreateFromArray(children[RIGHT],
        		res->dim, config, msg, problem);
        if(!(res->right)){
        	spKDTreeDestroy(res);
        	spKDArrayDestroy(kdArr, ALL_ROWS);
			spKDMultiArrayDestroy(children, true, ALL_ROWS, true, ALL_ROWS);
			*problem = true;
			return NULL;
        }
        spKDMultiArrayDestroy(children, false, false, false, false);
        return res;
    }
}
//test case 2
knnTestCaseData initializeKnnTestCase2(){
	//test case 2 data
	SPKDTreeNode kNNtestCase2Tree = NULL;
	SPBPQueue kNNtestCase2Queue = NULL;
	SPPoint kNNtestCase2QueryPoint = NULL;
	SPPoint* kNNtestCase2Points = NULL;
	int kNNtestCase2MaxDim = 2;
	int kNNtestCase2Size = 5;
	SP_KDTREE_SPLIT_METHOD kNNtestCase2SplitMethod = INCREMENTAL;
	int kNNtestCase2K = 3;
	knnTestCaseData caseData = NULL;
	double data1[2] = {1,2},data2[2] = {123,70},data3[2] = {2,7},data4[2] = {9,11},
			data5[2] = {3,4}, queryData[2] = {0,0};
	SPPoint p1,p2,p3,p4,p5;
	SPKDArray kdArr = NULL;

	spCalloc(caseData, knn_test_case_data, 1);

	kNNtestCase2Points = (SPPoint*)calloc(sizeof(SPPoint),kNNtestCase2Size);
	if (kNNtestCase2Points == NULL){
		spLoggerSafePrintError(ERROR_ALLOCATING_MEMORY,__FILE__,__FUNCTION__,__LINE__);
		return NULL;
	}
	caseData->points = kNNtestCase2Points;
	caseData->size = kNNtestCase2Size;

	p1 = spPointCreate(data1,kNNtestCase2MaxDim,1);
	if (p1 == NULL){
		spLoggerSafePrintError(ERROR_ALLOCATING_MEMORY,__FILE__,__FUNCTION__,__LINE__);
		return NULL;
	}
	p2 = spPointCreate(data2,kNNtestCase2MaxDim,2);
	if (p2 == NULL){
		spPointDestroy(p1);
		spLoggerSafePrintError(ERROR_ALLOCATING_MEMORY,__FILE__,__FUNCTION__,__LINE__);
		return NULL;
	}
	p3 = spPointCreate(data3,kNNtestCase2MaxDim,3);
	if (p3 == NULL){
		spPointDestroy(p1);
		spPointDestroy(p2);
		spLoggerSafePrintError(ERROR_ALLOCATING_MEMORY,__FILE__,__FUNCTION__,__LINE__);
		return NULL;
	}
	p4 = spPointCreate(data4,kNNtestCase2MaxDim,4);
	if (p4 == NULL){
		spPointDestroy(p1);
		spPointDestroy(p2);
		spPointDestroy(p3);
		spLoggerSafePrintError(ERROR_ALLOCATING_MEMORY,__FILE__,__FUNCTION__,__LINE__);
		return NULL;
	}
	p5 = spPointCreate(data5,kNNtestCase2MaxDim,5);
	if (p5 == NULL){
		spPointDestroy(p1);
		spPointDestroy(p2);
		spPointDestroy(p3);
		spPointDestroy(p4);
		spLoggerSafePrintError(ERROR_ALLOCATING_MEMORY,__FILE__,__FUNCTION__,__LINE__);
		return NULL;
	}
	kNNtestCase2Points[0] = p1;
	kNNtestCase2Points[1] = p2;
	kNNtestCase2Points[2] = p3;
	kNNtestCase2Points[3] = p4;
	kNNtestCase2Points[4] = p5;

	kNNtestCase2QueryPoint= spPointCreate(queryData,kNNtestCase2MaxDim,6);
	if (kNNtestCase2QueryPoint == NULL){
		spPointDestroy(p1);
		spPointDestroy(p2);
		spPointDestroy(p3);
		spPointDestroy(p4);
		spPointDestroy(p5);
		spLoggerSafePrintError(ERROR_ALLOCATING_MEMORY,__FILE__,__FUNCTION__,__LINE__);
		return NULL;
	}
	caseData->queryPoint = kNNtestCase2QueryPoint;

	kNNtestCase2Queue = spBPQueueCreate(kNNtestCase2K);
	if (kNNtestCase2Queue == NULL){
		spPointDestroy(p1);
		spPointDestroy(p2);
		spPointDestroy(p3);
		spPointDestroy(p4);
		spPointDestroy(p5);
		spLoggerSafePrintError(ERROR_ALLOCATING_MEMORY,__FILE__,__FUNCTION__,__LINE__);
		return NULL;
	}
	caseData->queue = kNNtestCase2Queue;

	kdArr = Init(kNNtestCase2Points,kNNtestCase2Size);
	kNNtestCase2Tree = InitKDTree(kdArr, kNNtestCase2SplitMethod);
	spKDArrayDestroy(kdArr);

	caseData->k = kNNtestCase2K;
	caseData->split_method = kNNtestCase2SplitMethod;
	caseData->tree = kNNtestCase2Tree;

	return caseData;
}
//test case 1
knnTestCaseData initializeKnnTestCase1(){
	//test case 1 data
	SPKDTreeNode kNNtestCase1Tree 	= NULL;
	SPBPQueue kNNtestCase1QueueKNN = NULL;
	SPPoint kNNtestCase1QueryPoint	= NULL;
	SPPoint* kNNtestCase1Points = NULL;
	int kNNtestCase1MaxDim = 3;
	int kNNtestCase1Size = 2;
	SP_KDTREE_SPLIT_METHOD kNNtestCase1SplitMethod = MAX_SPREAD;
	int kNNtestCase1K = 1;
	knnTestCaseData caseData = NULL;
	double data1[3] = {1,2,3}, data2[3] = {5,-7,13}, queryData[3] = {0,0,0};
	SPPoint p1,p2;
	SPKDArray kdArr = NULL;

	spCalloc(caseData, knn_test_case_data, 1);

	kNNtestCase1Points = (SPPoint*)calloc(sizeof(SPPoint),kNNtestCase1Size);
	if (kNNtestCase1Points == NULL){
		spLoggerSafePrintError(ERROR_ALLOCATING_MEMORY,__FILE__,__FUNCTION__,__LINE__);
		return NULL;
	}
	caseData->points = kNNtestCase1Points;
	caseData->size = kNNtestCase1Size;

	p1 = spPointCreate(data1,kNNtestCase1MaxDim,1);
	if (p1 == NULL){
		spLoggerSafePrintError(ERROR_ALLOCATING_MEMORY,__FILE__,__FUNCTION__,__LINE__);
		return NULL;
	}
	p2 = spPointCreate(data2,kNNtestCase1MaxDim,2);
	if (p2 == NULL){
		spPointDestroy(p1);
		spLoggerSafePrintError(ERROR_ALLOCATING_MEMORY,__FILE__,__FUNCTION__,__LINE__);
		return NULL;
	}

	kNNtestCase1Points[0] = p1;
	kNNtestCase1Points[1] = p2;

	kNNtestCase1QueryPoint = spPointCreate(queryData, kNNtestCase1MaxDim, 3);
	if (kNNtestCase1QueryPoint == NULL){
		spPointDestroy(p1);
		spPointDestroy(p2);
		spLoggerSafePrintError(ERROR_ALLOCATING_MEMORY,__FILE__,__FUNCTION__,__LINE__);
		return NULL;
	}
	caseData->queryPoint = kNNtestCase1QueryPoint;


	kNNtestCase1QueueKNN = spBPQueueCreate(kNNtestCase1K);
	if (kNNtestCase1QueueKNN == NULL){
		spPointDestroy(p1);
		spPointDestroy(p2);
		spLoggerSafePrintError(ERROR_ALLOCATING_MEMORY,__FILE__,__FUNCTION__,__LINE__);
		return NULL;
	}
	caseData->queue = kNNtestCase1QueueKNN;

	kdArr = Init(kNNtestCase1Points,kNNtestCase1Size);
	kNNtestCase1Tree = InitKDTree(kdArr, kNNtestCase1SplitMethod);
	spKDArrayDestroy(kdArr);

	caseData->k = kNNtestCase1K;
	caseData->split_method = kNNtestCase1SplitMethod;
	caseData->tree = kNNtestCase1Tree;

	return caseData;
}