-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
34 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |