From d83d1cc128516e264fd9b09a60db191e5b68cd44 Mon Sep 17 00:00:00 2001 From: Ed_ Date: Tue, 5 Mar 2024 10:16:23 -0500 Subject: [PATCH] Added push_back/pop_back for doubly-linked lists --- code/grime_linked_list.odin | 54 ++++++++++++++++++++++++++++--------- 1 file changed, 42 insertions(+), 12 deletions(-) diff --git a/code/grime_linked_list.odin b/code/grime_linked_list.odin index 0a296e8..dcf4fd7 100644 --- a/code/grime_linked_list.odin +++ b/code/grime_linked_list.odin @@ -2,32 +2,47 @@ // The code takes advantage of macros for the linked list interface in a way that odin doesn't really permit without a much worse interface. package sectr +LL_Node :: struct ( $ Type : typeid ) { + value : ^Type, + next : ^LL_Node(Type), +} + DLL_Node :: struct ( $ Type : typeid ) #raw_union { using _ : struct { - left, right : ^ Type, + left, right : ^Type, }, using _ : struct { - prev, next : ^ Type, + prev, next : ^Type, }, using _ : struct { - first, last : ^ Type, + first, last : ^Type, }, + using _ : struct { + bottom, top : ^Type, + } } DLL_NodeFull :: struct ( $ Type : typeid ) { - first, last, prev, next : ^ Type, -} - -DLL_NodeLR :: struct ( $ Type : typeid ) { - left, right : ^ Type, + using _ : DLL_NodeFL(Type), + prev, next : ^Type, } DLL_NodePN :: struct ( $ Type : typeid ) { - prev, next : ^ Type, + using _ : struct { + prev, next : ^Type, + } + using _ : struct { + left, right : ^Type, + } } -DLL_NodeFL :: struct ( $ Type : typeid ) { - first, last : ^ Type, +DLL_NodeFL :: struct ( $ Type : typeid ) #raw_union { + using _ : struct { + first, last : ^Type, + } + using _ : struct { + bottom, top: ^Type, + } } type_is_node :: #force_inline proc "contextless" ( $ Type : typeid ) -> bool @@ -36,7 +51,22 @@ type_is_node :: #force_inline proc "contextless" ( $ Type : typeid ) -> bool return type_has_field( type_elem_type(Type), "prev" ) && type_has_field( type_elem_type(Type), "next" ) } -dll_full_insert_raw :: proc "contextless" ( null : ^($ Type), parent, pos, node : ^ Type ) +dll_push_back :: #force_inline proc "contextless" ( current_ptr : ^(^ ($ Type)), node : ^Type ) { + current := (current_ptr ^) + + current.prev = current + current.next = node + (current_ptr ^) = node +} + +dll_pop_back :: #force_inline proc "contextless" ( current_ptr : ^(^ ($ Type)), node : ^Type ) { + current := (current_ptr ^) + + current.next = nil + (current_ptr ^) = node +} + +dll_full_insert_raw :: proc "contextless" ( null : ^($ Type), parent, pos, node : ^Type ) { if parent.first == null { parent.first = node