github.com/grailbio/bigslice@v0.0.0-20230519005545-30c4c12152ad/typecheck/typecheck.go (about)

     1  // Copyright 2018 GRAIL, Inc. All rights reserved.
     2  // Use of this source code is governed by the Apache 2.0
     3  // license that can be found in the LICENSE file.
     4  
     5  // Package typecheck contains a number of typechecking and inference
     6  // utilities for bigslice operators.
     7  package typecheck
     8  
     9  import (
    10  	"reflect"
    11  
    12  	"github.com/grailbio/bigslice/slicetype"
    13  )
    14  
    15  // Equal tells whether the the expected and actual slicetypes are equal.
    16  func Equal(expect, actual slicetype.Type) bool {
    17  	if got, want := actual.NumOut(), expect.NumOut(); got != want {
    18  		return false
    19  	}
    20  	for i := 0; i < expect.NumOut(); i++ {
    21  		if got, want := actual.Out(i), expect.Out(i); got != want {
    22  			return false
    23  		}
    24  	}
    25  	return true
    26  }
    27  
    28  // Slices returns a slicetype of the provided column values. If the passed
    29  // values are not valid column values, Slices returns false.
    30  func Slices(columns ...interface{}) (slicetype.Type, bool) {
    31  	types := make([]reflect.Type, len(columns))
    32  	for i, col := range columns {
    33  		t := reflect.TypeOf(col)
    34  		if t.Kind() != reflect.Slice {
    35  			return nil, false
    36  		}
    37  		types[i] = t.Elem()
    38  	}
    39  	return slicetype.New(types...), true
    40  }
    41  
    42  // Devectorize returns a devectorized version of the provided
    43  // slicetype: Each of the type's columns is expected to be a slice;
    44  // the returned type unwraps the slice from each column. If the
    45  // provided type is not a valid vectorized slice type, false is
    46  // returned.
    47  func Devectorize(typ slicetype.Type) (slicetype.Type, bool) {
    48  	elems := make([]reflect.Type, typ.NumOut())
    49  	for i := 0; i < typ.NumOut(); i++ {
    50  		t := typ.Out(i)
    51  		if t.Kind() != reflect.Slice {
    52  			return nil, false
    53  		}
    54  		elems[i] = t.Elem()
    55  	}
    56  	return slicetype.New(elems...), true
    57  }