From 1d1ae54f7c35b91b8bf844c65fdc75dd58030db7 Mon Sep 17 00:00:00 2001 From: Elazar Gershuni Date: Tue, 12 Mar 2024 14:23:15 +0200 Subject: [PATCH] rename CanonicalIntervalSet too Signed-off-by: Elazar Gershuni --- pkg/hypercube/hypercubeset.go | 38 +++++++++---------- pkg/hypercube/hypercubeset_test.go | 28 +++++++------- pkg/interval/intervalset.go | 60 +++++++++++++++--------------- pkg/ipblock/ipblock.go | 8 ++-- 4 files changed, 67 insertions(+), 67 deletions(-) diff --git a/pkg/hypercube/hypercubeset.go b/pkg/hypercube/hypercubeset.go index 0cd8d00..018a231 100644 --- a/pkg/hypercube/hypercubeset.go +++ b/pkg/hypercube/hypercubeset.go @@ -10,14 +10,14 @@ import ( // CanonicalSet is a canonical representation for set of n-dimensional cubes, from integer intervals type CanonicalSet struct { - layers map[*interval.CanonicalIntervalSet]*CanonicalSet + layers map[*interval.CanonicalSet]*CanonicalSet dimensions int } // NewCanonicalSet returns a new empty CanonicalSet with n dimensions func NewCanonicalSet(n int) *CanonicalSet { return &CanonicalSet{ - layers: map[*interval.CanonicalIntervalSet]*CanonicalSet{}, + layers: map[*interval.CanonicalSet]*CanonicalSet{}, dimensions: n, } } @@ -54,7 +54,7 @@ func (c *CanonicalSet) Union(other *CanonicalSet) *CanonicalSet { return nil } res := NewCanonicalSet(c.dimensions) - remainingFromOther := map[*interval.CanonicalIntervalSet]*interval.CanonicalIntervalSet{} + remainingFromOther := map[*interval.CanonicalSet]*interval.CanonicalSet{} for k := range other.layers { kCopy := k.Copy() remainingFromOther[k] = &kCopy @@ -152,7 +152,7 @@ func (c *CanonicalSet) Subtraction(other *CanonicalSet) *CanonicalSet { return res } -func (c *CanonicalSet) getIntervalSetUnion() *interval.CanonicalIntervalSet { +func (c *CanonicalSet) getIntervalSetUnion() *interval.CanonicalSet { res := interval.NewCanonicalIntervalSet() for k := range c.layers { res.Union(*k) @@ -209,7 +209,7 @@ func (c *CanonicalSet) Copy() *CanonicalSet { return res } -func getCubeStr(cube []*interval.CanonicalIntervalSet) string { +func getCubeStr(cube []*interval.CanonicalSet) string { strList := []string{} for _, v := range cube { strList = append(strList, "("+v.String()+")") @@ -228,19 +228,19 @@ func (c *CanonicalSet) String() string { return strings.Join(strList, "; ") } -// GetCubesList returns the list of cubes in c, each cube as a slice of CanonicalIntervalSet -func (c *CanonicalSet) GetCubesList() [][]*interval.CanonicalIntervalSet { - res := [][]*interval.CanonicalIntervalSet{} +// GetCubesList returns the list of cubes in c, each cube as a slice of CanonicalSet +func (c *CanonicalSet) GetCubesList() [][]*interval.CanonicalSet { + res := [][]*interval.CanonicalSet{} if c.dimensions == 1 { for k := range c.layers { - res = append(res, []*interval.CanonicalIntervalSet{k}) + res = append(res, []*interval.CanonicalSet{k}) } return res } for k, v := range c.layers { subRes := v.GetCubesList() for _, subList := range subRes { - cube := []*interval.CanonicalIntervalSet{k} + cube := []*interval.CanonicalSet{k} cube = append(cube, subList...) res = append(res, cube) } @@ -250,18 +250,18 @@ func (c *CanonicalSet) GetCubesList() [][]*interval.CanonicalIntervalSet { func (c *CanonicalSet) applyElementsUnionPerLayer() { type pair struct { - hc *CanonicalSet // hypercube set object - is []*interval.CanonicalIntervalSet // interval-set list + hc *CanonicalSet // hypercube set object + is []*interval.CanonicalSet // interval-set list } equivClasses := map[string]*pair{} for k, v := range c.layers { if _, ok := equivClasses[v.String()]; ok { equivClasses[v.String()].is = append(equivClasses[v.String()].is, k) } else { - equivClasses[v.String()] = &pair{hc: v, is: []*interval.CanonicalIntervalSet{k}} + equivClasses[v.String()] = &pair{hc: v, is: []*interval.CanonicalSet{k}} } } - newLayers := map[*interval.CanonicalIntervalSet]*CanonicalSet{} + newLayers := map[*interval.CanonicalSet]*CanonicalSet{} for _, p := range equivClasses { newVal := p.hc newKey := p.is[0] @@ -274,8 +274,8 @@ func (c *CanonicalSet) applyElementsUnionPerLayer() { } // FromCube returns a new CanonicalSet created from a single input cube -// the input cube is a slice of CanonicalIntervalSet, treated as ordered list of dimension values -func FromCube(cube []*interval.CanonicalIntervalSet) *CanonicalSet { +// the input cube is a slice of CanonicalSet, treated as ordered list of dimension values +func FromCube(cube []*interval.CanonicalSet) *CanonicalSet { if len(cube) == 0 { return nil } @@ -291,7 +291,7 @@ func FromCube(cube []*interval.CanonicalIntervalSet) *CanonicalSet { return res } -func FromCubeAsIntervals(values ...*interval.CanonicalIntervalSet) *CanonicalSet { +func FromCubeAsIntervals(values ...*interval.CanonicalSet) *CanonicalSet { return FromCube(values) } @@ -299,14 +299,14 @@ func FromCubeAsIntervals(values ...*interval.CanonicalIntervalSet) *CanonicalSet // the input cube is given as an ordered list of integer values, where each two values // represent the range (start,end) for a dimension value func FromCubeShort(values ...int64) *CanonicalSet { - cube := []*interval.CanonicalIntervalSet{} + cube := []*interval.CanonicalSet{} for i := 0; i < len(values); i += 2 { cube = append(cube, interval.FromInterval(values[i], values[i+1])) } return FromCube(cube) } -func copyIntervalSet(a *interval.CanonicalIntervalSet) *interval.CanonicalIntervalSet { +func copyIntervalSet(a *interval.CanonicalSet) *interval.CanonicalSet { res := a.Copy() return &res } diff --git a/pkg/hypercube/hypercubeset_test.go b/pkg/hypercube/hypercubeset_test.go index 9c1af9b..1959f97 100644 --- a/pkg/hypercube/hypercubeset_test.go +++ b/pkg/hypercube/hypercubeset_test.go @@ -9,12 +9,12 @@ import ( ) func TestHCBasic(t *testing.T) { - cube1 := []*interval.CanonicalIntervalSet{interval.FromInterval(1, 100)} - cube2 := []*interval.CanonicalIntervalSet{interval.FromInterval(1, 100)} - cube3 := []*interval.CanonicalIntervalSet{interval.FromInterval(1, 200)} - cube4 := []*interval.CanonicalIntervalSet{interval.FromInterval(1, 100), interval.FromInterval(1, 100)} - cube5 := []*interval.CanonicalIntervalSet{interval.FromInterval(1, 100), interval.FromInterval(1, 100)} - cube6 := []*interval.CanonicalIntervalSet{interval.FromInterval(1, 100), interval.FromInterval(1, 200)} + cube1 := []*interval.CanonicalSet{interval.FromInterval(1, 100)} + cube2 := []*interval.CanonicalSet{interval.FromInterval(1, 100)} + cube3 := []*interval.CanonicalSet{interval.FromInterval(1, 200)} + cube4 := []*interval.CanonicalSet{interval.FromInterval(1, 100), interval.FromInterval(1, 100)} + cube5 := []*interval.CanonicalSet{interval.FromInterval(1, 100), interval.FromInterval(1, 100)} + cube6 := []*interval.CanonicalSet{interval.FromInterval(1, 100), interval.FromInterval(1, 200)} a := hypercube.FromCube(cube1) b := hypercube.FromCube(cube2) @@ -57,7 +57,7 @@ func TestHCBasic(t *testing.T) { } func TestCopy(t *testing.T) { - cube1 := []*interval.CanonicalIntervalSet{interval.FromInterval(1, 100)} + cube1 := []*interval.CanonicalSet{interval.FromInterval(1, 100)} a := hypercube.FromCube(cube1) b := a.Copy() if !a.Equal(b) { @@ -72,8 +72,8 @@ func TestCopy(t *testing.T) { } func TestString(t *testing.T) { - cube1 := []*interval.CanonicalIntervalSet{interval.FromInterval(1, 100)} - cube2 := []*interval.CanonicalIntervalSet{interval.FromInterval(1, 100), interval.FromInterval(1, 100)} + cube1 := []*interval.CanonicalSet{interval.FromInterval(1, 100)} + cube2 := []*interval.CanonicalSet{interval.FromInterval(1, 100), interval.FromInterval(1, 100)} a := hypercube.FromCube(cube1) b := hypercube.FromCube(cube2) fmt.Println(a.String()) @@ -82,8 +82,8 @@ func TestString(t *testing.T) { } func TestOr(t *testing.T) { - cube1 := []*interval.CanonicalIntervalSet{interval.FromInterval(1, 100), interval.FromInterval(1, 100)} - cube2 := []*interval.CanonicalIntervalSet{interval.FromInterval(1, 90), interval.FromInterval(1, 200)} + cube1 := []*interval.CanonicalSet{interval.FromInterval(1, 100), interval.FromInterval(1, 100)} + cube2 := []*interval.CanonicalSet{interval.FromInterval(1, 90), interval.FromInterval(1, 200)} a := hypercube.FromCube(cube1) b := hypercube.FromCube(cube2) c := a.Union(b) @@ -94,19 +94,19 @@ func TestOr(t *testing.T) { } func addCube1Dim(o *hypercube.CanonicalSet, start, end int64) *hypercube.CanonicalSet { - cube := []*interval.CanonicalIntervalSet{interval.FromInterval(start, end)} + cube := []*interval.CanonicalSet{interval.FromInterval(start, end)} a := hypercube.FromCube(cube) return o.Union(a) } func addCube2Dim(o *hypercube.CanonicalSet, start1, end1, start2, end2 int64) *hypercube.CanonicalSet { - cube := []*interval.CanonicalIntervalSet{interval.FromInterval(start1, end1), interval.FromInterval(start2, end2)} + cube := []*interval.CanonicalSet{interval.FromInterval(start1, end1), interval.FromInterval(start2, end2)} a := hypercube.FromCube(cube) return o.Union(a) } func addCube3Dim(o *hypercube.CanonicalSet, s1, e1, s2, e2, s3, e3 int64) *hypercube.CanonicalSet { - cube := []*interval.CanonicalIntervalSet{ + cube := []*interval.CanonicalSet{ interval.FromInterval(s1, e1), interval.FromInterval(s2, e2), interval.FromInterval(s3, e3)} diff --git a/pkg/interval/intervalset.go b/pkg/interval/intervalset.go index 741f83f..5250123 100644 --- a/pkg/interval/intervalset.go +++ b/pkg/interval/intervalset.go @@ -6,24 +6,24 @@ import ( "sort" ) -// CanonicalIntervalSet is a canonical representation of a set of Interval objects -type CanonicalIntervalSet struct { +// CanonicalSet is a canonical representation of a set of Interval objects +type CanonicalSet struct { IntervalSet []Interval // sorted list of non-overlapping intervals } -func NewCanonicalIntervalSet() *CanonicalIntervalSet { - return &CanonicalIntervalSet{ +func NewCanonicalIntervalSet() *CanonicalSet { + return &CanonicalSet{ IntervalSet: []Interval{}, } } -// IsEmpty returns true if the CanonicalIntervalSet is empty -func (c *CanonicalIntervalSet) IsEmpty() bool { +// IsEmpty returns true if the CanonicalSet is empty +func (c *CanonicalSet) IsEmpty() bool { return len(c.IntervalSet) == 0 } -// Equal returns true if the CanonicalIntervalSet equals the input CanonicalIntervalSet -func (c *CanonicalIntervalSet) Equal(other CanonicalIntervalSet) bool { +// Equal returns true if the CanonicalSet equals the input CanonicalSet +func (c *CanonicalSet) Equal(other CanonicalSet) bool { if len(c.IntervalSet) != len(other.IntervalSet) { return false } @@ -36,7 +36,7 @@ func (c *CanonicalIntervalSet) Equal(other CanonicalIntervalSet) bool { } // AddInterval adds a new interval range to the set -func (c *CanonicalIntervalSet) AddInterval(v Interval) { +func (c *CanonicalSet) AddInterval(v Interval) { set := c.IntervalSet left := sort.Search(len(set), func(i int) bool { return set[i].End >= v.Start-1 @@ -53,8 +53,8 @@ func (c *CanonicalIntervalSet) AddInterval(v Interval) { c.IntervalSet = slices.Replace(c.IntervalSet, left, right, v) } -// AddHole updates the current CanonicalIntervalSet object by removing the input Interval from the set -func (c *CanonicalIntervalSet) AddHole(hole Interval) { +// AddHole updates the current CanonicalSet object by removing the input Interval from the set +func (c *CanonicalSet) AddHole(hole Interval) { newIntervalSet := []Interval{} for _, interval := range c.IntervalSet { newIntervalSet = append(newIntervalSet, interval.subtract(hole)...) @@ -66,8 +66,8 @@ func getNumAsStr(num int64) string { return fmt.Sprintf("%v", num) } -// String returns a string representation of the current CanonicalIntervalSet object -func (c *CanonicalIntervalSet) String() string { +// String returns a string representation of the current CanonicalSet object +func (c *CanonicalSet) String() string { if c.IsEmpty() { return "Empty" } @@ -82,25 +82,25 @@ func (c *CanonicalIntervalSet) String() string { return res[:len(res)-1] } -// Union updates the CanonicalIntervalSet object with the union result of the input CanonicalIntervalSet -func (c *CanonicalIntervalSet) Union(other CanonicalIntervalSet) { +// Union updates the CanonicalSet object with the union result of the input CanonicalSet +func (c *CanonicalSet) Union(other CanonicalSet) { for _, interval := range other.IntervalSet { c.AddInterval(interval) } } -// Copy returns a new copy of the CanonicalIntervalSet object -func (c *CanonicalIntervalSet) Copy() CanonicalIntervalSet { - return CanonicalIntervalSet{IntervalSet: append([]Interval(nil), c.IntervalSet...)} +// Copy returns a new copy of the CanonicalSet object +func (c *CanonicalSet) Copy() CanonicalSet { + return CanonicalSet{IntervalSet: append([]Interval(nil), c.IntervalSet...)} } -func (c *CanonicalIntervalSet) Contains(n int64) bool { +func (c *CanonicalSet) Contains(n int64) bool { i := FromInterval(n, n) return i.ContainedIn(*c) } -// ContainedIn returns true of the current CanonicalIntervalSet is contained in the input CanonicalIntervalSet -func (c *CanonicalIntervalSet) ContainedIn(other CanonicalIntervalSet) bool { +// ContainedIn returns true of the current CanonicalSet is contained in the input CanonicalSet +func (c *CanonicalSet) ContainedIn(other CanonicalSet) bool { larger := other.IntervalSet for _, target := range c.IntervalSet { left := sort.Search(len(larger), func(i int) bool { @@ -115,8 +115,8 @@ func (c *CanonicalIntervalSet) ContainedIn(other CanonicalIntervalSet) bool { return true } -// Intersect updates current CanonicalIntervalSet with intersection result of input CanonicalIntervalSet -func (c *CanonicalIntervalSet) Intersect(other CanonicalIntervalSet) { +// Intersect updates current CanonicalSet with intersection result of input CanonicalSet +func (c *CanonicalSet) Intersect(other CanonicalSet) { newIntervalSet := []Interval{} for _, interval := range c.IntervalSet { for _, otherInterval := range other.IntervalSet { @@ -126,8 +126,8 @@ func (c *CanonicalIntervalSet) Intersect(other CanonicalIntervalSet) { c.IntervalSet = newIntervalSet } -// Overlaps returns true if current CanonicalIntervalSet overlaps with input CanonicalIntervalSet -func (c *CanonicalIntervalSet) Overlaps(other *CanonicalIntervalSet) bool { +// 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 { if selfInterval.overlaps(otherInterval) { @@ -138,20 +138,20 @@ func (c *CanonicalIntervalSet) Overlaps(other *CanonicalIntervalSet) bool { return false } -// Subtraction updates current CanonicalIntervalSet with subtraction result of input CanonicalIntervalSet -func (c *CanonicalIntervalSet) Subtraction(other CanonicalIntervalSet) { +// Subtraction updates current CanonicalSet with subtraction result of input CanonicalSet +func (c *CanonicalSet) Subtraction(other CanonicalSet) { for _, i := range other.IntervalSet { c.AddHole(i) } } -func (c *CanonicalIntervalSet) IsSingleNumber() bool { +func (c *CanonicalSet) IsSingleNumber() bool { if len(c.IntervalSet) == 1 && c.IntervalSet[0].Start == c.IntervalSet[0].End { return true } return false } -func FromInterval(start, end int64) *CanonicalIntervalSet { - return &CanonicalIntervalSet{IntervalSet: []Interval{{Start: start, End: end}}} +func FromInterval(start, end int64) *CanonicalSet { + return &CanonicalSet{IntervalSet: []Interval{{Start: start, End: end}}} } diff --git a/pkg/ipblock/ipblock.go b/pkg/ipblock/ipblock.go index b8f979e..4968594 100644 --- a/pkg/ipblock/ipblock.go +++ b/pkg/ipblock/ipblock.go @@ -33,7 +33,7 @@ const ( // IPBlock captures a set of IP ranges type IPBlock struct { - ipRange interval.CanonicalIntervalSet + ipRange interval.CanonicalSet } // ToIPRanges returns a string of the ip ranges in the current IPBlock object @@ -211,7 +211,7 @@ func FromCidrOrAddress(s string) (*IPBlock, error) { // FromCidrList returns IPBlock object from multiple CIDRs given as list of strings func FromCidrList(cidrsList []string) (*IPBlock, error) { - res := &IPBlock{ipRange: interval.CanonicalIntervalSet{}} + res := &IPBlock{ipRange: interval.CanonicalSet{}} for _, cidr := range cidrsList { block, err := FromCidr(cidr) if err != nil { @@ -224,7 +224,7 @@ func FromCidrList(cidrsList []string) (*IPBlock, error) { // FromCidrExcept returns an IPBlock object from input cidr str an exceptions cidr str func FromCidrExcept(cidr string, exceptions []string) (*IPBlock, error) { - res := IPBlock{ipRange: interval.CanonicalIntervalSet{}} + res := IPBlock{ipRange: interval.CanonicalSet{}} span, err := cidrToInterval(cidr) if err != nil { return nil, err @@ -353,7 +353,7 @@ func IPBlockFromIPRangeStr(ipRangeStr string) (*IPBlock, error) { return nil, err } res := &IPBlock{} - res.ipRange = interval.CanonicalIntervalSet{IntervalSet: []interval.Interval{}} + res.ipRange = interval.CanonicalSet{IntervalSet: []interval.Interval{}} startIPNum := startIP.ipRange.IntervalSet[0].Start endIPNum := endIP.ipRange.IntervalSet[0].Start res.ipRange.IntervalSet = append(res.ipRange.IntervalSet, interval.Interval{Start: startIPNum, End: endIPNum})