11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
// 0 - 11, 1 - 110, 2 - 1100, 3 - 1101, 4 - 11000
package fibonacci
import (
"io"
)
type Numbers []uint64
var (
// Used for encoding and decoding byte values
bytesCodec = New(14)
)
|
>
|
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
// 0 - 11, 1 - 110, 2 - 1100, 3 - 1101, 4 - 11000
package fibonacci
import (
"io"
)
// Alias type with methods for encoding and decoding integers
type Numbers []uint64
var (
// Used for encoding and decoding byte values
bytesCodec = New(14)
)
|
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
value >>= 1
length++
}
return result + f[length] - 1, length + 1
}
func Writer(target io.Writer) io.Writer {
var enc encoder
enc.target = target
return &enc
}
type encoder struct {
target io.Writer
remaining byte
length byte
}
func (e *encoder) Write(input []byte) (int, error) {
var (
total int
err error
)
// Flush on a nil slice
|
>
|
>
|
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
|
value >>= 1
length++
}
return result + f[length] - 1, length + 1
}
// Returns a fibonacci encoder over the provided io.Writer
func Encoder(target io.Writer) io.Writer {
var enc encoder
enc.target = target
return &enc
}
type encoder struct {
target io.Writer
remaining byte
length byte
}
// Implements io.Writer
func (e *encoder) Write(input []byte) (int, error) {
var (
total int
err error
)
// Flush on a nil slice
|
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
// Account for the processed input byte
total++
}
return total, err
}
func Reader(source io.Reader) io.Reader {
var dec decoder
dec.source = source
return &dec
}
type decoder struct {
source io.Reader
buffer uint64
at byte
}
func (d *decoder) Read(output []byte) (int, error) {
var (
total int
err error
)
start:
|
>
|
>
|
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
// Account for the processed input byte
total++
}
return total, err
}
// Returns a fibonacci decoder over the provided io.Reader
func Decoder(source io.Reader) io.Reader {
var dec decoder
dec.source = source
return &dec
}
type decoder struct {
source io.Reader
buffer uint64
at byte
}
// Implements io.Reader
func (d *decoder) Read(output []byte) (int, error) {
var (
total int
err error
)
start:
|