-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsequence.go
69 lines (60 loc) · 1.39 KB
/
sequence.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
package tokei
import (
"errors"
"sort"
)
// enumerator is anything which can enumerate a list of ints.
type enumerator interface {
Enumerate() []int
}
// sequence describes a sequence of ints which can be enumerated.
type sequence struct {
start int
end int
step int
}
// newSequence creates a new sequence.
func newSequence(start, end, step int) (*sequence, error) {
if step < 0 || end < start {
return nil, errors.New("invalid sequence")
}
return &sequence{
start: start,
end: end,
step: step,
}, nil
}
// Enumerate returns a list of all ints in this sequence.
func (s sequence) Enumerate() []int {
output := make([]int, 0)
for i := s.start; i <= s.end; i += s.step {
output = append(output, i)
}
return output
}
// irregularSequence is a sequence which doesn't follow a regular pattern.
type irregularSequence struct {
entries []int
}
// newIrregularSequence creates a sequence from a list of ints.
func newIrregularSequence(entries []int) irregularSequence {
return irregularSequence{
entries: entries,
}
}
// Enumerate returns the list of ints, in order.
// It removes any duplicates.
func (s irregularSequence) Enumerate() []int {
output := make([]int, 0, len(s.entries))
seen := map[int]struct{}{}
for _, num := range s.entries {
_, ok := seen[num]
if ok {
continue
}
output = append(output, num)
seen[num] = struct{}{}
}
sort.Ints(output)
return output
}