Skip to content

Commit

Permalink
consistent use of pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
Elazar Gershuni committed Mar 12, 2024
1 parent 414c1bf commit a7c1205
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 85 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
REPOSITORY := github.com/np-guard/common
REPOSITORY := github.com/np-guard/models

mod: go.mod
@echo -- $@ --
Expand Down
55 changes: 23 additions & 32 deletions pkg/hypercube/hypercubeset.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,18 @@ func (c *CanonicalSet) Union(other *CanonicalSet) *CanonicalSet {
res := NewCanonicalSet(c.dimensions)
remainingFromOther := map[*interval.CanonicalSet]*interval.CanonicalSet{}
for k := range other.layers {
kCopy := k.Copy()
remainingFromOther[k] = &kCopy
remainingFromOther[k] = k.Copy()
}
for k, v := range c.layers {
remainingFromSelf := copyIntervalSet(k)
remainingFromSelf := k.Copy()
for otherKey, otherVal := range other.layers {
commonElem := copyIntervalSet(k)
commonElem.Intersect(*otherKey)
commonElem := k.Copy()
commonElem.Intersect(otherKey)
if commonElem.IsEmpty() {
continue
}
remainingFromOther[otherKey].Subtract(*commonElem)
remainingFromSelf.Subtract(*commonElem)
remainingFromOther[otherKey].Subtract(commonElem)
remainingFromSelf.Subtract(commonElem)
if c.dimensions == 1 {
res.layers[commonElem] = NewCanonicalSet(0)
continue
Expand Down Expand Up @@ -102,8 +101,8 @@ func (c *CanonicalSet) Intersect(other *CanonicalSet) *CanonicalSet {
res := NewCanonicalSet(c.dimensions)
for k, v := range c.layers {
for otherKey, otherVal := range other.layers {
commonELem := copyIntervalSet(k)
commonELem.Intersect(*otherKey)
commonELem := k.Copy()
commonELem.Intersect(otherKey)
if commonELem.IsEmpty() {
continue
}
Expand All @@ -128,14 +127,14 @@ func (c *CanonicalSet) Subtract(other *CanonicalSet) *CanonicalSet {
}
res := NewCanonicalSet(c.dimensions)
for k, v := range c.layers {
remainingFromSelf := copyIntervalSet(k)
remainingFromSelf := k.Copy()
for otherKey, otherVal := range other.layers {
commonELem := copyIntervalSet(k)
commonELem.Intersect(*otherKey)
commonELem := k.Copy()
commonELem.Intersect(otherKey)
if commonELem.IsEmpty() {
continue
}
remainingFromSelf.Subtract(*commonELem)
remainingFromSelf.Subtract(commonELem)
if c.dimensions == 1 {
continue
}
Expand All @@ -155,7 +154,7 @@ func (c *CanonicalSet) Subtract(other *CanonicalSet) *CanonicalSet {
func (c *CanonicalSet) getIntervalSetUnion() *interval.CanonicalSet {
res := interval.NewCanonicalIntervalSet()
for k := range c.layers {
res.Union(*k)
res.Union(k)
}
return res
}
Expand All @@ -171,17 +170,17 @@ func (c *CanonicalSet) ContainedIn(other *CanonicalSet) (bool, error) {
}
cInterval := c.getIntervalSetUnion()
otherInterval := other.getIntervalSetUnion()
return cInterval.ContainedIn(*otherInterval), nil
return cInterval.ContainedIn(otherInterval), nil
}

isSubsetCount := 0
for k, v := range c.layers {
currentLayer := copyIntervalSet(k)
currentLayer := k.Copy()
for otherKey, otherVal := range other.layers {
commonKey := copyIntervalSet(currentLayer)
commonKey.Intersect(*otherKey)
remaining := copyIntervalSet(currentLayer)
remaining.Subtract(*commonKey)
commonKey := currentLayer.Copy()
commonKey.Intersect(otherKey)
remaining := currentLayer.Copy()
remaining.Subtract(commonKey)
if !commonKey.IsEmpty() {
subContainment, err := v.ContainedIn(otherVal)
if !subContainment || err != nil {
Expand All @@ -203,8 +202,7 @@ func (c *CanonicalSet) ContainedIn(other *CanonicalSet) (bool, error) {
func (c *CanonicalSet) Copy() *CanonicalSet {
res := NewCanonicalSet(c.dimensions)
for k, v := range c.layers {
newKey := k.Copy()
res.layers[&newKey] = v.Copy()
res.layers[k.Copy()] = v.Copy()
}
return res
}
Expand Down Expand Up @@ -266,7 +264,7 @@ func (c *CanonicalSet) applyElementsUnionPerLayer() {
newVal := p.hc
newKey := p.is[0]
for i := 1; i < len(p.is); i += 1 {
newKey.Union(*p.is[i])
newKey.Union(p.is[i])
}
newLayers[newKey] = newVal
}
Expand All @@ -281,13 +279,11 @@ func FromCube(cube []*interval.CanonicalSet) *CanonicalSet {
}
if len(cube) == 1 {
res := NewCanonicalSet(1)
cubeVal := cube[0].Copy()
res.layers[&cubeVal] = NewCanonicalSet(0)
res.layers[cube[0].Copy()] = NewCanonicalSet(0)
return res
}
res := NewCanonicalSet(len(cube))
cubeVal := cube[0].Copy()
res.layers[&cubeVal] = FromCube(cube[1:])
res.layers[cube[0].Copy()] = FromCube(cube[1:])
return res
}

Expand All @@ -301,8 +297,3 @@ func FromCubeShort(values ...int64) *CanonicalSet {
}
return FromCube(cube)
}

func copyIntervalSet(a *interval.CanonicalSet) *interval.CanonicalSet {
res := a.Copy()
return &res
}
14 changes: 7 additions & 7 deletions pkg/interval/interval.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,27 +9,27 @@ type Interval struct {
}

// String returns a String representation of Interval object
func (i *Interval) String() string {
func (i Interval) String() string {
return fmt.Sprintf("[%v-%v]", i.Start, i.End)
}

// Equal returns true if current Interval obj is equal to the input Interval
func (i *Interval) Equal(x Interval) bool {
func (i Interval) Equal(x Interval) bool {
return i.Start == x.Start && i.End == x.End
}

func (i *Interval) overlaps(other Interval) bool {
func (i Interval) overlaps(other Interval) bool {
return other.End >= i.Start && other.Start <= i.End
}

func (i *Interval) isSubset(other Interval) bool {
func (i Interval) isSubset(other Interval) bool {
return other.Start <= i.Start && other.End >= i.End
}

// returns a list with up to 2 intervals
func (i *Interval) subtract(other Interval) []Interval {
func (i Interval) subtract(other Interval) []Interval {
if !i.overlaps(other) {
return []Interval{*i}
return []Interval{i}
}
if i.isSubset(other) {
return []Interval{}
Expand All @@ -44,7 +44,7 @@ func (i *Interval) subtract(other Interval) []Interval {
return []Interval{{Start: max(i.Start, other.End+1), End: i.End}}
}

func (i *Interval) intersection(other Interval) []Interval {
func (i Interval) intersection(other Interval) []Interval {
maxStart := max(i.Start, other.Start)
minEnd := min(i.End, other.End)
if minEnd < maxStart {
Expand Down
16 changes: 8 additions & 8 deletions pkg/interval/intervalset.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func (c *CanonicalSet) IsEmpty() bool {
}

// Equal returns true if the CanonicalSet equals the input CanonicalSet
func (c *CanonicalSet) Equal(other CanonicalSet) bool {
func (c *CanonicalSet) Equal(other *CanonicalSet) bool {
if len(c.IntervalSet) != len(other.IntervalSet) {
return false
}
Expand Down Expand Up @@ -83,24 +83,24 @@ func (c *CanonicalSet) String() string {
}

// Union updates the CanonicalSet object with the union result of the input CanonicalSet
func (c *CanonicalSet) Union(other CanonicalSet) {
func (c *CanonicalSet) Union(other *CanonicalSet) {
for _, interval := range other.IntervalSet {
c.AddInterval(interval)
}
}

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

func (c *CanonicalSet) Contains(n int64) bool {
i := FromInterval(n, n)
return i.ContainedIn(*c)
return i.ContainedIn(c)
}

// ContainedIn returns true of the current CanonicalSet is contained in the input CanonicalSet
func (c *CanonicalSet) ContainedIn(other CanonicalSet) bool {
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 @@ -116,7 +116,7 @@ func (c *CanonicalSet) ContainedIn(other CanonicalSet) bool {
}

// Intersect updates current CanonicalSet with intersection result of input CanonicalSet
func (c *CanonicalSet) Intersect(other CanonicalSet) {
func (c *CanonicalSet) Intersect(other *CanonicalSet) {
newIntervalSet := []Interval{}
for _, interval := range c.IntervalSet {
for _, otherInterval := range other.IntervalSet {
Expand All @@ -139,7 +139,7 @@ func (c *CanonicalSet) Overlaps(other *CanonicalSet) bool {
}

// Subtract updates current CanonicalSet with subtraction result of input CanonicalSet
func (c *CanonicalSet) Subtract(other CanonicalSet) {
func (c *CanonicalSet) Subtract(other *CanonicalSet) {
for _, i := range other.IntervalSet {
c.AddHole(i)
}
Expand Down
26 changes: 13 additions & 13 deletions pkg/interval/intervalset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,29 +28,29 @@ func TestIntervalSet(t *testing.T) {
is2.AddInterval(interval.Interval{6, 8})
require.Equal(t, "6-8", is2.String())
require.False(t, is2.IsSingleNumber())
require.False(t, is2.ContainedIn(*is1))
require.False(t, is1.ContainedIn(*is2))
require.False(t, is2.Equal(*is1))
require.False(t, is1.Equal(*is2))
require.False(t, is2.ContainedIn(is1))
require.False(t, is1.ContainedIn(is2))
require.False(t, is2.Equal(is1))
require.False(t, is1.Equal(is2))
require.True(t, is1.Overlaps(is2))
require.True(t, is2.Overlaps(is1))

is1.Subtract(*is2)
require.False(t, is2.ContainedIn(*is1))
require.False(t, is1.ContainedIn(*is2))
is1.Subtract(is2)
require.False(t, is2.ContainedIn(is1))
require.False(t, is1.ContainedIn(is2))
require.False(t, is1.Overlaps(is2))
require.False(t, is2.Overlaps(is1))

is1.Union(*is2)
is1.Union(*interval.FromInterval(7, 9))
require.True(t, is2.ContainedIn(*is1))
require.False(t, is1.ContainedIn(*is2))
is1.Union(is2)
is1.Union(interval.FromInterval(7, 9))
require.True(t, is2.ContainedIn(is1))
require.False(t, is1.ContainedIn(is2))
require.True(t, is1.Overlaps(is2))
require.True(t, is2.Overlaps(is1))

is3 := is1.Copy()
is3.Intersect(*is2)
require.True(t, is3.Equal(*is2))
is3.Intersect(is2)
require.True(t, is3.Equal(is2))
require.True(t, is2.ContainedIn(is3))

require.True(t, interval.FromInterval(1, 1).IsSingleNumber())
Expand Down
Loading

0 comments on commit a7c1205

Please sign in to comment.