github.com/anacrolix/torrent@v1.61.0/storage/possum/key-sorter.go (about)

     1  package possumTorrentStorage
     2  
     3  import (
     4  	"cmp"
     5  )
     6  
     7  // Sorts by a precomputed key but swaps on another slice at the same time.
     8  type keySorter[T any, K cmp.Ordered] struct {
     9  	orig []T
    10  	keys []K
    11  }
    12  
    13  func (o keySorter[T, K]) Len() int {
    14  	return len(o.keys)
    15  }
    16  
    17  func (o keySorter[T, K]) Less(i, j int) bool {
    18  	return o.keys[i] < o.keys[j]
    19  }
    20  
    21  func (o keySorter[T, K]) Swap(i, j int) {
    22  	o.keys[i], o.keys[j] = o.keys[j], o.keys[i]
    23  	o.orig[i], o.orig[j] = o.orig[j], o.orig[i]
    24  }