go to bug id or search bugs for
The SplDoublyLinkedList is great, but it lacks a way to insert a node somewhere in the middle of the list.
One could technically extend the class and provide that functionality, but due to the fact that there seem to be [documented] data members in SplDoublyLinkedList, the only way to do it would be to find the reference node, insert the new node, and move all of the following nodes up one slot in the list, resulting in O(n) performance for an insert.
I propose adding an insertAfterIterator() method which would place the new node after the current node the iterator points to.
Another option could be an insertAfterArrayKey() method which would act similarly, but would find the reference node via a given array key, which might be a little more straightforward to use in code.
Each should probably have an insertBefore*() counterpart as well.
Add a Patch
Add a Pull Request
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...
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
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.
I'm also running into this, usual implementations of DoublyLinkedList describe the
insertBefore and insertAfter methods, wondering why they were left out in this
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.
Automatic comment on behalf of email@example.com
Log: Fix #48358 add() method for SplDoublyLinkedLis