LinkedList helper functions (#493)
* Restore LinkedList helper functions * mem.Allocator
This commit is contained in:
parent
c2f3dc94eb
commit
e7a01c24a3
@ -1,6 +1,7 @@
|
|||||||
const debug = @import("debug.zig");
|
const debug = @import("debug.zig");
|
||||||
const assert = debug.assert;
|
const assert = debug.assert;
|
||||||
const mem = @import("mem.zig");
|
const mem = @import("mem.zig");
|
||||||
|
const Allocator = mem.Allocator;
|
||||||
|
|
||||||
/// Generic doubly linked list.
|
/// Generic doubly linked list.
|
||||||
pub fn LinkedList(comptime T: type) -> type {
|
pub fn LinkedList(comptime T: type) -> type {
|
||||||
@ -15,9 +16,9 @@ pub fn LinkedList(comptime T: type) -> type {
|
|||||||
|
|
||||||
pub fn init(data: &const T) -> Node {
|
pub fn init(data: &const T) -> Node {
|
||||||
Node {
|
Node {
|
||||||
.data = *data,
|
|
||||||
.prev = null,
|
.prev = null,
|
||||||
.next = null,
|
.next = null,
|
||||||
|
.data = *data,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -157,38 +158,57 @@ pub fn LinkedList(comptime T: type) -> type {
|
|||||||
return first;
|
return first;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Allocate a new node.
|
||||||
|
///
|
||||||
|
/// Arguments:
|
||||||
|
/// allocator: Dynamic memory allocator.
|
||||||
|
///
|
||||||
|
/// Returns:
|
||||||
|
/// A pointer to the new node.
|
||||||
|
pub fn allocateNode(list: &Self, allocator: &Allocator) -> %&Node {
|
||||||
|
allocator.create(Node)
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Deallocate a node.
|
||||||
|
///
|
||||||
|
/// Arguments:
|
||||||
|
/// node: Pointer to the node to deallocate.
|
||||||
|
/// allocator: Dynamic memory allocator.
|
||||||
|
pub fn destroyNode(list: &Self, node: &Node, allocator: &Allocator) {
|
||||||
|
allocator.destroy(node);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Allocate and initialize a node and its data.
|
||||||
|
///
|
||||||
|
/// Arguments:
|
||||||
|
/// data: The data to put inside the node.
|
||||||
|
/// allocator: Dynamic memory allocator.
|
||||||
|
///
|
||||||
|
/// Returns:
|
||||||
|
/// A pointer to the new node.
|
||||||
|
pub fn createNode(list: &Self, data: &const T, allocator: &Allocator) -> %&Node {
|
||||||
|
var node = %return list.allocateNode(allocator);
|
||||||
|
*node = Node.init(data);
|
||||||
|
return node;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn testAllocateNode(comptime T: type, list: &LinkedList(T), allocator: &mem.Allocator) -> %&LinkedList(T).Node {
|
|
||||||
allocator.create(LinkedList(T).Node)
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn testDestroyNode(comptime T: type, list: &LinkedList(T), node: &LinkedList(T).Node, allocator: &mem.Allocator) {
|
|
||||||
allocator.destroy(node);
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn testCreateNode(comptime T: type, list: &LinkedList(T), data: &const T, allocator: &mem.Allocator) -> %&LinkedList(T).Node {
|
|
||||||
var node = %return testAllocateNode(T, list, allocator);
|
|
||||||
*node = LinkedList(T).Node.init(data);
|
|
||||||
return node;
|
|
||||||
}
|
|
||||||
|
|
||||||
test "basic linked list test" {
|
test "basic linked list test" {
|
||||||
const allocator = &debug.global_allocator;
|
const allocator = &debug.global_allocator;
|
||||||
var list = LinkedList(u32).init();
|
var list = LinkedList(u32).init();
|
||||||
|
|
||||||
var one = %%testCreateNode(u32, &list, 1, allocator);
|
var one = %%list.createNode(1, allocator);
|
||||||
var two = %%testCreateNode(u32, &list, 2, allocator);
|
var two = %%list.createNode(2, allocator);
|
||||||
var three = %%testCreateNode(u32, &list, 3, allocator);
|
var three = %%list.createNode(3, allocator);
|
||||||
var four = %%testCreateNode(u32, &list, 4, allocator);
|
var four = %%list.createNode(4, allocator);
|
||||||
var five = %%testCreateNode(u32, &list, 5, allocator);
|
var five = %%list.createNode(5, allocator);
|
||||||
defer {
|
defer {
|
||||||
testDestroyNode(u32, &list, one, allocator);
|
list.destroyNode(one, allocator);
|
||||||
testDestroyNode(u32, &list, two, allocator);
|
list.destroyNode(two, allocator);
|
||||||
testDestroyNode(u32, &list, three, allocator);
|
list.destroyNode(three, allocator);
|
||||||
testDestroyNode(u32, &list, four, allocator);
|
list.destroyNode(four, allocator);
|
||||||
testDestroyNode(u32, &list, five, allocator);
|
list.destroyNode(five, allocator);
|
||||||
}
|
}
|
||||||
|
|
||||||
list.append(two); // {2}
|
list.append(two); // {2}
|
||||||
@ -197,7 +217,7 @@ test "basic linked list test" {
|
|||||||
list.insertBefore(five, four); // {1, 2, 4, 5}
|
list.insertBefore(five, four); // {1, 2, 4, 5}
|
||||||
list.insertAfter(two, three); // {1, 2, 3, 4, 5}
|
list.insertAfter(two, three); // {1, 2, 3, 4, 5}
|
||||||
|
|
||||||
// traverse forwards
|
// Traverse forwards.
|
||||||
{
|
{
|
||||||
var it = list.first;
|
var it = list.first;
|
||||||
var index: u32 = 1;
|
var index: u32 = 1;
|
||||||
@ -207,7 +227,7 @@ test "basic linked list test" {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// traverse backwards
|
// Traverse backwards.
|
||||||
{
|
{
|
||||||
var it = list.last;
|
var it = list.last;
|
||||||
var index: u32 = 1;
|
var index: u32 = 1;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user