Skip to content

Commit

Permalink
restructure examples and doc
Browse files Browse the repository at this point in the history
  • Loading branch information
gaissmai committed Jan 5, 2024
1 parent 0b6d460 commit 0902b0a
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 66 deletions.
56 changes: 41 additions & 15 deletions example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,52 @@ import (
"github.com/gaissmai/cidrtree"
)

func a(s string) netip.Addr {
return netip.MustParseAddr(s)
}

func p(s string) netip.Prefix {
return netip.MustParsePrefix(s)
}

var input = []netip.Prefix{
netip.MustParsePrefix("fe80::/10"),
netip.MustParsePrefix("172.16.0.0/12"),
netip.MustParsePrefix("10.0.0.0/24"),
netip.MustParsePrefix("::1/128"),
netip.MustParsePrefix("192.168.0.0/16"),
netip.MustParsePrefix("10.0.0.0/8"),
netip.MustParsePrefix("::/0"),
netip.MustParsePrefix("10.0.1.0/24"),
netip.MustParsePrefix("169.254.0.0/16"),
netip.MustParsePrefix("2000::/3"),
netip.MustParsePrefix("2001:db8::/32"),
netip.MustParsePrefix("127.0.0.0/8"),
netip.MustParsePrefix("127.0.0.1/32"),
netip.MustParsePrefix("192.168.1.0/24"),
p("fe80::/10"),
p("172.16.0.0/12"),
p("10.0.0.0/24"),
p("::1/128"),
p("192.168.0.0/16"),
p("10.0.0.0/8"),
p("::/0"),
p("10.0.1.0/24"),
p("169.254.0.0/16"),
p("2000::/3"),
p("2001:db8::/32"),
p("127.0.0.0/8"),
p("127.0.0.1/32"),
p("192.168.1.0/24"),
}

func ExampleTable_Fprint() {
func ExampleTable_Lookup() {
rtbl := new(cidrtree.Table)
for _, cidr := range input {
rtbl.Insert(cidr, nil)
}
rtbl.Fprint(os.Stdout)

fmt.Println()

ip := a("42.0.0.0")
lpm, value, ok := rtbl.Lookup(ip)
fmt.Printf("Lookup: %-20v lpm: %-15v value: %v, ok: %v\n", ip, lpm, value, ok)

ip = a("10.0.1.17")
lpm, value, ok = rtbl.Lookup(ip)
fmt.Printf("Lookup: %-20v lpm: %-15v value: %v, ok: %v\n", ip, lpm, value, ok)

ip = a("2001:7c0:3100:1::111")
lpm, value, ok = rtbl.Lookup(ip)
fmt.Printf("Lookup: %-20v lpm: %-15v value: %v, ok: %v\n", ip, lpm, value, ok)

// Output:
// ▼
// ├─ 10.0.0.0/8 (<nil>)
Expand All @@ -49,6 +71,10 @@ func ExampleTable_Fprint() {
// ├─ 2000::/3 (<nil>)
// │ └─ 2001:db8::/32 (<nil>)
// └─ fe80::/10 (<nil>)
//
// Lookup: 42.0.0.0 lpm: invalid Prefix value: <nil>, ok: false
// Lookup: 10.0.1.17 lpm: 10.0.1.0/24 value: <nil>, ok: true
// Lookup: 2001:7c0:3100:1::111 lpm: 2000::/3 value: <nil>, ok: true
}

func ExampleTable_Walk() {
Expand Down
51 changes: 0 additions & 51 deletions treap.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,31 +273,6 @@ func (n *node) walk(cb func(netip.Prefix, any) bool) bool {
// If the ip isn't covered by any CIDR, the zero value and false is returned.
//
// Lookup does not allocate memory.
//
// example:
//
// ▼
// ├─ 10.0.0.0/8
// │ ├─ 10.0.0.0/24
// │ └─ 10.0.1.0/24
// ├─ 127.0.0.0/8
// │ └─ 127.0.0.1/32
// ├─ 169.254.0.0/16
// ├─ 172.16.0.0/12
// └─ 192.168.0.0/16
// └─ 192.168.1.0/24
// ▼
// └─ ::/0
// ├─ ::1/128
// ├─ 2000::/3
// │ └─ 2001:db8::/32
// ├─ fc00::/7
// ├─ fe80::/10
// └─ ff00::/8
//
// rtbl.Lookup(42.0.0.0) returns (netip.Prefix{}, <nil>, false)
// rtbl.Lookup(10.0.1.17) returns (10.0.1.0/24, <value>, true)
// rtbl.Lookup(2001:7c0:3100:1::111) returns (2000::/3, <value>, true)
func (t Table) Lookup(ip netip.Addr) (lpm netip.Prefix, value any, ok bool) {
if ip.Is4() {
// don't return the depth
Expand Down Expand Up @@ -351,32 +326,6 @@ func (n *node) lpmIP(ip netip.Addr, depth int) (lpm netip.Prefix, value any, ok
// If the prefix isn't equal or covered by any CIDR in the table, the zero value and false is returned.
//
// LookupPrefix does not allocate memory.
//
// example:
//
// ▼
// ├─ 10.0.0.0/8
// │ ├─ 10.0.0.0/24
// │ └─ 10.0.1.0/24
// ├─ 127.0.0.0/8
// │ └─ 127.0.0.1/32
// ├─ 169.254.0.0/16
// ├─ 172.16.0.0/12
// └─ 192.168.0.0/16
// └─ 192.168.1.0/24
// ▼
// └─ ::/0
// ├─ ::1/128
// ├─ 2000::/3
// │ └─ 2001:db8::/32
// ├─ fc00::/7
// ├─ fe80::/10
// └─ ff00::/8
//
// rtbl.LookupPrefix(42.0.0.0/8) returns (netip.Prefix{}, <nil>, false)
// rtbl.LookupPrefix(10.0.1.0/29) returns (10.0.1.0/24, <value>, true)
// rtbl.LookupPrefix(192.168.0.0/16) returns (192.168.0.0/16, <value>, true)
// rtbl.LookupPrefix(2001:7c0:3100::/40) returns (2000::/3, <value>, true)
func (t Table) LookupPrefix(pfx netip.Prefix) (lpm netip.Prefix, value any, ok bool) {
pfx = pfx.Masked() // always canonicalize!

Expand Down

0 comments on commit 0902b0a

Please sign in to comment.