; =============================================================================
;
; Litos - Tree list
;
; =============================================================================
CODE_SECTION 32
; ------------- Macro - initialized tree list entry
%macro TREEHEAD 0
LISTHEAD ; sibling list
dd NULL ; no children
dd NULL ; pointer to parent (NULL=none)
%endmacro
; ------------- Macro - initialize tree list entry (%1=tree list entry)
%macro TREEINIT 1
LISTINIT %1 ; initialize sibling list
and dword [%1+TREE_Child],byte 0 ; no children
and dword [%1+TREE_Parent],byte 0 ; no parent
%endmacro
; ------------- Macro - add (initialized) tree list entry into tree list
; %1=parent, %2=tree list entry, %3 and %4=temporary registers
%macro TREEADD 4
mov %3,[%1+TREE_Child] ; get a child
or %3,%3 ; is any child?
jz %%L ; there is no child
LISTADD %3,%2,%4 ; add new entry into sibling list
%%L: mov [%1+TREE_Child],%2 ; set new entry as new child
mov [%2+TREE_Parent],%1 ; link parent to new entry
%endmacro
; ------------- Macro - delete tree list entry from the list
; %1=parent, %2=tree list entry, %3 and %4=temporary registers
%macro TREEDEL 4
LISTDEL %2,%3,%4 ; detach from sibling list
cmp %2,%3 ; is list empty?
jne %%L ; list is not empty
xor %3,%3 ; invalid child
%%L: mov [%1+TREE_Child],%3 ; set new child
%endmacro
; -----------------------------------------------------------------------------
; Initialize tree list entry
; -----------------------------------------------------------------------------
; INPUT: EBX = tree list entry
; -----------------------------------------------------------------------------
TreeInit: TREEINIT ebx ; initialize tree list entry
ret
; -----------------------------------------------------------------------------
; Add (initialized) new tree list entry into tree list
; -----------------------------------------------------------------------------
; INPUT: EAX = new tree list entry (must be initialized)
; EBX = parent tree list entry
; -----------------------------------------------------------------------------
TreeAdd: push ecx ; push ECX
push edx ; push EDX
TREEADD ebx,eax,ecx,edx ; add new tree list entry into the list
pop edx ; pop EDX
pop ecx ; pop ECX
ret
; -----------------------------------------------------------------------------
; Delete tree list entry from the list
; -----------------------------------------------------------------------------
; INPUT: EAX = tree list entry to delete
; EBX = parent tree list entry
; -----------------------------------------------------------------------------
TreeDel: push ecx ; push ECX
push edx ; push EDX
TREEDEL ebx,eax,ecx,edx ; delete tree list entry
pop edx ; pop EDX
pop ecx ; pop ECX
ret
|