ipcidr_node.go 779 B

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