Skip to content

Commit 808a701

Browse files
author
Sean Foley
committed
godoc additions and changes, changes to add-trie API
1 parent 2601df3 commit 808a701

File tree

4 files changed

+477
-29
lines changed

4 files changed

+477
-29
lines changed

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@ module github.com/seancfoley/ipaddress-go
1818

1919
go 1.18
2020

21-
require github.com/seancfoley/bintree v1.3.0
21+
require github.com/seancfoley/bintree v1.3.1

ipaddr/addrtrie.go

+22-14
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,7 @@ func (trie *Trie[T]) String() string {
402402
return trie.toTrie().String()
403403
}
404404

405-
// AddedNodesTreeString provides a flattened version of the trie showing only the contained added nodes and their containment structure, which is non-binary.
405+
// AddedNodesTreeString provides a string showing a flattened version of the trie showing only the contained added nodes and their containment structure, which is non-binary.
406406
// The root node is included, which may or may not be added.
407407
func (trie *Trie[T]) AddedNodesTreeString() string {
408408
return trie.toTrie().AddedNodesTreeString()
@@ -421,12 +421,16 @@ func (trie *Trie[T]) Add(addr T) bool {
421421

422422
// AddNode adds the address to this trie.
423423
// The address must match the same type and version of any existing addresses already in the trie.
424+
//
425+
// If the argument is not a single address nor prefix block, this method will panic.
426+
// The [Partition] type can be used to convert the argument to single addresses and prefix blocks before calling this method.
427+
//
424428
// The new or existing node for the address is returned.
425429
func (trie *Trie[T]) AddNode(addr T) *TrieNode[T] {
426430
return toAddressTrieNode(trie.addNode(addr))
427431
}
428432

429-
// AddTrie adds nodes for the keys in the trie with the root node as the passed in node.
433+
// AddTrie adds nodes for the keys from the trie with the argument trie root.
430434
// AddTrie returns the sub-node in the trie where the added trie begins, where the first node of the added trie is located.
431435
func (trie *Trie[T]) AddTrie(added *TrieNode[T]) *TrieNode[T] {
432436
return toAddressTrieNode(trie.addTrie(added.toBase()))
@@ -564,7 +568,7 @@ func (trie *Trie[T]) GetNode(addr T) *TrieNode[T] {
564568
// The [Partition] type can be used to convert the argument to single addresses and prefix blocks before calling this method.
565569
//
566570
// Use Contains to check for the existence of a given address in the trie,
567-
// as well as GetNode to search for all nodes including those not-added but also auto-generated nodes for subnet blocks.
571+
// as well as GetNode to search all nodes including those not-added but auto-generated for subnet blocks.
568572
func (trie *Trie[T]) GetAddedNode(addr T) *TrieNode[T] {
569573
return toAddressTrieNode[T](trie.getAddedNode(addr))
570574
}
@@ -820,7 +824,7 @@ func (trie *AssociativeTrie[T, V]) String() string {
820824
return trie.toTrie().String()
821825
}
822826

823-
// AddedNodesTreeString provides a flattened version of the trie showing only the contained added nodes and their containment structure, which is non-binary.
827+
// AddedNodesTreeString provides a string showing a flattened version of the trie showing only the contained added nodes and their containment structure, which is non-binary.
824828
// The root node is included, which may or may not be added.
825829
func (trie *AssociativeTrie[T, V]) AddedNodesTreeString() string {
826830
return trie.toTrie().AddedNodesTreeString()
@@ -838,12 +842,16 @@ func (trie *AssociativeTrie[T, V]) Add(addr T) bool {
838842
}
839843

840844
// AddNode adds the address key to this trie.
845+
//
846+
// If the argument is not a single address nor prefix block, this method will panic.
847+
// The [Partition] type can be used to convert the argument to single addresses and prefix blocks before calling this method.
848+
//
841849
// The new or existing node for the address is returned.
842850
func (trie *AssociativeTrie[T, V]) AddNode(addr T) *AssociativeTrieNode[T, V] {
843851
return toAssociativeTrieNode[T, V](trie.addNode(addr))
844852
}
845853

846-
// AddTrie adds nodes for the keys in the trie with the root node as the passed in node. To add both keys and values, use PutTrie.
854+
// AddTrie adds nodes for the keys from the trie with the argument trie root. To add both keys and values, use PutTrie.
847855
// AddTrie returns the sub-node in the trie where the added trie begins, where the first node of the added trie is located.
848856
func (trie *AssociativeTrie[T, V]) AddTrie(added *AssociativeTrieNode[T, V]) *AssociativeTrieNode[T, V] {
849857
return toAssociativeTrieNode[T, V](trie.addTrie(added.toBase()))
@@ -938,12 +946,12 @@ func (trie *AssociativeTrie[T, V]) LongestPrefixMatch(addr T) T {
938946
return trie.longestPrefixMatch(addr)
939947
}
940948

941-
// LongestPrefixMatchNode returns the node of the address with the longest matching prefix compared to the provided address.
949+
// LongestPrefixMatchNode returns the node of the address with the longest matching prefix compared to the provided address, or nil if no matching address.
942950
func (trie *AssociativeTrie[T, V]) LongestPrefixMatchNode(addr T) *AssociativeTrieNode[T, V] {
943951
return toAssociativeTrieNode[T, V](trie.longestPrefixMatchNode(addr))
944952
}
945953

946-
// ShortestPrefixMatch returns the address added to the trie with the shortest matching prefix compared to the provided address, or nil if no matching address.
954+
// ShortestPrefixMatch returns the node of the address added to the trie with the shortest matching prefix compared to the provided address, or nil if no matching address.
947955
func (trie *AssociativeTrie[T, V]) ShortestPrefixMatch(addr T) T {
948956
return trie.shortestPrefixMatch(addr)
949957
}
@@ -1155,22 +1163,22 @@ func (trie *AssociativeTrie[T, V]) DeepEqual(other *AssociativeTrie[T, V]) bool
11551163
return trie.toBase().deepEqual(other.toBase())
11561164
}
11571165

1158-
// Put associates the specified value with the specified key in this map.
1166+
// Put associates the specified value with the specified key in this trie.
11591167
//
11601168
// If the argument is not a single address nor prefix block, this method will panic.
11611169
// The [Partition] type can be used to convert the argument to single addresses and prefix blocks before calling this method.
11621170
//
1163-
// If this map previously contained a mapping for a key,
1171+
// If this trie previously contained a node for the given key,
11641172
// the old value is replaced by the specified value, and false is returned along with the old value.
1165-
// If this map did not previously contain a mapping for the key, true is returned along with a nil value.
1166-
// The boolean return value allows you to distinguish whether the address was previously mapped to nil or not mapped at all.
1173+
// If this trie did not previously contain a mapping for the key, true is returned along with the zero value.
1174+
// The boolean return value allows you to distinguish whether the address was previously mapped to the zero value or not mapped at all.
11671175
func (trie *AssociativeTrie[T, V]) Put(addr T, value V) (V, bool) {
11681176
return trie.put(addr, value)
11691177
}
11701178

1171-
// PutTrie adds nodes for the address keys and values in the trie with the root node as the passed in node. To add only the keys, use AddTrie.
1179+
// PutTrie adds nodes with the address keys and values from the trie with the argument trie root. To add only the keys, use AddTrie.
11721180
//
1173-
// For each added in the given node that does not exist in the trie, a copy of each node will be made,
1181+
// For each added node from the given trie that does not exist in this trie, a copy will be made,
11741182
// the copy including the associated value, and the copy will be inserted into the trie.
11751183
//
11761184
// The address type/version of the keys must match.
@@ -1246,7 +1254,7 @@ func (trie *AssociativeTrie[T, V]) RemapIfAbsent(addr T, supplier func() V) *Ass
12461254
// The [Partition] type can be used to convert the argument to single addresses and prefix blocks before calling this method.
12471255
//
12481256
// Returns the value for the given key.
1249-
// Returns nil if the contains no mapping for that key or if the mapped value is nil.
1257+
// Returns nil if the trie contains no mapping for that key or if the mapped value is nil.
12501258
func (trie *AssociativeTrie[T, V]) Get(addr T) (V, bool) {
12511259
return trie.get(addr)
12521260
}

ipaddr/cmd/main.go

+191
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,197 @@ func main() {
578578
fmt.Println("dual trie 0x format")
579579
fmt.Printf("%0x\n", dualTrie)
580580

581+
dualAddrs := []string{
582+
"ff80::",
583+
"ff80:8000::",
584+
"ff80:8000::/24",
585+
"ff80:8000::/32",
586+
"ff80:8000:c000::/34",
587+
"ff80:8000:c800::",
588+
"ff80:8000:cc00::/38",
589+
"ff80:8000:cc00::/40",
590+
"0.0.0.0/0",
591+
"128.0.0.0/8",
592+
"128.0.0.0/16",
593+
"128.0.128.0/24",
594+
"128.0.128.0",
595+
"2.2.3.128",
596+
"2.2.3.0/24",
597+
"2.2.4.0/24",
598+
"2.2.7.0/24",
599+
"2.2.4.3",
600+
"1::ffff:2:3:5",
601+
"1::ffff:2:3:4",
602+
"1::ffff:2:3:6",
603+
}
604+
dualAssocTrie := ipaddr.DualIPv4v6AssociativeTries[string]{}
605+
for _, addr := range dualAddrs {
606+
addressStr := ipaddr.NewIPAddressString(addr)
607+
address := addressStr.GetAddress()
608+
dualAssocTrie.Put(address, address.String())
609+
}
610+
fmt.Println("original dual")
611+
fmt.Println(dualAssocTrie)
612+
613+
dualAssocTrie2 := ipaddr.DualIPv4v6AssociativeTries[string]{}
614+
dualAssocTrie2.AddTrie(dualAssocTrie.GetIPv4Trie().GetRoot())
615+
dualAssocTrie2.AddTrie(dualAssocTrie.GetIPv6Trie().GetRoot())
616+
fmt.Println("dual copied no vals")
617+
fmt.Println(dualAssocTrie2)
618+
619+
dualAssocTrie3 := ipaddr.DualIPv4v6AssociativeTries[string]{}
620+
dualAssocTrie3.PutTrie(dualAssocTrie.GetIPv4Trie().GetRoot())
621+
dualAssocTrie3.PutTrie(dualAssocTrie.GetIPv6Trie().GetRoot())
622+
fmt.Println("dual copied vals")
623+
fmt.Println(dualAssocTrie3)
624+
625+
/*
626+
original dual
627+
628+
○ * (21)
629+
├─● 0.0.0.0/0 = 0.0.0.0/0 (10)
630+
│ ├─○ 2.2.0.0/21 (5)
631+
│ │ ├─● 2.2.3.0/24 = 2.2.3.0/24 (2)
632+
│ │ │ └─● 2.2.3.128 = 2.2.3.128 (1)
633+
│ │ └─○ 2.2.4.0/22 (3)
634+
│ │ ├─● 2.2.4.0/24 = 2.2.4.0/24 (2)
635+
│ │ │ └─● 2.2.4.3 = 2.2.4.3 (1)
636+
│ │ └─● 2.2.7.0/24 = 2.2.7.0/24 (1)
637+
│ └─● 128.0.0.0/8 = 128.0.0.0/8 (4)
638+
│ └─● 128.0.0.0/16 = 128.0.0.0/16 (3)
639+
│ └─● 128.0.128.0/24 = 128.0.128.0/24 (2)
640+
│ └─● 128.0.128.0 = 128.0.128.0 (1)
641+
└─○ ::/0 (11)
642+
├─○ 1::ffff:2:3:4/126 (3)
643+
│ ├─○ 1::ffff:2:3:4/127 (2)
644+
│ │ ├─● 1::ffff:2:3:4 = 1::ffff:2:3:4 (1)
645+
│ │ └─● 1::ffff:2:3:5 = 1::ffff:2:3:5 (1)
646+
│ └─● 1::ffff:2:3:6 = 1::ffff:2:3:6 (1)
647+
└─○ ff80::/16 (8)
648+
├─● ff80:: = ff80:: (1)
649+
└─● ff80:8000::/24 = ff80:8000::/24 (7)
650+
└─● ff80:8000::/32 = ff80:8000::/32 (6)
651+
├─● ff80:8000:: = ff80:8000:: (1)
652+
└─● ff80:8000:c000::/34 = ff80:8000:c000::/34 (4)
653+
└─○ ff80:8000:c800::/37 (3)
654+
├─● ff80:8000:c800:: = ff80:8000:c800:: (1)
655+
└─● ff80:8000:cc00::/38 = ff80:8000:cc00::/38 (2)
656+
└─● ff80:8000:cc00::/40 = ff80:8000:cc00::/40 (1)
657+
658+
dual copied no vals
659+
the map is to strings, so you see the zero value of strings
660+
661+
○ * (21)
662+
├─● 0.0.0.0/0 = (10)
663+
│ ├─○ 2.2.0.0/21 (5)
664+
│ │ ├─● 2.2.3.0/24 = (2)
665+
│ │ │ └─● 2.2.3.128 = (1)
666+
│ │ └─○ 2.2.4.0/22 (3)
667+
│ │ ├─● 2.2.4.0/24 = (2)
668+
│ │ │ └─● 2.2.4.3 = (1)
669+
│ │ └─● 2.2.7.0/24 = (1)
670+
│ └─● 128.0.0.0/8 = (4)
671+
│ └─● 128.0.0.0/16 = (3)
672+
│ └─● 128.0.128.0/24 = (2)
673+
│ └─● 128.0.128.0 = (1)
674+
└─○ ::/0 (11)
675+
├─○ 1::ffff:2:3:4/126 (3)
676+
│ ├─○ 1::ffff:2:3:4/127 (2)
677+
│ │ ├─● 1::ffff:2:3:4 = (1)
678+
│ │ └─● 1::ffff:2:3:5 = (1)
679+
│ └─● 1::ffff:2:3:6 = (1)
680+
└─○ ff80::/16 (8)
681+
├─● ff80:: = (1)
682+
└─● ff80:8000::/24 = (7)
683+
└─● ff80:8000::/32 = (6)
684+
├─● ff80:8000:: = (1)
685+
└─● ff80:8000:c000::/34 = (4)
686+
└─○ ff80:8000:c800::/37 (3)
687+
├─● ff80:8000:c800:: = (1)
688+
└─● ff80:8000:cc00::/38 = (2)
689+
└─● ff80:8000:cc00::/40 = (1)
690+
dual copied vals
691+
692+
○ * (21)
693+
├─● 0.0.0.0/0 = 0.0.0.0/0 (10)
694+
│ ├─○ 2.2.0.0/21 (5)
695+
│ │ ├─● 2.2.3.0/24 = 2.2.3.0/24 (2)
696+
│ │ │ └─● 2.2.3.128 = 2.2.3.128 (1)
697+
│ │ └─○ 2.2.4.0/22 (3)
698+
│ │ ├─● 2.2.4.0/24 = 2.2.4.0/24 (2)
699+
│ │ │ └─● 2.2.4.3 = 2.2.4.3 (1)
700+
│ │ └─● 2.2.7.0/24 = 2.2.7.0/24 (1)
701+
│ └─● 128.0.0.0/8 = 128.0.0.0/8 (4)
702+
│ └─● 128.0.0.0/16 = 128.0.0.0/16 (3)
703+
│ └─● 128.0.128.0/24 = 128.0.128.0/24 (2)
704+
│ └─● 128.0.128.0 = 128.0.128.0 (1)
705+
└─○ ::/0 (11)
706+
├─○ 1::ffff:2:3:4/126 (3)
707+
│ ├─○ 1::ffff:2:3:4/127 (2)
708+
│ │ ├─● 1::ffff:2:3:4 = 1::ffff:2:3:4 (1)
709+
│ │ └─● 1::ffff:2:3:5 = 1::ffff:2:3:5 (1)
710+
│ └─● 1::ffff:2:3:6 = 1::ffff:2:3:6 (1)
711+
└─○ ff80::/16 (8)
712+
├─● ff80:: = ff80:: (1)
713+
└─● ff80:8000::/24 = ff80:8000::/24 (7)
714+
└─● ff80:8000::/32 = ff80:8000::/32 (6)
715+
├─● ff80:8000:: = ff80:8000:: (1)
716+
└─● ff80:8000:c000::/34 = ff80:8000:c000::/34 (4)
717+
└─○ ff80:8000:c800::/37 (3)
718+
├─● ff80:8000:c800:: = ff80:8000:c800:: (1)
719+
└─● ff80:8000:cc00::/38 = ff80:8000:cc00::/38 (2)
720+
└─● ff80:8000:cc00::/40 = ff80:8000:cc00::/40 (1)
721+
722+
723+
*/
724+
725+
dualAssocTrie4 := ipaddr.DualIPv4v6AssociativeTries[(*ipaddr.IPAddress)]{}
726+
for i, addr := range dualAddrs {
727+
addressStr := ipaddr.NewIPAddressString(addr)
728+
address := addressStr.GetAddress()
729+
if i%2 == 0 {
730+
dualAssocTrie4.Put(address, address)
731+
} else {
732+
dualAssocTrie4.Add(address)
733+
}
734+
}
735+
fmt.Println("another dual")
736+
fmt.Println(dualAssocTrie4)
737+
738+
/*
739+
another dual
740+
741+
○ * (21)
742+
├─● 0.0.0.0/0 = 0.0.0.0/0 (10)
743+
│ ├─○ 2.2.0.0/21 (5)
744+
│ │ ├─● 2.2.3.0/24 = 2.2.3.0/24 (2)
745+
│ │ │ └─● 2.2.3.128 (1)
746+
│ │ └─○ 2.2.4.0/22 (3)
747+
│ │ ├─● 2.2.4.0/24 (2)
748+
│ │ │ └─● 2.2.4.3 (1)
749+
│ │ └─● 2.2.7.0/24 = 2.2.7.0/24 (1)
750+
│ └─● 128.0.0.0/8 (4)
751+
│ └─● 128.0.0.0/16 = 128.0.0.0/16 (3)
752+
│ └─● 128.0.128.0/24 (2)
753+
│ └─● 128.0.128.0 = 128.0.128.0 (1)
754+
└─○ ::/0 (11)
755+
├─○ 1::ffff:2:3:4/126 (3)
756+
│ ├─○ 1::ffff:2:3:4/127 (2)
757+
│ │ ├─● 1::ffff:2:3:4 (1)
758+
│ │ └─● 1::ffff:2:3:5 = 1::ffff:2:3:5 (1)
759+
│ └─● 1::ffff:2:3:6 = 1::ffff:2:3:6 (1)
760+
└─○ ff80::/16 (8)
761+
├─● ff80:: = ff80:: (1)
762+
└─● ff80:8000::/24 = ff80:8000::/24 (7)
763+
└─● ff80:8000::/32 (6)
764+
├─● ff80:8000:: (1)
765+
└─● ff80:8000:c000::/34 = ff80:8000:c000::/34 (4)
766+
└─○ ff80:8000:c800::/37 (3)
767+
├─● ff80:8000:c800:: (1)
768+
└─● ff80:8000:cc00::/38 = ff80:8000:cc00::/38 (2)
769+
└─● ff80:8000:cc00::/40 (1)
770+
*/
771+
581772
trie = ipaddr.IPv4AddressTrie{}
582773
fmt.Printf("%v %d %d %t %t",
583774
trie,

0 commit comments

Comments
 (0)