Skip to content

Commit

Permalink
do not expose interval.CanonicalSet.intervalSet
Browse files Browse the repository at this point in the history
Signed-off-by: Elazar Gershuni <[email protected]>
  • Loading branch information
Elazar Gershuni committed Mar 11, 2024
1 parent 0f4bba7 commit 505bb70
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 38 deletions.
4 changes: 2 additions & 2 deletions pkg/connection/connectionset.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,7 @@ func getCubeAsTCPorUDPItems(cube []*interval.CanonicalSet, isTCP bool) []netp.Pr
tcpItemsTemp = append(tcpItemsTemp, netp.TCPUDP{IsTCP: isTCP})
} else {
// iterate the intervals in the interval-set
for _, portRange := range srcPorts.IntervalSet {
for _, portRange := range srcPorts.Intervals() {
tcpRes := netp.TCPUDP{
IsTCP: isTCP,
PortRangePair: netp.PortRangePair{
Expand All @@ -363,7 +363,7 @@ func getCubeAsTCPorUDPItems(cube []*interval.CanonicalSet, isTCP bool) []netp.Pr
return tcpItemsTemp
}
tcpItemsFinal := []netp.Protocol{}
for _, portRange := range dstPorts.IntervalSet {
for _, portRange := range dstPorts.Intervals() {
for _, tcpItemTemp := range tcpItemsTemp {
item, _ := tcpItemTemp.(netp.TCPUDP)
tcpItemsFinal = append(tcpItemsFinal, netp.TCPUDP{
Expand Down
68 changes: 38 additions & 30 deletions pkg/interval/intervalset.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,37 @@ import (
"sort"
)

// CanonicalSet is a canonical representation of a set of Interval objects
// CanonicalSet is a set of int64 integers, implemented using an ordered slice of non-overlapping, non-touching interval
type CanonicalSet struct {
IntervalSet []Interval // sorted list of non-overlapping intervals
intervalSet []Interval
}

func NewCanonicalIntervalSet() *CanonicalSet {
return &CanonicalSet{
IntervalSet: []Interval{},
intervalSet: []Interval{},
}
}

// IsEmpty returns true if the CanonicalSet is empty
func (c *CanonicalSet) IsEmpty() bool {
return len(c.IntervalSet) == 0
return len(c.intervalSet) == 0
}

func (c *CanonicalSet) CalculateSize() int64 {
var res int64 = 0
for _, r := range c.IntervalSet {
for _, r := range c.intervalSet {
res += r.Size()
}
return res
}

// Equal returns true if the CanonicalSet equals the input CanonicalSet
func (c *CanonicalSet) Equal(other *CanonicalSet) bool {
if len(c.IntervalSet) != len(other.IntervalSet) {
if len(c.intervalSet) != len(other.intervalSet) {
return false
}
for index := range c.IntervalSet {
if !(c.IntervalSet[index].Equal(other.IntervalSet[index])) {
for index := range c.intervalSet {
if !(c.intervalSet[index].Equal(other.intervalSet[index])) {
return false
}
}
Expand All @@ -48,7 +48,7 @@ func (c *CanonicalSet) Equal(other *CanonicalSet) bool {

// AddInterval adds a new interval range to the set
func (c *CanonicalSet) AddInterval(v Interval) {
set := c.IntervalSet
set := c.intervalSet
left := sort.Search(len(set), func(i int) bool {
return set[i].End >= v.Start-1
})
Expand All @@ -61,7 +61,7 @@ func (c *CanonicalSet) AddInterval(v Interval) {
if right > 0 && set[right-1].End >= v.Start {
v.End = max(v.End, set[right-1].End)
}
c.IntervalSet = slices.Replace(c.IntervalSet, left, right, v)
c.intervalSet = slices.Replace(c.intervalSet, left, right, v)
}

// String returns a string representation of the current CanonicalSet object
Expand All @@ -70,7 +70,7 @@ func (c *CanonicalSet) String() string {
return "Empty"
}
res := ""
for _, interval := range c.IntervalSet {
for _, interval := range c.intervalSet {
if interval.Start != interval.End {
res += fmt.Sprintf("%v-%v", interval.Start, interval.End)
} else {
Expand All @@ -84,15 +84,15 @@ func (c *CanonicalSet) String() string {
// Union returns the union of the two sets
func (c *CanonicalSet) Union(other *CanonicalSet) *CanonicalSet {
res := c.Copy()
for _, interval := range other.IntervalSet {
for _, interval := range other.intervalSet {
res.AddInterval(interval)
}
return res
}

// Copy returns a new copy of the CanonicalSet object
func (c *CanonicalSet) Copy() *CanonicalSet {
return &CanonicalSet{IntervalSet: append([]Interval(nil), c.IntervalSet...)}
return &CanonicalSet{intervalSet: append([]Interval(nil), c.intervalSet...)}
}

func (c *CanonicalSet) Contains(n int64) bool {
Expand All @@ -102,8 +102,8 @@ func (c *CanonicalSet) Contains(n int64) bool {

// ContainedIn returns true of the current interval.CanonicalSet is contained in the input interval.CanonicalSet
func (c *CanonicalSet) ContainedIn(other *CanonicalSet) bool {
larger := other.IntervalSet
for _, target := range c.IntervalSet {
larger := other.intervalSet
for _, target := range c.intervalSet {
left := sort.Search(len(larger), func(i int) bool {
return larger[i].End >= target.End
})
Expand All @@ -119,18 +119,18 @@ func (c *CanonicalSet) ContainedIn(other *CanonicalSet) bool {
// Intersect returns the intersection of the current set with the input set
func (c *CanonicalSet) Intersect(other *CanonicalSet) *CanonicalSet {
res := NewCanonicalIntervalSet()
for _, interval := range c.IntervalSet {
for _, otherInterval := range other.IntervalSet {
res.IntervalSet = append(res.IntervalSet, interval.intersection(otherInterval)...)
for _, interval := range c.intervalSet {
for _, otherInterval := range other.intervalSet {
res.intervalSet = append(res.intervalSet, interval.intersection(otherInterval)...)
}
}
return res
}

// Overlaps returns true if current CanonicalSet overlaps with input CanonicalSet
func (c *CanonicalSet) Overlaps(other *CanonicalSet) bool {
for _, selfInterval := range c.IntervalSet {
for _, otherInterval := range other.IntervalSet {
for _, selfInterval := range c.intervalSet {
for _, otherInterval := range other.intervalSet {
if selfInterval.overlaps(otherInterval) {
return true
}
Expand All @@ -141,45 +141,53 @@ func (c *CanonicalSet) Overlaps(other *CanonicalSet) bool {

// Subtract returns the subtraction result of input CanonicalSet
func (c *CanonicalSet) Subtract(other *CanonicalSet) *CanonicalSet {
res := slices.Clone(c.IntervalSet)
for _, hole := range other.IntervalSet {
res := slices.Clone(c.intervalSet)
for _, hole := range other.intervalSet {
newIntervalSet := []Interval{}
for _, interval := range res {
newIntervalSet = append(newIntervalSet, interval.subtract(hole)...)
}
res = newIntervalSet
}
return &CanonicalSet{
IntervalSet: res,
intervalSet: res,
}
}

func (c *CanonicalSet) IsSingleNumber() bool {
if len(c.IntervalSet) == 1 && c.IntervalSet[0].Size() == 1 {
if len(c.intervalSet) == 1 && c.intervalSet[0].Size() == 1 {
return true
}
return false
}

func (c *CanonicalSet) Min() (int64, error) {
if len(c.IntervalSet) > 0 {
return c.IntervalSet[0].Start, nil
if len(c.intervalSet) > 0 {
return c.intervalSet[0].Start, nil
}
return 0, errors.New("empty interval set")
}

// Split returns a set of canonical set objects, each with a single interval
func (c *CanonicalSet) Split() []*CanonicalSet {
res := make([]*CanonicalSet, len(c.IntervalSet))
for index, ipr := range c.IntervalSet {
res := make([]*CanonicalSet, len(c.intervalSet))
for index, ipr := range c.intervalSet {
res[index] = CreateSetFromInterval(ipr.Start, ipr.End)
}
return res
}

func (c *CanonicalSet) Intervals() []Interval {
return slices.Clone(c.intervalSet)
}

func (c *CanonicalSet) NumIntervals() int {
return len(c.intervalSet)
}

func (c *CanonicalSet) Elements() []int64 {
res := []int64{}
for _, interval := range c.IntervalSet {
for _, interval := range c.intervalSet {
for i := interval.Start; i <= interval.End; i++ {
res = append(res, i)
}
Expand All @@ -188,5 +196,5 @@ func (c *CanonicalSet) Elements() []int64 {
}

func CreateSetFromInterval(start, end int64) *CanonicalSet {
return &CanonicalSet{IntervalSet: []Interval{{Start: start, End: end}}}
return &CanonicalSet{intervalSet: []Interval{{Start: start, End: end}}}
}
12 changes: 6 additions & 6 deletions pkg/ipblock/ipblock.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ func toIPRange(i interval.Interval) string {

// toIPRangesList: returns a list of the ip-ranges strings in the current IPBlock object
func (b *IPBlock) toIPRangesList() []string {
IPRanges := make([]string, len(b.ipRange.IntervalSet))
for index := range b.ipRange.IntervalSet {
IPRanges[index] = toIPRange(b.ipRange.IntervalSet[index])
IPRanges := make([]string, b.ipRange.NumIntervals())
for index, v := range b.ipRange.Intervals() {
IPRanges[index] = toIPRange(v)
}
return IPRanges
}
Expand Down Expand Up @@ -112,7 +112,7 @@ func (b *IPBlock) ipCount() int {

// Split returns a set of IpBlock objects, each with a single range of ips
func (b *IPBlock) Split() []*IPBlock {
res := make([]*IPBlock, len(b.ipRange.IntervalSet))
res := make([]*IPBlock, b.ipRange.NumIntervals())
for index, set := range b.ipRange.Split() {
res[index] = &IPBlock{
ipRange: set,
Expand Down Expand Up @@ -262,7 +262,7 @@ func cidrToIPRange(cidr string) (start, end int64, err error) {
// ToCidrList returns a list of CIDR strings for this IPBlock object
func (b *IPBlock) ToCidrList() []string {
cidrList := []string{}
for _, ipRange := range b.ipRange.IntervalSet {
for _, ipRange := range b.ipRange.Intervals() {
cidrList = append(cidrList, intervalToCidrList(ipRange)...)
}
return cidrList
Expand All @@ -276,7 +276,7 @@ func (b *IPBlock) ToCidrListString() string {
// ListToPrint: returns a uniform to print list s.t. each element contains either a single cidr or an ip range
func (b *IPBlock) ListToPrint() []string {
cidrsIPRangesList := []string{}
for _, ipRange := range b.ipRange.IntervalSet {
for _, ipRange := range b.ipRange.Intervals() {
cidr := intervalToCidrList(ipRange)
if len(cidr) == 1 {
cidrsIPRangesList = append(cidrsIPRangesList, cidr[0])
Expand Down

0 comments on commit 505bb70

Please sign in to comment.