github.com/hasnat/dolt/go@v0.0.0-20210628190320-9eb5d843fbb7/libraries/doltcore/sqle/setalgebra/finite_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  import (
    18  	"github.com/dolthub/dolt/go/store/hash"
    19  	"github.com/dolthub/dolt/go/store/types"
    20  )
    21  
    22  // FiniteSet is your typical computer science set representing a finite number of unique objects stored in a map. An
    23  // example would be the set of strings {"red","blue","green"}, or the set of numbers {5, 73, 127}.
    24  type FiniteSet struct {
    25  	// HashToVal is a map from the noms hash of a value to it's value
    26  	HashToVal map[hash.Hash]types.Value
    27  }
    28  
    29  // NewFiniteSet returns a FiniteSet constructed from the provided values
    30  func NewFiniteSet(nbf *types.NomsBinFormat, vals ...types.Value) (FiniteSet, error) {
    31  	hashToVal := make(map[hash.Hash]types.Value, len(vals))
    32  
    33  	for _, val := range vals {
    34  		h, err := val.Hash(nbf)
    35  
    36  		if err != nil {
    37  			return FiniteSet{}, err
    38  		}
    39  
    40  		hashToVal[h] = val
    41  	}
    42  
    43  	return FiniteSet{HashToVal: hashToVal}, nil
    44  }
    45  
    46  // Union takes the current set and another set and returns a set containing all values from both.
    47  func (fs FiniteSet) Union(other Set) (Set, error) {
    48  	switch otherTyped := other.(type) {
    49  	// set / set union is all the values from both sets
    50  	case FiniteSet:
    51  		return finiteSetUnion(fs, otherTyped)
    52  	case Interval:
    53  		return finiteSetIntervalUnion(fs, otherTyped)
    54  	case CompositeSet:
    55  		return finiteSetCompositeSetUnion(fs, otherTyped)
    56  	case EmptySet:
    57  		return fs, nil
    58  	case UniversalSet:
    59  		return otherTyped, nil
    60  	default:
    61  		panic("unknown set type")
    62  	}
    63  }
    64  
    65  // Interset takes the current set and another set and returns a set containing the values that are in both
    66  func (fs FiniteSet) Intersect(other Set) (Set, error) {
    67  	switch otherTyped := other.(type) {
    68  	case FiniteSet:
    69  		return finiteSetIntersection(fs, otherTyped)
    70  	case Interval:
    71  		return finiteSetIntervalIntersection(fs, otherTyped)
    72  	case CompositeSet:
    73  		return finiteSetCompositeSetIntersection(fs, otherTyped)
    74  	case EmptySet:
    75  		return otherTyped, nil
    76  	case UniversalSet:
    77  		return fs, nil
    78  	default:
    79  		panic("unknown set type")
    80  	}
    81  }