A doubly linked list suitable for stack allocation.
Add this to zz.toml
:
[dependencies]
doublelist = "*"
[repos]
doublelist = "git://github.com/jwerle/doublelist"
using doublelist
using log
fn main () -> int {
// allocate a new mutable list with tail size of 2 (the number of possible elements)
new+2 mut items = doublelist::make();
items.push("hello");
items.push("world");
// get an iterator to the list
let mut it = items.iterator();
while !it.ended {
let node = it.next();
log::info("%s", node);
}
return 0;
}
A doubly linked list suitable for stack allocation.
List
constructor. Initializes a List
pointer.
new+4 items = doublelist::make();
Pushes a value to the right of the list updating the tail returning the newly created list node.
let hello = l.rpush("hello");
let world = l.rpush("world");
See the Node API for more information about list nodes.
Pushes a value to the left of the list updating the head returning the newly created list node.
let world = l.lpush("world");
let hello = l.lpush("hello");
See the Node API for more information about list nodes.
Pops a value from the tail of the list.
let value = l.rpop();
Pops a value from the head of the list.
let value = l.lpop();
An alias for l.rpush(value)
.
l.push(value);
An alias for l.lpush(value)
.
l.unshift(value);
An alias for l.rpop()
.
let value = l.pop();
An alias for l.lpop()
.
let value = l.shift();
Creates and returns a stack allocated iterator for a list instance in the default iterator direction.
let it = l.iterator();
See the Iterator API for more information about list iterators.
Creates and returns a stack allocated iterator for a list instance in the "head" direction.
let it = list.iterator_head();
See the Iterator API for more information about list iterators.
Creates and returns a stack allocated iterator for a list instance in the "tail" direction.
let it = list.iterator_tail();
See the Iterator API for more information about list iterators.
Creates and returns a stack allocated iterator for a list instance in a given direction.
let it = list.iterator_with_direction(doublelist::HEAD); // default
// or
let it = list.iterator_with_direction(doublelist::TAIL);
See the Iterator API for more information about list iterators.
Finds and returns a list node that points to a value that matches the given
value pointer. A user supplied comparator function can be used by setting
l.compare
to a CompareNodeValueFunction(void a*, void *b) -> bool
type,
otherwise simple pointer comparison is used by default.
let node = l.find("hello");
Finds and returns the list node at a given index.
l.rpush("hello");
l.rpush("world");
let node = l.at(1);
static_attest(safe(node));
log::info("%s", node->value);
Finds a list node that points to a given value and removes it from the list.
list.rpush("hello");
list.rpush("world");
list.remove("world");
Returns a boolean indicating if a list contains a value.
l.push("hello");
if l.contains("hello") {
l.push("world");
}
Take a slice from the list at a depth offset.
void * mut values[2];
l.push("hello");
l.push("world");
l.slice(values, 0, 2);
log::info("%s %s", values[0], values[1]);
Compute the index of a node's value and return it. If not found, then -1 is returned. -1 is also returned if a compare function is not found.
new+2 l = doublelist::make();
l.push("hello");
l.push("world");
assert(0 == l.index("hello"));
assert(1 == l.index("world"));
An iterator context for a list.
Iterator
stack constructor with direction from a list. Initializes a
Iterator
pointer with a List
pointer and direction
. Typically, you
will never need to call this function.
A boolean that is set to true
when the iterator has reach the end of
the list after subsequent calls to it.next()
.
The direction in which the iterator will traverse the list. See Iterator Directions for more information on this value.
Returns a pointer to the next node in the list. This function will set
it.ended = true
when it has reach the end of the list.
let it = l.iterator();
while !it.ended {
let node = it.next();
}
Seeks the iterator to the node.
it.seek(l.at(l.index("foo")));
Marks the iterator as "ended". This will set it.ended = true
and
remove a reference to any nodes in the list.
Iterators can traverse a list in the doublelist::HEAD
(doublelist::iterator::Direction::Head
) or doublelist::TAIL
(doublelist::iterator::Direction::Tail
) directions.
A node structure to contain pointers to a list node's value, containing List structure, and next/prev nodes.
A pointer to the value this node points to.
A pointer to the next node in the list.
A pointer to the previous node in the list.
MIT