spl_ptr_llist_element *head;
spl_ptr_llist_element *tail;
spl_ptr_llist_dtor_func dtor;
+ spl_ptr_llist_ctor_func ctor;
int count;
} spl_ptr_llist;
-
typedef struct _spl_dllist_object spl_dllist_object;
typedef struct _spl_dllist_it spl_dllist_it;
static void spl_ptr_llist_zval_dtor(spl_ptr_llist_element *elem) { /* {{{ */
if (elem->data) {
zval_ptr_dtor((zval **)&elem->data);
- elem->data = NULL;
}
-
}
/* }}} */
}
/* }}} */
-static spl_ptr_llist *spl_ptr_llist_init(spl_ptr_llist_dtor_func dtor) /* {{{ */
+static spl_ptr_llist *spl_ptr_llist_init(spl_ptr_llist_ctor_func ctor, spl_ptr_llist_dtor_func dtor) /* {{{ */
{
spl_ptr_llist *llist = emalloc(sizeof(spl_ptr_llist));
llist->tail = NULL;
llist->count = 0;
llist->dtor = dtor;
+ llist->ctor = ctor;
return llist;
}
llist->head = elem;
llist->count++;
+
+ if (llist->ctor) {
+ llist->ctor(elem);
+ }
}
/* }}} */
llist->tail = elem;
llist->count++;
+
+ if (llist->ctor) {
+ llist->ctor(elem);
+ }
}
/* }}} */
llist->tail = tail->prev;
llist->count--;
data = tail->data;
+
+ if (llist->dtor) {
+ llist->dtor(tail);
+ }
+
tail->data = NULL;
SPL_LLIST_DELREF(tail);
llist->head = head->next;
llist->count--;
data = head->data;
+
+ if (llist->dtor) {
+ llist->dtor(head);
+ }
head->data = NULL;
SPL_LLIST_DELREF(head);
static void spl_dllist_object_free_storage(void *object TSRMLS_DC) /* {{{ */
{
spl_dllist_object *intern = (spl_dllist_object *)object;
+ zval *tmp = NULL;
zend_object_std_dtor(&intern->std TSRMLS_CC);
+ while(intern->llist->count > 0) {
+ tmp = (zval *)spl_ptr_llist_pop(intern->llist);
+ zval_ptr_dtor(&tmp);
+ }
+
spl_ptr_llist_destroy(intern->llist);
zval_ptr_dtor(&intern->retval);
}
/* }}} */
-static void spl_ptr_llist_copy(spl_ptr_llist *from, spl_ptr_llist *to, spl_ptr_llist_ctor_func ctor) /* {{{ */
+static void spl_ptr_llist_copy(spl_ptr_llist *from, spl_ptr_llist *to) /* {{{ */
{
- spl_ptr_llist_element *current = from->head, *next;
+ spl_ptr_llist_element *current = from->head, *next;
+ spl_ptr_llist_ctor_func ctor = from->ctor;
while (current) {
next = current->next;
+
if (ctor) {
ctor(current);
}
+
spl_ptr_llist_push(to, current->data);
current = next;
}
intern->ce_get_iterator = other->ce_get_iterator;
if (clone_orig) {
- intern->llist = (spl_ptr_llist *)spl_ptr_llist_init(spl_ptr_llist_zval_dtor);
- spl_ptr_llist_copy(other->llist, intern->llist, spl_ptr_llist_zval_ctor);
+ intern->llist = (spl_ptr_llist *)spl_ptr_llist_init(other->llist->ctor, other->llist->dtor);
+ spl_ptr_llist_copy(other->llist, intern->llist);
intern->traverse_pointer = intern->llist->head;
} else {
intern->llist = other->llist;
intern->flags = other->flags;
} else {
- intern->llist = (spl_ptr_llist *)spl_ptr_llist_init(spl_ptr_llist_zval_dtor);
+ intern->llist = (spl_ptr_llist *)spl_ptr_llist_init(spl_ptr_llist_zval_ctor, spl_ptr_llist_zval_dtor);
intern->traverse_pointer = intern->llist->head;
SPL_LLIST_CHECK_ADDREF(intern->traverse_pointer);
}
-
while (parent) {
if (parent == spl_ce_SplStack) {
intern->flags |= (SPL_DLLIST_IT_FIX | SPL_DLLIST_IT_LIFO);
} else if (parent == spl_ce_SplQueue) {
intern->flags |= SPL_DLLIST_IT_FIX;
}
+
if (parent == spl_ce_SplDoublyLinkedList) {
retval.handlers = &spl_handler_SplDoublyLinkedList;
break;
}
+
parent = parent->parent;
inherited = 1;
}
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &value) == FAILURE) {
return;
}
+
SEPARATE_ARG_IF_REF(value);
intern = (spl_dllist_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
-
spl_ptr_llist_push(intern->llist, value);
RETURN_TRUE;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &value) == FAILURE) {
return;
}
+
SEPARATE_ARG_IF_REF(value);
intern = (spl_dllist_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
-
spl_ptr_llist_unshift(intern->llist, value);
RETURN_TRUE;
/* }}} */
/* {{{ proto mixed SplDoublyLinkedList::pop() U
- Pop an element ouf of the SplDoublyLinkedList */
+ Pop an element out of the SplDoublyLinkedList */
SPL_METHOD(SplDoublyLinkedList, pop)
{
zval *value;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "") == FAILURE) {
return;
}
- intern = (spl_dllist_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
+ intern = (spl_dllist_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
value = (zval *)spl_ptr_llist_pop(intern->llist);
if (value == NULL) {
/* }}} */
/* {{{ proto mixed SplDoublyLinkedList::shift() U
- Shift an element ouf of the SplDoublyLinkedList */
+ Shift an element out of the SplDoublyLinkedList */
SPL_METHOD(SplDoublyLinkedList, shift)
{
zval *value;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "") == FAILURE) {
return;
}
- intern = (spl_dllist_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
- value = (zval *)spl_ptr_llist_shift(intern->llist);
+ intern = (spl_dllist_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
+ value = (zval *)spl_ptr_llist_shift(intern->llist);
if (value == NULL) {
zend_throw_exception(spl_ce_RuntimeException, "Can't shift from an empty datastructure", 0 TSRMLS_CC);
/* }}} */
/* {{{ proto mixed SplDoublyLinkedList::top() U
- Peak at the top element of the SplDoublyLinkedList */
+ Peek at the top element of the SplDoublyLinkedList */
SPL_METHOD(SplDoublyLinkedList, top)
{
zval *value;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "") == FAILURE) {
return;
}
+
intern = (spl_dllist_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
- value = (zval *)spl_ptr_llist_last(intern->llist);
+ value = (zval *)spl_ptr_llist_last(intern->llist);
if (value == NULL) {
- zend_throw_exception(spl_ce_RuntimeException, "Can't peak at an empty datastructure", 0 TSRMLS_CC);
+ zend_throw_exception(spl_ce_RuntimeException, "Can't peek at an empty datastructure", 0 TSRMLS_CC);
return;
}
/* }}} */
/* {{{ proto mixed SplDoublyLinkedList::bottom() U
- Peak at the bottom element of the SplDoublyLinkedList */
+ Peek at the bottom element of the SplDoublyLinkedList */
SPL_METHOD(SplDoublyLinkedList, bottom)
{
zval *value;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "") == FAILURE) {
return;
}
+
intern = (spl_dllist_object*)zend_object_store_get_object(getThis() TSRMLS_CC);
- value = (zval *)spl_ptr_llist_first(intern->llist);
+ value = (zval *)spl_ptr_llist_first(intern->llist);
if (value == NULL) {
- zend_throw_exception(spl_ce_RuntimeException, "Can't peak at an empty datastructure", 0 TSRMLS_CC);
+ zend_throw_exception(spl_ce_RuntimeException, "Can't peek at an empty datastructure", 0 TSRMLS_CC);
return;
}
/* }}} */
/* {{{ proto int SplDoublyLinkedList::isEmpty() U
- Return true of the SplDoublyLinkedList is empty. */
+ Return true if the SplDoublyLinkedList is empty. */
SPL_METHOD(SplDoublyLinkedList, isEmpty)
{
long count;
element = spl_ptr_llist_offset(intern->llist, index, intern->flags & SPL_DLLIST_IT_LIFO);
if (element != NULL) {
+ /* call dtor on the old element as in spl_ptr_llist_pop */
if (intern->llist->dtor) {
intern->llist->dtor(element);
}
+
+ /* the element is replaced, delref the old one as in
+ * SplDoublyLinkedList::pop() */
+ zval_ptr_dtor((zval **)&element->data);
element->data = value;
+
+ /* new element, call ctor as in spl_ptr_llist_push */
+ if (intern->llist->ctor) {
+ intern->llist->ctor(element);
+ }
} else {
zend_throw_exception(spl_ce_OutOfRangeException, "Offset invalid", 0 TSRMLS_CC);
return;
if (element->prev) {
element->prev->next = element->next;
}
+
if (element->next) {
element->next->prev = element->prev;
}
+
/* take care of head/tail */
if (element == llist->head) {
llist->head = element->next;
}
+
if (element == llist->tail) {
llist->tail = element->prev;
}
+
/* finally, delete the element */
llist->count--;
if(llist->dtor) {
llist->dtor(element);
}
+
+ zval_ptr_dtor((zval **)&element->data);
+ element->data = NULL;
+
SPL_LLIST_DELREF(element);
} else {
zend_throw_exception(spl_ce_OutOfRangeException, "Offset invalid", 0 TSRMLS_CC);
if (iterator->flags & SPL_DLLIST_IT_LIFO) {
iterator->traverse_pointer = old->prev;
iterator->traverse_position--;
+
if (iterator->flags & SPL_DLLIST_IT_DELETE) {
zval *prev = (zval *)spl_ptr_llist_pop(object->llist);
+
if (prev) {
zval_ptr_dtor((zval **)&prev);
}
} else {
iterator->traverse_pointer = old->next;
iterator->traverse_position++;
+
if (iterator->flags & SPL_DLLIST_IT_DELETE) {
zval *prev = (zval *)spl_ptr_llist_shift(object->llist);
+
if (prev) {
zval_ptr_dtor((zval **)&prev);
}
spl_ptr_llist *llist = object->llist;
SPL_LLIST_CHECK_DELREF(iterator->traverse_pointer);
+
if (iterator->flags & SPL_DLLIST_IT_LIFO) {
iterator->traverse_position = llist->count-1;
iterator->traverse_pointer = llist->tail;
iterator->traverse_position = 0;
iterator->traverse_pointer = llist->head;
}
+
SPL_LLIST_CHECK_ADDREF(iterator->traverse_pointer);
}
/* }}} */
};
/* }}} */
-
PHP_MINIT_FUNCTION(spl_dllist) /* {{{ */
{
REGISTER_SPL_STD_CLASS_EX(SplDoublyLinkedList, spl_dllist_object_new, spl_funcs_SplDoublyLinkedList);