-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathtypes.go
308 lines (241 loc) · 5.76 KB
/
types.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
// Copyright 2011 Huan Du. All rights reserved.
// Licensed under the MIT license that can be found in the LICENSE file.
package skiplist
import (
"bytes"
"fmt"
"reflect"
)
// Key types for all built-in types.
// We can use these type as key type when creating a new skip list.
//
// list := New(Int) // Use int as key.
const (
Byte = byteType
ByteAsc = Byte
ByteDesc = -Byte
Rune = runeType
RuneAsc = Rune
RuneDesc = -Rune
Int = intType
IntAsc = Int
IntDesc = -Int
Int8 = int8Type
Int8Asc = Int8
Int8Desc = -Int8
Int16 = int16Type
Int16Asc = Int16
Int16Desc = -Int16
Int32 = int32Type
Int32Asc = Int32
Int32Desc = -Int32
Int64 = int64Type
Int64Asc = Int64
Int64Desc = -Int64
Uint = uintType
UintAsc = Uint
UintDesc = -Uint
Uint8 = uint8Type
Uint8Asc = Uint8
Uint8Desc = -Uint8
Uint16 = uint16Type
Uint16Asc = Uint16
Uint16Desc = -Uint16
Uint32 = uint32Type
Uint32Asc = Uint32
Uint32Desc = -Uint32
Uint64 = uint64Type
Uint64Asc = Uint64
Uint64Desc = -Uint64
Uintptr = uintptrType
UintptrAsc = Uintptr
UintptrDesc = -Uintptr
Float32 = float32Type
Float32Asc = Float32
Float32Desc = -Float32
Float64 = float64Type
Float64Asc = Float64
Float64Desc = -Float64
String = stringType
StringAsc = String
StringDesc = -String
Bytes = bytesType
BytesAsc = Bytes
BytesDesc = -Bytes
)
const (
byteType = keyType(reflect.Uint8)
runeType = keyType(reflect.Int32)
intType = keyType(reflect.Int)
int8Type = keyType(reflect.Int8)
int16Type = keyType(reflect.Int16)
int32Type = keyType(reflect.Int32)
int64Type = keyType(reflect.Int64)
uintType = keyType(reflect.Uint)
uint8Type = keyType(reflect.Uint8)
uint16Type = keyType(reflect.Uint16)
uint32Type = keyType(reflect.Uint32)
uint64Type = keyType(reflect.Uint64)
uintptrType = keyType(reflect.Uintptr)
float32Type = keyType(reflect.Float32)
float64Type = keyType(reflect.Float64)
stringType = keyType(reflect.String)
bytesType = keyType(reflect.Slice)
)
type keyType int
var _ Comparable = keyType(0)
func (kt keyType) kind() (kind reflect.Kind, reversed bool) {
if kt < 0 {
reversed = true
kt = -kt
}
kind = reflect.Kind(kt)
return
}
func (kt keyType) Compare(lhs, rhs interface{}) int {
val1 := reflect.ValueOf(lhs)
val2 := reflect.ValueOf(rhs)
kind, reversed := kt.kind()
result := compareTypes(val1, val2, kind)
if reversed {
result = -result
}
return result
}
var typeOfBytes = reflect.TypeOf([]byte(nil))
func compareTypes(lhs, rhs reflect.Value, kind reflect.Kind) int {
switch kind {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uintptr,
reflect.Float32, reflect.Float64:
v1 := calcScore(lhs)
v2 := calcScore(rhs)
if v1 > v2 {
return 1
}
if v1 < v2 {
return -1
}
return 0
case reflect.Int64:
v1 := lhs.Int()
v2 := rhs.Int()
if v1 > v2 {
return 1
}
if v1 < v2 {
return -1
}
return 0
case reflect.Uint64:
v1 := lhs.Uint()
v2 := rhs.Uint()
if v1 > v2 {
return 1
}
if v1 < v2 {
return -1
}
return 0
case reflect.String:
v1 := lhs.String()
v2 := rhs.String()
if v1 == v2 {
return 0
}
if v1 > v2 {
return 1
}
return -1
case reflect.Slice:
if lhs.Type().ConvertibleTo(typeOfBytes) && rhs.Type().ConvertibleTo(typeOfBytes) {
bytes1 := lhs.Convert(typeOfBytes).Interface().([]byte)
bytes2 := rhs.Convert(typeOfBytes).Interface().([]byte)
return bytes.Compare(bytes1, bytes2)
}
}
panic("never be here")
}
var numberLikeKinds = [...]bool{
reflect.Int: true,
reflect.Int8: true,
reflect.Int16: true,
reflect.Int32: true,
reflect.Int64: true,
reflect.Uint: true,
reflect.Uint8: true,
reflect.Uint16: true,
reflect.Uint32: true,
reflect.Uint64: true,
reflect.Uintptr: true,
reflect.Float32: true,
reflect.Float64: true,
reflect.String: false,
reflect.Slice: false,
}
func (kt keyType) CalcScore(key interface{}) float64 {
k := reflect.ValueOf(key)
kind, reversed := kt.kind()
if kk := k.Kind(); kk != kind {
// Special case for constant values.
// It allows us to write code like following without panic.
//
// list := skiplist.New(skiplist.Float64)
// list.Set(123, "foo") // 123 is int instead of float64.
if numberLikeKinds[kind] && (kk == reflect.Int || kk == reflect.Float64) {
// By pass the check.
} else {
name := kind.String()
if kind == reflect.Slice {
name = "[]byte"
}
panic(fmt.Errorf("skiplist: key type must be %v, but actual type is %v", name, k.Type()))
}
}
score := calcScore(k)
if reversed {
score = -score
}
return score
}
func calcScore(val reflect.Value) (score float64) {
switch val.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
score = float64(val.Int())
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
score = float64(val.Uint())
case reflect.Float32, reflect.Float64:
score = val.Float()
case reflect.String:
var hash uint64
str := val.String()
l := len(str)
// only use first 8 bytes
if l > 8 {
l = 8
}
// Consider str as a Big-Endian uint64.
for i := 0; i < l; i++ {
shift := uint(64 - 8 - i*8)
hash |= uint64(str[i]) << shift
}
score = float64(hash)
case reflect.Slice:
if val.Type().ConvertibleTo(typeOfBytes) {
var hash uint64
data := val.Convert(typeOfBytes).Interface().([]byte)
l := len(data)
// only use first 8 bytes
if l > 8 {
l = 8
}
// Consider str as a Big-Endian uint64.
for i := 0; i < l; i++ {
shift := uint(64 - 8 - i*8)
hash |= uint64(data[i]) << shift
}
score = float64(hash)
}
}
return
}