github.com/puellanivis/breton@v0.2.16/lib/sort/string.template (about) 1 package sort 2 3 import ( 4 "{{.Lib}}" 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 {{.Lib}}.Compare(p[i], p[j]) < 0 } 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 // Compare implements Comparer. 21 func (p {{.Name}}Slice) Compare(i, j int) int { 22 return {{.Lib}}.Compare(p[i], p[j]) 23 } 24 25 // CompareFunc implements Comparer. 26 func (p {{.Name}}Slice) CompareFunc(x interface{}) func(int) int { 27 e := x.({{.Type}}) 28 return func(i int) int { 29 return {{.Lib}}.Compare(p[i], e) 30 } 31 } 32 33 // RadixRange implements RadixInterface. 34 func (p {{.Name}}Slice) RadixRange() (int, int) { 35 r := 0 36 for _, s := range p { 37 if len(s) > r { 38 r = len(s) 39 } 40 } 41 return 0, r * 8 42 } 43 44 // RadixFunc implements RadixInterface. 45 func (p {{.Name}}Slice) RadixFunc(r int) RadixTest { 46 n := r / 8 47 mask := byte(1 << uint(7-(r&0x7))) 48 49 return func(i int) bool { 50 if n >= len(p[i]) { 51 return false 52 } 53 54 return p[i][n]&mask != 0 55 } 56 } 57 58 // Sort is a convenience method. 59 func (p {{.Name}}Slice) Sort() { radix(p) } 60 61 // Radix is a convenience method. 62 func (p {{.Name}}Slice) Radix() { radix(p) } 63 64 // Search is a convenience method. 65 func (p {{.Name}}Slice) Search(x {{.Type}}) int { return Search{{.Name}}s(p, x) } 66 67 // SearchFor is a convenience method. 68 func (p {{.Name}}Slice) SearchFor(x interface{}) int { return Search{{.Name}}s(p, x.({{.Type}})) } 69 70 // {{.Name}}s sorts a slice of {{.Type}}s in increasing order. 71 func {{.Name}}s(a []{{.Type}}) { radix({{.Name}}Slice(a)) } 72 73 //Search{{.Name}}s searches for x in a sorted slice of {{.Type}}s and returns the index 74 // as specified by sort.Search. The return value is the index to insert x if x is not present (it could be len(a)). 75 // The slice must be sorted in ascending order. 76 func Search{{.Name}}s(a []{{.Type}}, x {{.Type}}) int { 77 return sort.Search(len(a), func(i int) bool { return {{.Lib}}.Compare(a[i], x) >= 0 }) 78 } 79 80 // {{.Name}}sAreSorted tests whether a slice of {{.Type}}s is sorted in increasing order. 81 func {{.Name}}sAreSorted(a []{{.Type}}) bool { return sort.IsSorted({{.Name}}Slice(a)) }