blob: d1927613d7ef381d73a5fbfb3887e83d4b362f9b [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
55// Sort sorts data. It does O(n*log(n)) comparisons.
56// The sort is not guaranteed to be stable.
57//
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.
70 sort.Sort(&s)
71 return s.ret()
72}
73
74// SortStable sorts data while keeping the original order of equal elements.
75// It does O(n*log(n)) comparisons.
76//
Marcel van Lohuizendf7da7b2019-12-23 17:46:52 +010077// See Sort for an example usage.
Marcel van Lohuizenc441dd32019-12-23 16:56:21 +010078//
79func SortStable(list []cue.Value, cmp cue.Value) (sorted []cue.Value, err error) {
80 s := valueSorter{list, cmp, nil}
81 sort.Stable(&s)
82 return s.ret()
83}
84
85// Strings sorts a list of strings in increasing order.
86func SortStrings(a []string) []string {
87 sort.Strings(a)
88 return a
89}
90
91// IsSorted tests whether a list is sorted.
92//
93// See Sort for an example comparator.
94func IsSorted(list []cue.Value, cmp cue.Value) bool {
95 s := valueSorter{list, cmp, nil}
96 return sort.IsSorted(&s)
97}
98
99// IsSortedStrings tests whether a list is a sorted lists of strings.
100func IsSortedStrings(a []string) bool {
101 return sort.StringsAreSorted(a)
102}