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: To use this package the first thing we have to do is create an instance:
```go ```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. 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: We can add intervals by passing an `Interval` object:
```go ```go
intvl := &interval.Interval{Low: ageFrom, High: ageTo, Object: myObject} intvl := &intervals.Interval{Low: ageFrom, High: ageTo, Object: myObject}
err := intvls.AddInterval(intvl) err := intvls.AddInterval(intvl)
if err != nil { if err != nil {
fmt.Printf("invalid interval discarded: %v\n", err) fmt.Printf("invalid interval discarded: %v\n", err)

2
add.go
View File

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

File diff suppressed because it is too large Load Diff

View File

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

View File

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

View File

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

View File

@ -1,4 +1,4 @@
package interval_test package intervals_test
import ( import (
"testing" "testing"
@ -67,8 +67,8 @@ func TestGaps(t *testing.T) {
demo312 := buildIntervalsDemo312() demo312 := buildIntervalsDemo312()
tt := []struct { tt := []struct {
name string name string
intvls interval.Intervals intvls intervals.Intervals
expectedGaps []interval.Interval expectedGaps []intervals.Interval
}{ }{
{name: "demo001", intvls: demo001.Intervals, expectedGaps: demo001.ExpectedGaps}, {name: "demo001", intvls: demo001.Intervals, expectedGaps: demo001.ExpectedGaps},
{name: "demo002", intvls: demo002.Intervals, expectedGaps: demo002.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 { func (intvls *intervals) GetIntervals() []*Interval {
// sort intervals (if necessary) // sort intervals (if necessary)

View File

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

View File

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

View File

@ -1,4 +1,4 @@
package interval package intervals
import ( import (
"math" "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 // NewIntervalsDefault is a constructor that returns an instance of the Intervals interface with default values
func NewIntervalsDefault() Intervals { 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 // New is a constructor that returns an instance of the Intervals interface
func NewIntervals(minLow, maxHigh int, lowInclusive, highInclusive, selfAdjustMinLow, selfAdjustMaxHigh bool) Intervals { func New(minLow, maxHigh int, lowInclusive, highInclusive, selfAdjustMinLow, selfAdjustMaxHigh bool) Intervals {
return &intervals{ return &intervals{
MinLow: minLow, MinLow: minLow,
MaxHigh: maxHigh, MaxHigh: maxHigh,

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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