From 40b03b571413a10f5b847e4e59b1ec6297b2eb21 Mon Sep 17 00:00:00 2001 From: Peter McGoron Date: Wed, 4 Sep 2024 01:12:55 -0400 Subject: [PATCH] set:refactor maps --- set.scm | 57 ++++++++++++++++++++++++++++----------------------------- 1 file changed, 28 insertions(+), 29 deletions(-) diff --git a/set.scm b/set.scm index 94a6e1d..2334c93 100644 --- a/set.scm +++ b/set.scm @@ -289,6 +289,30 @@ (lambda (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 ;;; ;;;;;;;;;;; @@ -309,37 +333,12 @@ (list-set! (cdr lst) (- n 1) val))))) (define map:string<=> (set:<=>-to-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 -;;; (UPDATE NODE-WITH-KEY NODE), where NODE-WITH-KEY is an empty node -;;; with the key KEY, and (UPDATE NODE-WITH-KEY #F) if no NODE is -;;; found. -(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 '())))) +(define smap:update (map:update %smap:update)) +(define smap:insert (map:insert %smap:update)) +(define smap:search (map:search map:string<=>)) +(define smap:delete (map:delete map:string<=>)) ;;; ;;;;; ;;; Tests