-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtype_seq.go
104 lines (90 loc) · 1.63 KB
/
type_seq.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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
101
102
103
104
package mtcl
import (
"fmt"
"math"
"math/big"
)
type Seq struct {
Start *Int
End *Int
Step *Int
}
func (s *Seq) String() string {
return fmt.Sprintf("%v:%v:%v", s.Start, s.End, s.Step)
}
func (*Seq) value() {}
func (*Seq) Type() string { return "seq" }
func (s *Seq) Expand() (values Values) {
iter := s.Iterator()
values = make(Values, 0, s.Len())
var temp [1]Value
for {
ok, err := iter(Values(temp[:]))
if err != nil {
return nil
} else if !ok {
break
}
values = append(values, temp[0])
}
return values
}
func (s *Seq) Len() int {
pos := new(big.Int).Set(&s.Start.Int)
end := &s.End.Int
dir := end.Cmp(pos)
if dir == 0 {
return 1
}
step := &s.Step.Int
if step.Sign() != dir {
return 0 // Cannot compute.
}
delta := new(big.Int).Sub(end, pos)
delta, mod := delta.DivMod(delta, step, new(big.Int))
if mod.Sign() == 0 {
delta = delta.Add(delta, big.NewInt(1))
}
if !delta.IsInt64() {
return math.MaxInt64
}
estimate := delta.Int64()
if estimate < 0 {
estimate = -estimate
}
if estimate > math.MaxInt32 {
return math.MaxInt32
}
return int(estimate)
}
func (s *Seq) Iterator() Iterator {
pos := new(big.Int).Set(&s.Start.Int)
end := &s.End.Int
dir := end.Cmp(pos)
if dir == 0 {
return EmptyIterator()
}
step := &s.Step.Int
if step.Sign() != dir {
return EmptyIterator()
}
next := func() *Int {
if end.Cmp(pos) == -dir {
return nil
}
n := &Int{}
n.Set(pos)
pos = pos.Add(pos, step)
return n
}
return func(out Values) (ok bool, err error) {
for i := range out {
n := next()
if n == nil {
return false, nil
}
out[i] = n
}
return true, nil
}
}