Avoid pointless scan of entire table in collapse-wildcard-sequences

This commit is contained in:
Tony Garnock-Jones 2014-07-18 21:25:25 -07:00
parent a86a0cd326
commit 1dc38bd9c1
1 changed files with 21 additions and 7 deletions

View File

@ -402,13 +402,27 @@
;; equivalent to (wildcard-sequence m'). This is nearly the inverse of
;; expand-wildseq.
(define (collapse-wildcard-sequences m)
(match m
[(hash-table ((== ?) (and w (wildcard-sequence wk)))
((? key-close?) k))
(if (requal? k wk) w m)]
[(hash-table ((== ?) (and w (wildcard-sequence wk))))
w]
[_ m]))
(if (hash? m)
(case (hash-count m)
[(2)
(if (and (hash-has-key? m ?)
(hash-has-key? m EOS))
(let ((w (hash-ref m ?))
(k (hash-ref m EOS)))
(if (and (wildcard-sequence? w)
(requal? (wildcard-sequence-matcher w) k))
w
m))
m)]
[(1)
(if (hash-has-key? m ?)
(let ((w (hash-ref m ?)))
(if (wildcard-sequence? w)
w
m))
m)]
[else m])
m))
;; Sigma -> Boolean
;; True iff k represents the start of a compound datum.