/* find the best swap available and do it */ float FindMaxGpAndSwap() { ModuleRecPtr mrA, mrPrevA, mrB, mrPrevB; ModuleRecPtr maxA, maxPrevA, maxB, maxPrevB; float gp, gpMax; gpMax = -9999999; maxA = maxPrevA = maxB = maxPrevB = NULL; for (mrA = groupA.head, mrPrevA = NULL; mrA != NULL; mrPrevA = mrA, mrA = (*mrA).next) { for (mrB = groupB.head, mrPrevB = NULL; mrB != NULL; mrPrevB = mrB, mrB = (*mrB).next) { #ifdef KS_MODE gp = D[(*mrA).module] + D[(*mrB).module] - CAiBj(mrA, mrB); #else /* !KS_MODE */ gp = D[(*mrA).module] + D[(*mrB).module] - 2*CAiBj(mrA, mrB); #endif /* !KS_MODE */ if (gp > gpMax) { gpMax = gp; maxA = mrA; maxPrevA = mrPrevA; maxB = mrB; maxPrevB = mrPrevB; } } } /* swap the nodes out, into the swap lists */ assert(maxA != NULL); SwapNode(maxPrevA, maxA, &(groupA), &(swapToB)); assert(maxB != NULL); SwapNode(maxPrevB, maxB, &(groupB), &(swapToA)); /* update the inverse mapping, these two node are now gone */ assert(moduleToGroup[(*maxA).module] == GroupA); moduleToGroup[(*maxA).module] = SwappedToB; assert(moduleToGroup[(*maxB).module] == GroupB); moduleToGroup[(*maxB).module] = SwappedToA; #ifndef KS_MODE /* update the Ds */ UpdateDs(maxA, GroupA); UpdateDs(maxB, GroupB); #endif /* !KS_MODE */ return gpMax; }
slist_node* PartitionSList(slist_node* n1, slist_node* n2, COMP pred) { slist_node* pivot = n1; int pdata = pivot->d; slist_node* pBBhead = nullptr, *pBBtail = nullptr; ////Error Prone slist_node* tmp = n1->next; slist_node* psmalltail = nullptr; while (tmp){ /*if ((tmp->d) > pdata){*/ if (pred(tmp->d, pdata)){ if (pBBhead == pBBtail && pBBtail == nullptr){ pBBhead = pBBtail = tmp; } else{ pBBtail = tmp; } } else{ if (pBBhead){ SwapNode(pBBhead, tmp); psmalltail = pBBhead; pBBhead = pBBhead->next; pBBtail = tmp; } else{ psmalltail = tmp; } } tmp = tmp->next; } if (psmalltail){ SwapNode(pivot, psmalltail); }else{ psmalltail = pivot; } return psmalltail;/// return the pivot }
/* and >0 if first greater */ void SortList(struct List *list, int (*cmp)(), char mode) { struct Node *node, *succ; int flag; flag=TRUE; while (flag==TRUE) { flag=FALSE; node=(struct Node *)list->Head.Succ; while ( (node->Succ!=NULL)&&(node->Succ->Succ!=NULL) ) { succ=(struct Node *)node->Succ; if (cmp(node,succ,mode)>0) { SwapNode(node,succ); flag=TRUE; } else node=succ; } } }
BOOL Set<K,ElementTraits>::Delete( Node* pZ ) { if (pZ == NULL) return false; Node* pY = NULL; Node* pX = NULL; if (IsNil(pZ->m_pLeft) || IsNil(pZ->m_pRight)) pY = pZ; else pY = Successor(pZ); if (!IsNil(pY->m_pLeft)) pX = pY->m_pLeft; else pX = pY->m_pRight; pX->m_pParent = pY->m_pParent; if (IsNil(pY->m_pParent)) m_pRoot = pX; else if (pY == pY->m_pParent->m_pLeft) pY->m_pParent->m_pLeft = pX; else pY->m_pParent->m_pRight = pX; if (pY->m_Status == Node::Black) DeleteFixup(pX); if (pY != pZ) SwapNode(pY, pZ); if (m_pRoot != NULL) SetNil(&m_pRoot->m_pParent); delete pZ; return true; }
/* Partition (and thereby QuickSort) is currently not working. The element swapping causes problems. If the Units List can be copied properly it should be fixed. */ struct Node *partition(struct Node **lb,struct Node **rb,int (*cmp)(), char mode) { struct Node *pivot,*i, *j, *temp; int done; /* record if pointers cross (means we're done!) */ /******************************************************** * partition list [lb..rb], and return pointer to pivot * ********************************************************/ /* scan from both ends, swapping when needed */ /* care must be taken not to address outside [lb..rb] with pointers */ i = *lb; j = *rb; /* If there are only two elements to compare make a quick check and put them in the right order */ if(i->Succ==j) { if((int)cmp(i,j,mode)>0) { SwapNode(i,j); *lb=j; *rb=i; } return j; } /* Select a pivot in the middle of lb and rb, by stepping j twice as */ /* fast as i and stopping when j reaches the end. */ j=*lb; while(j!=*rb && j->Succ!=*rb) { j=(struct Node *)j->Succ; j=(struct Node *)j->Succ; i=(struct Node *)i->Succ; } pivot=i; done = 0; i = *lb; j = *rb; while(1) { while ((int)cmp(j, pivot,mode)>0 && j!=pivot) { j = j->Pred; if (i == j) done = 1; } while ((int)cmp(i, pivot,mode)<=0 && i!=pivot) { i = i->Succ; if (i == j) done = 1; } if (done==1) { return pivot; } if(i==pivot) { temp=j->Succ; j->Pred->Succ=j->Succ; j->Succ->Pred=j->Pred; j->Succ=pivot; j->Pred=pivot->Pred; pivot->Pred=j; j->Pred->Succ=j; if(j==*rb) { *rb=temp->Pred; } j=temp; } if(j==pivot) { temp=i->Pred; i->Pred->Succ=i->Succ; i->Succ->Pred=i->Pred; i->Pred=pivot; i->Succ=pivot->Succ; pivot->Succ=i; i->Succ->Pred=i; if(i==*lb) { *lb=temp->Succ; } i=temp; } if(i==j) { return pivot; } if(i!=pivot && j!=pivot) { SwapNode(i,j); temp=i; i=j; j=temp; if(i==*lb) { *lb=j; } else if(j==*lb) { *lb=i; } if(i==*rb) { *rb=j; } else if(j==*rb) { *rb=i; } } /* examine next element */ if(i!=pivot) { i = i->Succ; } if(j!=pivot) { j = j->Pred; } if (i == j) { return pivot; /* done=1;*/ } } }