github.com/blend/go-sdk@v1.20220411.3/mathutil/copy_sort.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package mathutil
     9  
    10  import (
    11  	"sort"
    12  	"time"
    13  )
    14  
    15  // CopySort copies and sorts an array of floats.
    16  func CopySort(input []float64) []float64 {
    17  	copy := Copy(input)
    18  	sort.Float64s(copy)
    19  	return copy
    20  }
    21  
    22  // Copy copies an array of float64s.
    23  func Copy(input []float64) []float64 {
    24  	output := make([]float64, len(input))
    25  	copy(output, input)
    26  	return output
    27  }
    28  
    29  // CopySortInts copies and sorts an array of floats.
    30  func CopySortInts(input []int) []int {
    31  	copy := CopyInts(input)
    32  	sort.Ints(copy)
    33  	return copy
    34  }
    35  
    36  // CopyInts copies an array of float64s.
    37  func CopyInts(input []int) []int {
    38  	output := make([]int, len(input))
    39  	copy(output, input)
    40  	return output
    41  }
    42  
    43  // CopySortDurations copies and sorts an array of floats.
    44  func CopySortDurations(input []time.Duration) []time.Duration {
    45  	copy := CopyDurations(input)
    46  	sort.Sort(Durations(copy))
    47  	return copy
    48  }
    49  
    50  // CopyDurations copies an array of time.Duration.
    51  func CopyDurations(input []time.Duration) []time.Duration {
    52  	output := make([]time.Duration, len(input))
    53  	copy(output, input)
    54  	return output
    55  }
    56  
    57  // Durations is an array of durations.
    58  type Durations []time.Duration
    59  
    60  // Len implements sort.Sorter
    61  func (d Durations) Len() int {
    62  	return len(d)
    63  }
    64  
    65  // Swap implements sort.Sorter
    66  func (d Durations) Swap(i, j int) {
    67  	d[i], d[j] = d[j], d[i]
    68  }
    69  
    70  // Less implements sort.Sorter
    71  func (d Durations) Less(i, j int) bool {
    72  	return d[i] < d[j]
    73  }