@@ -7,15 +7,12 @@ import (
77 "time"
88)
99
10- // Interval defines a time period that is constained by two time boundaries, a
11- // start time that is part of the interval and an end time which is excluded
12- // from the interval.
10+ // Interval defines a period that is constained by two time boundaries, a start
11+ // time that is part of the interval and an end time which is excluded from the
12+ // interval.
1313//
1414// |----------i----------[
1515// start end
16- //
17- // i.Include(start) == true
18- // i.Include(end) == false
1916type Interval struct {
2017 Start time.Time
2118 End time.Time
@@ -42,27 +39,28 @@ func (i Interval) Duration() time.Duration {
4239}
4340
4441// Include tests if input time is within the interval. Note that if input is
42+ //
4543// equal to the end of the interval, then false is returned.
4644//
47- // interval: |------------i------------[
48- // input: |
45+ // interval: |------------i------------[
46+ // input: |
4947func (i Interval ) Include (input time.Time ) bool {
5048 return (i .Start .Before (input ) || i .Start .Equal (input )) && i .End .After (input )
5149}
5250
5351// Equal tests that the input interval has the time time boundaries as Interval.
5452//
55- // interval: |------------i------------[
56- // input: |----------input----------[
53+ // interval: |------------i------------[
54+ // input: |----------input----------[
5755func (i Interval ) Equal (input Interval ) bool {
5856 return i .Start .Equal (input .Start ) && i .End .Equal (input .End )
5957}
6058
6159// Engulf tests that the input interval is within Interval. Returns true also if
6260// both intervals are equal.
6361//
64- // interval: |------------i------------[
65- // input: |---input---[
62+ // interval: |------------i------------[
63+ // input: |---input---[
6664func (i Interval ) Engulf (input Interval ) bool {
6765 return (i .Start .Before (input .Start ) || i .Start .Equal (input .Start )) &&
6866 (i .End .After (input .End ) || i .End .Equal (input .End ))
@@ -71,32 +69,32 @@ func (i Interval) Engulf(input Interval) bool {
7169// Overlap tests if input overlaps with Interval. Sharing opposite time
7270// boundaries is not enough to overlap.
7371//
74- // interval: |------------i------------[
75- // input: |----input---[
76- // input: |----input----[
77- // input: |---input---[
78- // input: |-------------input-------------[
72+ // interval: |------------i------------[
73+ // input: |----input---[
74+ // input: |----input----[
75+ // input: |---input---[
76+ // input: |-------------input-------------[
7977func (i Interval ) Overlap (input Interval ) bool {
8078 return ! ((input .End .Before (i .Start ) || input .End .Equal (i .Start )) ||
8179 (input .Start .After (i .End ) || input .Start .Equal (i .End )))
8280}
8381
8482// Contiguous tests if input is contiguous to Interval.
8583//
86- // interval: |----------i----------[
87- // input: |--input--[
88- // input: |--input--[
84+ // interval: |----------i----------[
85+ // input: |--input--[
86+ // input: |--input--[
8987func (i Interval ) Contiguous (input Interval ) bool {
9088 return i .End .Equal (input .Start ) || i .Start .Equal (input .End )
9189}
9290
9391// Sub substracts input to Interval.
9492//
95- // interval: |------------i------------[
96- // input: |------input------[
97- // output: |----i'----[
98- // input: |--input--[
99- // output: |--i'---[ |--i"---[
93+ // interval: |------------i------------[
94+ // input: |------input------[
95+ // output: |----i'----[
96+ // input: |--input--[
97+ // output: |--i'---[ |--i"---[
10098func (i Interval ) Sub (input Interval ) Intervals {
10199 if input .Equal (i ) || input .Engulf (i ) {
102100 return Intervals {}
@@ -137,8 +135,10 @@ func (i Interval) Sub(input Interval) Intervals {
137135 }
138136}
139137
138+ // Intervals is a slice of Interval.
140139type Intervals []Interval
141140
141+ // String returns a string representation of Intervals.
142142func (is Intervals ) String () string {
143143 strs := make ([]string , 0 , len (is ))
144144 for _ , s := range is {
@@ -148,6 +148,8 @@ func (is Intervals) String() string {
148148 return fmt .Sprintf ("[%s]" , strings .Join (strs , ", " ))
149149}
150150
151+ // Equal tests that the input Intervals contains the same intervals as
152+ // Intervals in no particular order.
151153func (is Intervals ) Equal (input Intervals ) bool {
152154 if len (is ) != len (input ) {
153155 return false
@@ -181,6 +183,7 @@ func (is Intervals) Equal(input Intervals) bool {
181183 return true
182184}
183185
186+ // Swap swaps the intervals with indexes i and j.
184187func (is Intervals ) Swap (i , j int ) {
185188 is [i ], is [j ] = is [j ], is [i ]
186189}
0 commit comments