github.com/tunabay/go-bitarray@v1.3.1/bitarray_sort.go (about)

     1  // Copyright (c) 2021 Hirotsuna Mizuno. All rights reserved.
     2  // Use of this source code is governed by the MIT license that can be found in
     3  // the LICENSE file.
     4  
     5  package bitarray
     6  
     7  import (
     8  	"sort"
     9  )
    10  
    11  // Slice attaches the methods of sort.Interface to []*BitArray, sorting in
    12  // increasing order.
    13  type Slice []*BitArray
    14  
    15  func (s Slice) Len() int           { return len(s) }
    16  func (s Slice) Less(i, j int) bool { return Compare(s[i], s[j]) < 0 }
    17  func (s Slice) Swap(i, j int)      { s[i], s[j] = s[j], s[i] }
    18  
    19  // Sort is a convenience method: s.Sort() calls sort.Sort(s).
    20  func (s Slice) Sort() { sort.Sort(s) }
    21  
    22  // Search searches for x in the sorted slice s using binary search and returns
    23  // the index. The return value is the index to insert x if x is not present (it
    24  // could be s.Len()). The slice must be sorted in ascending order.
    25  func (s Slice) Search(x BitArrayer) int {
    26  	return sort.Search(len(s), func(i int) bool { return 0 <= Compare(s[i], x) })
    27  }