-
Notifications
You must be signed in to change notification settings - Fork 0
/
key.go
110 lines (99 loc) · 2.46 KB
/
key.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package alternator
import (
"bytes"
"crypto/sha1"
"fmt"
"io"
"math"
"unsafe"
)
const initSeed = 500
// Key is a sha1 hash. A key is used as the primary identifier of entries in Alternator.
// Additionally, node identifiers are also a key, and so nodes share the key-space with entries,
// so that responsibility for key-value pairs is assigned to the successor of the key.
type Key [sha1.Size]byte
// MaxKey is the highest possible key in the key-space.
var MaxKey = [sha1.Size]byte{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
// MinKey is the lowest possible key in the key-space.
var MinKey = [sha1.Size]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
func (k Key) String() string {
keyOut := k[:]
if !fullKeys {
keyOut = k[0:10]
}
return fmt.Sprintf(k.xColor()+"%x\x1b[0m", keyOut)
}
// Compare compares two keys. Behaves just like bytes.Compare.
func (k Key) Compare(other Key) int {
return bytes.Compare(k[:], other[:])
}
// SliceToKey converts a slice to a Key (which is an alias for a byte array).
func SliceToKey(src []byte) (dst Key) {
dst = *(*[sha1.Size]byte)(unsafe.Pointer(&src[0]))
return
}
// inRange checks if test is in the key range [from, to].
func inRange(test, from, to Key) bool {
if from.Compare(to) < 0 {
return (test.Compare(from) > 0) && (test.Compare(to) < 0)
} else if from.Compare(to) > 0 {
return ((test.Compare(from) > 0) && (test.Compare(MaxKey) <= 0)) ||
((test.Compare(to) < 0) && (test.Compare(MinKey) >= 0))
} else {
return (test.Compare(from) == 0) || (test.Compare(to) == 0)
}
}
// StringToKey hashes a string, returning the hash as a key
func StringToKey(str string) Key {
h := sha1.New()
io.WriteString(h, str)
return SliceToKey(h.Sum(nil))
}
// xColor creates terminal truecolor escape sequence for the given key
func (k Key) xColor() string {
if len(k) < 1 {
return ""
}
// Convert to [0-1] range
f := float64(k[0]) / float64(255)
/*convert to long rainbow RGB*/
a := (1 - f) / 0.2
X := math.Floor(a)
Y := int(math.Floor(255 * (a - X)))
// fmt.Printf("f: %f, a: %f, x: %f, y: %d\n", f, a, X, Y)
var r, g, b int
switch X {
case 0:
r = 255
g = Y
b = 0
break
case 1:
r = 255 - Y
g = 255
b = 0
break
case 2:
r = 0
g = 255
b = Y
break
case 3:
r = 0
g = 255 - Y
b = 255
break
case 4:
r = Y
g = 0
b = 255
break
case 5:
r = 255
g = 0
b = 255
break
}
// Use first three bytes as RGB code
return fmt.Sprintf("\x1b[38;2;%d;%d;%dm", r, g, b)
}