Fix rt-lookup lol

This commit is contained in:
xenia 2020-01-31 22:11:42 -05:00
parent c9b6e44fc7
commit 0ee1393fd6
2 changed files with 19 additions and 10 deletions

View File

@ -60,8 +60,11 @@
(subnet-mask sub)))
(define (subnet->bl sub)
(for/list ([i (in-range (subnet-mask sub))])
(= 1 (bitwise-and 1 (arithmetic-shift (subnet-ip sub) (- i 31))))))
(ip->bl (subnet-ip sub) (subnet-mask sub)))
(define (ip->bl ip [bits 32])
(for/list ([i (in-range bits)])
(= 1 (bitwise-and 1 (arithmetic-shift ip (- i 31))))))
(define (string->peer str)
(match-define (list ip type) (string-split str "-"))

View File

@ -22,7 +22,7 @@
#:break (not (equal? b1 b2))
1))
(define (rt-partial-iterate node key)
(define (rt-partial-iterate node key [visited '()])
(cond
[(empty? key) (list 'exact node)]
[else
@ -34,9 +34,9 @@
[next-common-len (and next-label (bl-common-len next-label key))])
(cond
[(and next-edge (= next-common-len (length next-label)))
(rt-partial-iterate next-target (drop key next-common-len))]
[next-edge (list 'partial node next-edge next-common-len key)]
[else (list 'no-match node key)]))]))
(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))]))]))
(define (rt-update! node key updater failure-result)
(define (insert-node! node key)
@ -64,15 +64,21 @@
(let ([d (rt-node-data node)])
(updater
(if (eq? d empty-node-data) (failure-result) d))))]
[(list 'partial node orig-edge prefix-len partial-key)
[(list 'partial node orig-edge prefix-len partial-key visited)
(split-node! node partial-key orig-edge prefix-len)]
[(list 'no-match node partial-key)
[(list 'no-match node partial-key visited)
(insert-node! node partial-key)]))
(define (rt-lookup node key)
(define (find-first-with-data nodes)
(for/first ([node nodes]
#:when (not (eq? empty-node-data (rt-node-data node))))
(rt-node-data node)))
(match (rt-partial-iterate node key)
[(list 'exact node) (rt-node-data node)]
[_ #f]))
[(list 'exact node)
(let ([d (rt-node-data node)])
(if (eq? empty-node-data d) #f d))]
[(list sym . rst) (find-first-with-data (last rst))]))
; (define test (make-rt))
; (define (test-insert! x)