static void _svoGNGInit(svo_gng_t *gng) { const void *is; svo_gng_node_t *n1 = NULL, *n2 = NULL; size_t i; bor_real_t maxbeta; gng->cycle = 1L; // initialize error heap if (gng->err_heap) borPairHeapDel(gng->err_heap); gng->err_heap = borPairHeapNew(errHeapLT, (void *)gng); // precompute beta^n if (gng->beta_n) BOR_FREE(gng->beta_n); gng->beta_n = BOR_ALLOC_ARR(bor_real_t, gng->params.lambda); gng->beta_n[0] = gng->params.beta; for (i = 1; i < gng->params.lambda; i++){ gng->beta_n[i] = gng->beta_n[i - 1] * gng->params.beta; } // precompute beta^(n * lambda) if (gng->beta_lambda_n) BOR_FREE(gng->beta_lambda_n); maxbeta = gng->beta_n[gng->params.lambda - 1]; gng->beta_lambda_n_len = 1000; gng->beta_lambda_n = BOR_ALLOC_ARR(bor_real_t, gng->beta_lambda_n_len); gng->beta_lambda_n[0] = maxbeta; for (i = 1; i < gng->beta_lambda_n_len; i++){ gng->beta_lambda_n[i] = gng->beta_lambda_n[i - 1] * maxbeta; } if (gng->ops.init){ OPS(gng, init)(&n1, &n2, OPS_DATA(gng, init)); }else{ is = OPS(gng, input_signal)(OPS_DATA(gng, input_signal)); n1 = OPS(gng, new_node)(is, OPS_DATA(gng, new_node)); is = OPS(gng, input_signal)(OPS_DATA(gng, input_signal)); n2 = OPS(gng, new_node)(is, OPS_DATA(gng, new_node)); } nodeAdd(gng, n1); nodeAdd(gng, n2); edgeNew(gng, n1, n2); }
int main() { treeNode *root; int i; int x; for (i = 0; i <= 15; ++i) { for (x = 0; x <= 4; x++){ // This is just here to throw in some duplicate values to see how they're handled. nodeAdd(x, &root); } nodeAdd(i, &root); } recursePrint(root); return 0; }
// Adds a value to the tree, creating a root node if necessary void nodeAdd(int leefVal, treeNode **node){ // Here we set up a superpointer so that we can actually point it at more pointers. treeNode *holderNode = NULL; // Will be used for the insertion // If there's not a node where there needs to be, then allocate the memory, // and assign our data appropriately to a temporary node, then swap it in. if ( *node == NULL ){ holderNode = (treeNode*)malloc(sizeof(treeNode)); holderNode->leftChild = NULL; holderNode->rightChild = NULL; holderNode->value = leefVal; *node = holderNode; return; } if ( leefVal > (*node)->value ){ // If it's bigger than our node, check out what's happening on the right node nodeAdd(leefVal, &(*node)->rightChild); } if (leefVal < (*node)->value){ // Otherwise, check out the left node nodeAdd(leefVal, &(*node)->leftChild); } if (leefVal == (*node)->value){ // Oh noes, it's equal :( Now we gotta do some black magic... // So we allocate the appropriate placeholder node holderNode = (treeNode*)malloc(sizeof(treeNode)); holderNode->leftChild = NULL; holderNode->rightChild = (*node)->rightChild; // Here we set the it so that the right-side child of the temporary node points to the same node that is currently being pointed to by the main tree node. It might look like this: // root placeholder // / \ / // / \ / // y z holderNode->value = leefVal; (*node)->rightChild = holderNode; // Here, we implicitly sever the connection between the original two nodes, and connect our placeholder node as the man in the middle. } }
/* * input: * Puzzle is the sudoku * return: * 1 if puzzle is unique and will add to dict * 0 if not */ int end(int puzzle[9][9]) { #if PARR==1 return 1; #endif //Create bare minumum puzzle int* newAddon = malloc(6*sizeof(int)); newAddon[0] = puzzle [3][0]*10000000+puzzle[4][0]*1000000+puzzle[5][0]*100000+puzzle[3][1]*10000+puzzle[4][1]*1000+puzzle[5][1]*100+ puzzle[3][2]*10+puzzle[4][2]; newAddon[1] = puzzle [6][0]*10000000+puzzle[7][0]*1000000+puzzle[8][0]*100000+puzzle[6][1]*10000+puzzle[7][1]*1000+puzzle[8][1]*100+puzzle[6][2]*10+puzzle[7][2]; newAddon[2] = puzzle [0][3]*10000000+puzzle[1][3]*1000000+puzzle[2][3]*100000+puzzle[0][4]*10000+puzzle[1][4]*1000+puzzle[2][4]*100+puzzle[0][5]*10+puzzle[1][5]; newAddon[3] = puzzle [6][3]*10000000+puzzle[7][3]*1000000+puzzle[8][3]*100000+puzzle[6][4]*10000+puzzle[7][4]*1000+puzzle[8][4]*100+puzzle[6][5]*10+puzzle[7][5]; newAddon[4] = puzzle [0][6]*10000000+puzzle[0][7]*1000000+puzzle[0][8]*100000+puzzle[1][7]*10000+puzzle[1][8]*1000+puzzle[1][9]*100+puzzle[2][7]*10+puzzle[2][8]; newAddon[5] = puzzle [3][6]*10000000+puzzle[4][6]*1000000+puzzle[5][6]*100000+puzzle[3][7]*10000+puzzle[4][7]*1000+puzzle[5][7]*100+puzzle[3][8]*10+puzzle[4][8]; if(checkUnique(newAddon))//If puzzle is new { nodeAdd(head,newAddon); return 1; } return 0; }