예제 #1
0
파일: binoheap.c 프로젝트: aki5/libheap
static force_inline void
mergen(Binoheap *a, Binonode **b, int nb)
{
	int i, nchld;
	int mini;
	long long minkey;
	nchld = a->nchld > nb ? a->nchld : nb;
/* static alloc
	if(nchld+1 > a->achld){
		a->achld = Nchld;
		a->chld = realloc(a->chld, a->achld * sizeof a->chld[0]);
		for(i = a->nchld; i < a->achld; i++)
			a->chld[i] = NULL;
	}
*/
	Binonode *cp = NULL;
	mini = -1;
	minkey = (1ull<<63)-1;
	for(i = 0; i < nchld; i++){
		Binonode *ap, *bp;
		ap = a->chld[i];
		bp = i < nb ? b[i] : NULL;
		if(ap != NULL && bp != NULL){ // ABc, ABC
			a->chld[i] = cp;
			cp = merge1(ap, bp);
		} else if(cp != NULL){ // abC, aBC, AbC
			if(ap == NULL)
				ap = bp;
			if(ap != NULL){
				a->chld[i] = NULL;
				cp = merge1(ap, cp);
			} else {
				a->chld[i] = cp;
				cp = NULL;
			}
		} else if(bp != NULL){ // aBc
			a->chld[i] = bp;
		}
		// else if(ap != NULL){ a->roots[i] = ap; } // Abc
		// else {} // abc is a nop too

		if(a->chld[i] != NULL && a->chld[i]->key < minkey){
			minkey = a->chld[i]->key;
			mini = i;
		}
	}
	if(cp != NULL){
		if(cp->key < minkey)
			mini = i;
		a->chld[i++] = cp;
	}
	a->nchld = i;
	a->mini = mini;
	
	if(a->mini != -1)
		for(i = 0; i < a->chld[mini]->nchld; i += 8)
			prefetch((long *)a->chld[mini] + i);
}
예제 #2
0
파일: SkewHeap.c 프로젝트: l-iberty/MyCode
SkewHeap merge(SkewHeap H1, SkewHeap H2) {
	if (H1 == NULL)
		return H2;
	if (H2 == NULL)
		return H1;
	if (H1->key < H2->key)
		return merge1(H1, H2);
	else
		return merge1(H2, H1);
}
예제 #3
0
prio_queue
merge(prio_queue q1, prio_queue q2)
{
	if (!q1) {
		return q2;
	}
	if (!q2) {
		return q1;
	}

	/*q1是根节点小的那颗树 */
	if (q1->data < q2->data) {
		return merge1(q1, q2);
	} else
		return merge1(q2, q1);
}
예제 #4
0
void Damage::Merge (BoxObj& newb) {
    BoxObj* a1, *a2;
    int newArea, area1, area2, diff1, diff2, diff3, maximum;
    Iterator i;

    FirstArea(i);
    a1 = GetArea(i);
    Next(i);
    a2 = GetArea(i);
    BoxObj merge1(*a1 + newb);
    BoxObj merge2(*a2 + newb);
    BoxObj merge3(*a1 + *a2);

    newArea = Area(newb);
    area1 = Area(*a1);
    area2 = Area(*a2);
    diff1 = area1 + newArea - Area(merge1);
    diff2 = area2 + newArea - Area(merge2);
    diff3 = area1 + area2 - Area(merge3);
    maximum = max(max(diff1, diff2), diff3);

    if (maximum == diff1) {
	if (a2->Intersects(merge1)) {
	    *a1 = merge1 + *a2;
            DeleteArea(a2);
	} else {
	    *a1 = merge1;
	}

    } else if (maximum == diff2) {
	if (a1->Intersects(merge2)) {
	    *a2 = merge2 + *a1;
            DeleteArea(a1);
	} else {
	    *a2 = merge2;
	}

    } else {
	if (newb.Intersects(merge3)) {
	    *a1 = merge3 + newb;
            DeleteArea(a2);
	} else {
	    *a1 = merge3;
	    *a2 = newb;
	}
    }
}
int main()
{
    {
        int a[6] = {1,3,4,6,8,9};
        int b[12] = {0,2,5,7,10,11};
        merge1(a, b, 6);
        for(int i = 0;i < 12;++i)
            std::cout<<b[i]<<",";
        std::cout<<"\n";
    }
    std::cout<<"---\n";
    {
        int a[6] = {1,3,4,6,8,9};
        int b[12] = {0,2,5,7,10,11};
        merge2(a, b, 6);
        for(int i = 0;i < 12;++i)
            std::cout<<b[i]<<",";
        std::cout<<"\n";
    }
}
예제 #6
0
 vector<Interval> merge(vector<Interval>& intervals) 
 {
     vector<Interval> ret = merge1( intervals); 
     return ret;
 }