-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsplit.go
331 lines (305 loc) · 10.7 KB
/
split.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
package goip
import (
"container/list"
"math/bits"
)
type seriesStack struct {
seriesPairs []ExtendedIPSegmentSeries
indexes []int
bits []BitCount
}
// init grows to have capacity at least as large as size.
func (stack *seriesStack) init(size int) {
if stack.seriesPairs == nil {
stack.seriesPairs = make([]ExtendedIPSegmentSeries, 0, size<<1)
stack.indexes = make([]int, 0, size)
stack.bits = make([]BitCount, 0, size)
}
}
func (stack *seriesStack) push(lower, upper ExtendedIPSegmentSeries, previousSegmentBits BitCount, currentSegment int) {
stack.seriesPairs = append(stack.seriesPairs, lower, upper)
stack.indexes = append(stack.indexes, currentSegment)
stack.bits = append(stack.bits, previousSegmentBits)
}
func (stack *seriesStack) pop() (popped bool, lower, upper ExtendedIPSegmentSeries, previousSegmentBits BitCount, currentSegment int) {
seriesPairs := stack.seriesPairs
length := len(seriesPairs)
if length <= 0 {
return
}
length--
upper = seriesPairs[length]
length--
lower = seriesPairs[length]
stack.seriesPairs = seriesPairs[:length]
indexes := stack.indexes
length = len(indexes) - 1
currentSegment = indexes[length]
stack.indexes = indexes[:length]
stackbits := stack.bits
previousSegmentBits = stackbits[length]
stack.bits = stackbits[:length]
popped = true
return
}
func checkPrefixBlockFormat(container, contained ExtendedIPSegmentSeries, checkEqual bool) (result ExtendedIPSegmentSeries) {
if container.IsPrefixed() && container.IsSinglePrefixBlock() {
result = container
} else if checkEqual && contained.IsPrefixed() && container.CompareSize(contained) == 0 && contained.IsSinglePrefixBlock() {
result = contained
} else {
result = container.AssignPrefixForSingleBlock() // this returns nil if cannot be a prefix block
}
return
}
func checkPrefixBlockContainment(first, other ExtendedIPSegmentSeries) ExtendedIPSegmentSeries {
if first.Contains(other) {
return checkPrefixBlockFormat(first, other, true)
} else if other.Contains(first) {
return checkPrefixBlockFormat(other, first, false)
}
return nil
}
func applyOperatorToLowerUpper(first, other ExtendedIPSegmentSeries, removePrefixes bool,
operatorFunctor func(lower, upper ExtendedIPSegmentSeries) []ExtendedIPSegmentSeries) []ExtendedIPSegmentSeries {
var lower, upper ExtendedIPSegmentSeries
if seriesValsSame(first, other) {
if removePrefixes && first.IsPrefixed() {
if other.IsPrefixed() {
lower = first.WithoutPrefixLen()
} else {
lower = other
}
} else {
lower = first
}
upper = lower.GetUpper()
lower = lower.GetLower()
} else {
firstLower := first.GetLower()
otherLower := other.GetLower()
firstUpper := first.GetUpper()
otherUpper := other.GetUpper()
if LowValueComparator.CompareSeries(firstLower, otherLower) > 0 {
lower = otherLower
} else {
lower = firstLower
}
if LowValueComparator.CompareSeries(firstUpper, otherUpper) < 0 {
upper = otherUpper
} else {
upper = firstUpper
}
if removePrefixes {
lower = lower.WithoutPrefixLen()
upper = upper.WithoutPrefixLen()
}
}
return operatorFunctor(lower, upper)
}
func splitIntoPrefixBlocks(lower, upper ExtendedIPSegmentSeries) (blocks []ExtendedIPSegmentSeries) {
var stack seriesStack
var currentSegment int
var previousSegmentBits BitCount
blocks = make([]ExtendedIPSegmentSeries, 0, IPv6BitCount)
segCount := lower.GetDivisionCount()
bitsPerSegment := lower.GetBitsPerSegment()
for {
// Find first non-matching bit.
var differing SegInt
for ; currentSegment < segCount; currentSegment++ {
lowerSeg := lower.GetGenericSegment(currentSegment)
upperSeg := upper.GetGenericSegment(currentSegment)
lowerValue := lowerSeg.GetSegmentValue() // these are single addresses, so lower or upper value no different here
upperValue := upperSeg.GetSegmentValue()
differing = lowerValue ^ upperValue
if differing != 0 {
break
}
previousSegmentBits += bitsPerSegment
}
if differing == 0 {
// all bits match, it's just a single address
blocks = append(blocks, lower.ToPrefixBlockLen(lower.GetBitCount()))
} else {
differingIsLowestBit := differing == 1
if differingIsLowestBit && currentSegment+1 == segCount {
// only the very last bit differs, so we have a prefix block right there
blocks = append(blocks, lower.ToPrefixBlockLen(lower.GetBitCount()-1))
} else {
highestDifferingBitInRange := BitCount(bits.LeadingZeros32(uint32(differing))) - (32 - bitsPerSegment)
differingBitPrefixLen := highestDifferingBitInRange + previousSegmentBits
if lower.IncludesZeroHostLen(differingBitPrefixLen) && upper.IncludesMaxHostLen(differingBitPrefixLen) {
// full range at the differing bit, we have a single prefix block
blocks = append(blocks, lower.ToPrefixBlockLen(differingBitPrefixLen))
} else {
// neither a prefix block nor a single address
// we split into two new ranges to continue
// starting from the differing bit,
// lower top becomes 1000000...
// upper bottom becomes 01111111...
// so in each new range, the differing bit is at least one further to the right (or more)
lowerTop, _ := upper.ToZeroHostLen(differingBitPrefixLen + 1)
upperBottom := lowerTop.Increment(-1)
if differingIsLowestBit {
previousSegmentBits += bitsPerSegment
currentSegment++
}
stack.init(int(IPv6BitCount))
stack.push(lowerTop, upper, previousSegmentBits, currentSegment) // do upper one later
upper = upperBottom // do lower one now
continue
}
}
}
var popped bool
if popped, lower, upper, previousSegmentBits, currentSegment = stack.pop(); !popped {
return blocks
}
}
}
func wrapNonNilInSlice(result ExtendedIPSegmentSeries) []ExtendedIPSegmentSeries {
if result != nil {
return []ExtendedIPSegmentSeries{result}
}
return nil
}
// getSpanningPrefixBlocks returns the smallest set of prefix blocks
// that spans both this and the supplied address or subnet.
func getSpanningPrefixBlocks(first, other ExtendedIPSegmentSeries) []ExtendedIPSegmentSeries {
result := checkPrefixBlockContainment(first, other)
if result != nil {
return wrapNonNilInSlice(result)
}
return applyOperatorToLowerUpper(first, other, true, splitIntoPrefixBlocks)
}
func checkSequentialBlockFormat(container, contained ExtendedIPSegmentSeries, checkEqual bool) (result ExtendedIPSegmentSeries) {
if !container.IsPrefixed() {
if container.IsSequential() {
result = container
}
} else if checkEqual && !contained.IsPrefixed() && container.CompareSize(contained) == 0 {
if contained.IsSequential() {
result = contained
}
} else if container.IsSequential() {
result = container.WithoutPrefixLen()
}
return
}
func checkSequentialBlockContainment(first, other ExtendedIPSegmentSeries) ExtendedIPSegmentSeries {
if first.Contains(other) {
return checkSequentialBlockFormat(first, other, true)
} else if other.Contains(first) {
return checkSequentialBlockFormat(other, first, false)
}
return nil
}
func splitIntoSequentialBlocks(lower, upper ExtendedIPSegmentSeries) (blocks []ExtendedIPSegmentSeries) {
segCount := lower.GetDivisionCount()
if segCount == 0 {
return []ExtendedIPSegmentSeries{lower}
}
var segSegment int
var toAdd list.List
var stack seriesStack
var currentSegment int
var previousSegmentBits BitCount
var lowerValue, upperValue SegInt
bitsPerSegment := lower.GetBitsPerSegment()
blocks = make([]ExtendedIPSegmentSeries, 0, IPv6SegmentCount)
toAdd.Init()
for {
for {
segSegment = currentSegment
lowerSeg := lower.GetGenericSegment(currentSegment)
upperSeg := upper.GetGenericSegment(currentSegment)
currentSegment++
lowerValue = lowerSeg.GetSegmentValue() // these are single addresses, so lower or upper value no different here
upperValue = upperSeg.GetSegmentValue()
previousSegmentBits += bitsPerSegment
if lowerValue != upperValue || currentSegment >= segCount {
break
}
}
if lowerValue == upperValue {
blocks = append(blocks, lower)
} else {
lowerIsLowest := lower.IncludesZeroHostLen(previousSegmentBits)
higherIsHighest := upper.IncludesMaxHostLen(previousSegmentBits)
if lowerIsLowest {
if higherIsHighest {
// full range
series := lower.ToBlock(segSegment, lowerValue, upperValue)
blocks = append(blocks, series)
} else {
topLower, _ := upper.ToZeroHostLen(previousSegmentBits)
middleUpper := topLower.Increment(-1)
series := lower.ToBlock(segSegment, lowerValue, middleUpper.GetGenericSegment(segSegment).GetSegmentValue())
blocks = append(blocks, series)
lower = topLower
continue
}
} else if higherIsHighest {
bottomUpper, _ := lower.ToMaxHostLen(previousSegmentBits)
topLower := bottomUpper.Increment(1)
series := topLower.ToBlock(segSegment, topLower.GetGenericSegment(segSegment).GetSegmentValue(), upperValue)
toAdd.PushFront(series)
upper = bottomUpper
continue
} else {
// from top to bottom we have: top - topLower - middleUpper - middleLower - bottomUpper - lower
topLower, _ := upper.ToZeroHostLen(previousSegmentBits)
middleUpper := topLower.Increment(-1)
bottomUpper, _ := lower.ToMaxHostLen(previousSegmentBits)
middleLower := bottomUpper.Increment(1)
if LowValueComparator.CompareSeries(middleLower, middleUpper) <= 0 {
series := middleLower.ToBlock(
segSegment,
middleLower.GetGenericSegment(segSegment).GetSegmentValue(),
middleUpper.GetGenericSegment(segSegment).GetSegmentValue())
toAdd.PushFront(series)
}
stack.init(IPv6SegmentCount)
stack.push(topLower, upper, previousSegmentBits, currentSegment) // do this one later
upper = bottomUpper
continue
}
}
if toAdd.Len() != 0 {
for {
saved := toAdd.Front()
if saved == nil {
break
}
toAdd.Remove(saved)
blocks = append(blocks, saved.Value.(ExtendedIPSegmentSeries))
}
}
var popped bool
if popped, lower, upper, previousSegmentBits, currentSegment = stack.pop(); !popped {
return blocks
}
}
}
func spanWithPrefixBlocks(orig ExtendedIPSegmentSeries) (list []ExtendedIPSegmentSeries) {
iterator := orig.SequentialBlockIterator()
for iterator.HasNext() {
list = append(list, iterator.Next().SpanWithPrefixBlocks()...)
}
return list
}
func spanWithSequentialBlocks(orig ExtendedIPSegmentSeries) (list []ExtendedIPSegmentSeries) {
iterator := orig.SequentialBlockIterator()
for iterator.HasNext() {
list = append(list, iterator.Next())
}
return list
}
func getSpanningSequentialBlocks(first, other ExtendedIPSegmentSeries) []ExtendedIPSegmentSeries {
result := checkSequentialBlockContainment(first, other)
if result != nil {
return wrapNonNilInSlice(result)
}
return applyOperatorToLowerUpper(first, other, true, splitIntoSequentialBlocks)
}