File: tree_node_v4.go

package info (click to toggle)
golang-github-kentik-patricia 0.0~git20200128.c35d94c-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 3,368 kB
  • sloc: makefile: 38
file content (49 lines) | stat: -rw-r--r-- 1,253 bytes parent folder | download
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
package float64_tree

import (
	"math/bits"

	"github.com/kentik/patricia"
)

const _leftmost32Bit = uint32(1 << 31)

type treeNodeV4 struct {
	Left         uint // left node index: 0 for not set
	Right        uint // right node index: 0 for not set
	prefix       uint32
	prefixLength uint
	TagCount     int
}

// See how many bits match the input address
func (n *treeNodeV4) MatchCount(address patricia.IPv4Address) uint {
	var length uint
	if address.Length > n.prefixLength {
		length = n.prefixLength
	} else {
		length = address.Length
	}

	matches := uint(bits.LeadingZeros32(n.prefix ^ address.Address))
	if matches > length {
		return length
	}
	return matches
}

// ShiftPrefix shifts the prefix by the input shiftCount
func (n *treeNodeV4) ShiftPrefix(shiftCount uint) {
	n.prefix <<= shiftCount
	n.prefixLength -= shiftCount
}

// IsLeftBitSet returns whether the leftmost bit is set
func (n *treeNodeV4) IsLeftBitSet() bool {
	return n.prefix >= _leftmost32Bit
}

// MergeFromNodes updates the prefix and prefix length from the two input nodes
func (n *treeNodeV4) MergeFromNodes(left *treeNodeV4, right *treeNodeV4) {
	n.prefix, n.prefixLength = patricia.MergePrefixes32(left.prefix, left.prefixLength, right.prefix, right.prefixLength)
}