diff --git a/net/ip.go b/net/ip.go index 1ce30e8..75cb356 100644 --- a/net/ip.go +++ b/net/ip.go @@ -4,6 +4,7 @@ Package net provides utility functions for working with IPs (net.IP). package net import ( + "bytes" "encoding/binary" "fmt" "math" @@ -236,7 +237,7 @@ func (n Network) LeastCommonBitPosition(n1 Network) (uint, error) { // Equal is the equality test for 2 networks. func (n Network) Equal(n1 Network) bool { - return n.String() == n1.String() + return bytes.Equal(n.IPNet.IP, n1.IPNet.IP) && bytes.Equal(n.IPNet.Mask, n1.IPNet.Mask) } func (n Network) String() string { diff --git a/trie.go b/trie.go index 938d625..7383b13 100644 --- a/trie.go +++ b/trie.go @@ -135,11 +135,12 @@ func (p *prefixTrie) Len() int { func (p *prefixTrie) String() string { children := []string{} padding := strings.Repeat("| ", p.level()+1) - for bits, child := range p.children { - if child == nil { + lens := len(p.children) + for bits := 0; bits < lens; bits++ { + if p.children[bits] == nil { continue } - childStr := fmt.Sprintf("\n%s%d--> %s", padding, bits, child.String()) + childStr := fmt.Sprintf("\n%s%d--> %s", padding, bits, p.children[bits].String()) children = append(children, childStr) } return fmt.Sprintf("%s (target_pos:%d:has_entry:%t)%s", p.network, @@ -314,9 +315,10 @@ func (p *prefixTrie) compressPathIfPossible() error { // Find lone child. var loneChild *prefixTrie - for _, child := range p.children { - if child != nil { - loneChild = child + lens := len(p.children) + for bits := 0; bits < lens; bits++ { + if p.children[bits] != nil { + loneChild = p.children[bits] break } } @@ -338,8 +340,9 @@ func (p *prefixTrie) compressPathIfPossible() error { func (p *prefixTrie) childrenCount() int { count := 0 - for _, child := range p.children { - if child != nil { + lens := len(p.children) + for bits := 0; bits < lens; bits++ { + if p.children[bits] != nil { count++ } } @@ -379,14 +382,17 @@ func (p *prefixTrie) walkDepth() <-chan RangerEntry { entries <- p.entry } childEntriesList := []<-chan RangerEntry{} - for _, trie := range p.children { - if trie == nil { + lens := len(p.children) + for bits := 0; bits < lens; bits++ { + if p.children[bits] == nil { continue } - childEntriesList = append(childEntriesList, trie.walkDepth()) + childEntriesList = append(childEntriesList, p.children[bits].walkDepth()) } - for _, childEntries := range childEntriesList { - for entry := range childEntries { + + lensx := len(childEntriesList) + for i := 0; i < lensx; i++ { + for entry := range childEntriesList[i] { entries <- entry } }