-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: Sort hdrhistogram proto and merge sorted slices
- Loading branch information
Showing
4 changed files
with
108 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package hdrhistogram | ||
|
||
import "golang.org/x/exp/constraints" | ||
|
||
// SortBy sorts 2 slices together. SortBy.By will be sorted and corresponding | ||
// swaps will happen in SortBy.Other. | ||
type SortBy[T constraints.Ordered, T2 any] struct { | ||
By []T | ||
Other []T2 | ||
} | ||
|
||
func (s SortBy[T, T2]) Len() int { | ||
return len(s.By) | ||
} | ||
|
||
func (s SortBy[T, T2]) Swap(i, j int) { | ||
s.By[i], s.By[j] = s.By[j], s.By[i] | ||
s.Other[i], s.Other[j] = s.Other[j], s.Other[i] | ||
} | ||
|
||
func (s SortBy[T, T2]) Less(i, j int) bool { | ||
return s.By[i] < s.By[j] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package hdrhistogram | ||
|
||
import ( | ||
"fmt" | ||
"sort" | ||
) | ||
|
||
func Example() { | ||
x := []int{3, 2, 1} | ||
y := []string{"a", "b", "c"} | ||
sort.Sort(SortBy[int, string]{x, y}) | ||
fmt.Println(x, y) | ||
// Output: | ||
// [1 2 3] [c b a] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters