sort-slice.go.tmpl 2.0 KB

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