CS3700-project2/radix-tree.rkt

118 lines
4.3 KiB
Racket
Raw Normal View History

#lang racket
2020-02-01 02:19:55 +00:00
(require "iputil.rkt")
2020-02-01 04:39:42 +00:00
(provide make-rt rt-update! rt-lookup rt-dump rt-flatten)
(struct rt-node [edge0 edge1 data] #:transparent #:mutable)
(struct rt-edge [label target] #:transparent)
2020-02-01 02:27:51 +00:00
(define empty-node-data (gensym 'MEOW))
2020-02-01 02:19:55 +00:00
(define (make-rt)
2020-02-01 02:19:55 +00:00
(rt-node #f #f empty-node-data))
;; Helper functions for rt-node struct access based on whether the edge is 1 or 0
(define (rt-getter bit) (if bit rt-node-edge1 rt-node-edge0))
(define (rt-setter bit) (if bit set-rt-node-edge1! set-rt-node-edge0!))
2020-02-01 02:19:55 +00:00
(define (bl-common-len bl1 bl2)
(for/sum ([b1 (in-list bl1)]
[b2 (in-list bl2)])
#:break (not (equal? b1 b2))
1))
2020-02-01 03:11:42 +00:00
(define (rt-partial-iterate node key [visited '()])
(cond
2020-02-01 02:19:55 +00:00
[(empty? key) (list 'exact node)]
[else
2020-02-01 02:19:55 +00:00
(let* ([bit (first key)]
[getter (rt-getter bit)]
[next-edge (getter node)]
[next-label (and next-edge (rt-edge-label next-edge))]
[next-target (and next-edge (rt-edge-target next-edge))]
2020-02-01 02:19:55 +00:00
[next-common-len (and next-label (bl-common-len next-label key))])
(cond
2020-02-01 02:19:55 +00:00
[(and next-edge (= next-common-len (length next-label)))
2020-02-01 03:11:42 +00:00
(rt-partial-iterate next-target (drop key next-common-len) (cons node visited))]
[next-edge (list 'partial node next-edge next-common-len key (cons node visited))]
[else (list 'no-match node key (cons node visited))]))]))
2020-02-01 02:19:55 +00:00
(define (rt-update! node key updater failure-result)
(define (insert-node! node key)
(let* ([bit (first key)]
[setter! (rt-setter bit)])
2020-02-01 02:19:55 +00:00
(setter! node (rt-edge key (rt-node #f #f (updater (failure-result)))))))
(define (split-node! node key orig-edge prefix-len)
(let* ([bit (first key)]
[setter! (rt-setter bit)]
[orig-label (rt-edge-label orig-edge)]
[orig-target (rt-edge-target orig-edge)]
2020-02-01 02:19:55 +00:00
[new-orig-edge (rt-edge (drop orig-label prefix-len) orig-target)]
[new-insert-edge (rt-edge (drop key prefix-len)
(rt-node #f #f (updater (failure-result))))]
[diff-bit (list-ref key prefix-len)]
[common-node (rt-node (if diff-bit new-orig-edge new-insert-edge)
(if diff-bit new-insert-edge new-orig-edge)
2020-02-01 02:19:55 +00:00
empty-node-data)]
[common-edge (rt-edge (take key prefix-len) common-node)])
(setter! node common-edge)))
(match (rt-partial-iterate node key)
2020-02-01 02:19:55 +00:00
[(list 'exact node)
(set-rt-node-data!
node
(let ([d (rt-node-data node)])
(updater
(if (eq? d empty-node-data) (failure-result) d))))]
2020-02-01 03:11:42 +00:00
[(list 'partial node orig-edge prefix-len partial-key visited)
2020-02-01 02:19:55 +00:00
(split-node! node partial-key orig-edge prefix-len)]
2020-02-01 03:11:42 +00:00
[(list 'no-match node partial-key visited)
2020-02-01 02:19:55 +00:00
(insert-node! node partial-key)]))
2020-02-01 03:22:00 +00:00
(define (rt-lookup node key failure-result)
2020-02-01 03:11:42 +00:00
(define (find-first-with-data nodes)
2020-02-01 03:22:00 +00:00
(or (for/first ([node (in-list nodes)]
#:when (not (eq? empty-node-data (rt-node-data node))))
(rt-node-data node))
(failure-result)))
(find-first-with-data
(match (rt-partial-iterate node key)
[(list 'exact node)
(list node)]
[(list _ ... visited)
visited])))
2020-02-01 04:39:42 +00:00
(define (rt-flatten node)
(if node
(let* ([e1 (rt-node-edge0 node)]
[e2 (rt-node-edge1 node)]
[data (rt-node-data node)]
[rst (append (rt-flatten e1) (rt-flatten e2))])
(if (equal? data empty-node-data) rst (cons data rst)))
'()))
; (define test (make-rt))
; (define (test-insert! x)
2020-02-01 02:19:55 +00:00
; (rt-update!
; test
; (map (curry equal? #\1) (string->list x))
; (lambda (_) x)
; (lambda () x)))
; (test-insert! "0001")
2020-02-01 02:19:55 +00:00
; ; (test-insert! "1000")
; ; (test-insert! "1010")
; (test-insert! "0011")
; (test-insert! "0000")
2020-02-01 02:19:55 +00:00
(define (rt-dump node [prefix ""])
(displayln (format "~a node ~a" prefix (rt-node-data node)))
(define edge0 (rt-node-edge0 node))
(define edge1 (rt-node-edge1 node))
(when edge0
(displayln (format "~a edge0 ~a" prefix (rt-edge-label edge0)))
(rt-dump (rt-edge-target edge0) (string-append prefix " ")))
(when edge1
(displayln (format "~a edge1 ~a" prefix (rt-edge-label edge1)))
(rt-dump (rt-edge-target edge1) (string-append prefix " "))))
2020-02-01 03:22:00 +00:00
;(rt-dump test)