github.com/blend/go-sdk@v1.20220411.3/consistenthash/insertion_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 consistenthash
     9  
    10  import "sort"
    11  
    12  // InsertionSort inserts an bucket into a hashring by binary searching
    13  // for the index which would satisfy the overall "sorted" status of the ring
    14  // returning the updated hashring.
    15  func InsertionSort(ring []HashedBucket, item HashedBucket) []HashedBucket {
    16  	destination := sort.Search(len(ring), func(index int) bool {
    17  		return ring[index].Hashcode >= item.Hashcode
    18  	})
    19  	ring = append(ring, HashedBucket{})
    20  	copy(ring[destination+1:], ring[destination:])
    21  	ring[destination] = item
    22  	return ring
    23  }