Heap-Fibonacci-Fast

 view release on metacpan or  search on metacpan

Fast.xs  view on Meta::CPAN

top(obj)
		SV* obj
	CODE:
		struct fibheap* heap = (struct fibheap*)SvIV(SvRV(obj));

		if(heap->fh_min == NULL){
			RETVAL = &PL_sv_undef;
		}else{
			SV* min = heap->fh_min->fhe_data;
			SvREFCNT_inc(min);
			RETVAL = min;
		}

	OUTPUT:
		RETVAL

int
count(obj)
		SV* obj
	CODE:
		struct fibheap* heap = (struct fibheap*)SvIV(SvRV(obj));
		RETVAL = heap->fh_n;
	OUTPUT:
		RETVAL

SV*
top_key(obj)
		SV* obj
	CODE:
		struct fibheap* heap = (struct fibheap*)SvIV(SvRV(obj));

		if(heap->fh_keys == callback){
			croak("top_key() is only applicable for keyed heaps");
		}

		if(heap->fh_min == NULL){
			RETVAL = &PL_sv_undef;
		}else{
			RETVAL = newSViv(heap->fh_min->fhe_key);
		}

	OUTPUT:
		RETVAL

void
remove(obj, elem)
		SV* obj
		SV* elem
	CODE:
		struct fibheap* heap = (struct fibheap*)SvIV(SvRV(obj));

		if (!SvOK(elem))
			croak("Undef supplied for remove()");

		fh_deleteel(heap, (struct fibheap_el *)SvIV(elem));

void
extract_upto(obj, upto_key)
		SV* obj
		SV* upto_key
	PPCODE:
		if(!SvOK(upto_key)){
			croak("Undef supplied as key for extract_upto()");
		}

		struct fibheap* heap = (struct fibheap*)SvIV(SvRV(obj));
		int upto_intkey;

		switch (heap->fh_keys) {
			case min_keyed:
				upto_intkey = SvIV(upto_key);
				while(
					heap->fh_min != NULL
						&&
					int_key_min_compare(heap->fh_min->fhe_key, upto_intkey) <= 0
				){
					XPUSHs(sv_2mortal((SV*)fh_extractmin(heap)));
				}
				break;

			case max_keyed:
				upto_intkey = SvIV(upto_key);
				while(
					heap->fh_min != NULL
						&&
					int_key_max_compare(heap->fh_min->fhe_key, upto_intkey) <= 0
				){
					XPUSHs(sv_2mortal((SV*)fh_extractmin(heap)));
				}
				break;

			case callback:
				while(
					heap->fh_min != NULL
						&&
					data_compare(heap, heap->fh_min->fhe_data, upto_key) <= 0
				){
					XPUSHs(sv_2mortal((SV*)fh_extractmin(heap)));
					PUTBACK;
				}
				break;
		}

void
key_insert(obj, ...)
		SV* obj
	PPCODE:
		struct fibheap* heap;
		SV *ret, *elem;
		int key;
		I32 gimme;
		int i;

		heap = (struct fibheap*)SvIV(SvRV(obj));
		if(heap->fh_keys == callback){
			croak("key_insert() is only applicable for keyed heaps");
		}

		items -= 1;
		if (items == 0){
			XSRETURN_EMPTY;
		}

		if (items % 2 != 0){
			croak("Odd number of parameters supplied for key_insert()");
		}

		gimme = GIMME_V;
		for(i = 0; i < items; i += 2){
			key = SvIV(ST(i + 1));
			elem = ST(i + 2);

			if(!SvOK(elem)){
				croak("Undef supplied as value for key_insert()");
			}

			SvREFCNT_inc(elem);
			if ((gimme == G_ARRAY) || (i == 0 && gimme == G_SCALAR)){
				ret = newSViv(fh_insertkey(heap, key, elem));
				SvREADONLY_on(ret);
				XPUSHs(sv_2mortal(ret));
			}else{
				(void)fh_insertkey(heap, key, elem);
			}
		}

void
insert(obj, ...)
		SV* obj
	PPCODE:
		struct fibheap* heap;
		SV *ret, *elem;
		I32 gimme;
		int i;

		items -= 1;
		if (items == 0){
			XSRETURN_EMPTY;
		}

		heap = (struct fibheap*)SvIV(SvRV(obj));
		if(heap->fh_keys != callback){
			croak("insert() is not applicable for keyed heaps");
		}

		gimme = GIMME_V;
		for(i = 0; i < items; i++){
			elem = ST(i + 1);

			if(!SvOK(elem)){
				croak("Undef supplied as value for insert()");
			}

			SvREFCNT_inc(elem);
			if ((gimme == G_ARRAY) || (i == 0 && gimme == G_SCALAR)){
				ret = newSViv(fh_insert(heap, elem));
				SvREADONLY_on(ret);
				XPUSHs(sv_2mortal(ret));
			}else{
				(void)fh_insert(heap, elem);
			}
		}

void
clear(obj)
		SV* obj
	CODE:
		struct fibheap* heap = (struct fibheap*)SvIV(SvRV(obj));

		fh_emptyheap(heap);

void
absorb(to, from)
		SV* to
		SV* from
	CODE:
		struct fibheap* heap_to = (struct fibheap*)SvIV(SvRV(to));
		struct fibheap* heap_from = (struct fibheap*)SvIV(SvRV(from));

		if(heap_from->fh_keys != heap_to->fh_keys)
			croak("Can't union heaps of different types");

		if(heap_from->fh_keys == callback)
			if(SvRV(heap_from->comparator) != SvRV(heap_to->comparator))
				croak("Can't union heaps with different compare callbacks");

		fh_union(heap_to, heap_from);
		SvSetSV(from, &PL_sv_undef);

SV*



( run in 1.973 second using v1.01-cache-2.11-cpan-71847e10f99 )