github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/x/time/ranges.go (about)

     1  // Copyright (c) 2016 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package time
    22  
    23  import (
    24  	"bytes"
    25  	"container/list"
    26  )
    27  
    28  // Ranges is a collection of time ranges.
    29  type Ranges interface {
    30  	AddRange(Range)
    31  	AddRanges(Ranges)
    32  	RemoveRange(Range)
    33  	RemoveRanges(Ranges)
    34  	Overlaps(Range) bool
    35  	Iter() *RangeIter
    36  	Clone() Ranges
    37  	Len() int
    38  	IsEmpty() bool
    39  	String() string
    40  }
    41  
    42  type ranges struct {
    43  	sortedRanges *list.List
    44  }
    45  
    46  // NewRanges constructs a new Ranges object comprising the provided ranges.
    47  func NewRanges(in ...Range) Ranges {
    48  	res := &ranges{sortedRanges: list.New()}
    49  	for _, r := range in {
    50  		res.AddRange(r)
    51  	}
    52  	return res
    53  }
    54  
    55  // Len returns the number of ranges included.
    56  func (tr *ranges) Len() int {
    57  	return tr.sortedRanges.Len()
    58  }
    59  
    60  // IsEmpty returns true if the list of time ranges is empty.
    61  func (tr *ranges) IsEmpty() bool {
    62  	return tr.Len() == 0
    63  }
    64  
    65  // Overlaps checks if the range overlaps with any of the ranges in the collection.
    66  func (tr *ranges) Overlaps(r Range) bool {
    67  	if r.IsEmpty() {
    68  		return false
    69  	}
    70  	e := tr.findFirstNotBefore(r)
    71  	if e == nil {
    72  		return false
    73  	}
    74  	lr := e.Value.(Range)
    75  	return lr.Overlaps(r)
    76  }
    77  
    78  // AddRange adds the time range to the collection of ranges.
    79  func (tr *ranges) AddRange(r Range) {
    80  	tr.addRangeInPlace(r)
    81  }
    82  
    83  // AddRanges adds the time ranges.
    84  func (tr *ranges) AddRanges(other Ranges) {
    85  	it := other.Iter()
    86  	for it.Next() {
    87  		tr.addRangeInPlace(it.Value())
    88  	}
    89  }
    90  
    91  // RemoveRange removes the time range from the collection of ranges.
    92  func (tr *ranges) RemoveRange(r Range) {
    93  	tr.removeRangeInPlace(r)
    94  }
    95  
    96  // RemoveRanges removes the given time ranges from the current one.
    97  func (tr *ranges) RemoveRanges(other Ranges) {
    98  	it := other.Iter()
    99  	for it.Next() {
   100  		tr.removeRangeInPlace(it.Value())
   101  	}
   102  }
   103  
   104  // Iter returns an iterator that iterates over the time ranges included.
   105  func (tr *ranges) Iter() *RangeIter {
   106  	return newRangeIter(tr.sortedRanges)
   107  }
   108  
   109  // Clone makes a clone of the time ranges.
   110  func (tr *ranges) Clone() Ranges {
   111  	res := &ranges{sortedRanges: list.New()}
   112  	for e := tr.sortedRanges.Front(); e != nil; e = e.Next() {
   113  		res.sortedRanges.PushBack(e.Value.(Range))
   114  	}
   115  	return res
   116  }
   117  
   118  // String returns the string representation of the range.
   119  func (tr *ranges) String() string {
   120  	var buf bytes.Buffer
   121  	buf.WriteString("[")
   122  	for e := tr.sortedRanges.Front(); e != nil; e = e.Next() {
   123  		buf.WriteString(e.Value.(Range).String())
   124  		if e.Next() != nil {
   125  			buf.WriteString(",")
   126  		}
   127  	}
   128  	buf.WriteString("]")
   129  	return buf.String()
   130  }
   131  
   132  // addRangeInPlace adds r to tr in place without creating a new copy.
   133  func (tr *ranges) addRangeInPlace(r Range) {
   134  	if r.IsEmpty() {
   135  		return
   136  	}
   137  
   138  	e := tr.findFirstNotBefore(r)
   139  	for e != nil {
   140  		lr := e.Value.(Range)
   141  		ne := e.Next()
   142  		if !lr.Overlaps(r) {
   143  			break
   144  		}
   145  		r = r.Merge(lr)
   146  		tr.sortedRanges.Remove(e)
   147  		e = ne
   148  	}
   149  	if e == nil {
   150  		tr.sortedRanges.PushBack(r)
   151  		return
   152  	}
   153  	tr.sortedRanges.InsertBefore(r, e)
   154  }
   155  
   156  func (tr *ranges) removeRangeInPlace(r Range) {
   157  	if r.IsEmpty() {
   158  		return
   159  	}
   160  	e := tr.findFirstNotBefore(r)
   161  	for e != nil {
   162  		lr := e.Value.(Range)
   163  		ne := e.Next()
   164  		if !lr.Overlaps(r) {
   165  			return
   166  		}
   167  		res := lr.Subtract(r)
   168  		if res == nil {
   169  			tr.sortedRanges.Remove(e)
   170  		} else {
   171  			e.Value = res[0]
   172  			if len(res) == 2 {
   173  				tr.sortedRanges.InsertAfter(res[1], e)
   174  			}
   175  		}
   176  		e = ne
   177  	}
   178  }
   179  
   180  // findFirstNotBefore finds the first interval that's not before r.
   181  func (tr *ranges) findFirstNotBefore(r Range) *list.Element {
   182  	if tr.sortedRanges == nil {
   183  		return nil
   184  	}
   185  	e := tr.sortedRanges.Front()
   186  	for ; e != nil; e = e.Next() {
   187  		if !e.Value.(Range).Before(r) {
   188  			return e
   189  		}
   190  	}
   191  	return nil
   192  }