add DLLInsert

This commit is contained in:
Matthew 2025-09-15 14:37:31 +10:00
parent b026f78ec2
commit 6757559089

32
util.d
View File

@ -215,6 +215,36 @@ DLLPush(T, U)(T* list, U* node, U* nil)
}
}
void
DLLInsert(T, U)(T* list, U* node, U* prev, U* nil)
{
node.next = node.prev = nil;
if(CheckNil(nil, list.first) && CheckNil(nil, list.last))
{
assert(CheckNil(nil, prev));
list.first = list.last = node;
}
else if(list.first == prev && list.last == prev)
{
list.last = node;
node.prev = prev;
prev.next = node;
}
else if(list.last == prev)
{
prev.next = node;
node.prev = prev;
list.last = node;
}
else
{
node.next = prev.next;
node.prev = prev;
prev.next = node;
}
}
struct Stack(T)
{
Node!(T)* top;
@ -986,6 +1016,8 @@ unittest
DLLRemove(&list, &nodes[1], null);
TestDLList(&list, res1);
DLLInsert(&list, &nodes[1], &nodes[0], null);
}
{ // MemCpy