Skip to content

Commit

Permalink
Make the code more uniform
Browse files Browse the repository at this point in the history
  • Loading branch information
xqgex committed Sep 14, 2016
1 parent 3dec054 commit 7322d0a
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 150 deletions.
57 changes: 31 additions & 26 deletions KDTreeNode.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,34 +139,36 @@ bool recKNNSearch(KDTreeNode kdTree,SPBPQueue bpq,SPPoint feature){
if (kdTree == NULL){ // subtree is NULL
return true;
} else if (kdTree->data != NULL) { // kdTree is a leaf
element = spListElementCreate(spPointGetIndex(kdTree->data),
spPointL2SquaredDistance(kdTree->data,feature));
if (element == NULL){
element = spListElementCreate(spPointGetIndex(kdTree->data),spPointL2SquaredDistance(kdTree->data,feature));
if (element == NULL) {
PRINT_ERROR_LOGGER(MEMORY_ALLOCATION_ERROR,__FILE__,__func__,__LINE__);
}
if (spBPQueueEnqueue(bpq,element) == SP_BPQUEUE_OUT_OF_MEMORY){
if (spBPQueueEnqueue(bpq,element) == SP_BPQUEUE_OUT_OF_MEMORY) {
PRINT_ERROR_LOGGER(MEMORY_ALLOCATION_ERROR,__FILE__,__func__,__LINE__);
}
spListElementDestroy(element);
return true;
} else if (spPointGetAxisCoor(feature,kdTree->dim) <= kdTree->val) {
if(!recKNNSearch(kdTree->left,bpq,feature)) // recursively search the left subtree
if (!recKNNSearch(kdTree->left,bpq,feature)) { // recursively search the left subtree
return false;
}
leftSide = true;
} else {
if(!recKNNSearch(kdTree->right,bpq,feature)) // recursively search the right subtree
if (!recKNNSearch(kdTree->right,bpq,feature)) { // recursively search the right subtree
return false;
}
}
// check if the candidate hypersphere crosses the splitting plane
diff = (kdTree->val)-spPointGetAxisCoor(feature,kdTree->dim);
if(!spBPQueueIsFull(bpq) || (diff*diff) < spBPQueueMaxValue(bpq)){
if(leftSide){
if(!recKNNSearch(kdTree->right,bpq,feature)) // recursively search the right subtree
if (!spBPQueueIsFull(bpq) || (diff*diff) < spBPQueueMaxValue(bpq)) {
if (leftSide) {
if (!recKNNSearch(kdTree->right,bpq,feature)) { // recursively search the right subtree
return false;
}
else{
if(!recKNNSearch(kdTree->left,bpq,feature)) // recursively search the left subtree
}
} else {
if (!recKNNSearch(kdTree->left,bpq,feature)) { // recursively search the left subtree
return false;
}
}
}
return true;
Expand All @@ -180,48 +182,48 @@ int* kNearestNeighborsSearch(SPConfig config, KDTreeNode kdTree, SPPoint feature
int* NNArray;
SPListElement element;
// Function body
if (kdTree == NULL){
if (kdTree == NULL) {
PRINT_ERROR_LOGGER(KDTREE_IS_NULL,__FILE__,__func__,__LINE__);
return NULL;
}
KNN = spConfigGetKNN(config,&config_msg);
if (config_msg != SP_CONFIG_SUCCESS){
if (config_msg != SP_CONFIG_SUCCESS) {
PRINT_ERROR_LOGGER(GET_KNN_FAIL_ERROR,__FILE__,__func__,__LINE__);
return NULL;
}
bpq = spBPQueueCreate(KNN);
if (bpq == NULL){
if (bpq == NULL) {
PRINT_ERROR_LOGGER(MEMORY_ALLOCATION_ERROR,__FILE__,__func__,__LINE__);
return NULL;
}
if(!recKNNSearch(kdTree,bpq,feature)){
if (!recKNNSearch(kdTree,bpq,feature)) {
PRINT_ERROR_LOGGER(MEMORY_ALLOCATION_ERROR,__FILE__,__func__,__LINE__);
spBPQueueDestroy(bpq);
return NULL;
}
NNArray = (int*)malloc(sizeof(int)*KNN);
if (NNArray == NULL){
if (NNArray == NULL) {
PRINT_ERROR_LOGGER(MEMORY_ALLOCATION_ERROR,__FILE__,__func__,__LINE__);
spBPQueueDestroy(bpq);
return NULL;
}
for (i=0;i<KNN;i++){
if (spBPQueueIsEmpty(bpq)){
for (i=0;i<KNN;i++) {
if (spBPQueueIsEmpty(bpq)) {
PRINT_ERROR_LOGGER(BPQ_EMPTY_ERROR,__FILE__,__func__,__LINE__);
free(NNArray);
spBPQueueDestroy(bpq);
return NULL;
}
element = spBPQueuePeek(bpq);
if (element == NULL){
element = spBPQueuePeek(bpq);
if (element == NULL) {
PRINT_ERROR_LOGGER(MEMORY_ALLOCATION_ERROR,__FILE__,__func__,__LINE__);
free(NNArray);
spBPQueueDestroy(bpq);
return NULL;
}
NNArray[i] = spListElementGetIndex(element);
spListElementDestroy(element);
if(spBPQueueDequeue(bpq)!= SP_BPQUEUE_SUCCESS){
if (spBPQueueDequeue(bpq)!= SP_BPQUEUE_SUCCESS) {
free(NNArray);
spBPQueueDestroy(bpq);
PRINT_ERROR_LOGGER(BPQ_EMPTY_ERROR,__FILE__,__func__,__LINE__);
Expand Down Expand Up @@ -251,22 +253,25 @@ int* closestImagesQuery(SPConfig config, KDTreeNode kdTree, SPPoint* queryArray,
return NULL;
}
KNN = spConfigGetKNN(config,&config_msg);
if (config_msg != SP_CONFIG_SUCCESS)
if (config_msg != SP_CONFIG_SUCCESS) {
return NULL;
}
imageHitsArray = (int*)calloc(numOfImages,sizeof(int)); // Feature's hit counter
closestImages = (int*)malloc(sizeof(int)*numOfSimilarImages); // Store the results
if (imageHitsArray == NULL || closestImages == NULL) { // Memory allocation error
if(imageHitsArray)
if(imageHitsArray) {
free(imageHitsArray);
if(closestImages)
}
if(closestImages) {
free(closestImages);
}
PRINT_ERROR_LOGGER(MEMORY_ALLOCATION_ERROR,__FILE__,__func__,__LINE__);
return NULL;
}
// Function code
for (i=0;i<numOfFeat;i++) { // For each feature find the closest features's image index
bestMatches = kNearestNeighborsSearch(config,kdTree,queryArray[i]);
if(!bestMatches){
if(!bestMatches) {
free(imageHitsArray);
free(closestImages);
PRINT_ERROR_LOGGER(MEMORY_ALLOCATION_ERROR,__FILE__,__func__,__LINE__);
Expand Down
42 changes: 25 additions & 17 deletions SPConfig.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,18 +95,18 @@ void SetDefaultConfigValues(SPConfig config) {
*
* @param config - The configuration structure.
*/
bool SetDefaultConfigString(SPConfig config, SP_CONFIG_MSG* msg){
if (config->spPCAFilename == NULL){
config->spPCAFilename = (char*) malloc(strlen("pca.yml")+1);
if (config->spPCAFilename == NULL){
bool SetDefaultConfigString(SPConfig config, SP_CONFIG_MSG* msg) {
if (config->spPCAFilename == NULL) {
config->spPCAFilename = (char*)malloc(strlen("pca.yml")+1);
if (config->spPCAFilename == NULL) {
*msg = SP_CONFIG_ALLOC_FAIL;
return false;
}
strcpy(config->spPCAFilename, "pca.yml");
}
if (config->spLoggerFilename == NULL){
config->spLoggerFilename = (char*) malloc(strlen("stdout")+1);
if (config->spLoggerFilename == NULL){
if (config->spLoggerFilename == NULL) {
config->spLoggerFilename = (char*)malloc(strlen("stdout")+1);
if (config->spLoggerFilename == NULL) {
*msg = SP_CONFIG_ALLOC_FAIL;
return false;
}
Expand Down Expand Up @@ -142,15 +142,15 @@ bool setConfigParameters(const SPConfig config,const char* variableName,const ch
}
if (strcmp(variableName,"spImagesDirectory") == 0) {
config->spImagesDirectory = (char*) malloc(strlen(value)+1);
if (config->spImagesDirectory == NULL){
if (config->spImagesDirectory == NULL) {
*msg = SP_CONFIG_ALLOC_FAIL;
return false;
}
strcpy(config->spImagesDirectory, value);
*nonDefaultParam = true;
} else if (strcmp(variableName,"spImagesPrefix") == 0) {
config->spImagesPrefix = (char*) malloc(strlen(value)+1);
if (config->spImagesPrefix == NULL){
if (config->spImagesPrefix == NULL) {
*msg = SP_CONFIG_ALLOC_FAIL;
return false;
}
Expand All @@ -159,7 +159,7 @@ bool setConfigParameters(const SPConfig config,const char* variableName,const ch
} else if (strcmp(variableName,"spImagesSuffix") == 0) {
if ((strcmp(value,".jpg") || strcmp(value,".png") || strcmp(value,".bmp") || strcmp(value,".gif")) != false) {
config->spImagesSuffix = (char*) malloc(strlen(value)+1);
if (config->spImagesSuffix == NULL){
if (config->spImagesSuffix == NULL) {
*msg = SP_CONFIG_ALLOC_FAIL;
return false;
}
Expand All @@ -186,7 +186,7 @@ bool setConfigParameters(const SPConfig config,const char* variableName,const ch
}
} else if (strcmp(variableName,"spPCAFilename") == 0) {
config->spPCAFilename = (char*) malloc(strlen(value)+1);
if (config->spPCAFilename == NULL){
if (config->spPCAFilename == NULL) {
*msg = SP_CONFIG_ALLOC_FAIL;
return false;
}
Expand Down Expand Up @@ -250,7 +250,7 @@ bool setConfigParameters(const SPConfig config,const char* variableName,const ch
}
} else if (strcmp(variableName,"spLoggerFilename") == 0) {
config->spLoggerFilename = (char*) malloc(strlen(value)+1);
if (config->spLoggerFilename == NULL){
if (config->spLoggerFilename == NULL) {
*msg = SP_CONFIG_ALLOC_FAIL;
return false;
}
Expand Down Expand Up @@ -334,14 +334,22 @@ bool ParseLine(char* line, char* variableName, char* value, SP_CONFIG_MSG* msg)
* If any parameter is NULL pointer (allocation fail or never allocated), then the function ignore it.
*/
void FreeParseConfig(char* buffer, char* line, char* value, char* variableName) {
if (variableName)
if (variableName) {
free(variableName);
if (value)
variableName = NULL;
}
if (value) {
free(value);
if (buffer)
value = NULL;
}
if (buffer) {
free(buffer);
if (line)
buffer = NULL;
}
if (line) {
free(line);
line = NULL;
}
}
/*
* The function remove all the occurrences of a given character from a given string
Expand Down Expand Up @@ -459,7 +467,7 @@ SPConfig spConfigCreate(const char* filename, SP_CONFIG_MSG* msg) {
PRINT(errorMsg);
return NULL;
}
config = (SPConfig) malloc(sizeof(struct sp_config_t));
config = (SPConfig)malloc(sizeof(struct sp_config_t));
if (config == NULL) {
*msg = SP_CONFIG_ALLOC_FAIL;
ConfigErrorMsg(filename,lineNumber,msg,errorMsg);
Expand Down
1 change: 0 additions & 1 deletion SPConfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
/**
* A data-structure which is used for configuring the system.
*/

typedef enum sp_config_msg_t {
SP_CONFIG_MISSING_DIR,
SP_CONFIG_MISSING_PREFIX,
Expand Down
3 changes: 1 addition & 2 deletions SPKDArray.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ struct kd_array_pair_t {
};

//
int spKDArrayCompare(const void *aIn, const void *bIn, const void *thunkIn)
{
int spKDArrayCompare(const void *aIn, const void *bIn, const void *thunkIn) {
const int *a = aIn, *b = bIn;
const double *thunk = thunkIn;
if (thunk[*a] < thunk[*b]) {
Expand Down
2 changes: 1 addition & 1 deletion SPLogger.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ SP_LOGGER_MSG spLoggerCreate(const char* filename, SP_LOGGER_LEVEL level) {
if (logger != NULL) { // Already defined
return SP_LOGGER_DEFINED;
}
logger = (SPLogger) malloc(sizeof(*logger));
logger = (SPLogger)malloc(sizeof(*logger));
if (logger == NULL) { // Allocation failure
return SP_LOGGER_OUT_OF_MEMORY;
}
Expand Down
Loading

0 comments on commit 7322d0a

Please sign in to comment.