-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
76 lines (66 loc) · 1.3 KB
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Copyright (c) 2019 Meng Huang ([email protected])
// This package is licensed under a MIT license that can be found in the LICENSE file.
package raft
import (
"fmt"
"unsafe"
)
func cloneString(str string) string {
c := make([]byte, len(str))
copy(c, str)
return *(*string)(unsafe.Pointer(&c))
}
func cloneBytes(b []byte) []byte {
c := make([]byte, len(b))
copy(c, b)
return c
}
// Address returns a raft node address with the given host and port.
func Address(host string, port int) string {
return fmt.Sprintf("%s:%d", host, port)
}
func minUint64(a, b uint64) uint64 {
if a < b {
return a
}
return b
}
func maxUint64(a, b uint64) uint64 {
if a > b {
return a
}
return b
}
func quickSort(a []uint64, left, right int) {
length := len(a)
if length <= 1 {
return
}
if left == -999 {
left = 0
}
if right == -999 {
right = length - 1
}
var partitonIndex int
if left < right {
partitonIndex = partition(a, left, right)
quickSort(a, left, partitonIndex-1)
quickSort(a, partitonIndex+1, right)
}
}
func partition(a []uint64, left, right int) int {
pivot := left
index := pivot + 1
for i := index; i <= right; i++ {
if a[i] < a[pivot] {
swap(a, i, index)
index++
}
}
swap(a, pivot, index-1)
return index - 1
}
func swap(a []uint64, i, j int) {
a[i], a[j] = a[j], a[i]
}