-
Notifications
You must be signed in to change notification settings - Fork 1
/
uniq.go
150 lines (116 loc) · 3.91 KB
/
uniq.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package ranges
type uniqResult[T any] struct {
r InputRange[T]
cb func(a, b T) bool
}
func (ur *uniqResult[T]) Empty() bool {
return ur.r.Empty()
}
func (ur *uniqResult[T]) Front() T {
return ur.r.Front()
}
func (ur *uniqResult[T]) PopFront() {
front := ur.Front()
ur.r.PopFront()
for !ur.Empty() && ur.cb(front, ur.Front()) {
ur.r.PopFront()
}
}
type uniqForwardResult[T any] struct {
uniqResult[T]
}
func (ufr *uniqForwardResult[T]) Save() ForwardRange[T] {
return &uniqForwardResult[T]{uniqResult[T]{ufr.r.(ForwardRange[T]).Save(), ufr.cb}}
}
type uniqBidirectionalResult[T any] struct {
uniqResult[T]
}
func (ubr *uniqBidirectionalResult[T]) Back() T {
return ubr.r.(BidirectionalRange[T]).Back()
}
func (ubr *uniqBidirectionalResult[T]) PopBack() {
back := ubr.Back()
ubr.r.(BidirectionalRange[T]).PopBack()
for !ubr.Empty() && ubr.cb(ubr.Back(), back) {
ubr.r.(BidirectionalRange[T]).PopBack()
}
}
func (ubr *uniqBidirectionalResult[T]) Save() ForwardRange[T] {
return ubr.SaveB()
}
func (ubr *uniqBidirectionalResult[T]) SaveB() BidirectionalRange[T] {
return &uniqBidirectionalResult[T]{uniqResult[T]{ubr.r.(BidirectionalRange[T]).SaveB(), ubr.cb}}
}
type uniqComparableResult[T comparable] struct {
r InputRange[T]
}
func (ur *uniqComparableResult[T]) Empty() bool {
return ur.r.Empty()
}
func (ur *uniqComparableResult[T]) Front() T {
return ur.r.Front()
}
func (ur *uniqComparableResult[T]) PopFront() {
front := ur.Front()
ur.r.PopFront()
for !ur.Empty() && front == ur.Front() {
ur.r.PopFront()
}
}
type uniqComparableForwardResult[T comparable] struct {
uniqComparableResult[T]
}
func (ucfr *uniqComparableForwardResult[T]) Save() ForwardRange[T] {
return &uniqComparableForwardResult[T]{uniqComparableResult[T]{ucfr.r.(ForwardRange[T]).Save()}}
}
type uniqComparableBidirectionalResult[T comparable] struct {
uniqComparableResult[T]
}
func (ucbr *uniqComparableBidirectionalResult[T]) Back() T {
return ucbr.r.(BidirectionalRange[T]).Back()
}
func (ucbr *uniqComparableBidirectionalResult[T]) PopBack() {
back := ucbr.Back()
ucbr.r.(BidirectionalRange[T]).PopBack()
for !ucbr.Empty() && ucbr.Back() == back {
ucbr.r.(BidirectionalRange[T]).PopBack()
}
}
func (ucbr *uniqComparableBidirectionalResult[T]) Save() ForwardRange[T] {
return ucbr.SaveB()
}
func (ucbr *uniqComparableBidirectionalResult[T]) SaveB() BidirectionalRange[T] {
return &uniqComparableBidirectionalResult[T]{uniqComparableResult[T]{ucbr.r.(BidirectionalRange[T]).SaveB()}}
}
// Uniq removes adjacent entries where `cb(a, b) == true`
func Uniq[T any](r InputRange[T], cb func(a, b T) bool) InputRange[T] {
return &uniqResult[T]{r, cb}
}
// UniqF is `Uniq` where the range can be saved.
func UniqF[T any](r ForwardRange[T], cb func(a, b T) bool) ForwardRange[T] {
return &uniqForwardResult[T]{uniqResult[T]{r, cb}}
}
// UniqB is `UniqF` that can be shrunk from the back.
func UniqB[T any](r BidirectionalRange[T], cb func(a, b T) bool) BidirectionalRange[T] {
return &uniqBidirectionalResult[T]{uniqResult[T]{r, cb}}
}
// UniqS is `UniqB` accepting a slice.
func UniqS[T any](r []T, cb func(a, b T) bool) BidirectionalRange[T] {
return UniqB(SliceRange(r), cb)
}
// UniqComparable removes adjacent entries where `a == b`
func UniqComparable[T comparable](r InputRange[T]) InputRange[T] {
return &uniqComparableResult[T]{r}
}
// UniqComparableF is `UniqComparable` where the range can be saved.
func UniqComparableF[T comparable](r ForwardRange[T]) ForwardRange[T] {
return &uniqComparableForwardResult[T]{uniqComparableResult[T]{r}}
}
// UniqComparableB is `UniqComparableF` that can be shrunk from the back.
func UniqComparableB[T comparable](r BidirectionalRange[T]) BidirectionalRange[T] {
return &uniqComparableBidirectionalResult[T]{uniqComparableResult[T]{r}}
}
// UniqComparableS is `UniqComparableB` accepting a slice.
func UniqComparableS[T comparable](r []T) BidirectionalRange[T] {
return UniqComparableB(SliceRange(r))
}