From b5b5dff0cbcd8163cda9af1d56113d912f61c673 Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Sat, 15 Dec 2018 22:20:59 -0800 Subject: [PATCH] bitfield tests pass --- xenomorph/xenomorph/bitfield.rkt | 35 +++++++------------------------- 1 file changed, 7 insertions(+), 28 deletions(-) diff --git a/xenomorph/xenomorph/bitfield.rkt b/xenomorph/xenomorph/bitfield.rkt index 4cd03728..3314654b 100644 --- a/xenomorph/xenomorph/bitfield.rkt +++ b/xenomorph/xenomorph/bitfield.rkt @@ -7,45 +7,24 @@ approximates https://github.com/mbutterick/restructure/blob/master/src/Bitfield.coffee |# -#;(define/post-decode (xbitfield-decode xb [port-arg (current-input-port)] #:parent [parent #f]) - (define port (->input-port port-arg)) - (parameterize ([current-input-port port]) - )) - -#;(define/pre-encode (xbitfield-encode xb flag-hash [port-arg (current-output-port)] #:parent [parent #f]) - (define port (if (output-port? port-arg) port-arg (open-output-bytes))) - (parameterize ([current-output-port port]) - - (unless port-arg (get-output-bytes port)))) - -#;(define (xbitfield-size xb [valĀ #f] #:parent [parent #f]) - ) - -#;(struct xbitfield xbase (type flags) #:transparent - #:methods gen:xenomorphic - [(define decode xbitfield-decode) - (define xdecode xbitfield-decode) - (define encode xbitfield-encode) - (define size xbitfield-size)]) - (define xbitfield% (class xenobase% (super-new) (init-field type flags) - (define flag-hash (mhasheq)) - (define/augment (xxdecode port parent) + + (define flag-hash (mhasheq)) (define val (send type xxdecode port)) - (for ([(flag i) (in-indexed flags)] + (for ([(flag idx) (in-indexed flags)] #:when flag) - (hash-set! flag-hash flag (bitwise-bit-set? val i))) + (hash-set! flag-hash flag (bitwise-bit-set? val idx))) flag-hash) - (define/augment (xxencode array port [parent #f]) - (define bit-int (for/sum ([(flag i) (in-indexed flags)] + (define/augment (xxencode flag-hash port [parent #f]) + (define bit-int (for/sum ([(flag idx) (in-indexed flags)] #:when (and flag (dict-ref flag-hash flag #f))) - (arithmetic-shift 1 i))) + (arithmetic-shift 1 idx))) (send type xxencode bit-int port)) (define/augment (xxsize [val #f] [parent #f])