github.com/patricebensoussan/go/codec@v1.2.99/sort-slice.go.tmpl (about) 1 // Copyright (c) 2012-2020 Ugorji Nwoke. All rights reserved. 2 // Use of this source code is governed by a MIT license found in the LICENSE file. 3 4 // Code generated from sort-slice.go.tmpl - DO NOT EDIT. 5 6 {{/* 7 xxxSlice 8 xxxIntf 9 xxxIntfSlice 10 xxxRv 11 xxxRvSlice 12 13 I'm now going to create them for 14 - sortables 15 - sortablesplus 16 17 With the parameters passed in sortables or sortablesplus, 18 'time, 'bytes' are special, and correspond to time.Time and []byte respectively. 19 */}} 20 21 package codec 22 23 import "time" 24 import "reflect" 25 import "bytes" 26 27 {{/* func init() { _ = time.Unix } */}} 28 29 {{define "T"}} 30 func (p {{ .Type }}) Len() int { return len(p) } 31 func (p {{ .Type }}) Swap(i, j int) { p[uint(i)], p[uint(j)] = p[uint(j)], p[uint(i)] } 32 func (p {{ .Type }}) Less(i, j int) bool { 33 {{ if eq .Kind "bool" }} return !p[uint(i)]{{.V}} && p[uint(j)]{{.V}} 34 {{ else if eq .Kind "float32" }} return p[uint(i)]{{.V}} < p[uint(j)]{{.V}} || isNaN32(p[uint(i)]{{.V}}) && !isNaN32(p[uint(j)]{{.V}}) 35 {{ else if eq .Kind "float64" }} return p[uint(i)]{{.V}} < p[uint(j)]{{.V}} || isNaN64(p[uint(i)]{{.V}}) && !isNaN64(p[uint(j)]{{.V}}) 36 {{ else if eq .Kind "time" }} return p[uint(i)]{{.V}}.Before(p[uint(j)]{{.V}}) 37 {{ else if eq .Kind "bytes" }} return bytes.Compare(p[uint(i)]{{.V}}, p[uint(j)]{{.V}}) == -1 38 {{ else }} return p[uint(i)]{{.V}} < p[uint(j)]{{.V}} 39 {{ end -}} 40 } 41 {{end}} 42 43 {{range $i, $v := sortables }}{{ $t := tshort $v }} 44 type {{ $v }}Slice []{{ $t }} 45 {{template "T" args "Kind" $v "Type" (print $v "Slice") "V" ""}} 46 {{end}} 47 48 {{range $i, $v := sortablesplus }}{{ $t := tshort $v }} 49 50 type {{ $v }}Rv struct { 51 v {{ $t }} 52 r reflect.Value 53 } 54 type {{ $v }}RvSlice []{{ $v }}Rv 55 {{template "T" args "Kind" $v "Type" (print $v "RvSlice") "V" ".v"}} 56 57 {{if eq $v "bytes" "string" -}} 58 type {{ $v }}Intf struct { 59 v {{ $t }} 60 i interface{} 61 } 62 type {{ $v }}IntfSlice []{{ $v }}Intf 63 {{template "T" args "Kind" $v "Type" (print $v "IntfSlice") "V" ".v"}} 64 {{end}} 65 66 {{end}}