Index: src/0dev.org/encoding/fibonacci/fib.go ================================================================== --- src/0dev.org/encoding/fibonacci/fib.go +++ src/0dev.org/encoding/fibonacci/fib.go @@ -17,13 +17,26 @@ // Alias type with methods for encoding and decoding integers type Numbers []uint64 var ( - // Used for encoding and decoding byte values - bytesCodec = New(14) + // Used for decoding byte values + codec Numbers + // Used for encoding byte values + // The lower 16 bits store the encoded value itself + // while the remaining upper ones store its length + lookup [256]uint32 ) + +func init() { + codec = New(16) + for i := uint64(0); i < 256; i++ { + val, len := codec.Code(i) + lookup[i] |= uint32(val) + lookup[i] |= uint32(len) << 16 + } +} // Returns a slice with fibonacci numbers up to the given length func New(size int) Numbers { var fibs Numbers = make(Numbers, size) copy(fibs, []uint64{1, 1}) @@ -106,11 +119,11 @@ return 0, err } for _, currentByte := range input { // Get the fibonacci code and bit length for the current byte - enc, len := bytesCodec.Code(uint64(currentByte)) + enc, len := uint16(lookup[currentByte]), byte(lookup[currentByte]>>16) // Add current bits to higher positions e.remaining |= byte(enc << e.length) // maximum length of added bits to e.remaining @@ -187,11 +200,11 @@ ) start: // While we have suitable buffered data and enough output space for (len(output) > 0) && ((d.buffer & (d.buffer >> 1)) > 0) { - val, len := bytesCodec.Decode(d.buffer) + val, len := codec.Decode(d.buffer) // Store the decoded byte output[0] = byte(val) // Advance the internal and output buffers