Compare commits
8 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
3e69a488f6 | ||
|
e73c928dad | ||
|
7ab61f4312 | ||
|
658f7dc53f | ||
|
54e008c3e2 | ||
|
e7d0bf85e2 | ||
|
81987af8d7 | ||
|
59657e8ba8 |
3
.travis.yml
Normal file
3
.travis.yml
Normal file
@@ -0,0 +1,3 @@
|
||||
language: go
|
||||
go:
|
||||
- '1.10'
|
11
CHANGELOG.md
Normal file
11
CHANGELOG.md
Normal file
@@ -0,0 +1,11 @@
|
||||
# Changelog
|
||||
|
||||
## [0.1.1] - 2018-07-05
|
||||
|
||||
### Changed
|
||||
|
||||
- Renamed package name from 'interval' to 'intervals'
|
||||
|
||||
## [0.1.0] - 2018-07-03
|
||||
|
||||
- First release
|
13
README.md
13
README.md
@@ -1,4 +1,4 @@
|
||||
# intervals [](https://opensource.org/licenses/MIT) [](https://godoc.org/github.com/centraldereservas/intervals)
|
||||
# intervals [](https://travis-ci.org/centraldereservas/intervals) [](https://opensource.org/licenses/MIT) [](https://godoc.org/github.com/centraldereservas/intervals)
|
||||
|
||||
Provides a helper to work with integer intervals detecting gaps, merged and overlapped sections.
|
||||
|
||||
@@ -12,7 +12,7 @@ go get github.com/centraldereservas/intervals
|
||||
|
||||
Why we need to have control over a list of intervals?
|
||||
|
||||
Because provider API's have product prices depending on the age, so they assign a price amount to different age bands.
|
||||
Because the API providers have their product prices depending on the age, so they assign a price amount to different age bands.
|
||||
|
||||
As a result we have a bunch of age bands (with its corresponding price) which can have:
|
||||
|
||||
@@ -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)
|
||||
@@ -144,10 +144,13 @@ Output:
|
||||
go run ./example/main.go
|
||||
```
|
||||
|
||||
This example generates an output image file exposing the data analyzed:
|
||||
The following image is an example where `lowInclusive` and `highInclusive` are both `true`:
|
||||
|
||||

|
||||
|
||||
The next image is another example where `lowInclusive` is `true` but `highInclusive` is `false`:
|
||||
|
||||

|
||||
|
||||
## References
|
||||
|
||||
|
1182
demo_test.go
1182
demo_test.go
File diff suppressed because it is too large
Load Diff
@@ -1,4 +0,0 @@
|
||||
# Changelog
|
||||
|
||||
## [0.0.1] - 2018-06-28
|
||||
- First commit
|
@@ -6,7 +6,7 @@ import (
|
||||
"log"
|
||||
"os"
|
||||
|
||||
"bitbucket.org/differenttravel/interval"
|
||||
"github.com/centraldereservas/intervals"
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -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,14 +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)
|
||||
}
|
||||
}
|
||||
intervals.Sort()
|
||||
return intervals
|
||||
return intvls
|
||||
}
|
||||
|
BIN
example/out2.png
Normal file
BIN
example/out2.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 40 KiB |
36
find_test.go
36
find_test.go
@@ -1,13 +1,13 @@
|
||||
package interval_test
|
||||
package intervals_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"bitbucket.org/differenttravel/interval"
|
||||
"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},
|
||||
|
@@ -1,9 +1,9 @@
|
||||
package interval_test
|
||||
package intervals_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"bitbucket.org/differenttravel/interval"
|
||||
"github.com/centraldereservas/intervals"
|
||||
)
|
||||
|
||||
func TestGaps(t *testing.T) {
|
||||
@@ -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
2
get.go
@@ -1,4 +1,4 @@
|
||||
package interval
|
||||
package intervals
|
||||
|
||||
func (intvls *intervals) GetIntervals() []*Interval {
|
||||
// sort intervals (if necessary)
|
||||
|
@@ -1,4 +1,4 @@
|
||||
package interval
|
||||
package intervals
|
||||
|
||||
import "fmt"
|
||||
|
||||
|
@@ -1,4 +1,4 @@
|
||||
package interval
|
||||
package intervals
|
||||
|
||||
import "fmt"
|
||||
|
||||
|
@@ -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,
|
||||
|
@@ -1,9 +1,9 @@
|
||||
package interval_test
|
||||
package intervals_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"bitbucket.org/differenttravel/interval"
|
||||
"github.com/centraldereservas/intervals"
|
||||
)
|
||||
|
||||
func TestMerge(t *testing.T) {
|
||||
@@ -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},
|
||||
|
@@ -1,4 +1,4 @@
|
||||
package interval
|
||||
package intervals
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
@@ -1,9 +1,9 @@
|
||||
package interval_test
|
||||
package intervals_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"bitbucket.org/differenttravel/interval"
|
||||
"github.com/centraldereservas/intervals"
|
||||
)
|
||||
|
||||
func TestOverlapped(t *testing.T) {
|
||||
@@ -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},
|
||||
|
@@ -1,10 +1,10 @@
|
||||
package interval_test
|
||||
package intervals_test
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"bitbucket.org/differenttravel/interval"
|
||||
"github.com/centraldereservas/intervals"
|
||||
)
|
||||
|
||||
func TestReport(t *testing.T) {
|
||||
@@ -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},
|
||||
}
|
||||
|
Reference in New Issue
Block a user