A doubly linked list
Example:
var o = new de.polygonal.ds.Dll<Int>();
for (i in 0...4) o.append(i);
trace(o); //outputs:
[ Dll size=4
head
0 -> 0
1 -> 1
2 -> 2
3 -> 3
]
Constructor
Variables
read onlyisCircular:Bool
True if this list is circular.
A list is circular if the tail points to the head and vice versa.
read onlykey:Int
A unique identifier for this object.
A hash table transforms this key into an index of an array element by using a hash function.
reuseIterator:Bool
If true, reuses the iterator object instead of allocating a new one when calling this.iterator().
The default is false.
If this value is true, nested iterations will fail as only one iteration is allowed at a time.
Methods
Appends val to the tail of this list by creating a DllNode object storing val.
Returns:
the appended node storing val.
clear (?gc:Bool):Void
Removes all elements.
Parameters:
gc | if true, nodes, pointers and elements are nullified upon removal so the garbage collector can reclaim used memory. |
|---|
clone (?byRef:Bool, ?copier:T ‑> T):Collection<T>
Creates and returns a shallow copy (structure only - default) or deep copy (structure & elements) of this list.
If byRef is true, primitive elements are copied by value whereas objects are copied by reference.
If byRef is false, the copier function is used for copying elements. If omitted, clone() is called on each element assuming all elements implement Cloneable.
Makes this list circular by connecting the tail to the head and vice versa.
Silently fails if this list is already closed.
Concatenates this list with list by appending all elements of list to this list.
This list and list are left untouched.
Returns:
a new list containing the elements of both lists.
createNode (val:T):DllNode<T>
Creates and returns a new DllNode object storing the value val and pointing to this list.
inline forEach (f:T ‑> Int ‑> T):Dll<T>
Calls f on all elements.
The function signature is: f(input, index):output
- input: current element
- index: the index number of the given element (0=head)
- output: element to be stored at given index
free ():Void
Destroys this object by explicitly nullifying all nodes, pointers and data for GC'ing used resources.
Improves GC efficiency/performance (optional).
getRange (fromIndex:Int, toIndex:Int):List<T>
Returns a Dll object storing elements in the range [fromIndex, toIndex).
If toIndex is negative, the value represents the number of elements.
insert (index:Int, val:T):Void
Inserts val before the element at index (0=head).
If index equals this.size, val gets appended to the end of the list.
insertAfter (node:DllNode<T>, val:T):DllNode<T>
Inserts val after node by creating a DllNode object storing val.
Returns:
the inserted node storing val.
insertBefore (node:DllNode<T>, val:T):DllNode<T>
Inserts val before node by creating a DllNode object storing val.
Returns:
the inserted node storing val.
Returns a new DllIterator object to iterate over all elements contained in this doubly linked list.
Returns a CircularDllIterator iterator object if circular is true.
The elements are visited from head to tail.
If performance is crucial, use the following loop instead:
//open list:
var node = myDll.head;
while (node != null)
{
var element = node.val;
node = node.next;
}
//circular list:
var node = myDll.head;
for (i in 0...list.size)
{
var element = node.val;
node = node.next;
}
See:
join (sep:String):String
Converts the data in the linked list to strings, inserts sep between the elements, concatenates them, and returns the resulting string.
lastNodeOf (val:T, ?from:DllNode<T>):DllNode<T>
Searches for val in this list from tail to head starting at node from.
If from is null, the search starts at the tail of this list.
Returns:
the node containing val or null if such a node does not exist.
Merges this list with list by linking both lists together.
The merge operation destroys list so it should be discarded.
nodeOf (val:T, ?from:DllNode<T>):DllNode<T>
Searches for val in this list from head to tail starting at node from.
If from is null, the search starts at the head of this list.
Returns:
the node containing val or null if such a node does not exist.
Makes this list non-circular by disconnecting the tail from the head and vice versa.
Silently fails if this list is already non-circular.
Prepends val to the head of this list by creating a DllNode object storing val.
Returns:
the prepended node storing val.
remove (val:T):Bool
Removes all nodes storing val.
Returns:
true if at least one occurrence of val was removed.
shuffle (?rvals:Array<Float>):Dll<T>
Shuffles the elements of this collection by using the Fisher-Yates algorithm.
Parameters:
rvals | a list of random double values in the interval [0, 1) defining the new positions of the elements.
If omitted, random values are generated on-the-fly by calling |
|---|
sort (?cmp:T ‑> T ‑> Int, ?useInsertionSort:Bool):Dll<T>
Sorts the elements of this list using the merge sort algorithm.
Parameters:
cmp | a comparison function.
|
|---|---|
useInsertionSort | if true, the linked list is sorted using the insertion sort algorithm. This is faster for nearly sorted lists. |