-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdrop.go
129 lines (99 loc) · 2.7 KB
/
drop.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package ranges
// dropResult implements Drop
type dropResult[T any] struct {
remaining int
ir InputRange[T]
}
func (dr *dropResult[T]) prime() {
for dr.remaining != 0 && !dr.ir.Empty() {
dr.ir.PopFront()
dr.remaining--
}
}
func (dr *dropResult[T]) Empty() bool {
dr.prime()
return dr.ir.Empty()
}
func (dr *dropResult[T]) Front() T {
dr.prime()
return dr.ir.Front()
}
func (dr *dropResult[T]) PopFront() {
dr.prime()
dr.ir.PopFront()
}
// dropForwardResult implements DropF
type dropForwardResult[T any] struct {
dropResult[T]
}
func (dr *dropForwardResult[T]) Save() ForwardRange[T] {
return &dropForwardResult[T]{dropResult[T]{dr.remaining, dr.ir.(ForwardRange[T]).Save()}}
}
// dropBidirectionalResult implements DropB
type dropBidirectionalResult[T any] struct {
dropForwardResult[T]
}
func (dr *dropBidirectionalResult[T]) PopBack() {
dr.prime()
dr.ir.(BidirectionalRange[T]).PopBack()
}
func (dr *dropBidirectionalResult[T]) Back() T {
dr.prime()
return dr.ir.(BidirectionalRange[T]).Back()
}
func (dr *dropBidirectionalResult[T]) SaveB() BidirectionalRange[T] {
return &dropBidirectionalResult[T]{
dropForwardResult[T]{dropResult[T]{dr.remaining, dr.ir.(BidirectionalRange[T]).SaveB()}},
}
}
// dropRandomAccessResult implements `DropR`
type dropRandomAccessResult[T any] struct {
dropBidirectionalResult[T]
}
func (dr *dropRandomAccessResult[T]) Get(index int) T {
dr.prime()
return dr.ir.(RandomAccessRange[T]).Get(index)
}
func (dr *dropRandomAccessResult[T]) Len() int {
dr.prime()
return dr.ir.(RandomAccessRange[T]).Len()
}
func (dr *dropRandomAccessResult[T]) SaveR() RandomAccessRange[T] {
return &dropRandomAccessResult[T]{
dropBidirectionalResult[T]{
dropForwardResult[T]{
dropResult[T]{dr.remaining, dr.ir.(RandomAccessRange[T]).SaveR()},
},
},
}
}
// Drop creates a range without the first up to `count` elements
func Drop[T any](r InputRange[T], count int) InputRange[T] {
if count < 0 {
count = 0
}
return &dropResult[T]{count, r}
}
// DropF is `Drop` where the range can be saved.
func DropF[T any](r ForwardRange[T], count int) ForwardRange[T] {
if count < 0 {
count = 0
}
return &dropForwardResult[T]{dropResult[T]{count, r}}
}
// DropB is `DropF` that can be shrunk from the back.
func DropB[T any](r BidirectionalRange[T], count int) BidirectionalRange[T] {
if count < 0 {
count = 0
}
return &dropBidirectionalResult[T]{dropForwardResult[T]{dropResult[T]{count, r}}}
}
// DropR is `DropB` permitting random access.
func DropR[T any](r RandomAccessRange[T], count int) RandomAccessRange[T] {
if count < 0 {
count = 0
}
return &dropRandomAccessResult[T]{
dropBidirectionalResult[T]{dropForwardResult[T]{dropResult[T]{count, r}}},
}
}