Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

convenience method added #101

Merged
merged 10 commits into from
Sep 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
strategy:
matrix:
go-version: ['stable', 'oldstable']
os: [ubuntu-latest, macos-latest, windows-latest]
os: [ubuntu-latest, macos-latest]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/setup-go@v5
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,11 @@ The API has changed in v0.4.2, 0.5.3, v0.6.3, v0.10.1, v0.11.0, v0.12.0, v0.12.6
and/or writers.

func (t *Table[V]) Insert(pfx netip.Prefix, val V)
func (t *Table[V]) Update(pfx netip.Prefix, cb func(val V, ok bool) V) (newVal V)
func (t *Table[V]) Delete(pfx netip.Prefix)

func (t *Table[V]) Get(pfx netip.Prefix) (val V, ok bool)
func (t *Table[V]) Update(pfx netip.Prefix, cb func(val V, ok bool) V) (newVal V)
func (t *Table[V]) GetAndDelete(pfx netip.Prefix) (val V, ok bool)

func (t *Table[V]) Union(o *Table[V])
func (t *Table[V]) Clone() *Table[V]
Expand Down
1,026 changes: 512 additions & 514 deletions allot_lookuptbl.go

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions base_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,14 @@ func cmpIndexRank(aIdx, bIdx uint) int {
if aBits <= bBits {
return -1
}

return 1
}

if aOctet < bOctet {
return -1
}

return 1
}

Expand Down
8 changes: 8 additions & 0 deletions table_cb.go → deprecated.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ func (t *Table[V]) EachLookupPrefix(pfx netip.Prefix, yield func(pfx netip.Prefi

// do not allocate
path := ip.As16()

octets := path[:]
if is4 {
octets = octets[12:]
}

copy(path[:], octets)

// see comment in Insert()
Expand All @@ -47,6 +49,7 @@ func (t *Table[V]) EachLookupPrefix(pfx netip.Prefix, yield func(pfx netip.Prefi

// run variable, used after for loop
var i int

var octet byte

// find last node
Expand All @@ -59,6 +62,7 @@ func (t *Table[V]) EachLookupPrefix(pfx netip.Prefix, yield func(pfx netip.Prefi
if c == nil {
break
}

n = c
}

Expand All @@ -77,6 +81,7 @@ func (t *Table[V]) EachLookupPrefix(pfx netip.Prefix, yield func(pfx netip.Prefi
// early exit
return
}

continue
}

Expand Down Expand Up @@ -107,10 +112,12 @@ func (t *Table[V]) EachSubnet(pfx netip.Prefix, yield func(pfx netip.Prefix, val

// do not allocate
path := ip.As16()

octets := path[:]
if is4 {
octets = octets[12:]
}

copy(path[:], octets)

// see comment in Insert()
Expand All @@ -126,6 +133,7 @@ func (t *Table[V]) EachSubnet(pfx netip.Prefix, yield func(pfx netip.Prefix, val
for i, octet := range octets {
if i == lastOctetIdx {
_ = n.eachSubnet(path, i, is4, lastOctet, lastOctetBits, yield)

return
}

Expand Down
79 changes: 79 additions & 0 deletions deprecated_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package bart

import (
"net/netip"
"reflect"
"testing"
)

func TestEachSubnetCompare(t *testing.T) {
t.Parallel()

pfxs := randomPrefixes(10_000)

fast := &Table[int]{}
gold := goldTable[int](pfxs)

for _, pfx := range pfxs {
fast.Insert(pfx.pfx, pfx.val)
}

for range 10_000 {
pfx := randomPrefix()
goldPfxs := gold.subnets(pfx)

var fastPfxs []netip.Prefix
values := map[netip.Prefix]int{}

fast.EachSubnet(pfx, func(p netip.Prefix, val int) bool {
fastPfxs = append(fastPfxs, p)
values[p] = val
return true
})

if !reflect.DeepEqual(goldPfxs, fastPfxs) {
t.Fatalf("\nEachSubnets(%q):\ngot: %v\nwant: %v", pfx, fastPfxs, goldPfxs)
}

// also check the values handled by yield function
for pfx, val := range values {
got, ok := fast.Get(pfx)

if !ok || got != val {
t.Fatalf("EachSubnets: Get(%q), got: %d,%v, want: %d,%v", pfx, got, ok, val, true)
}
}
}
}

func TestEachLookupPrefix(t *testing.T) {
t.Parallel()

pfxs := randomPrefixes(10_000)

fast := Table[int]{}
gold := goldTable[int](pfxs)

for _, pfx := range pfxs {
fast.Insert(pfx.pfx, pfx.val)
}

var fastPfxs []netip.Prefix

for range 10_000 {
pfx := randomPrefix()

goldPfxs := gold.lookupPrefixReverse(pfx)

fastPfxs = nil

fast.EachLookupPrefix(pfx, func(p netip.Prefix, _ int) bool {
fastPfxs = append(fastPfxs, p)
return true
})

if !reflect.DeepEqual(goldPfxs, fastPfxs) {
t.Fatalf("\nEachSupernet(%q):\ngot: %v\nwant: %v", pfx, fastPfxs, goldPfxs)
}
}
}
12 changes: 12 additions & 0 deletions dumper.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const (
func (t *Table[V]) dumpString() string {
w := new(strings.Builder)
t.dump(w)

return w.String()
}

Expand Down Expand Up @@ -60,6 +61,7 @@ func (t *Table[V]) dumpString() string {
func (t *Table[V]) dump(w io.Writer) {
if !t.isInit() {
fmt.Fprint(w, "")

return
}

Expand Down Expand Up @@ -111,6 +113,7 @@ func (n *node[V]) dump(w io.Writer, path [16]byte, depth int, is4 bool) {
octet, pfxLen := idxToPfx(idx)
fmt.Fprintf(w, " %s/%d", octetFmt(octet, is4), pfxLen)
}

fmt.Fprintln(w)

// print the values for this node
Expand All @@ -119,6 +122,7 @@ func (n *node[V]) dump(w io.Writer, path [16]byte, depth int, is4 bool) {
for _, val := range n.prefixes {
fmt.Fprintf(w, " %v", val)
}

fmt.Fprintln(w)
}

Expand All @@ -131,6 +135,7 @@ func (n *node[V]) dump(w io.Writer, path [16]byte, depth int, is4 bool) {
octet := byte(addr)
fmt.Fprintf(w, " %s", octetFmt(octet, is4))
}

fmt.Fprintln(w)
}
}
Expand All @@ -140,6 +145,7 @@ func octetFmt(octet byte, is4 bool) string {
if is4 {
return fmt.Sprintf("%d", octet)
}

return fmt.Sprintf("0x%02x", octet)
}

Expand All @@ -155,17 +161,21 @@ func ipStridePath(path [16]byte, depth int, is4 bool) string {
if i != 0 {
buf.WriteString(".")
}

buf.WriteString(strconv.Itoa(int(b)))
}

return buf.String()
}

for i, b := range path[:depth] {
if i != 0 && i%2 == 0 {
buf.WriteString(":")
}

buf.WriteString(fmt.Sprintf("%02x", b))
}

return buf.String()
}

Expand All @@ -181,6 +191,7 @@ func (nt nodeType) String() string {
case intermediateNode:
return "IMED"
}

panic("unreachable")
}

Expand All @@ -204,5 +215,6 @@ func (n *node[V]) hasType() nodeType {
if lenPefixes != 0 && lenChilds != 0 {
return fullNode
}

panic("unreachable")
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ module github.com/gaissmai/bart

go 1.22.0

require github.com/bits-and-blooms/bitset v1.14.2
require github.com/bits-and-blooms/bitset v1.14.3
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
github.com/bits-and-blooms/bitset v1.14.2 h1:YXVoyPndbdvcEVcseEovVfp0qjJp7S+i5+xgp/Nfbdc=
github.com/bits-and-blooms/bitset v1.14.2/go.mod h1:7hO7Gc7Pp1vODcmWvKMRA9BNmbv6a/7QIWpPxHddWR8=
github.com/bits-and-blooms/bitset v1.14.3 h1:Gd2c8lSNf9pKXom5JtD7AaKO8o7fGQ2LtFj1436qilA=
github.com/bits-and-blooms/bitset v1.14.3/go.mod h1:7hO7Gc7Pp1vODcmWvKMRA9BNmbv6a/7QIWpPxHddWR8=
2 changes: 2 additions & 0 deletions jsonify.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func (t *Table[V]) DumpList4() []DumpListNode[V] {
if !t.isInit() {
return nil
}

return t.root4.dumpListRec(0, zeroPath, 0, true)
}

Expand All @@ -55,6 +56,7 @@ func (t *Table[V]) DumpList6() []DumpListNode[V] {
if !t.isInit() {
return nil
}

return t.root6.dumpListRec(0, zeroPath, 0, false)
}

Expand Down
Loading
Loading