github.com/ugorji/go/codec@v1.2.13-0.20240307214044-07c54c229a5a/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 (
    24  	"time"
    25  	"reflect"
    26  	"bytes"
    27  )
    28  
    29  {{/* func init() { _ = time.Unix } */}}
    30  
    31  {{define "T"}}
    32  func (p {{ .Type }}) Len() int           { return len(p) }
    33  func (p {{ .Type }}) Swap(i, j int)      { p[uint(i)], p[uint(j)] = p[uint(j)], p[uint(i)] }
    34  func (p {{ .Type }}) Less(i, j int) bool {
    35  	{{ if eq .Kind "bool"         }} return !p[uint(i)]{{.V}} && p[uint(j)]{{.V}}
    36      {{ else if eq .Kind "float32" }} return p[uint(i)]{{.V}} < p[uint(j)]{{.V}} || isNaN32(p[uint(i)]{{.V}}) && !isNaN32(p[uint(j)]{{.V}})
    37      {{ else if eq .Kind "float64" }} return p[uint(i)]{{.V}} < p[uint(j)]{{.V}} || isNaN64(p[uint(i)]{{.V}}) && !isNaN64(p[uint(j)]{{.V}})
    38      {{ else if eq .Kind "time"    }} return p[uint(i)]{{.V}}.Before(p[uint(j)]{{.V}})
    39      {{ else if eq .Kind "bytes"   }} return bytes.Compare(p[uint(i)]{{.V}}, p[uint(j)]{{.V}}) == -1
    40      {{ else                    }} return p[uint(i)]{{.V}} < p[uint(j)]{{.V}}
    41      {{ end -}}
    42  }
    43  {{end}}
    44  
    45  {{range $i, $v := sortables }}{{ $t := tshort $v }}
    46  type {{ $v }}Slice []{{ $t }}
    47  {{template "T" args "Kind" $v "Type" (print $v "Slice") "V" ""}}
    48  {{end}}
    49  
    50  {{range $i, $v := sortablesplus }}{{ $t := tshort $v }}
    51  
    52  type {{ $v }}Rv struct {
    53  	v {{ $t }}
    54  	r reflect.Value
    55  }
    56  type {{ $v }}RvSlice []{{ $v }}Rv
    57  {{template "T" args "Kind" $v "Type" (print $v "RvSlice") "V" ".v"}}
    58  
    59  {{if eq $v "bytes" "string" -}}
    60  type {{ $v }}Intf struct {
    61  	v {{ $t }}
    62  	i interface{}
    63  }
    64  type {{ $v }}IntfSlice []{{ $v }}Intf
    65  {{template "T" args "Kind" $v "Type" (print $v "IntfSlice") "V" ".v"}}
    66  {{end}}
    67  
    68  {{end}}