Edit report at https://bugs.php.net/bug.php?id=48358&edit=1
ID: 48358 Updated by: d...@php.net Reported by: dannel at aaronexodus dot com Summary: SplDoublyLinkedList needs an insertAfterIterator() method or something similar -Status: Assigned +Status: Closed Type: Feature/Change Request Package: SPL related PHP Version: 5.3.0RC2 Assigned To: colder Block user comment: N Private report: N New Comment: Automatic comment on behalf of mba...@inviqa.com Revision: http://git.php.net/?p=php-src.git;a=commit;h=42574690795937290a4ad6d319f917c077542953 Log: Fix #48358 add() method for SplDoublyLinkedLis Previous Comments: ------------------------------------------------------------------------ [2012-11-29 14:13:27] col...@php.net It is not as easy as one might think. The state of iterators are separated from the state of the list itself. This allows for multiple concurrent iterations on the same list but it complicates the issue at hand. I see four viable alternatives (example for insertAfter): - SplDoublyLinkedList::insertAfter(value) -- O(1), but would work only for explicit iteration using Iterator methods, not foreach()! which works using a fresh iterator. - SPLDoublyLinkedList::insertAfter(index, value) -- O(n), not exactly what we want but could be working. - SPLDoublyLinkedList::insertAfter(iterator, value) -- O(1), needs validation of the iterator itself. - SplDoublyLinkedList generates iterators of the class SplDoublyLinkedListIterator, which define a insertAfter(value), would require SplDLL to be an IteratorAggregate instead of Iterator. Those are not mutually exclusive, but alternative #1 or #3 seem the most useful . Note that while traversing elements should be easily doable, since list elements are ref-counted, the key() of iterators might return inconsistent results if you allow the middle of the lists to be expanded/reduced. Making it consistent would make it O(n), so it's probably not a big problem to keep it that way. ------------------------------------------------------------------------ [2012-11-29 11:00:49] rdo...@php.net I'm also running into this, usual implementations of DoublyLinkedList describe the insertBefore and insertAfter methods, wondering why they were left out in this implementation. ------------------------------------------------------------------------ [2012-11-01 00:57:12] dnwake at gmail dot com This is ridiculous. Efficiency of inserting and deleting in the middle of the list is the main motivation for using a linked list in the first place. ------------------------------------------------------------------------ [2011-04-15 21:23:51] omercan at gmail dot com O(n) complexity should be expected from a list data structure along with no array access. The drawback of lists is they lack direct access to the elements by their position. Well, that's not the case with SplDoublyLinkedList because there's array access. So, it's a different implementation as far as it seems. In my opinion, the problem with SplDoublyLinkedList is that the main operations are left to be implemented in the user land, which require iterating over the list in each. This class can be much more valuable if the following operations are included: clear() -> clear all elements remove($value) -> return number of removals b/c $value can be more than 1 sort(Closure $predicate) -> sort without keeping key association swap($list) -> swap with another list or a Traversable instance unique() -> return the unique values in the list, possibly in a new list ------------------------------------------------------------------------ [2010-05-22 13:46:01] 1000235409 at smail dot shnu dot edn dot cn i have encountered the same problem today, after this was modified for 1 year... but i have a solution to solve this problem. write anohter class with a lots of spldoublylinkedlists inside it. however, complex algorithm may be used... ------------------------------------------------------------------------ The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online at https://bugs.php.net/bug.php?id=48358 -- Edit this bug report at https://bugs.php.net/bug.php?id=48358&edit=1