set:refactor maps

This commit is contained in:
Peter McGoron 2024-09-04 01:12:55 -04:00
parent ad49a18de5
commit 40b03b5714
1 changed files with 28 additions and 29 deletions

57
set.scm
View File

@ -289,6 +289,30 @@
(lambda (node) (lambda (node)
(cdr (set:get node '=)))) (cdr (set:get node '=))))
(define map:update
(lambda (%update-recursive)
(lambda (tree key update)
(%update-recursive tree (map:empty-node key '())
update))))
(define map:insert
(lambda (%update-recursive)
(let ((insert (set:insert %update-recursive)))
(lambda (tree key val)
(insert tree (map:empty-node key val))))))
(define map:search
(lambda (<=>)
(let ((search (set:in <=>)))
(lambda (tree key)
(search tree (cons key '()))))))
(define map:delete
(lambda (<=>)
(let ((delete (set:delete <=>)))
(lambda (tree key)
(delete tree (map:empty-node key '()))))))
;;; ;;;;;;;;;;; ;;; ;;;;;;;;;;;
;;; For strings ;;; For strings
;;; ;;;;;;;;;;; ;;; ;;;;;;;;;;;
@ -309,37 +333,12 @@
(list-set! (cdr lst) (- n 1) val))))) (list-set! (cdr lst) (- n 1) val)))))
(define map:string<=> (set:<=>-to-map string<=>)) (define map:string<=> (set:<=>-to-map string<=>))
(define %smap:update (set:update map:string<=>)) (define %smap:update (set:update map:string<=>))
(define %smap:insert (set:insert %smap:update))
;;; (SMAP:UPDATE TREE KEY UPDATE) updates NODE in TREE with (define smap:update (map:update %smap:update))
;;; (UPDATE NODE-WITH-KEY NODE), where NODE-WITH-KEY is an empty node (define smap:insert (map:insert %smap:update))
;;; with the key KEY, and (UPDATE NODE-WITH-KEY #F) if no NODE is (define smap:search (map:search map:string<=>))
;;; found. (define smap:delete (map:delete map:string<=>))
(define smap:update
(lambda (tree key update)
(%smap:update tree (map:empty-node key '()) update)))
;;; (SMAP:INSERT TREE KEY VAL) inserts (CONS KEY VAL) into TREE, and
;;; returns (CONS NEWROOT FOUND), where NEWROOT is the new root of
;;; the tree, and FOUND is #F if no element matching KEY was found,
;;; or the matching element if found.
(define smap:insert
(lambda (tree key val)
(%smap:insert tree (map:empty-node key val))))
(define %smap:search (set:in map:string<=>))
;;; (SMAP:SEARCH TREE KEY)
(define smap:search
(lambda (tree key)
(%smap:search tree (cons key '()))))
(define %smap:delete (set:delete map:string<=>))
(define smap:delete
(lambda (tree key)
(%smap:delete tree (map:empty-node key '()))))
;;; ;;;;; ;;; ;;;;;
;;; Tests ;;; Tests