github.com/puellanivis/breton@v0.2.16/lib/sort/uint.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 << uint({{.MSB}})) - 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  	return bits.LeadingZeros{{.Width}}(uint{{.Width}}(bitMask)), end
    58  }
    59  
    60  // RadixFunc implements RadixInterface.
    61  func (p {{.Name}}Slice) RadixFunc(r int) RadixTest {
    62  	mask := {{.Type}}(1) << uint({{.MSB}}-r)
    63  	return func(i int) bool {
    64  		return p[i]&mask != 0
    65  	}
    66  }
    67  
    68  // Sort is a convenience method.
    69  func (p {{.Name}}Slice) Sort() { radix(p) }
    70  
    71  // Radix is a convenience method.
    72  func (p {{.Name}}Slice) Radix() { radix(p) }
    73  
    74  // Search is a convenience method.
    75  func (p {{.Name}}Slice) Search(x {{.Type}}) int { return Search{{.Name}}s(p, x) }
    76  
    77  // SearchFor is a convenience method.
    78  func (p {{.Name}}Slice) SearchFor(x interface{}) int { return Search{{.Name}}s(p, x.({{.Type}})) }
    79  
    80  // {{.Name}}s sorts a slice of {{.Type}}s in increasing order.
    81  func {{.Name}}s(a []{{.Type}}) { radix({{.Name}}Slice(a)) }
    82  
    83  // Search{{.Name}}s searches for x in a sorted slice of {{.Type}}s and returns the index as specified by sort.Search.
    84  // The return value is the index to insert x if x is not present (it could be len(a)).
    85  // The slice must be sorted in ascending order.
    86  func Search{{.Name}}s(a []{{.Type}}, x {{.Type}}) int {
    87  	return sort.Search(len(a), func(i int) bool { return a[i] >= x })
    88  }
    89  
    90  // {{.Name}}sAreSorted tests whether a slice of {{.Type}}s is sorted in increasing order.
    91  func {{.Name}}sAreSorted(a []{{.Type}}) bool { return sort.IsSorted({{.Name}}Slice(a)) }