trie.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package geodb
  2. import (
  3. "math"
  4. "net"
  5. )
  6. type trie_Node struct {
  7. childrens [2]*trie_Node
  8. cc string
  9. }
  10. // Initializing the root of the trie
  11. type trie struct {
  12. root *trie_Node
  13. }
  14. func ipToBytes(ip string) []byte {
  15. // Parse the IP address string into a net.IP object
  16. parsedIP := net.ParseIP(ip)
  17. // Convert the IP address to a 4-byte slice
  18. ipBytes := parsedIP.To4()
  19. if ipBytes == nil {
  20. //This is an IPv6 address
  21. ipBytes = parsedIP.To16()
  22. }
  23. return ipBytes
  24. }
  25. // inititlaizing a new trie
  26. func newTrie() *trie {
  27. t := new(trie)
  28. t.root = new(trie_Node)
  29. return t
  30. }
  31. // Passing words to trie
  32. func (t *trie) insert(ipAddr string, cc string) {
  33. ipBytes := ipToBytes(ipAddr)
  34. current := t.root
  35. for _, b := range ipBytes {
  36. //For each byte in the ip address
  37. //each byte is 8 bit
  38. for j := 0; j < 8; j++ {
  39. bitwise := (b&uint8(math.Pow(float64(2), float64(j))) > 0)
  40. bit := 0b0000
  41. if bitwise {
  42. bit = 0b0001
  43. }
  44. if current.childrens[bit] == nil {
  45. current.childrens[bit] = &trie_Node{
  46. childrens: [2]*trie_Node{},
  47. cc: cc,
  48. }
  49. }
  50. current = current.childrens[bit]
  51. }
  52. }
  53. /*
  54. for i := 63; i >= 0; i-- {
  55. bit := (ipInt64 >> uint(i)) & 1
  56. if current.childrens[bit] == nil {
  57. current.childrens[bit] = &trie_Node{
  58. childrens: [2]*trie_Node{},
  59. cc: cc,
  60. }
  61. }
  62. current = current.childrens[bit]
  63. }
  64. */
  65. }
  66. func isReservedIP(ip string) bool {
  67. parsedIP := net.ParseIP(ip)
  68. if parsedIP == nil {
  69. return false
  70. }
  71. // Check if the IP address is a loopback address
  72. if parsedIP.IsLoopback() {
  73. return true
  74. }
  75. // Check if the IP address is in the link-local address range
  76. if parsedIP.IsLinkLocalUnicast() || parsedIP.IsLinkLocalMulticast() {
  77. return true
  78. }
  79. if parsedIP.IsPrivate() {
  80. return true
  81. }
  82. // If the IP address is not a reserved address, return false
  83. return false
  84. }
  85. // Initializing the search for word in node
  86. func (t *trie) search(ipAddr string) string {
  87. if isReservedIP(ipAddr) {
  88. return ""
  89. }
  90. ipBytes := ipToBytes(ipAddr)
  91. current := t.root
  92. for _, b := range ipBytes {
  93. //For each byte in the ip address
  94. //each byte is 8 bit
  95. for j := 0; j < 8; j++ {
  96. bitwise := (b&uint8(math.Pow(float64(2), float64(j))) > 0)
  97. bit := 0b0000
  98. if bitwise {
  99. bit = 0b0001
  100. }
  101. if current.childrens[bit] == nil {
  102. return current.cc
  103. }
  104. current = current.childrens[bit]
  105. }
  106. }
  107. /*
  108. for i := 63; i >= 0; i-- {
  109. bit := (ipInt64 >> uint(i)) & 1
  110. if current.childrens[bit] == nil {
  111. return current.cc
  112. }
  113. current = current.childrens[bit]
  114. }
  115. */
  116. if len(current.childrens) == 0 {
  117. return current.cc
  118. }
  119. //Not found
  120. return ""
  121. }