-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerator.go
More file actions
75 lines (66 loc) · 2.07 KB
/
generator.go
File metadata and controls
75 lines (66 loc) · 2.07 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package sqrt
import (
"math/big"
)
// Interface Generator lazily generates the digits of a Number.
type Generator interface {
// Generate returns the digits of the mantissa and the exponent for a
// Number. Numbers are of the form mantissa*10^exp where mantissa is
// is between 0.1 inclusive and 1.0 exclusive. Calling digits() returns
// each digit of the mantissa in turn. The first call to digits() cannot
// return 0 because of the range of values a mantissa can have. If a
// call to digits() returns -1, this means that there are no more digits
// in the mantissa. If the first call to digits() return -1, that means
// that the resulting Number is zero. Once a call to digits() returns -1,
// all successive calls to digits() must also return -1. digits() must
// return values between 0 and 9 or -1.
Generate() (digits func() int, exp int)
}
func newNRootGenerator(
num, denom *big.Int, newManager func() rootManager) Generator {
result := &nrootGenerator{newManager: newManager}
result.num.Set(num)
result.denom.Set(denom)
return result
}
func newRepeatingGenerator(fixed, repeating []int, exp int) Generator {
var result repeatingGenerator
result.fixed = append([]int(nil), fixed...)
result.repeating = append([]int(nil), repeating...)
result.exp = exp
return &result
}
type repeatingGenerator struct {
fixed []int
repeating []int
exp int
}
func (g *repeatingGenerator) Generate() (func() int, int) {
fixedIndex := 0
repeatingIndex := 0
gen := func() int {
if fixedIndex < len(g.fixed) {
temp := g.fixed[fixedIndex]
fixedIndex++
return temp
}
if len(g.repeating) == 0 {
return -1
}
temp := g.repeating[repeatingIndex]
repeatingIndex = (repeatingIndex + 1) % len(g.repeating)
return temp
}
return gen, g.exp
}
type nrootGenerator struct {
num big.Int
denom big.Int
newManager func() rootManager
}
func (g *nrootGenerator) Generate() (func() int, int) {
manager := g.newManager()
groups, exp := computeGroupsFromRational(
&g.num, &g.denom, manager.Base(new(big.Int)))
return computeRootDigits(groups, manager), exp
}