github.com/kelleygo/clashcore@v1.0.2/component/trie/ipcidr_node.go (about)

     1  package trie
     2  
     3  import "errors"
     4  
     5  var (
     6  	ErrorOverMaxValue = errors.New("the value don't over max value")
     7  )
     8  
     9  type IpCidrNode struct {
    10  	Mark     bool
    11  	child    map[uint32]*IpCidrNode
    12  	maxValue uint32
    13  }
    14  
    15  func NewIpCidrNode(mark bool, maxValue uint32) *IpCidrNode {
    16  	ipCidrNode := &IpCidrNode{
    17  		Mark:     mark,
    18  		child:    map[uint32]*IpCidrNode{},
    19  		maxValue: maxValue,
    20  	}
    21  
    22  	return ipCidrNode
    23  }
    24  
    25  func (n *IpCidrNode) addChild(value uint32) error {
    26  	if value > n.maxValue {
    27  		return ErrorOverMaxValue
    28  	}
    29  
    30  	n.child[value] = NewIpCidrNode(false, n.maxValue)
    31  	return nil
    32  }
    33  
    34  func (n *IpCidrNode) hasChild(value uint32) bool {
    35  	return n.getChild(value) != nil
    36  }
    37  
    38  func (n *IpCidrNode) getChild(value uint32) *IpCidrNode {
    39  	if value <= n.maxValue {
    40  		return n.child[value]
    41  	}
    42  
    43  	return nil
    44  }