int main(){ //pNode root = createNode(88); //appendChildNode(root, createNode(90)); //appendChildNode(root, createNode(80)); //appendChildNode(root, createNode(94)); //appendChildNode(root, createNode(74)); //appendChildNode(root->leftChild, createNode(60)); //appendChildNode(root->leftChild->rightSibling, createNode(55)); //appendChildNode(root->leftChild->rightSibling, createNode(50)); //appendChildNode(root->leftChild->rightSibling->rightSibling->rightSibling, createNode(30)); ////printTree(root, 0); //printNodesAtLevel(root, 0); //destroyNode(root); Node* Root = createNode('A'); Node* B = createNode('B'); Node* C = createNode('C'); Node* D = createNode('D'); Node* E = createNode('E'); Node* F = createNode('F'); Node* G = createNode('G'); Node* H = createNode('H'); Node* I = createNode('I'); Node* J = createNode('J'); Node* K = createNode('K'); appendChildNode(Root, B); appendChildNode(B,C); appendChildNode(B, D); appendChildNode(D, E); appendChildNode(D, F); appendChildNode(Root, G); appendChildNode(G, H); appendChildNode(Root, I); appendChildNode(I, J); appendChildNode(J, K); //printTree(Root, 0); printNodesAtLevel(Root, 2); destroyNode(Root); return 0; }
clientNode * socketArray(int sock, int method, int sockIsIndex) { int i; static clientNode * head = NULL; static int size = 0; static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; clientNode * ptr = NULL; method = method & 7;//only use first 3 bits; pthread_mutex_lock(&mutex); //find method if (method & 1 == 1) { ptr = findNode(head, sock, sockIsIndex); if (ptr != NULL) { pthread_mutex_unlock(&mutex); return ptr; }//END IF }//END IF //create method if ((method & 2) == 2 && sockIsIndex == FALSE) { createNode(&head, sock, &size); if (head->prev == NULL) head->prev = head; pthread_mutex_unlock(&mutex); return head->prev; }//END IF //destroy method if ((method & 4) == 4 && sockIsIndex == FALSE) { //printf("\nPRE-DELETE\n"); //listNodes(head); destroyNode(&head, sock, &size); //printf("POST-DELETE\n"); //listNodes(head); }//END IF pthread_mutex_unlock(&mutex); return NULL; }//END FUNCTION
// If its UB is greater than the current LB, add it to the heap. // Otherwise kill it. void inspectNode(Knapsack* ks, Heap* h, Node* n) { pthread_rwlock_rdlock(&ks->_lblock); if ((int)(n->_upperBound) > ks->_lowerBound) { pthread_rwlock_unlock(&ks->_lblock); pthread_mutex_lock(&ks->_mtx); //while (pthread_mutex_trylock(&ks->_mtx)) usleep(1000); add(h,n); pthread_cond_signal(&ks->_cond); pthread_mutex_unlock(&ks->_mtx); } else { pthread_rwlock_unlock(&ks->_lblock); destroyNode(n); pthread_mutex_lock(&ks->_counterMutex); (ks->_nodesProcessed)++; pthread_mutex_unlock(&ks->_counterMutex); } }
void * List_removePosition(List * list, int index) { ListNode * nodeToRemove; void * data; if(list == NULL) return NULL; if(index < 0 || index >= List_getLength(list)) return NULL; nodeToRemove = detachAtIndex(list, index); if(nodeToRemove == NULL) return NULL; data = nodeToRemove->data; destroyNode(nodeToRemove); return data; }
void destroyList(Node *listHead) { Node *toDestroy; Node *temp; if (listHead != NULL) { temp = listHead; while (temp != NULL) { toDestroy = temp; temp = temp->next; destroyNode (toDestroy); } } }
static void clearText(CBuffer* cb, boolean recycle) { cbnode_t* n, *np; assert(cb); n = cb->head; while(n) { np = n->next; if(!recycle) { destroyNode(n); } else { moveNodeForReuse(cb, n); } n = np; } cb->head = cb->tail = NULL; cb->numLines = 0; }
void kill(void) { fprintf(stderr, "Destroying Client %p\n", focusedNode); if (isClient(focusedNode)) { /* Save closest client and destroy node */ Node *newFocus = getClosestClient(focusedNode); if (focusedNode == viewNode) viewNode = viewNode -> parent; if ( isOnlyChild(focusedNode) && focusedNode -> parent) { viewNode = focusedNode -> parent -> parent ? focusedNode -> parent -> parent : focusedNode -> parent; } Node *oldFocus = focusedNode; focusNode(newFocus, NULL, True, True); destroyNode(oldFocus); rePlaceNode(viewNode); } }
int main() { char comm[50]; int pos; init(); while(scanf("%s",comm) != EOF) { pos = 0; sscanf(comm+1,"%d",&pos); if(comm[0] == '0') { printf("exit"); break; } if(pos == 0) continue; switch(comm[0]) { case 'P': print(pos); fflush(stdin); break; case 'D': del(pos); fflush(stdin); break; case 'I': ins(pos, comm+3); fflush(stdin); break; default : break; } } destroyNode(); return 0; }
void deleteKey(HashTable *table,char *strKey){ //printf("~~~~~~~~~~~~DELETING KEY~~~~~~~~~~\n"); //Node *dnode = findKey(table,strKey); uint32_t k = prehash(strKey); Node *dnode = findNode(table->values[k % table->maxSize],strKey); if(dnode){ //printf("Found a node with : %s : %s \n",dnode->key,dnode->value); if(dnode->previous){ //printf("It had a previous value\n"); if(dnode->next){ // printf("It had a next value\n"); Node *p = dnode->previous; p->next = dnode->next; p->next->previous = p; }else{ // printf("It didn't have a next value\n"); Node *p = dnode->previous; p->next = NULL; } destroyNode(dnode); }else{ //printf("It was the first in the chain\n"); free(dnode->value); free(dnode->key); if(dnode->next){ table->values[k % table->maxSize] = dnode->next; }else{ table->values[k % table->maxSize] = NULL; } } table->count--; if(table->count <= (table->maxSize/4)){ shrink(table); } } }
/* remove and return the first element of the list, if list is empty program is halted with an error message */ NodeData removeFirst (LinkedList * list) { /* make sure list is not empty */ assert (list->head != NULL && list->tail != NULL); /* get data to return */ NodeData retVal = list->head->data; /* find new beginning of list, just a simple follow the link to the next node, so O(1) time */ Node * newHead = list->head->next; /* special case: if list has only 1 element update tail */ if (list->head == list->tail) { list->tail = newHead; } /* destroy the old node & update the head of the list */ destroyNode(list->head); list->head = newHead; return retVal; }
/* remove and return the last element of the list, if list is empty program is halted with an error message */ NodeData removeLast (LinkedList * list) { /* make sure list is not empty */ assert (list->head != NULL && list->tail != NULL); /* grab the data to return */ NodeData retVal = list->tail->data; Node * newEnd; /* special case: when list has only 1 element */ if (list->head == list->tail) { newEnd = NULL; list->head = newEnd; } else { /* find new end of list (the node preceeding tail). Note that this is currently a O(n) algorithm, could be improved to O(1) if Node were doubly-linked instead of singly-linked */ newEnd = list->head; while (newEnd->next != list->tail) newEnd = newEnd->next; newEnd->next = NULL; } /* assert (newEnd == the new end of the list) */ /* assert (retVal == the data to be returned) */ /* free up the node & update the list */ destroyNode (list->tail); list->tail = newEnd; return retVal; }
void processNode(Knapsack* ks, Heap* h, Node* n) { char* x; int i; double est; Item* nextItem = ks->_items[n->_depth+1]; // right node stuff x = (char*)malloc(sizeof(char)*(n->_depth+2)); for (i=0; i<=n->_depth; i++) { x[i] = n->_x[i]; } x[n->_depth+1] = 0; est = upperBound(ks, n->_depth+2, n->_capacity); double rightUB = n->_value + est; Node* rightNode = initNode(n->_value, n->_depth+1, rightUB, n->_capacity, x); //inspectNode(ks, h, rightNode); Node* leftNode = NULL; double leftUB = 0.0; // left node stuff if (nextItem->_weight <= n->_capacity) { x = (char*)malloc(sizeof(char)*(n->_depth+2)); for (i=0; i<=n->_depth; i++) { x[i] = n->_x[i]; } x[n->_depth+1] = 1; int leftValue = n->_value + nextItem->_value; int leftCapacity = n->_capacity - nextItem->_weight; est = upperBound(ks, n->_depth+2, leftCapacity); leftUB = leftValue + est; leftNode = initNode(leftValue, n->_depth+1, leftUB, leftCapacity, x); pthread_rwlock_rdlock(&ks->_lblock); if (leftUB >= (double)ks->_lowerBound) { pthread_rwlock_unlock(&ks->_lblock); inspectNode(ks, h, rightNode); tryProcessNode(ks, h, leftNode); } else { pthread_rwlock_unlock(&ks->_lblock); destroyNode(leftNode); destroyNode(rightNode); pthread_mutex_lock(&ks->_counterMutex); (ks->_nodesProcessed) += 2; pthread_mutex_unlock(&ks->_counterMutex); } } else { pthread_rwlock_rdlock(&ks->_lblock); if (rightUB >= (double)ks->_lowerBound) { pthread_rwlock_unlock(&ks->_lblock); tryProcessNode(ks, h, rightNode); } else { pthread_rwlock_unlock(&ks->_lblock); destroyNode(rightNode); pthread_mutex_lock(&ks->_counterMutex); (ks->_nodesProcessed)++; pthread_mutex_unlock(&ks->_counterMutex); } } destroyNode(n); pthread_mutex_lock(&ks->_counterMutex); (ks->_nodesProcessed)++; pthread_mutex_unlock(&ks->_counterMutex); }
/// virtual destructor virtual ~PionLockedQueue() { clear(); destroyNode(m_tail_ptr); }
void BinaryTree::DestroyTree() { destroyNode(root); }
LIST_FOREACH(list, first, next, cur) { destroyNode(cur->value); if (cur->prev) free(cur->prev); }
insertVal *split(node *n, int leaf){ node *group1, *group2; insertVal *ret; int imax, jmax, i,j; rect *mbr1, *mbr2; imax = jmax = -1; if(insertMethod == 1) selectRect1(n->values, &imax, &jmax); else selectRect2(n->values, &imax, &jmax); group1 = makeNode(leaf); group1->values[group1->size++] = n->values[imax]; group1->MBR = dupRect(group1->values[0]->r); n->values[imax]=NULL; group2 = makeNode(leaf); group2->values[group2->size++] = n->values[jmax]; group2->MBR = dupRect(group2->values[0]->r); n->values[jmax]=NULL; float dif=0, maxdif=0; int idif, count1 = 1, count2 = 1; float delta1, delta2; while(count1 < b+1 && count2 < b+1){ maxdif = 0, idif = 0; for (i=0;i<2*b+1;i++){ if(n->values[i]!=NULL){ delta1 = deltaMBR(group1->MBR,n->values[i]->r); delta2 = deltaMBR(group2->MBR,n->values[i]->r); if(delta1 < delta2) dif = delta2-delta1; else dif = delta1-delta2; if(dif>=maxdif) maxdif = dif, idif=i; } } if(deltaMBR(group1->MBR,n->values[idif]->r)<=deltaMBR(group2->MBR,n->values[idif]->r)){ group1->values[group1->size++] = n->values[idif]; count1++; increaseMBR(group1->MBR, n->values[idif]->r); } else{ group2->values[group2->size++] = n->values[idif]; count2++; increaseMBR(group2->MBR, n->values[idif]->r); } n->values[idif] = NULL; } while(count1 < b){ for (i=0;i<2*b+1;i++){ if(n->values[i]!=NULL){ group1->values[count1++] = n->values[i]; increaseMBR(group1->MBR, n->values[i]->r); n->values[i] = NULL; group1->size++; } } } while(count2 < b){ for (i=0;i<2*b+1;i++){ if(n->values[i]!=NULL){ group2->values[count2++] = n->values[i]; increaseMBR(group2->MBR, n->values[i]->r); n->values[i] = NULL; group2->size++; } } } ret = new(insertVal); ret->node1 = group1; ret->node2 = group2; destroyNode(n); writeNode(group1); writeNode(group2); return ret; }
OGRE3DPointRenderable::~OGRE3DPointRenderable() { destroyNode(mNode); mNode = 0; }
// Replaces two consecutive nodes into a single equivalent node // The extra memory is freed void concatenateNodes(Node * nodeA, Node * nodeB, Graph * graph) { PassageMarkerI marker, tmpMarker; Node *twinA = getTwinNode(nodeA); Node *twinB = getTwinNode(nodeB); Arc *arc; Category cat; // Arc management: // Freeing useless arcs while (getArc(nodeA) != NULL) destroyArc(getArc(nodeA), graph); // Correct arcs for (arc = getArc(nodeB); arc != NULL; arc = getNextArc(arc)) { if (getDestination(arc) != twinB) createAnalogousArc(nodeA, getDestination(arc), arc, graph); else createAnalogousArc(nodeA, twinA, arc, graph); } // Passage marker management in node A: for (marker = getMarker(nodeA); marker != NULL_IDX; marker = getNextInNode(marker)) if (isTerminal(marker)) incrementFinishOffset(marker, getNodeLength(nodeB)); // Swapping new born passageMarkers from B to A for (marker = getMarker(nodeB); marker != NULL_IDX; marker = tmpMarker) { tmpMarker = getNextInNode(marker); if (isInitial(marker) || getNode(getPreviousInSequence(marker)) != nodeA) { extractPassageMarker(marker); transposePassageMarker(marker, nodeA); incrementFinishOffset(getTwinMarker(marker), getNodeLength(nodeA)); } else disconnectNextPassageMarker(getPreviousInSequence (marker), graph); } // Read starts concatenateReadStarts(nodeA, nodeB, graph); // Gaps appendNodeGaps(nodeA, nodeB, graph); // Descriptor management (node) appendDescriptors(nodeA, nodeB); // Update uniqueness: setUniqueness(nodeA, getUniqueness(nodeA) || getUniqueness(nodeB)); // Update virtual coverage for (cat = 0; cat < CATEGORIES; cat++) incrementVirtualCoverage(nodeA, cat, getVirtualCoverage(nodeB, cat)); // Update original virtual coverage for (cat = 0; cat < CATEGORIES; cat++) incrementOriginalVirtualCoverage(nodeA, cat, getOriginalVirtualCoverage (nodeB, cat)); // Freeing gobbled node destroyNode(nodeB, graph); }
void destroyTree(Tree *tree) { destroyNode(tree->root); tree->root = NULL; }
void LinkedList::destroyAll(){ while(Head != NULL){ destroyNode(Tail); } }
/* Remove a node from the linked list * * node The node to remove * * discussion Ensure that last doesn't point to this node! */ inline void removeNode(InDel *node) { node->prev->next = node->next; if(node->next) node->next->prev = node->prev; destroyNode(node); }
// The function front scans the frontier of nodePtr. It returns the keys // of the leaves found in the frontier of nodePtr in a SListPure. // These keys include keys of direction indicators detected in the frontier. // // CAREFUL: Funktion marks all full nodes for destruction. // Only to be used in connection with replaceRoot. // void EmbedPQTree::front( PQNode<edge,indInfo*,bool>* nodePtr, SListPure<PQBasicKey<edge,indInfo*,bool>*> &keys) { Stack<PQNode<edge,indInfo*,bool>*> S; S.push(nodePtr); while (!S.empty()) { PQNode<edge,indInfo*,bool> *checkNode = S.pop(); if (checkNode->type() == PQNodeRoot::leaf) keys.pushBack((PQBasicKey<edge,indInfo*,bool>*) checkNode->getKey()); else { PQNode<edge,indInfo*,bool>* firstSon = 0; if (checkNode->type() == PQNodeRoot::PNode) { firstSon = checkNode->referenceChild(); } else if (checkNode->type() == PQNodeRoot::QNode) { firstSon = checkNode->getEndmost(RIGHT); // By this, we make sure that we start on the left side // since the left endmost child will be on top of the stack } if (firstSon->status() == INDICATOR) { keys.pushBack((PQBasicKey<edge,indInfo*,bool>*) firstSon->getNodeInfo()); m_pertinentNodes->pushBack(firstSon); destroyNode(firstSon); } else S.push(firstSon); PQNode<edge,indInfo*,bool> *nextSon = firstSon->getNextSib(0); PQNode<edge,indInfo*,bool> *oldSib = firstSon; while (nextSon && nextSon != firstSon) { if (nextSon->status() == INDICATOR) { // Direction indicators point with their left sibling pointer // in the direction of their sequence. If an indicator is scanned // from the opposite direction, coming from its right sibling // the corresponding sequence must be reversed. if (oldSib == nextSon->getSib(LEFT)) //Direction changed nextSon->getNodeInfo()->userStructInfo()->changeDir = true; keys.pushBack((PQBasicKey<edge,indInfo*,bool>*) nextSon->getNodeInfo()); m_pertinentNodes->pushBack(nextSon); } else S.push(nextSon); PQNode<edge,indInfo*,bool> *holdSib = nextSon->getNextSib(oldSib); oldSib = nextSon; nextSon = holdSib; } } } }
static boolean pushNeighbours(Node * node, Node * oppositeNode, Coordinate distance, boolean force_jumps) { Node *candidate; Coordinate oldLength = getNodeLength(node); MiniConnection *localConnect; NodeList *path, *tmp; if ((path = pathIsClear(node, oppositeNode, distance))) { while (path) { candidate = path->node; tmp = path->next; deallocateNodeList(path); path = tmp; /////////////////////////////////////// // Stepping forward to destination // /////////////////////////////////////// if (getUniqueness(candidate)) { concatenateReadStarts(node, candidate, graph); concatenateLongReads(node, candidate, graph); absorbExtension(node, candidate); // Scaffold changes recenterNode(node, oldLength); recenterLocalScaffold(node, oldLength); absorbExtensionInScaffold(node, candidate); // Read coverage #ifndef SINGLE_COV_CAT Category cat; for (cat = 0; cat < CATEGORIES; cat++) { incrementVirtualCoverage(node, cat, getVirtualCoverage(candidate, cat)); incrementOriginalVirtualCoverage(node, cat, getOriginalVirtualCoverage(candidate, cat)); } #else incrementVirtualCoverage(node, getVirtualCoverage(candidate)); #endif if (getNodeStatus(candidate)) { localConnect = &localScaffold[getNodeID (candidate) + nodeCount (graph)]; if (localConnect->frontReference) { destroyConnection (localConnect-> frontReference, getNodeID(node)); localConnect-> frontReference = NULL; } if (localConnect->backReference) { destroyConnection (localConnect-> backReference, -getNodeID(node)); localConnect-> backReference = NULL; } unmarkNode(candidate, localConnect); } if (getNodeStatus(getTwinNode(candidate))) { localConnect = &localScaffold[-getNodeID (candidate) + nodeCount (graph)]; if (localConnect->frontReference) { destroyConnection (localConnect-> frontReference, getNodeID(node)); localConnect-> frontReference = NULL; } if (localConnect->backReference) { destroyConnection (localConnect-> backReference, -getNodeID(node)); localConnect-> backReference = NULL; } unmarkNode(getTwinNode(candidate), localConnect); } destroyNode(candidate, graph); return true; } else { adjustShortReads(node, candidate); adjustLongReads(node, getNodeLength(candidate)); absorbExtension(node, candidate); } } } if (force_jumps && oppositeNode && abs_ID(getNodeID(oppositeNode)) < abs_ID(getNodeID(node))) { distance -= getNodeLength(node) / 2; distance -= getNodeLength(oppositeNode) / 2; if (distance > 10) { adjustShortReadsByLength(node, distance); adjustLongReads(node, distance); appendGap(node, distance, graph); } else { adjustShortReadsByLength(node, 10); adjustLongReads(node, 10); appendGap(node, 10, graph); } concatenateReadStarts(node, oppositeNode, graph); concatenateLongReads(node, oppositeNode, graph); absorbExtension(node, oppositeNode); // Scaffold changes recenterNode(node, oldLength); recenterLocalScaffold(node, oldLength); absorbExtensionInScaffold(node, oppositeNode); // Read coverage #ifndef SINGLE_COV_CAT Category cat; for (cat = 0; cat < CATEGORIES; cat++) incrementVirtualCoverage(node, cat, getVirtualCoverage(oppositeNode, cat)); #else incrementVirtualCoverage(node, getVirtualCoverage(oppositeNode)); #endif if (getNodeStatus(oppositeNode)) { localConnect = &localScaffold[getNodeID(oppositeNode) + nodeCount(graph)]; if (localConnect->frontReference) { destroyConnection(localConnect-> frontReference, getNodeID(node)); localConnect->frontReference = NULL; } if (localConnect->backReference) { destroyConnection(localConnect-> backReference, -getNodeID(node)); localConnect->backReference = NULL; } unmarkNode(oppositeNode, localConnect); } if (getNodeStatus(getTwinNode(oppositeNode))) { localConnect = &localScaffold[-getNodeID(oppositeNode) + nodeCount(graph)]; if (localConnect->frontReference) { destroyConnection(localConnect-> frontReference, getNodeID(node)); localConnect->frontReference = NULL; } if (localConnect->backReference) { destroyConnection(localConnect-> backReference, -getNodeID(node)); localConnect->backReference = NULL; } unmarkNode(getTwinNode(oppositeNode), localConnect); } destroyNode(oppositeNode, graph); } return false; }
static Node *bypass() { Node *bypass = getNode(path); Node *next = NULL; Arc *arc; PassageMarkerI nextMarker; // Remove unwanted arcs while (getArc(bypass) != NULL) destroyArc(getArc(bypass), graph); // Update extensive variables (length + descriptors + passage markers) while (!isTerminal(path)) { nextMarker = getNextInSequence(path); next = getNode(nextMarker); while (next == bypass) { disconnectNextPassageMarker(path, graph); destroyPassageMarker(nextMarker); nextMarker = getNextInSequence(path); next = getNode(nextMarker); } if (next == NULL) return bypass; // Overall node update if (!getUniqueness(next)) { adjustShortReads(bypass, getNextInSequence(path)); appendSequence(bypass, sequences, getNextInSequence(path), graph); } else { concatenateReadStarts(bypass, next, graph); #ifndef SINGLE_COV_CAT Category cat; for (cat = 0; cat < CATEGORIES; cat++) { // Update virtual coverage incrementVirtualCoverage(bypass, cat, getVirtualCoverage(next, cat)); // Update original virtual coverage incrementOriginalVirtualCoverage(bypass, cat, getOriginalVirtualCoverage(next, cat)); } #else incrementVirtualCoverage(bypass, getVirtualCoverage(next)); #endif appendDescriptors(bypass, next); } // Members updateMembers(bypass, next); // Termination if (isTerminal(path) || getUniqueness(next)) break; } // Remove unique groupies from arrival admitGroupies(next, bypass); // Copy destination arcs for (arc = getArc(next); arc != NULL; arc = getNextArc(arc)) { if (getDestination(arc) == next) continue; else if (getDestination(arc) == getTwinNode(next)) createAnalogousArc(bypass, getTwinNode(bypass), arc, graph); else createAnalogousArc(bypass, getDestination(arc), arc, graph); } destroyNode(next, graph); return bypass; }
int main (int argc, char* argv[]) { /* Read from file input */ FILE *fp; Item* temp; Knapsack* ks = (Knapsack*)malloc(sizeof(Knapsack)); char* fName = argv[1]; fp = fopen(fName,"r"); fscanf(fp,"%d", &ks->_nbItems); // We randomly chose nbItems/4 as an initial heap size. // It's something to tweak as we go on Heap* h = initHeap(ks->_nbItems/4); ks->_items = (Item**)malloc(sizeof(Item*)*ks->_nbItems); ks->_lowerBound = 0; // Each row of input has an unused counter variable at the beginning. // We'll use fscanf to dump it into j. int i, j; /* For each of the items, read in their values and create the node */ for (i = 0; i < ks->_nbItems; i++) { ks->_items[i] = (Item*)malloc(sizeof(Item)); fscanf(fp,"%d %d %d", &j, &ks->_items[i]->_value, &ks->_items[i]->_weight); } ks->_bestX = (char*)calloc(ks->_nbItems, sizeof(char)); fscanf(fp,"%d",&ks->_capacity); fclose(fp); /* Sort 'em */ qsort(ks->_items, ks->_nbItems, sizeof(Item*), compare); /* Begin port of bb() */ double rootUB = upperBound(ks, 0, ks->_capacity); // I don't understand why depth is -1 here but I'll go with it Node* n = initNode(0, -1, rootUB, ks->_capacity, NULL); add(h, n); /* Begin port of run() */ int nodesProcessed = 0; while (!isEmpty(h)) { n = removeMax(h); if (n->_depth >= (ks->_nbItems-1)) { if (n->_value > ks->_lowerBound) { printf("tighten LB to %d\n", n->_value); ks->_lowerBound = n->_value; for (i=0; i < n->_depth+1; i++) { ks->_bestX[i] = n->_x[i]; } for (i=n->_depth+1; i<ks->_nbItems; i++) { ks->_bestX[i] = 0; } destroyNode(n); } } else { processNode(ks, h, n); nodesProcessed++; } } printf("Found solution: %d\n", ks->_lowerBound); printf("Best x="); for (i=0; i<ks->_nbItems-1; i++) { printf("%d,",ks->_bestX[i]); } printf("%d\n",ks->_bestX[ks->_nbItems-1]); printf("Nodes processed: %d\n", nodesProcessed); // Fix those pesky memory leaks. destroyKnapsack(ks); destroyHeap(h); return 0; }
// Replaces two consecutive nodes into a single equivalent node // The extra memory is freed void concatenateStringOfNodes(Node * nodeA, Graph * graph) { Node *twinA = getTwinNode(nodeA); Node * nodeB = nodeA; Node * twinB; Node *currentNode, *nextNode; Coordinate totalLength = 0; PassageMarkerI marker, tmpMarker; Arc *arc; Category cat; while (simpleArcCount(nodeB) == 1 && simpleArcCount(getTwinNode (getDestination(getArc(nodeB)))) == 1 && getDestination(getArc(nodeB)) != getTwinNode(nodeB) && getDestination(getArc(nodeB)) != nodeA) { totalLength += getNodeLength(nodeB); nodeB = getDestination(getArc(nodeB)); } twinB = getTwinNode(nodeB); totalLength += getNodeLength(nodeB); reallocateNodeDescriptor(nodeA, totalLength); currentNode = nodeA; while (currentNode != nodeB) { currentNode = getDestination(getArc(currentNode)); // Passage marker management in node A: for (marker = getMarker(nodeA); marker != NULL_IDX; marker = getNextInNode(marker)) if (getNode(getNextInSequence(marker)) != currentNode) incrementFinishOffset(marker, getNodeLength(currentNode)); // Swapping new born passageMarkers from B to A for (marker = getMarker(currentNode); marker != NULL_IDX; marker = tmpMarker) { tmpMarker = getNextInNode(marker); if (isInitial(marker) || getNode(getPreviousInSequence(marker)) != nodeA) { extractPassageMarker(marker); transposePassageMarker(marker, nodeA); incrementFinishOffset(getTwinMarker(marker), getNodeLength(nodeA)); } else disconnectNextPassageMarker(getPreviousInSequence (marker), graph); } // Read starts concatenateReadStarts(nodeA, currentNode, graph); // Gaps appendNodeGaps(nodeA, currentNode, graph); // Update uniqueness: setUniqueness(nodeA, getUniqueness(nodeA) || getUniqueness(currentNode)); // Update virtual coverage for (cat = 0; cat < CATEGORIES; cat++) incrementVirtualCoverage(nodeA, cat, getVirtualCoverage(currentNode, cat)); // Update original virtual coverage for (cat = 0; cat < CATEGORIES; cat++) incrementOriginalVirtualCoverage(nodeA, cat, getOriginalVirtualCoverage (currentNode, cat)); // Descriptor management (node) directlyAppendDescriptors(nodeA, currentNode, totalLength); } // Correct arcs for (arc = getArc(nodeB); arc != NULL; arc = getNextArc(arc)) { if (getDestination(arc) != twinB) createAnalogousArc(nodeA, getDestination(arc), arc, graph); else createAnalogousArc(nodeA, twinA, arc, graph); } // Freeing gobbled nodes currentNode = getTwinNode(nodeB); while (currentNode != getTwinNode(nodeA)) { arc = getArc(currentNode); nextNode = getDestination(arc); destroyNode(currentNode, graph); currentNode = nextNode; } }