static bool bpqEmptyTest() { SPBPQueue source, source2; SPListElement e1; source = NULL; ASSERT_TRUE(spBPQueueIsEmpty(source) == true); // check edge case source = spBPQueueCreate(maxSize); ASSERT_TRUE(spBPQueueIsEmpty(source) == true); // check that new queue is empty // insert a new element and check that not empty e1 = spListElementCreate(1, 1.0); source2 = quickQ(1, e1); ASSERT_TRUE(spBPQueueIsEmpty(source2) == false); // remove the element and check that empty spBPQueueDequeue(source2); ASSERT_TRUE(spBPQueueIsEmpty(source) == true); // free memory spBPQueueDestroy(source); spBPQueueDestroy(source2); spListElementDestroy(e1); return true; }
SP_BPQUEUE_MSG spBPQueueDequeue(SPBPQueue source) { SPListElement first; SP_LIST_MSG actionStatus; spMinimalVerifyArguments(source != NULL, SP_BPQUEUE_INVALID_ARGUMENT); if (spBPQueueIsEmpty(source)) return SP_BPQUEUE_EMPTY; first = spListGetFirst(source->queue); if (first == NULL) return SP_BPQUEUE_EMPTY; // if we have 1 items -> last is first -> we should free its pointer if (spBPQueueSize(source) == 1) { spListElementDestroy(source->maxElement); source->maxElement = NULL; } actionStatus = spListRemoveCurrent(source->queue); if (actionStatus != SP_LIST_SUCCESS) return SP_BPQUEUE_EMPTY; return SP_BPQUEUE_SUCCESS; }
double spBPQueueMaxValue(SPBPQueue source){ assert(source); if (spBPQueueIsEmpty(source)){ return -1; } return spListElementGetValue(spListGetLast(source->list)); }
SPListElement spBPQueuePeekLast(SPBPQueue source){ assert(source); if(spBPQueueIsEmpty(source)){ return NULL; } SPListElement ret = spListGetLast(source->list); if (!ret){ return NULL;} return spListElementCopy(ret); }
SP_BPQUEUE_MSG spBPQueueEnqueue(SPBPQueue source, SPListElement element){ // variables declaration// int i; SPList source_list = source->list; SPListElement elem_to_comp; int list_size = spListGetSize(source_list); // check for bad arguments or allocation problems// if (!source || !element || !source_list){ return SP_BPQUEUE_INVALID_ARGUMENT; } // insertion process// if (spBPQueueIsEmpty(source)){ spListInsertFirst(source_list, element); return SP_BPQUEUE_SUCCESS; } else{ elem_to_comp = spListGetFirst(source_list); /*starts from the bottom and loop for next source list element up until you find the one which is bigger than the inserted one and insert the element before that one. you will stop after you ran out of elements to look for */ for(i=0; i<list_size; i++){ if (spListElementGetValue(element) <= spListElementGetValue(elem_to_comp)){// element is smaller than next elem, insert before current if (spBPQueueIsFull(source)){ if (spListInsertBeforeCurrent(source_list, element) == SP_LIST_OUT_OF_MEMORY){ return SP_BPQUEUE_OUT_OF_MEMORY; } spListGetLast(source_list); spListRemoveCurrent(source_list); return SP_BPQUEUE_SUCCESS; } else{ if (spListInsertBeforeCurrent(source_list, element) == SP_LIST_OUT_OF_MEMORY){ return SP_BPQUEUE_OUT_OF_MEMORY; } return SP_BPQUEUE_SUCCESS; } }else{ elem_to_comp = spListGetNext(source_list); } } } if (!spBPQueueIsFull(source)){//queue isn't full but this item is the biggest if (spListInsertLast(source_list, element)== SP_LIST_OUT_OF_MEMORY){ return SP_BPQUEUE_OUT_OF_MEMORY; } return SP_BPQUEUE_SUCCESS; } return SP_BPQUEUE_SUCCESS; // the element is larger than all others }
//null test bool runKnnNullTests(){ SPBPQueue tempQueue = NULL; SPPoint point = generateRandomPoint(4,1); tempQueue = spBPQueueCreate(4); kNearestNeighbors(NULL,tempQueue, point); ASSERT_TRUE(spBPQueueIsEmpty(tempQueue)); spPointDestroy(point); spBPQueueDestroy(tempQueue); return true; }
SP_BPQUEUE_MSG spBPQueueDequeue(SPBPQueue source) { if (source == NULL) { return SP_BPQUEUE_INVALID_ARGUMENT; } if (spBPQueueIsEmpty(source)) { return SP_BPQUEUE_EMPTY; } spListGetFirst(source->innerList); spListRemoveCurrent(source->innerList); return SP_BPQUEUE_SUCCESS; }
//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; }
SP_BPQUEUE_MSG spBPQueueEnqueue(SPBPQueue source, SPListElement element) { spMinimalVerifyArguments(source != NULL && source->queue != NULL && element != NULL, SP_BPQUEUE_INVALID_ARGUMENT); if (spBPQueueGetMaxSize(source) == 0) return SP_BPQUEUE_FULL; // the list is full and the element is greater than all the current items if (spBPQueueIsFull(source) && spListElementCompare(element, source->maxElement) >= 0) { return SP_BPQUEUE_FULL; } if (spBPQueueIsEmpty(source)) return spBPQueueInsertIfEmpty(source,element); //insert to a non empty queue return spBPQueueInsertNotEmpty(source, element); }
SP_BPQUEUE_MSG spBPQueueDequeue(SPBPQueue source){ // check for bad arguments// if(!source || !source->list){ return SP_BPQUEUE_INVALID_ARGUMENT; } // variables declaration// SPList source_list = source->list; if (spBPQueueIsEmpty(source)){ return SP_BPQUEUE_EMPTY; } else{ spListGetFirst(source_list); spListRemoveCurrent(source_list); return SP_BPQUEUE_SUCCESS; } }
SPListElement spBPQueueLastElement(SPBPQueue source) { if (source == NULL || spBPQueueIsEmpty(source)) { return NULL; } return spListGetLast(source->innerList); }