 USING: accessors arrays assocs byte-vectors combinators
 combinators.smart compression.huffman fry hashtables io.binary
 kernel literals locals math math.bitwise math.order math.ranges
-sequences sorting ;
+sequences sorting memoize combinators.short-circuit ;
 QUALIFIED-WITH: bitstreams bs
 IN: compression.inflate
 
 <PRIVATE
 
-: enum>seq ( assoc -- seq )
-    dup keys [ ] [ max ] map-reduce 1 + f <array>
-    [ '[ swap _ set-nth ] assoc-each ] keep ;
-
 ERROR: zlib-unimplemented ;
 ERROR: bad-zlib-data ;
 ERROR: bad-zlib-header ;
-    
+
 :: check-zlib-header ( data -- )
     16 data bs:peek 2 >le be> 31 mod    ! checksum
-    0 assert=                           
+    0 assert=
     4 data bs:read 8 assert=            ! compression method: deflate
     4 data bs:read                      ! log2(max length)-8, 32K max
-    7 <= [ bad-zlib-header ] unless     
-    5 data bs:seek                      ! drop check bits 
+    7 <= [ bad-zlib-header ] unless
+    5 data bs:seek                      ! drop check bits
     1 data bs:read 0 assert=            ! dictionnary - not allowed in png
     2 data bs:seek                      ! compression level; ignore
     ;
 
 CONSTANT: clen-shuffle { 16 17 18 0 8 7 9 6 10 5 11 4 12 3 13 2 14 1 15 }
 
-: get-table ( values size -- table ) 
-    16 f <array> clone <enum> 
+: get-table ( values size -- table )
+    16 f <array> clone <enum>
     [ '[ _ push-at ] 2each ] keep seq>> 1 tail [ natural-sort ] map ;
 
 :: decode-huffman-tables ( bitstream -- tables )


CONSTANT: dist-table
    {
        1 2 3 4 
        5 7 9 13 
        17 25 33 49
        65 97 129 193
        257 385 513 769

    clen-shuffle swap head
    dup [ drop 3 bitstream bs:read ] map
    get-table
    bitstream swap <huffman-decoder>
    [ 2dup + ] dip swap :> k!
    '[
        _ read1-huff2
