terminal: change pagelist linked list to an intrusive linked list

This commit is contained in:
Mitchell Hashimoto
2024-11-07 15:05:13 -08:00
parent 3aff43b2e8
commit 1335af3e4a
2 changed files with 27 additions and 12 deletions

View File

@@ -11,15 +11,19 @@ pub fn DoublyLinkedList(comptime T: type) type {
return struct {
const Self = @This();
first: ?*T = null,
last: ?*T = null,
/// The type of the node in the list. This makes it easy to get the
/// node type from the list type.
pub const Node = T;
first: ?*Node = null,
last: ?*Node = null,
/// Insert a new node after an existing one.
///
/// Arguments:
/// node: Pointer to a node in the list.
/// new_node: Pointer to the new node to insert.
pub fn insertAfter(list: *Self, node: *T, new_node: *T) void {
pub fn insertAfter(list: *Self, node: *Node, new_node: *Node) void {
new_node.prev = node;
if (node.next) |next_node| {
// Intermediate node.
@@ -38,7 +42,7 @@ pub fn DoublyLinkedList(comptime T: type) type {
/// Arguments:
/// node: Pointer to a node in the list.
/// new_node: Pointer to the new node to insert.
pub fn insertBefore(list: *Self, node: *T, new_node: *T) void {
pub fn insertBefore(list: *Self, node: *Node, new_node: *Node) void {
new_node.next = node;
if (node.prev) |prev_node| {
// Intermediate node.
@@ -56,7 +60,7 @@ pub fn DoublyLinkedList(comptime T: type) type {
///
/// Arguments:
/// new_node: Pointer to the new node to insert.
pub fn append(list: *Self, new_node: *T) void {
pub fn append(list: *Self, new_node: *Node) void {
if (list.last) |last| {
// Insert after last.
list.insertAfter(last, new_node);
@@ -70,7 +74,7 @@ pub fn DoublyLinkedList(comptime T: type) type {
///
/// Arguments:
/// new_node: Pointer to the new node to insert.
pub fn prepend(list: *Self, new_node: *T) void {
pub fn prepend(list: *Self, new_node: *Node) void {
if (list.first) |first| {
// Insert before first.
list.insertBefore(first, new_node);
@@ -87,7 +91,7 @@ pub fn DoublyLinkedList(comptime T: type) type {
///
/// Arguments:
/// node: Pointer to the node to be removed.
pub fn remove(list: *Self, node: *T) void {
pub fn remove(list: *Self, node: *Node) void {
if (node.prev) |prev_node| {
// Intermediate node.
prev_node.next = node.next;
@@ -109,7 +113,7 @@ pub fn DoublyLinkedList(comptime T: type) type {
///
/// Returns:
/// A pointer to the last node in the list.
pub fn pop(list: *Self) ?*T {
pub fn pop(list: *Self) ?*Node {
const last = list.last orelse return null;
list.remove(last);
return last;
@@ -119,7 +123,7 @@ pub fn DoublyLinkedList(comptime T: type) type {
///
/// Returns:
/// A pointer to the first node in the list.
pub fn popFirst(list: *Self) ?*T {
pub fn popFirst(list: *Self) ?*Node {
const first = list.first orelse return null;
list.remove(first);
return first;