Login
7 branches 0 tags
Ben (Win10) Fixed some calloc issues on DOS 89d8095 3 years ago 476 Commits
nujel / stdlib / collections / tree.nuj
; Nujel - Copyright (C) 2020-2021 - Benjamin Vincent Schulenburg
; This project uses the MIT license, a copy should be included under /LICENSE

;; Some functions about trees

[defun tree/zip [keys values]
       "Return a tree where KEYS point to VALUES"
       [def ret @[]]
       [for-in [key keys]
	      [tree/set! ret key [car values]]
	      [cdr! values]]
       ret]

[defun tree/+= [t k v]
       "Increment value at K in T by V"
       [tree/set! t k [+ v [int [tree/get t k]]]]]

[defmacro tree/-= [t k v]
       "Decrement value at K in T by V"
       `[tree/+= ~t ~k [- ~v]]]

[defmacro tree/++ [t k]
       "Increment value at K in T by 1"
       `[tree/+= ~t ~k 1]]

[defmacro tree/-- [t k]
       "Increment value at K in T by 1"
       `[tree/-= ~t ~k 1]]

[defun tree/equal? [a b]
       "Compares two trees for equality"
       [if [and [tree? a]
                [tree? b]]
           [and [== [tree/key* a]
                    [tree/key* b]]
                [equal? [tree/value* a]
                        [tree/value* b]]
                [tree/equal? [tree/left* a]
                             [tree/left* b]]
                [tree/equal? [tree/right* a]
                             [tree/right* b]]]
           [equal? a b]]]