github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/libraries/doltcore/sqle/setalgebra/composite_set.go (about)

     1  // Copyright 2020 Dolthub, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package setalgebra
    16  
    17  // CompositeSet is a set which is made up of a FiniteSet and one or more non overlapping intervals such as
    18  // {n | n < 0 or n > 100} (set of all numbers n below 0 or greater than 100) this set contains 2 non overlapping intervals
    19  // and an empty finite set. Alternatively {n | n < 0 or {5,10,15}} (set of all numbers n below 0 or n equal to 5, 10 or 15)
    20  // which would be represented by one Interval and a FiniteSet containing 5,10, and 15.
    21  type CompositeSet struct {
    22  	// Set contains a set of points in the set.  None of these points should be in the Intervals
    23  	Set FiniteSet
    24  	// Intervals is a slice of non overlapping Interval objects in sorted order.
    25  	Intervals []Interval
    26  }
    27  
    28  // Union takes the current set and another set and returns a set containing all values from both.
    29  func (cs CompositeSet) Union(other Set) (Set, error) {
    30  	switch otherTyped := other.(type) {
    31  	case FiniteSet:
    32  		return finiteSetCompositeSetUnion(otherTyped, cs)
    33  	case Interval:
    34  		return intervalCompositeSetUnion(otherTyped, cs)
    35  	case CompositeSet:
    36  		return compositeUnion(cs, otherTyped)
    37  	case EmptySet:
    38  		return cs, nil
    39  	case UniversalSet:
    40  		return otherTyped, nil
    41  	}
    42  
    43  	panic("unknown set type")
    44  
    45  }
    46  
    47  // Interset takes the current set and another set and returns a set containing the values that are in both
    48  func (cs CompositeSet) Intersect(other Set) (Set, error) {
    49  	switch otherTyped := other.(type) {
    50  	case FiniteSet:
    51  		return finiteSetCompositeSetIntersection(otherTyped, cs)
    52  	case Interval:
    53  		return intervalCompositeSetIntersection(otherTyped, cs)
    54  	case CompositeSet:
    55  		return compositeIntersection(otherTyped, cs)
    56  	case EmptySet:
    57  		return EmptySet{}, nil
    58  	case UniversalSet:
    59  		return cs, nil
    60  	}
    61  
    62  	panic("unknown set type")
    63  }