-
Notifications
You must be signed in to change notification settings - Fork 1
/
padright.go
70 lines (56 loc) · 1.5 KB
/
padright.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
package ranges
type padRightResult[T any] struct {
r InputRange[T]
value T
remaining int
}
func (pr *padRightResult[T]) Empty() bool {
return pr.r.Empty() && pr.remaining == 0
}
func (pr *padRightResult[T]) Front() T {
if pr.r.Empty() {
if pr.remaining == 0 {
panic("Front() accessed on an empty PadRight result")
}
return pr.value
}
return pr.r.Front()
}
func (pr *padRightResult[T]) PopFront() {
if pr.r.Empty() {
if pr.remaining != 0 {
pr.remaining--
} else {
panic("PopFront() accessed on an empty PadRight result")
}
} else {
if pr.remaining != 0 {
pr.remaining--
}
pr.r.PopFront()
}
}
type padRightFResult[T any] struct {
padRightResult[T]
}
func (pr *padRightFResult[T]) Save() ForwardRange[T] {
return &padRightFResult[T]{padRightResult[T]{pr.r.(ForwardRange[T]).Save(), pr.value, pr.remaining}}
}
// PadRight adds up to `count` `value` elements to the end of a range to ensure it's at least `count` elements long.
func PadRight[T any](r InputRange[T], value T, count int) InputRange[T] {
if count < 0 {
count = 0
}
return &padRightResult[T]{r, value, count}
}
// PadRightF is `PadRight` returning a ForwardRange
func PadRightF[T any](r ForwardRange[T], value T, count int) ForwardRange[T] {
if count < 0 {
count = 0
}
return &padRightFResult[T]{padRightResult[T]{r, value, count}}
}
// PadRightS is `PadRightF` accepting a slice
func PadRightS[T any](r []T, value T, count int) ForwardRange[T] {
return PadRightF(SliceRange(r), value, count)
}