-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathsort.go
53 lines (48 loc) · 1.41 KB
/
sort.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package slice
import "github.com/clipperhouse/typewriter"
var sort = &typewriter.Template{
Name: "Sort",
Text: `
// Sort returns a new ordered {{.SliceName}}. See: http://clipperhouse.github.io/gen/#Sort
func (rcv {{.SliceName}}) Sort() {{.SliceName}} {
result := make({{.SliceName}}, len(rcv))
copy(result, rcv)
sort.Sort(result)
return result
}
`,
TypeConstraint: typewriter.Constraint{Ordered: true},
}
var isSorted = &typewriter.Template{
Name: "IsSorted",
Text: `
// IsSorted reports whether {{.SliceName}} is sorted. See: http://clipperhouse.github.io/gen/#Sort
func (rcv {{.SliceName}}) IsSorted() bool {
return sort.IsSorted(rcv)
}
`,
TypeConstraint: typewriter.Constraint{Ordered: true},
}
var sortDesc = &typewriter.Template{
Name: "SortDesc",
Text: `
// SortDesc returns a new reverse-ordered {{.SliceName}}. See: http://clipperhouse.github.io/gen/#Sort
func (rcv {{.SliceName}}) SortDesc() {{.SliceName}} {
result := make({{.SliceName}}, len(rcv))
copy(result, rcv)
sort.Sort(sort.Reverse(result))
return result
}
`,
TypeConstraint: typewriter.Constraint{Ordered: true},
}
var isSortedDesc = &typewriter.Template{
Name: "IsSortedDesc",
Text: `
// IsSortedDesc reports whether {{.SliceName}} is reverse-sorted. See: http://clipperhouse.github.io/gen/#Sort
func (rcv {{.SliceName}}) IsSortedDesc() bool {
return sort.IsSorted(sort.Reverse(rcv))
}
`,
TypeConstraint: typewriter.Constraint{Ordered: true},
}