Skip to content

Commit

Permalink
rename CanonicalIntervalSet too
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 12, 2024
1 parent 409b0f2 commit 1d1ae54
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 67 deletions.
38 changes: 19 additions & 19 deletions pkg/hypercube/hypercubeset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()+")")
Expand All @@ -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)
}
Expand All @@ -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]
Expand All @@ -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
}
Expand All @@ -291,22 +291,22 @@ func FromCube(cube []*interval.CanonicalIntervalSet) *CanonicalSet {
return res
}

func FromCubeAsIntervals(values ...*interval.CanonicalIntervalSet) *CanonicalSet {
func FromCubeAsIntervals(values ...*interval.CanonicalSet) *CanonicalSet {
return FromCube(values)
}

// FromCubeShort returns a new CanonicalSet created from a single input cube
// 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
}
28 changes: 14 additions & 14 deletions pkg/hypercube/hypercubeset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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) {
Expand All @@ -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())
Expand All @@ -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)
Expand All @@ -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)}
Expand Down
60 changes: 30 additions & 30 deletions pkg/interval/intervalset.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
Expand All @@ -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)...)
Expand All @@ -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"
}
Expand All @@ -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 {
Expand All @@ -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 {
Expand All @@ -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) {
Expand All @@ -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}}}
}
8 changes: 4 additions & 4 deletions pkg/ipblock/ipblock.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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})
Expand Down

0 comments on commit 1d1ae54

Please sign in to comment.