-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpartition.go
284 lines (255 loc) · 9.52 KB
/
partition.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package goip
import "math/big"
var (
_ SpanPartitionConstraint[*IPAddress]
_ SpanPartitionConstraint[*IPv4Address]
_ SpanPartitionConstraint[*IPv6Address]
_ SpanPartitionConstraint[*IPAddressSection]
_ SpanPartitionConstraint[*IPv4AddressSection]
_ SpanPartitionConstraint[*IPv6AddressSection]
_ IteratePartitionConstraint[*Address]
_ IteratePartitionConstraint[*IPAddress]
_ IteratePartitionConstraint[*IPv4Address]
_ IteratePartitionConstraint[*IPv6Address]
_ IteratePartitionConstraint[*MACAddress]
_ IteratePartitionConstraint[*IPAddressSection]
_ IteratePartitionConstraint[*IPv4AddressSection]
_ IteratePartitionConstraint[*IPv6AddressSection]
_ IteratePartitionConstraint[*MACAddressSection]
_ MappedPartition[*Address, any] = ApplyForEach[*Address, any](nil, nil)
_ MappedPartition[*IPAddress, any] = ApplyForEach[*IPAddress, any](nil, nil)
_ MappedPartition[*IPv4Address, any] = ApplyForEach[*IPv4Address, any](nil, nil)
_ MappedPartition[*IPv6Address, any] = ApplyForEach[*IPv6Address, any](nil, nil)
_ MappedPartition[*MACAddress, any] = ApplyForEach[*MACAddress, any](nil, nil)
)
// MappedPartition is a mapping from the address types in a [Partition] to values of a generic type V.
type MappedPartition[T GenericKeyConstraint[T], V any] map[Key[T]]V
// Partition is a collection of items (such as addresses)
// partitioned from an original item (such as a subnet).
// Much like an iterator,
// the elements of a partition can be iterated just once (using the iterator,
// using ForEach, or using any other iteration),
// after which it becomes empty.
type Partition[T any] struct {
single T
original T
hasSingle bool
iterator Iterator[T]
count *big.Int
}
// ForEach calls the given action on each partition element.
func (part *Partition[T]) ForEach(action func(T)) {
if part.iterator == nil {
if part.hasSingle {
part.hasSingle = false
action(part.single)
}
} else {
iterator := part.iterator
for iterator.HasNext() {
action(iterator.Next())
}
part.iterator = nil
}
}
// Iterator provides an iterator to iterate through each element of the partition.
func (part *Partition[T]) Iterator() Iterator[T] {
if part.iterator == nil {
if part.hasSingle {
part.hasSingle = false
res := &singleIterator[T]{original: part.single}
return res
}
return nil
}
res := part.iterator
part.iterator = nil
return res
}
func (part *Partition[T]) predicateForEach(predicate func(T) bool, returnEarly bool) bool {
if part.iterator == nil {
return predicate(part.single)
}
result := true
iterator := part.iterator
for iterator.HasNext() {
if !predicate(iterator.Next()) {
result = false
if returnEarly {
break
}
}
}
return result
}
// PredicateForEach applies the supplied predicate operation to each element of the partition,
// returning true if they all return true, false otherwise
func (part *Partition[T]) PredicateForEach(predicate func(T) bool) bool {
return part.predicateForEach(predicate, false)
}
func (part *Partition[T]) predicateForAny(predicate func(address T) bool, returnEarly bool) bool {
return !part.predicateForEach(func(addr T) bool {
return !predicate(addr)
}, returnEarly)
}
// PredicateForAny applies the supplied predicate operation to each element of the partition,
// returning true if the given predicate returns true for any of the elements.
func (part *Partition[T]) PredicateForAny(predicate func(T) bool) bool {
return part.predicateForAny(predicate, false)
}
// PredicateForEachEarly applies the supplied predicate operation to each element of the partition,
// returning false if the given predicate returns false for any of the elements.
//
// The method returns when one application of the predicate returns false (determining the overall result)
func (part *Partition[T]) PredicateForEachEarly(predicate func(T) bool) bool {
return part.predicateForEach(predicate, false)
}
// PredicateForAnyEarly applies the supplied predicate operation to each element of the partition,
// returning true if the given predicate returns true for any of the elements.
//
// The method returns when one application of the predicate returns true (determining the overall result)
func (part *Partition[T]) PredicateForAnyEarly(predicate func(T) bool) bool {
return part.predicateForAny(predicate, true)
}
// SpanPartitionConstraint is the generic type constraint for IP subnet spanning partitions.
type SpanPartitionConstraint[T any] interface {
AddressDivisionSeries
PrefixedConstraint[T]
SpanWithPrefixBlocks() []T
}
// IteratePartitionConstraint is the generic type constraint for IP subnet and IP section iteration partitions.
type IteratePartitionConstraint[T any] interface {
AddressDivisionSeries
PrefixedConstraint[T]
AssignMinPrefixForBlock() T
PrefixBlockIterator() Iterator[T]
Iterator() Iterator[T]
}
// ApplyForEachConditionally supplies to the given function each element of the given partition,
// inserting return values into the returned map as directed. When the action returns true as the second return value,
// then the other return value is added to the map.
func ApplyForEachConditionally[T GenericKeyConstraint[T], V any](part *Partition[T], action func(T) (V, bool)) MappedPartition[T, V] {
results := make(map[Key[T]]V)
if action != nil && part != nil {
part.ForEach(func(addr T) {
if result, ok := action(addr); ok {
results[addr.ToGenericKey()] = result
}
})
}
return results
}
// ApplyForEach supplies to the given function each element of the given partition,
// inserting return values into the returned map.
func ApplyForEach[T GenericKeyConstraint[T], V any](part *Partition[T], action func(T) V) MappedPartition[T, V] {
results := make(map[Key[T]]V)
if action != nil && part != nil {
part.ForEach(func(addr T) {
results[addr.ToGenericKey()] = action(addr)
})
}
return results
}
// PartitionWithSpanningBlocks partitions the address series into prefix blocks and single addresses.
//
// This method iterates through a list of prefix blocks of different sizes that span the entire subnet.
func PartitionWithSpanningBlocks[T SpanPartitionConstraint[T]](newAddr T) *Partition[T] {
if !newAddr.IsMultiple() {
if !newAddr.IsPrefixed() {
return &Partition[T]{
original: newAddr,
single: newAddr,
hasSingle: true,
count: bigOneConst(),
}
}
return &Partition[T]{
original: newAddr,
single: newAddr.WithoutPrefixLen(),
hasSingle: true,
count: bigOneConst(),
}
} else if newAddr.IsSinglePrefixBlock() {
return &Partition[T]{
original: newAddr,
single: newAddr,
hasSingle: true,
count: bigOneConst(),
}
}
blocks := newAddr.SpanWithPrefixBlocks()
return &Partition[T]{
original: newAddr,
iterator: &sliceIterator[T]{blocks},
count: big.NewInt(int64(len(blocks))),
}
}
// PartitionWithSingleBlockSize partitions the address series into prefix blocks and single addresses.
//
// This method chooses the maximum block size for a list of prefix blocks contained by the address or subnet,
// and then iterates to produce blocks of that size.
func PartitionWithSingleBlockSize[T IteratePartitionConstraint[T]](newAddr T) *Partition[T] {
if !newAddr.IsMultiple() {
if !newAddr.IsPrefixed() {
return &Partition[T]{
original: newAddr,
single: newAddr,
hasSingle: true,
count: bigOneConst(),
}
}
return &Partition[T]{
original: newAddr,
single: newAddr.WithoutPrefixLen(),
hasSingle: true,
count: bigOneConst(),
}
} else if newAddr.IsSinglePrefixBlock() {
return &Partition[T]{
original: newAddr,
single: newAddr,
hasSingle: true,
count: bigOneConst(),
}
}
// prefix blocks are handled as prefix blocks,
// such as 1.2.*.*, which is handled as prefix block iterator for 1.2.0.0/16,
// but 1.2.3-4.5 is handled as iterator with no prefix lengths involved
series := newAddr.AssignMinPrefixForBlock()
if series.GetPrefixLen().bitCount() != newAddr.GetBitCount() {
return &Partition[T]{
original: newAddr,
iterator: series.PrefixBlockIterator(),
count: series.GetPrefixCountLen(series.GetPrefixLen().bitCount()),
}
}
return &Partition[T]{
original: newAddr,
iterator: newAddr.WithoutPrefixLen().Iterator(),
count: newAddr.GetCount(),
}
}
// PartitionIpv6WithSpanningBlocks partitions the IPv6 address into prefix blocks and single addresses.
//
// This function is here for backwards compatibility, PartitionWithSpanningBlocks is recommended instead.
func PartitionIpv6WithSpanningBlocks(newAddr *IPv6Address) *Partition[*IPv6Address] {
return PartitionWithSpanningBlocks(newAddr)
}
// PartitionIpv4WithSpanningBlocks partitions the IPv4 address into prefix blocks and single addresses.
//
// This function is here for backwards compatibility, PartitionWithSpanningBlocks is recommended instead.
func PartitionIpv4WithSpanningBlocks(newAddr *IPv4Address) *Partition[*IPv4Address] {
return PartitionWithSpanningBlocks(newAddr)
}
// PartitionIPv6WithSingleBlockSize partitions the IPv6 address into prefix blocks and single addresses.
//
// This function is here for backwards compatibility, PartitionWithSingleBlockSize is recommended instead.
func PartitionIPv6WithSingleBlockSize(newAddr *IPv6Address) *Partition[*IPv6Address] {
return PartitionWithSingleBlockSize(newAddr)
}
// PartitionIPv4WithSingleBlockSize partitions the IPv4 address into prefix blocks and single addresses.
//
// This function is here for backwards compatibility, PartitionWithSingleBlockSize is recommended instead.
func PartitionIPv4WithSingleBlockSize(newAddr *IPv4Address) *Partition[*IPv4Address] {
return PartitionWithSingleBlockSize(newAddr)
}