blob: 6e7125a83f7c3ab5252bf666c4ae65e54caeb710 [file] [log] [blame]
Marcel van Lohuizenc441dd32019-12-23 16:56:21 +01001// Copyright 2018 The CUE Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15// Copyright 2018 The Go Authors. All rights reserved.
16// Use of this source code is governed by a BSD-style
17// license that can be found in the LICENSE file.
18
19package list
20
21import (
22 "sort"
23
Marcel van Lohuizen845df052020-07-26 13:15:45 +020024 "cuelang.org/go/cue"
Marcel van Lohuizenc441dd32019-12-23 16:56:21 +010025)
26
27// valueSorter defines a sort.Interface; implemented in cue/builtinutil.go.
28type valueSorter struct {
29 a []cue.Value
30 cmp cue.Value
31 err error
32}
33
34func (s *valueSorter) ret() ([]cue.Value, error) {
Marcel van Lohuizenabce1452020-07-27 17:40:33 +020035 if s.err != nil {
36 return nil, s.err
37 }
38 // The input slice is already a copy and that we can modify it safely.
39 return s.a, nil
Marcel van Lohuizenc441dd32019-12-23 16:56:21 +010040}
41
Marcel van Lohuizenabce1452020-07-27 17:40:33 +020042func (s *valueSorter) Len() int { return len(s.a) }
43func (s *valueSorter) Swap(i, j int) { s.a[i], s.a[j] = s.a[j], s.a[i] }
44func (s *valueSorter) Less(i, j int) bool {
45 v := s.cmp.Fill(s.a[i], "x")
46 v = v.Fill(s.a[j], "y")
47 isLess, err := v.Lookup("less").Bool()
48 if err != nil && s.err == nil {
49 s.err = err
50 return true
51 }
52 return isLess
53}
Marcel van Lohuizenc441dd32019-12-23 16:56:21 +010054
Marcel van Lohuizen99d18dc2020-10-05 15:27:22 +020055// Sort sorts data while keeping the original order of equal elements.
56// It does O(n*log(n)) comparisons.
Marcel van Lohuizenc441dd32019-12-23 16:56:21 +010057//
Marcel van Lohuizen1d213a32020-05-27 17:35:48 +020058// cmp is a struct of the form {T: _, x: T, y: T, less: bool}, where
Marcel van Lohuizenc441dd32019-12-23 16:56:21 +010059// less should reflect x < y.
60//
61// Example:
62//
63// Sort([2, 3, 1], list.Ascending)
64//
Marcel van Lohuizen0afa3772020-02-13 20:49:24 +010065// Sort{{a: 2}, {a: 3}, {a: 1}, {x: {}, y: {}, less: x.a < y.a}}
Marcel van Lohuizenc441dd32019-12-23 16:56:21 +010066//
67func Sort(list []cue.Value, cmp cue.Value) (sorted []cue.Value, err error) {
68 s := valueSorter{list, cmp, nil}
69 // The input slice is already a copy and that we can modify it safely.
Marcel van Lohuizen99d18dc2020-10-05 15:27:22 +020070 sort.Stable(&s)
Marcel van Lohuizenc441dd32019-12-23 16:56:21 +010071 return s.ret()
72}
73
Marcel van Lohuizen99d18dc2020-10-05 15:27:22 +020074// Deprecated: use Sort, which is always stable
Marcel van Lohuizenc441dd32019-12-23 16:56:21 +010075func SortStable(list []cue.Value, cmp cue.Value) (sorted []cue.Value, err error) {
76 s := valueSorter{list, cmp, nil}
77 sort.Stable(&s)
78 return s.ret()
79}
80
81// Strings sorts a list of strings in increasing order.
82func SortStrings(a []string) []string {
83 sort.Strings(a)
84 return a
85}
86
87// IsSorted tests whether a list is sorted.
88//
89// See Sort for an example comparator.
90func IsSorted(list []cue.Value, cmp cue.Value) bool {
91 s := valueSorter{list, cmp, nil}
92 return sort.IsSorted(&s)
93}
94
95// IsSortedStrings tests whether a list is a sorted lists of strings.
96func IsSortedStrings(a []string) bool {
97 return sort.StringsAreSorted(a)
98}