colder          Tue Jan 22 19:51:11 2008 UTC

  Modified files:              
    /php-src    NEWS 
    /php-src/ext/spl    spl_dllist.c 
  Log:
  News: already in 5_3
  Typos (Dan Scott)
  ctor inside the structure. dtor/ctor used on push/pop for better potential 
interoperability
  
http://cvs.php.net/viewvc.cgi/php-src/NEWS?r1=1.2154&r2=1.2155&diff_format=u
Index: php-src/NEWS
diff -u php-src/NEWS:1.2154 php-src/NEWS:1.2155
--- php-src/NEWS:1.2154 Mon Jan 21 21:54:00 2008
+++ php-src/NEWS        Tue Jan 22 19:51:11 2008
@@ -59,7 +59,6 @@
 - Added shm_has_var() function. (Mike)
 - Added str_getcsv() function. (Sara)
 - Added ext/hash support to ext/session's ID generator. (Sara)
-- Added SplDoublyLinkedList, SplStack, SplQueue classes. (Etienne)
 
 - Fixed bug #40325 (Vary: header missing in gzip output handlers). (Mike)
 
http://cvs.php.net/viewvc.cgi/php-src/ext/spl/spl_dllist.c?r1=1.4&r2=1.5&diff_format=u
Index: php-src/ext/spl/spl_dllist.c
diff -u php-src/ext/spl/spl_dllist.c:1.4 php-src/ext/spl/spl_dllist.c:1.5
--- php-src/ext/spl/spl_dllist.c:1.4    Sun Jan 20 12:51:33 2008
+++ php-src/ext/spl/spl_dllist.c        Tue Jan 22 19:51:11 2008
@@ -16,7 +16,7 @@
    +----------------------------------------------------------------------+
  */
 
-/* $Id: spl_dllist.c,v 1.4 2008/01/20 12:51:33 colder Exp $ */
+/* $Id: spl_dllist.c,v 1.5 2008/01/22 19:51:11 colder Exp $ */
 
 #ifdef HAVE_CONFIG_H
 # include "config.h"
@@ -75,10 +75,10 @@
        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;
 
@@ -109,9 +109,7 @@
 static void spl_ptr_llist_zval_dtor(spl_ptr_llist_element *elem) { /* {{{ */
        if (elem->data) {
                zval_ptr_dtor((zval **)&elem->data);
-               elem->data = NULL;
        }
-
 }
 /* }}} */
 
@@ -120,7 +118,7 @@
 }
 /* }}} */
 
-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));
 
@@ -128,6 +126,7 @@
        llist->tail  = NULL;
        llist->count = 0;
        llist->dtor  = dtor;
+       llist->ctor  = ctor;
 
        return llist;
 }
@@ -199,6 +198,10 @@
 
        llist->head = elem;
        llist->count++;
+
+       if (llist->ctor) {
+               llist->ctor(elem);
+       }
 }
 /* }}} */
 
@@ -219,6 +222,10 @@
 
        llist->tail = elem;
        llist->count++;
+
+       if (llist->ctor) {
+               llist->ctor(elem);
+       }
 }
 /* }}} */
 
@@ -240,6 +247,11 @@
        llist->tail = tail->prev;
        llist->count--;
        data = tail->data;
+
+       if (llist->dtor) {
+               llist->dtor(tail);
+       }
+
        tail->data = NULL;
 
        SPL_LLIST_DELREF(tail);
@@ -290,6 +302,10 @@
        llist->head = head->next;
        llist->count--;
        data = head->data;
+
+       if (llist->dtor) {
+               llist->dtor(head);
+       }
        head->data = NULL;
 
        SPL_LLIST_DELREF(head);
@@ -301,9 +317,15 @@
 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);
 
@@ -311,15 +333,18 @@
 }
 /* }}} */
 
-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;
        }
@@ -354,8 +379,8 @@
                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;
@@ -365,22 +390,23 @@
 
                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;
        }
@@ -458,10 +484,10 @@
        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;
@@ -478,10 +504,10 @@
        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;
@@ -489,7 +515,7 @@
 /* }}} */
 
 /* {{{ 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;
@@ -498,8 +524,8 @@
        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) {
@@ -512,7 +538,7 @@
 /* }}} */
 
 /* {{{ 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;
@@ -521,9 +547,9 @@
        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);
@@ -535,7 +561,7 @@
 /* }}} */
 
 /* {{{ 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;
@@ -544,11 +570,12 @@
        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;
        }
 
@@ -557,7 +584,7 @@
 /* }}} */
 
 /* {{{ 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;
@@ -566,11 +593,12 @@
        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;
        }
 
@@ -594,7 +622,7 @@
 /* }}} */
 
 /* {{{ 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;
@@ -754,10 +782,20 @@
                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;
@@ -795,22 +833,30 @@
                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);
@@ -875,8 +921,10 @@
                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);
                                }
@@ -884,8 +932,10 @@
                } 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);
                                }
@@ -905,6 +955,7 @@
        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;
@@ -912,6 +963,7 @@
                iterator->traverse_position = 0;
                iterator->traverse_pointer  = llist->head;
        }
+
        SPL_LLIST_CHECK_ADDREF(iterator->traverse_pointer);
 }
 /* }}} */
@@ -1002,7 +1054,6 @@
 };
 /* }}} */
 
-
 PHP_MINIT_FUNCTION(spl_dllist) /* {{{ */
 {
        REGISTER_SPL_STD_CLASS_EX(SplDoublyLinkedList, spl_dllist_object_new, 
spl_funcs_SplDoublyLinkedList);

-- 
PHP CVS Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to