Skip to content

Commit

Permalink
store wildcardSubs as bools
Browse files Browse the repository at this point in the history
  • Loading branch information
jub0bs committed Jan 30, 2025
1 parent 4a314b8 commit b567e15
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 39 deletions.
17 changes: 7 additions & 10 deletions internal/origins/radix/radix.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,6 @@ type node struct {
}

func (n *node) add(elem int, toWildcardSet bool) {
if n.set == nil {
n.set = NewSet(elem, toWildcardSet)
return
}
if n.set.Contains(WildcardElem, false) || n.set.Contains(WildcardElem, true) { // nothing to do
return
}
n.set.Add(elem, toWildcardSet)
}

Expand All @@ -196,14 +189,12 @@ type edges = map[byte]*node
// using suf as a base suffix.
func (n *node) Elems(dst *[]string, suf string) {
suf = n.suf + suf
for port, wildcardSub := range n.set {
for port, wildcardSub := range n.set.m {
var (
s string
prefix string
)
switch port {
case WildcardElem:
s = suf + ":*"
case 0:
s = suf
default:
Expand All @@ -214,6 +205,12 @@ func (n *node) Elems(dst *[]string, suf string) {
}
*dst = append(*dst, prefix+s)
}
if n.set.anyPort {
*dst = append(*dst, suf+":*")
}
if n.set.anyPortAndSub {
*dst = append(*dst, "*"+suf+":*")
}
for _, child := range n.edges {
child.Elems(dst, suf)
}
Expand Down
56 changes: 27 additions & 29 deletions internal/origins/radix/set.go
Original file line number Diff line number Diff line change
@@ -1,38 +1,36 @@
package radix

import "slices"

// A Set represents a mathematical set of ints or strings.
type Set map[int]bool

// NewSet returns a Set that contains first and all elements of rest,
// but no other elements.
func NewSet(elem int, wildcardSub bool) Set {
return Set{elem: wildcardSub}
type Set struct {
m map[int]bool
anyPort bool
anyPortAndSub bool
}

// Add adds e to s.
func (s Set) Add(e int, wildcardSub bool) {
s[e] = wildcardSub
}

// Contains returns true if e is an element of s, and false otherwise.
func (s Set) Contains(e int, wildcardSub bool) bool {
v, found := s[e]
return found && v == wildcardSub
}
func (s *Set) Add(elem int, wildcardSub bool) {
if elem == WildcardElem {
if wildcardSub {
s.anyPortAndSub = true
} else {
s.anyPort = true
}
s.m = nil
return
}
if s.anyPort && !wildcardSub || s.anyPortAndSub && wildcardSub {
s.m = nil
return
}
if s.m == nil {
s.m = make(map[int]bool)
}
s.m[elem] = wildcardSub

// Size returns the cardinality of s.
func (s Set) Size() int {
return len(s)
}

// ToSortedSlice returns a sorted slice containing the results.
func (s Set) ToSortedSlice() []int {
res := make([]int, 0, len(s))
for elem := range s {
res = append(res, elem)
func (s Set) Contains(elem int, wildcardSub bool) bool {
if elem == WildcardElem {
return s.anyPort && !wildcardSub || s.anyPortAndSub && wildcardSub
}
slices.Sort(res)
return res
v, found := s.m[elem]
return found && v == wildcardSub
}

0 comments on commit b567e15

Please sign in to comment.