github.com/puellanivis/breton@v0.2.16/lib/sort/int.template (about)

     1  package sort
     2  
     3  import (
     4  	"math/bits"
     5  	"sort"
     6  )
     7  
     8  // {{.Name}}Slice attaches the methods of sort.Interface to []{{.Type}}, sorting in increasing order.
     9  type {{.Name}}Slice []{{.Type}}
    10  
    11  // Len implements sort.Interface.
    12  func (p {{.Name}}Slice) Len() int { return len(p) }
    13  
    14  // Less implements sort.Interface.
    15  func (p {{.Name}}Slice) Less(i, j int) bool { return p[i] < p[j] }
    16  
    17  // Swap implements sort.Interface.
    18  func (p {{.Name}}Slice) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
    19  
    20  func cmp{{.Name}}(x, y {{.Type}}) int {
    21  	if x == y {
    22  		return 0
    23  	}
    24  
    25  	if x < y {
    26  		return -1
    27  	}
    28  
    29  	return +1
    30  }
    31  
    32  // Compare implements Comparer.
    33  func (p {{.Name}}Slice) Compare(i, j int) int {
    34  	return cmp{{.Name}}(p[i], p[j])
    35  }
    36  
    37  // CompareFunc implements Comparer.
    38  func (p {{.Name}}Slice) CompareFunc(x interface{}) func(int) int {
    39  	e := x.({{.Type}})
    40  	return func(i int) int {
    41  		return cmp{{.Name}}(p[i], e)
    42  	}
    43  }
    44  
    45  // RadixRange implements RadixInterface.
    46  func (p {{.Name}}Slice) RadixRange() (int, int) {
    47  	allBits := {{.Type}}(^1)
    48  	var anyBits {{.Type}}
    49  	for _, v := range p {
    50  		anyBits |= v
    51  		allBits &= v
    52  	}
    53  	bitMask := anyBits &^ allBits
    54  
    55  	end := {{.MSB}} - bits.TrailingZeros{{.Width}}(uint{{.Width}}(bitMask))
    56  
    57  	if bitMask < 0 {
    58  		return 0, end
    59  	}
    60  
    61  	return bits.LeadingZeros{{.Width}}(uint{{.Width}}(bitMask)), end
    62  }
    63  
    64  // RadixFunc implements RadixInterface.
    65  func (p {{.Name}}Slice) RadixFunc(r int) RadixTest {
    66  	if r == 0 {
    67  		return func(i int) bool {
    68  			return p[i] >= 0
    69  		}
    70  	}
    71  
    72  	mask := {{.Type}}(1) << uint({{.MSB}}-r)
    73  	return func(i int) bool {
    74  		return p[i]&mask != 0
    75  	}
    76  }
    77  
    78  // Sort is a convenience method.
    79  func (p {{.Name}}Slice) Sort() { radix(p) }
    80  
    81  // Radix is a convenience method.
    82  func (p {{.Name}}Slice) Radix() { radix(p) }
    83  
    84  // Search is a convenience method.
    85  func (p {{.Name}}Slice) Search(x {{.Type}}) int { return Search{{.Name}}s(p, x) }
    86  
    87  // SearchFor is a convenience method.
    88  func (p {{.Name}}Slice) SearchFor(x interface{}) int { return Search{{.Name}}s(p, x.({{.Type}})) }
    89  
    90  // {{.Name}}s sorts a slice of {{.Type}}s in increasing order.
    91  func {{.Name}}s(a []{{.Type}}) { radix({{.Name}}Slice(a)) }
    92  
    93  // Search{{.Name}}s searches for x in a sorted slice of {{.Type}}s and returns the index as specified by sort.Search.
    94  // The return value is the index to insert x if x is not present (it could be len(a)).
    95  // The slice must be sorted in ascending order.
    96  func Search{{.Name}}s(a []{{.Type}}, x {{.Type}}) int {
    97  	return sort.Search(len(a), func(i int) bool { return a[i] >= x })
    98  }
    99  
   100  // {{.Name}}sAreSorted tests whether a slice of {{.Type}}s is sorted in increasing order.
   101  func {{.Name}}sAreSorted(a []{{.Type}}) bool { return sort.IsSorted({{.Name}}Slice(a)) }