Marcel van Lohuizen | c441dd3 | 2019-12-23 16:56:21 +0100 | [diff] [blame] | 1 | // 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 | |
| 19 | package list |
| 20 | |
| 21 | import ( |
| 22 | "sort" |
| 23 | |
Marcel van Lohuizen | 845df05 | 2020-07-26 13:15:45 +0200 | [diff] [blame] | 24 | "cuelang.org/go/cue" |
Marcel van Lohuizen | c441dd3 | 2019-12-23 16:56:21 +0100 | [diff] [blame] | 25 | ) |
| 26 | |
| 27 | // valueSorter defines a sort.Interface; implemented in cue/builtinutil.go. |
| 28 | type valueSorter struct { |
| 29 | a []cue.Value |
| 30 | cmp cue.Value |
| 31 | err error |
| 32 | } |
| 33 | |
| 34 | func (s *valueSorter) ret() ([]cue.Value, error) { |
Marcel van Lohuizen | abce145 | 2020-07-27 17:40:33 +0200 | [diff] [blame] | 35 | 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 Lohuizen | c441dd3 | 2019-12-23 16:56:21 +0100 | [diff] [blame] | 40 | } |
| 41 | |
Marcel van Lohuizen | abce145 | 2020-07-27 17:40:33 +0200 | [diff] [blame] | 42 | func (s *valueSorter) Len() int { return len(s.a) } |
| 43 | func (s *valueSorter) Swap(i, j int) { s.a[i], s.a[j] = s.a[j], s.a[i] } |
| 44 | func (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 Lohuizen | c441dd3 | 2019-12-23 16:56:21 +0100 | [diff] [blame] | 54 | |
Marcel van Lohuizen | 99d18dc | 2020-10-05 15:27:22 +0200 | [diff] [blame] | 55 | // Sort sorts data while keeping the original order of equal elements. |
| 56 | // It does O(n*log(n)) comparisons. |
Marcel van Lohuizen | c441dd3 | 2019-12-23 16:56:21 +0100 | [diff] [blame] | 57 | // |
Marcel van Lohuizen | 1d213a3 | 2020-05-27 17:35:48 +0200 | [diff] [blame] | 58 | // cmp is a struct of the form {T: _, x: T, y: T, less: bool}, where |
Marcel van Lohuizen | c441dd3 | 2019-12-23 16:56:21 +0100 | [diff] [blame] | 59 | // less should reflect x < y. |
| 60 | // |
| 61 | // Example: |
| 62 | // |
| 63 | // Sort([2, 3, 1], list.Ascending) |
| 64 | // |
Marcel van Lohuizen | 0afa377 | 2020-02-13 20:49:24 +0100 | [diff] [blame] | 65 | // Sort{{a: 2}, {a: 3}, {a: 1}, {x: {}, y: {}, less: x.a < y.a}} |
Marcel van Lohuizen | c441dd3 | 2019-12-23 16:56:21 +0100 | [diff] [blame] | 66 | // |
| 67 | func 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 Lohuizen | 99d18dc | 2020-10-05 15:27:22 +0200 | [diff] [blame] | 70 | sort.Stable(&s) |
Marcel van Lohuizen | c441dd3 | 2019-12-23 16:56:21 +0100 | [diff] [blame] | 71 | return s.ret() |
| 72 | } |
| 73 | |
Marcel van Lohuizen | 99d18dc | 2020-10-05 15:27:22 +0200 | [diff] [blame] | 74 | // Deprecated: use Sort, which is always stable |
Marcel van Lohuizen | c441dd3 | 2019-12-23 16:56:21 +0100 | [diff] [blame] | 75 | func 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. |
| 82 | func 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. |
| 90 | func 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. |
| 96 | func IsSortedStrings(a []string) bool { |
| 97 | return sort.StringsAreSorted(a) |
| 98 | } |