Doubly-Linked

 view release on metacpan or  search on metacpan

lib/Doubly/Linked.xs  view on Meta::CPAN

			hv_store(nexting, "prev", 4, undef, 0);
		}
	} else {
		hv_store(hash, "data", 4, undef, 0);
	}

	return data;
}

SV * _remove_from_start(SV * self) {
	dTHX;
	if (_is_undef(self)) {
		return &PL_sv_undef;
	}

	self = _goto_start(self);

	return _remove(self);
}

SV * _remove_from_end(SV * self) {
	dTHX;

	if (_is_undef(self)) {
		return &PL_sv_undef;
	}

	self = _goto_end(self);

	return _remove(self);
}

SV * _remove_from_pos (SV * self, int pos) {
	dTHX;

	if (_is_undef(self)) {
		return &PL_sv_undef;
	}

	self = _goto_start(self);
	HV * hash = (HV*)SvRV(self);

	int i = 0;
	for (i = 0; i < pos; i++) {
		if (_defined(hash, "next", 4)) {
			self = *hv_fetch(hash, "next", 4, 0);
			hash = (HV*)SvRV(self);
		}
	}

	return _remove(self);
}

void _destroy (SV * self) {
	dTHX;

	if (_is_undef(self)) {
		return;
	}

	/* Collect all nodes first, incrementing refcounts to keep them alive */
	AV * nodes = newAV();
	self = _goto_start(self);
	HV * hash = (HV*)SvRV(self);

	av_push(nodes, SvREFCNT_inc(self));
	while (_defined(hash, "next", 4)) {
		self = *hv_fetch(hash, "next", 4, 0);
		av_push(nodes, SvREFCNT_inc(self));
		hash = (HV*)SvRV(self);
	}

	/* Now clear all links in all nodes */
	int i;
	for (i = 0; i <= av_len(nodes); i++) {
		SV ** node_ptr = av_fetch(nodes, i, 0);
		if (node_ptr && SvROK(*node_ptr)) {
			HV * h = (HV*)SvRV(*node_ptr);
			hv_store(h, "prev", 4, newSV(0), 0);
			hv_store(h, "next", 4, newSV(0), 0);
			hv_store(h, "data", 4, newSV(0), 0);
		}
	}

	/* Free the nodes array (and decrement all the refcounts we incremented) */
	SvREFCNT_dec((SV*)nodes);
}


MODULE = Doubly::Linked  PACKAGE = Doubly::Linked
PROTOTYPES: DISABLE

SV *
new(...)
	CODE:
		RETVAL = _new(ST(0), items > 1 ? newSVsv(ST(1)) : &PL_sv_undef);
	OUTPUT:
		RETVAL

SV *
length(self)
	SV * self
	CODE:
		RETVAL = _length(self);
	OUTPUT:
		RETVAL

SV *
data(self, ...)
	SV * self
	CODE:
		HV * hash = (HV*)SvRV(self);
		if (items > 1) {
			hv_store(hash, "data", 4, newSVsv(ST(1)), 0);
		}
		SV * data = newSVsv(*hv_fetch(hash, "data", 4, 0));
		RETVAL = data;
	OUTPUT:
		RETVAL

SV *



( run in 2.199 seconds using v1.01-cache-2.11-cpan-5735350b133 )