github.com/hamba/slices@v0.2.1-0.20220316050741-75c057d92699/lesser.go (about)

     1  package slices
     2  
     3  import (
     4  	"unsafe"
     5  )
     6  
     7  type lesserFn func(unsafe.Pointer) func(i, j int) bool
     8  
     9  // LesserOf returns a lesser function for slice sorting.
    10  func LesserOf(slice interface{}) func(i, j int) bool {
    11  	fn := lesserOf(slice)
    12  	if fn == nil {
    13  		panic("slice is not supported slice type")
    14  	}
    15  
    16  	ptr := noescape(ptrOf(slice))
    17  	return fn(ptr)
    18  }
    19  
    20  func lesserOf(slice interface{}) lesserFn {
    21  	switch slice.(type) {
    22  	case []string:
    23  		return stringLesser
    24  	case []int:
    25  		return intLesser
    26  	case []int8:
    27  		return int8Lesser
    28  	case []int16:
    29  		return int16Lesser
    30  	case []int32:
    31  		return int32Lesser
    32  	case []int64:
    33  		return int64Lesser
    34  	case []uint:
    35  		return uintLesser
    36  	case []uint8:
    37  		return uint8Lesser
    38  	case []uint16:
    39  		return uint16Lesser
    40  	case []uint32:
    41  		return uint32Lesser
    42  	case []uint64:
    43  		return uint64Lesser
    44  	case []float32:
    45  		return float32Lesser
    46  	case []float64:
    47  		return float64Lesser
    48  	}
    49  	return nil
    50  }