// Code generated by automation. DO NOT EDIT

package generics_tree

import (
	"math/bits"

	"github.com/kentik/patricia"
)

const _leftmost32Bit = uint32(1 << 31)

type treeNodeV4[T any] 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[T]) 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[T]) ShiftPrefix(shiftCount uint) {
	n.prefix <<= shiftCount
	n.prefixLength -= shiftCount
}

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

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