-
Notifications
You must be signed in to change notification settings - Fork 0
/
fibonacci.go
58 lines (49 loc) · 1.14 KB
/
fibonacci.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// go:generate gen
package sequences
import "math/big"
type bigInt struct{}
type bigRat struct{}
// +gen sequenceGenerator:"Iterator[int64,uint64,*bigInt]"
type FibonacciGenerator struct {
bufferSize int
repeatOne bool
}
func (fg *FibonacciGenerator) newInt64Generator() func() int64 {
var prev, cur int64 = 1, 1
if fg.repeatOne {
prev = 0
}
return func() int64 {
prev, cur = cur, prev+cur
return prev
}
}
func (fg *FibonacciGenerator) newUint64Generator() func() uint64 {
var prev, cur uint64 = 1, 1
if fg.repeatOne {
prev = 0
}
return func() uint64 {
prev, cur = cur, prev+cur
return prev
}
}
func (fg *FibonacciGenerator) newBigIntGenerator() func() *big.Int {
prev, cur := big.NewInt(1), big.NewInt(1)
if fg.repeatOne {
prev.SetInt64(0)
}
return func() *big.Int {
prev, cur = cur, new(big.Int).Add(prev, cur)
return prev
}
}
func (fg *FibonacciGenerator) SetChannelBuffer(size int) {
if size < 0 {
panic("Buffer size cannot be less than zero.")
}
fg.bufferSize = size
}
func NewFibonacciGenerator(repeatInitialOne bool) *FibonacciGenerator {
return &FibonacciGenerator{bufferSize: 5, repeatOne: repeatInitialOne}
}