renamed pkg from interval to intervals

This commit is contained in:
Daniel Gil 2018-07-05 09:04:38 +02:00
parent 7ab61f4312
commit e73c928dad
22 changed files with 651 additions and 651 deletions

View File

@ -46,7 +46,7 @@ API documentation is available on [godoc.org][doc].
To use this package the first thing we have to do is create an instance:
```go
intvls := interval.NewIntervals(minLow, maxHigh, lowInclusive, highInclusive, selfAdjustMinLow, selfAdjustMaxHigh)
intvls := intervals.New(minLow, maxHigh, lowInclusive, highInclusive, selfAdjustMinLow, selfAdjustMaxHigh)
```
where `minLow` and `maxHigh` are integers that defines the accepted integer space to work with, anything else outside will be rejected. The booleans `lowInclusive` and `highInclusive` indicates if the values of a range (low, high) are inclusive or not. The booleans `selfAdjustMinLow`and `selfAdjustMaxHigh` indicates that we do not need to specify the minLow or maxHigh in the constructor, but those values will be self adjusted automatically taking the lower and greatest value respectively of all intervals added.
@ -63,7 +63,7 @@ Once initialized, we can proced to add new intervals.
We can add intervals by passing an `Interval` object:
```go
intvl := &interval.Interval{Low: ageFrom, High: ageTo, Object: myObject}
intvl := &intervals.Interval{Low: ageFrom, High: ageTo, Object: myObject}
err := intvls.AddInterval(intvl)
if err != nil {
fmt.Printf("invalid interval discarded: %v\n", err)

2
add.go
View File

@ -1,4 +1,4 @@
package interval
package intervals
import "math"

File diff suppressed because it is too large Load Diff

View File

@ -20,9 +20,9 @@ func main() {
if err != nil {
log.Fatalf("could not read %s: %v", filename, err)
}
intervals := initIntervals(xys)
ip := interval.NewPlot(intervals.IsLowInclusive(), intervals.IsHighInclusive())
err = ip.PlotData("out.png", intervals, true, true, true, true)
intvls := initIntervals(xys)
ip := intervals.NewPlot(intvls.IsLowInclusive(), intvls.IsHighInclusive())
err = ip.PlotData("out.png", intvls, true, true, true, true)
if err != nil {
log.Fatalf("could not plot data: %v", err)
}
@ -59,7 +59,7 @@ func readData(path string) ([]xy, error) {
return xys, nil
}
func initIntervals(xys []xy) interval.Intervals {
func initIntervals(xys []xy) intervals.Intervals {
// initialize Intervals
minLow := MinX
maxHigh := MaxX
@ -67,13 +67,13 @@ func initIntervals(xys []xy) interval.Intervals {
highInclusive := true
selfAdjustMinLow := false
selfAdjustMaxHigh := true
intervals := interval.NewIntervals(minLow, maxHigh, lowInclusive, highInclusive, selfAdjustMinLow, selfAdjustMaxHigh)
intvls := intervals.New(minLow, maxHigh, lowInclusive, highInclusive, selfAdjustMinLow, selfAdjustMaxHigh)
for _, xy := range xys {
err := intervals.AddInterval(&interval.Interval{Low: xy.x, High: xy.y})
err := intvls.AddInterval(&intervals.Interval{Low: xy.x, High: xy.y})
if err != nil {
fmt.Printf("invalid interval discarded: %v\n", err)
}
}
return intervals
return intvls
}

BIN
example/out2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 40 KiB

View File

@ -1,4 +1,4 @@
package interval
package intervals
import "fmt"

View File

@ -1,4 +1,4 @@
package interval_test
package intervals_test
import (
"fmt"
@ -7,7 +7,7 @@ import (
"github.com/centraldereservas/intervals"
)
func initIntervalsForDemo001() interval.Intervals {
func initIntervalsForDemo001() intervals.Intervals {
// initialize Intervals
minLow := 0
maxHigh := 100
@ -15,28 +15,28 @@ func initIntervalsForDemo001() interval.Intervals {
highInclusive := true
selfAdjustMinLow := false
selfAdjustMaxHigh := true
itvls := interval.NewIntervals(minLow, maxHigh, lowInclusive, highInclusive, selfAdjustMinLow, selfAdjustMaxHigh)
itvls := intervals.New(minLow, maxHigh, lowInclusive, highInclusive, selfAdjustMinLow, selfAdjustMaxHigh)
// add new intervals
if err := itvls.AddInterval(&interval.Interval{Low: 5, High: 7}); err != nil {
if err := itvls.AddInterval(&intervals.Interval{Low: 5, High: 7}); err != nil {
fmt.Printf("invalid interval discarded: %v\n", err)
}
if err := itvls.AddInterval(&interval.Interval{Low: 2, High: 4}); err != nil {
if err := itvls.AddInterval(&intervals.Interval{Low: 2, High: 4}); err != nil {
fmt.Printf("invalid interval discarded: %v\n", err)
}
if err := itvls.AddInterval(&interval.Interval{Low: 3, High: 6}); err != nil {
if err := itvls.AddInterval(&intervals.Interval{Low: 3, High: 6}); err != nil {
fmt.Printf("invalid interval discarded: %v\n", err)
}
if err := itvls.AddInterval(&interval.Interval{Low: 18, High: 20}); err != nil {
if err := itvls.AddInterval(&intervals.Interval{Low: 18, High: 20}); err != nil {
fmt.Printf("invalid interval discarded: %v\n", err)
}
if err := itvls.AddInterval(&interval.Interval{Low: 20, High: 30}); err != nil {
if err := itvls.AddInterval(&intervals.Interval{Low: 20, High: 30}); err != nil {
fmt.Printf("invalid interval discarded: %v\n", err)
}
if err := itvls.AddInterval(&interval.Interval{Low: 25, High: 28}); err != nil {
if err := itvls.AddInterval(&intervals.Interval{Low: 25, High: 28}); err != nil {
fmt.Printf("invalid interval discarded: %v\n", err)
}
if err := itvls.AddInterval(&interval.Interval{Low: 30, High: 32}); err != nil {
if err := itvls.AddInterval(&intervals.Interval{Low: 30, High: 32}); err != nil {
fmt.Printf("invalid interval discarded: %v\n", err)
}
return itvls
@ -45,17 +45,17 @@ func initIntervalsForDemo001() interval.Intervals {
// matches for value=2
func buildFindDemo001() demo {
itvls := initIntervalsForDemo001()
matches := []interval.Interval{}
matches = append(matches, interval.Interval{Low: 2, High: 4})
matches := []intervals.Interval{}
matches = append(matches, intervals.Interval{Low: 2, High: 4})
return demo{Intervals: itvls, ExpectedFindMatches: matches, ValueToFind: 2}
}
// matches for value=4
func buildFindDemo002() demo {
itvls := initIntervalsForDemo001()
matches := []interval.Interval{}
matches = append(matches, interval.Interval{Low: 2, High: 4})
matches = append(matches, interval.Interval{Low: 3, High: 6})
matches := []intervals.Interval{}
matches = append(matches, intervals.Interval{Low: 2, High: 4})
matches = append(matches, intervals.Interval{Low: 3, High: 6})
return demo{Intervals: itvls, ExpectedFindMatches: matches, ValueToFind: 4}
}
@ -66,8 +66,8 @@ func TestFindIntervalsForValue(t *testing.T) {
tt := []struct {
name string
valueToFind int
intvls interval.Intervals
expectedMatches []interval.Interval
intvls intervals.Intervals
expectedMatches []intervals.Interval
}{
{name: "demo001", valueToFind: demo001.ValueToFind, intvls: demo001.Intervals, expectedMatches: demo001.ExpectedFindMatches},
{name: "demo002", valueToFind: demo002.ValueToFind, intvls: demo002.Intervals, expectedMatches: demo002.ExpectedFindMatches},

View File

@ -1,4 +1,4 @@
package interval
package intervals
import "fmt"

View File

@ -1,4 +1,4 @@
package interval_test
package intervals_test
import (
"testing"
@ -67,8 +67,8 @@ func TestGaps(t *testing.T) {
demo312 := buildIntervalsDemo312()
tt := []struct {
name string
intvls interval.Intervals
expectedGaps []interval.Interval
intvls intervals.Intervals
expectedGaps []intervals.Interval
}{
{name: "demo001", intvls: demo001.Intervals, expectedGaps: demo001.ExpectedGaps},
{name: "demo002", intvls: demo002.Intervals, expectedGaps: demo002.ExpectedGaps},

2
get.go
View File

@ -1,4 +1,4 @@
package interval
package intervals
func (intvls *intervals) GetIntervals() []*Interval {
// sort intervals (if necessary)

View File

@ -1,4 +1,4 @@
package interval
package intervals
import "fmt"

View File

@ -1,4 +1,4 @@
package interval
package intervals
import "fmt"

View File

@ -1,4 +1,4 @@
package interval
package intervals
import (
"math"
@ -77,11 +77,11 @@ func (itvls *intervals) String() string {
// NewIntervalsDefault is a constructor that returns an instance of the Intervals interface with default values
func NewIntervalsDefault() Intervals {
return NewIntervals(defaultMinLow, defaultMaxHigh, defaultLowInclusive, defaultHighInclusive, defaultSelfAdjustMinLow, defaultSelfAdjustMaxHigh)
return New(defaultMinLow, defaultMaxHigh, defaultLowInclusive, defaultHighInclusive, defaultSelfAdjustMinLow, defaultSelfAdjustMaxHigh)
}
// NewIntervals is a constructor that returns an instance of the Intervals interface
func NewIntervals(minLow, maxHigh int, lowInclusive, highInclusive, selfAdjustMinLow, selfAdjustMaxHigh bool) Intervals {
// New is a constructor that returns an instance of the Intervals interface
func New(minLow, maxHigh int, lowInclusive, highInclusive, selfAdjustMinLow, selfAdjustMaxHigh bool) Intervals {
return &intervals{
MinLow: minLow,
MaxHigh: maxHigh,

View File

@ -1,4 +1,4 @@
package interval
package intervals
import "fmt"

View File

@ -1,4 +1,4 @@
package interval_test
package intervals_test
import (
"testing"
@ -67,8 +67,8 @@ func TestMerge(t *testing.T) {
demo312 := buildIntervalsDemo312()
tt := []struct {
name string
intvls interval.Intervals
expectedMerges []interval.Interval
intvls intervals.Intervals
expectedMerges []intervals.Interval
}{
{name: "demo001", intvls: demo001.Intervals, expectedMerges: demo001.ExpectedMerges},
{name: "demo002", intvls: demo002.Intervals, expectedMerges: demo002.ExpectedMerges},

View File

@ -1,4 +1,4 @@
package interval
package intervals
import (
"fmt"

View File

@ -1,4 +1,4 @@
package interval_test
package intervals_test
import (
"testing"
@ -67,8 +67,8 @@ func TestOverlapped(t *testing.T) {
demo312 := buildIntervalsDemo312()
tt := []struct {
name string
intvls interval.Intervals
expectedOverlaps []interval.Interval
intvls intervals.Intervals
expectedOverlaps []intervals.Interval
}{
{name: "demo001", intvls: demo001.Intervals, expectedOverlaps: demo001.ExpectedOverlaps},
{name: "demo002", intvls: demo002.Intervals, expectedOverlaps: demo002.ExpectedOverlaps},

View File

@ -1,4 +1,4 @@
package interval
package intervals
import (
"fmt"

View File

@ -1,4 +1,4 @@
package interval
package intervals
import "fmt"

View File

@ -1,4 +1,4 @@
package interval_test
package intervals_test
import (
"fmt"
@ -14,41 +14,41 @@ func TestReport(t *testing.T) {
highInclusive := true
selfAdjustMinLow := false
selfAdjustMaxHigh := true
itvls := interval.NewIntervals(minLow, maxHigh, lowInclusive, highInclusive, selfAdjustMinLow, selfAdjustMaxHigh)
itvls := intervals.New(minLow, maxHigh, lowInclusive, highInclusive, selfAdjustMinLow, selfAdjustMaxHigh)
var err error
err = itvls.AddInterval(&interval.Interval{Low: 5, High: 7})
err = itvls.AddInterval(&intervals.Interval{Low: 5, High: 7})
if err != nil {
fmt.Printf("invalid interval discarded: %v\n", err)
}
err = itvls.AddInterval(&interval.Interval{Low: 2, High: 4})
err = itvls.AddInterval(&intervals.Interval{Low: 2, High: 4})
if err != nil {
fmt.Printf("invalid interval discarded: %v\n", err)
}
err = itvls.AddInterval(&interval.Interval{Low: 3, High: 6})
err = itvls.AddInterval(&intervals.Interval{Low: 3, High: 6})
if err != nil {
fmt.Printf("invalid interval discarded: %v\n", err)
}
err = itvls.AddInterval(&interval.Interval{Low: 18, High: 20})
err = itvls.AddInterval(&intervals.Interval{Low: 18, High: 20})
if err != nil {
fmt.Printf("invalid interval discarded: %v\n", err)
}
err = itvls.AddInterval(&interval.Interval{Low: 20, High: 30})
err = itvls.AddInterval(&intervals.Interval{Low: 20, High: 30})
if err != nil {
fmt.Printf("invalid interval discarded: %v\n", err)
}
err = itvls.AddInterval(&interval.Interval{Low: 25, High: 28})
err = itvls.AddInterval(&intervals.Interval{Low: 25, High: 28})
if err != nil {
fmt.Printf("invalid interval discarded: %v\n", err)
}
err = itvls.AddInterval(&interval.Interval{Low: 30, High: 32})
err = itvls.AddInterval(&intervals.Interval{Low: 30, High: 32})
if err != nil {
fmt.Printf("invalid interval discarded: %v\n", err)
}
tt := []struct {
name string
itvls interval.Intervals
itvls intervals.Intervals
}{
{name: "normal case", itvls: itvls},
}

View File

@ -1,4 +1,4 @@
package interval
package intervals
import "sort"

View File

@ -1,4 +1,4 @@
package interval
package intervals
func min(a, b int) int {
if a < b {