|
|
|
@ -24,9 +24,14 @@ https://github.com/mbutterick/restructure/blob/master/src/Array.coffee
|
|
|
|
|
[(count) (for/list ([i (in-range len)])
|
|
|
|
|
(send type decode stream this))])))
|
|
|
|
|
|
|
|
|
|
(define/override (size array)
|
|
|
|
|
(unless (list? array) (raise-argument-error 'Array:size "list" array))
|
|
|
|
|
(* (send type size) (length array)))
|
|
|
|
|
(define/override (size [array #f])
|
|
|
|
|
(report* _length array)
|
|
|
|
|
(when (and array (not (list? array)))
|
|
|
|
|
(raise-argument-error 'Array:size "list" array))
|
|
|
|
|
(cond
|
|
|
|
|
[(not array) (* (send type size) (resolveLength _length (+DecodeStream) #f))]
|
|
|
|
|
[(Number? _length) (send _length size)]
|
|
|
|
|
[else (* (send type size) (length array))]))
|
|
|
|
|
|
|
|
|
|
(define/augride (encode stream array [parent #f])
|
|
|
|
|
(unless (list? array) (raise-argument-error 'Array:encode "list" array))
|
|
|
|
@ -51,17 +56,17 @@ approximates
|
|
|
|
|
https://github.com/mbutterick/restructure/blob/master/src/LazyArray.coffee
|
|
|
|
|
|#
|
|
|
|
|
|
|
|
|
|
(define-subclass object% (InnerLazyArray type [_length #f] [stream #f] [ctx #f])
|
|
|
|
|
(define-subclass object% (InnerLazyArray type [_length #f] [stream #f] [parent #f])
|
|
|
|
|
(field [base (· stream pos)]
|
|
|
|
|
[items (mhash)]) ; implement with hash (random add) rather than array
|
|
|
|
|
|
|
|
|
|
(define/public-final (get index)
|
|
|
|
|
(when (or (< index 0) (<= _length index))
|
|
|
|
|
(raise-argument-error 'InnerLazyArray:get (format "non-negative index less than length ~a of array" _length) index))
|
|
|
|
|
(raise-argument-error 'InnerLazyArray:get (format "array index between 0 and ~a" _length) index))
|
|
|
|
|
(hash-ref! items index (λ ()
|
|
|
|
|
(define stashed-pos (· stream pos))
|
|
|
|
|
(send stream pos (+ base (* index (send type size))))
|
|
|
|
|
(define new-val (send type decode stream ctx))
|
|
|
|
|
(define new-val (send type decode stream parent))
|
|
|
|
|
(send stream pos stashed-pos)
|
|
|
|
|
new-val)))
|
|
|
|
|
|
|
|
|
@ -74,10 +79,10 @@ https://github.com/mbutterick/restructure/blob/master/src/LazyArray.coffee
|
|
|
|
|
(define/override (decode stream [parent #f])
|
|
|
|
|
(define len (resolveLength _length stream parent))
|
|
|
|
|
(define res (+InnerLazyArray type len stream parent))
|
|
|
|
|
(send stream pos (+ (· stream pos) (* _length (send type size)))) ; skip the bytes that LazyArray would occupy
|
|
|
|
|
(send stream pos (+ (· stream pos) (* len (send type size)))) ; skip the bytes that LazyArray would occupy
|
|
|
|
|
res)
|
|
|
|
|
|
|
|
|
|
(define/override (size val)
|
|
|
|
|
(define/override (size [val #f])
|
|
|
|
|
(super size (if (InnerLazyArray? val)
|
|
|
|
|
(send val toArray)
|
|
|
|
|
val)))
|
|
|
|
@ -91,9 +96,9 @@ https://github.com/mbutterick/restructure/blob/master/src/LazyArray.coffee
|
|
|
|
|
(define bstr #"ABCD1234")
|
|
|
|
|
(define ds (+DecodeStream bstr))
|
|
|
|
|
(define la (+LazyArray uint8 4))
|
|
|
|
|
(define ila (send la decode ds))
|
|
|
|
|
(check-equal? (send ds pos) 4)
|
|
|
|
|
(check-equal? (send ila get 1) 66)
|
|
|
|
|
(check-equal? (send ila get 3) 68)
|
|
|
|
|
(check-equal? (send ds pos) 4)
|
|
|
|
|
(check-equal? (send ila toArray) '(65 66 67 68)))
|
|
|
|
|
(define ila (send la decode ds))
|
|
|
|
|
(check-equal? (send ds pos) 4)
|
|
|
|
|
(check-equal? (send ila get 1) 66)
|
|
|
|
|
(check-equal? (send ila get 3) 68)
|
|
|
|
|
(check-equal? (send ds pos) 4)
|
|
|
|
|
(check-equal? (send ila toArray) '(65 66 67 68)))
|
|
|
|
|