Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
gaissmai committed Sep 2, 2024
1 parent 65275aa commit ed740a8
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 5 deletions.
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 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
3 changes: 2 additions & 1 deletion deprecated_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,14 @@ func TestEachLookupPrefix(t *testing.T) {
}

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
Expand All @@ -73,6 +75,5 @@ func TestEachLookupPrefix(t *testing.T) {
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: 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
17 changes: 16 additions & 1 deletion node.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,14 @@ func (n *node[V]) insertPrefix(idx uint, val V) (ok bool) {
// prefix exists, overwrite val
if n.prefixesBitset.Test(idx) {
n.prefixes[n.prefixRank(idx)] = val

return false
}

// new, insert into bitset and slice
n.prefixesBitset.Set(idx)
n.prefixes = slices.Insert(n.prefixes, n.prefixRank(idx), val)

return true
}

Expand Down Expand Up @@ -112,6 +114,7 @@ func (n *node[V]) updatePrefix(octet byte, prefixLen int, cb func(V, bool) V) (n

// if prefix is set, get current value
var oldVal V

if wasPresent = n.prefixesBitset.Test(idx); wasPresent {
rnk = n.prefixRank(idx)
oldVal = n.prefixes[rnk]
Expand All @@ -123,6 +126,7 @@ func (n *node[V]) updatePrefix(octet byte, prefixLen int, cb func(V, bool) V) (n
// prefix is already set, update and return value
if wasPresent {
n.prefixes[rnk] = newVal

return
}

Expand Down Expand Up @@ -174,6 +178,7 @@ func (n *node[V]) getValueOK(idx uint) (val V, ok bool) {
if n.prefixesBitset.Test(idx) {
return n.prefixes[n.prefixRank(idx)], true
}

return
}

Expand All @@ -186,6 +191,7 @@ func (n *node[V]) mustGetValue(idx uint) V {
// allStrideIndexes returns all baseIndexes set in this stride node in ascending order.
func (n *node[V]) allStrideIndexes(buffer []uint) []uint {
_, buffer = n.prefixesBitset.NextSetMany(0, buffer)

return buffer
}

Expand All @@ -203,6 +209,7 @@ func (n *node[V]) insertChild(octet byte, child *node[V]) {
// child exists, overwrite it
if n.childrenBitset.Test(uint(octet)) {
n.children[n.childRank(octet)] = child

return
}

Expand Down Expand Up @@ -240,6 +247,7 @@ func (n *node[V]) getChild(octet byte) *node[V] {
// panics if the buffer isn't big enough.
func (n *node[V]) allChildAddrs(buffer []uint) []uint {
_, buffer = n.childrenBitset.NextSetMany(0, buffer)

return buffer
}

Expand Down Expand Up @@ -268,15 +276,16 @@ func (n *node[V]) eachSubnet(path [16]byte, depth int, is4 bool, octet byte, pfx
// ###############################################################
// 1. collect all indices in n covered by prefix
// ###############################################################

pfxFirstAddr := uint(octet)
pfxLastAddr := uint(octet | ^netMask[pfxLen])

idxBackingArray := [maxNodePrefixes]uint{}
allCoveredIndices := idxBackingArray[:0]

var idx uint

var ok bool

for {
if idx, ok = n.prefixesBitset.NextSet(idx); !ok {
break
Expand All @@ -291,6 +300,7 @@ func (n *node[V]) eachSubnet(path [16]byte, depth int, is4 bool, octet byte, pfx
if thisFirstAddr >= pfxFirstAddr && thisLastAddr <= pfxLastAddr {
allCoveredIndices = append(allCoveredIndices, idx)
}

idx++
}

Expand All @@ -305,6 +315,7 @@ func (n *node[V]) eachSubnet(path [16]byte, depth int, is4 bool, octet byte, pfx
allCoveredAddrs := addrBackingArray[:0]

var addr uint

for {
if addr, ok = n.childrenBitset.NextSet(addr); !ok {
break
Expand Down Expand Up @@ -353,6 +364,7 @@ func (n *node[V]) eachSubnet(path [16]byte, depth int, is4 bool, octet byte, pfx
// early exit
return false
}

cursor++
}

Expand Down Expand Up @@ -427,6 +439,7 @@ func (n *node[V]) unionRec(o *node[V]) (duplicates int) {
duplicates += nc.unionRec(oc)
}
}

return duplicates
}

Expand Down Expand Up @@ -539,6 +552,7 @@ func (n *node[V]) allRecSorted(path [16]byte, depth int, is4 bool, yield func(ne
// early exit
return false
}

childCursor++
}

Expand All @@ -554,6 +568,7 @@ func (n *node[V]) allRecSorted(path [16]byte, depth int, is4 bool, yield func(ne
for j := childCursor; j < len(childAddrs); j++ {
addr := childAddrs[j]
c := n.children[j]

path[depth] = byte(addr)
if !c.allRecSorted(path, depth+1, is4, yield) {
// early exit
Expand Down
10 changes: 8 additions & 2 deletions overlaps.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ func (n *node[V]) overlapsRec(o *node[V]) bool {
if nPfxLen == 1 && nChildLen == 0 {
// get the single prefix from n
idx, _ := n.prefixesBitset.NextSet(0)

return o.overlapsPrefix(idxToPfx(idx))
}

// special case, overlapsPrefix is faster
if oPfxLen == 1 && oChildLen == 0 {
// get the single prefix from o
idx, _ := o.prefixesBitset.NextSet(0)

return n.overlapsPrefix(idxToPfx(idx))
}

Expand Down Expand Up @@ -102,13 +104,13 @@ func (n *node[V]) overlapsRoutes(o *node[V]) bool {

// zip, range over n and o together to help chance on its way
for nOK || oOK {

if nOK {
// does any route in o overlap this prefix from n
if nIdx, nOK = n.prefixesBitset.NextSet(nIdx); nOK {
if o.lpmTest(nIdx) {
return true
}

nIdx++
}
}
Expand All @@ -119,6 +121,7 @@ func (n *node[V]) overlapsRoutes(o *node[V]) bool {
if n.lpmTest(oIdx) {
return true
}

oIdx++
}
}
Expand All @@ -140,13 +143,15 @@ func (n *node[V]) overlapsChildsIn(o *node[V]) bool {
// do range over, not so many childs and maybe to many prefixes
if doRange {
var oAddr uint

ok := true
for ok {
// does any route in o overlap this child from n
if oAddr, ok = o.childrenBitset.NextSet(oAddr); ok {
if n.lpmTest(hostIndex(byte(oAddr))) {
return true
}

oAddr++
}
}
Expand Down Expand Up @@ -214,9 +219,11 @@ func (n *node[V]) overlapsOneChildIn(o *node[V]) bool {
// get the single addr and child
addr, _ := o.childrenBitset.NextSet(0)
oChild := o.children[0]

if nChild := n.getChild(byte(addr)); nChild != nil {
return nChild.overlapsRec(oChild)
}

return false
}

Expand All @@ -243,7 +250,6 @@ func (n *node[V]) overlapsOneRouteIn(o *node[V]) bool {
// overlapsPrefix returns true if node overlaps with prefix.
func (n *node[V]) overlapsPrefix(octet byte, pfxLen int) bool {
// 1. Test if any route in this node overlaps prefix?

idx := pfxToIdx(octet, pfxLen)
if n.lpmTest(idx) {
return true
Expand Down
Loading

0 comments on commit ed740a8

Please sign in to comment.