forked from eosspark/geos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fixed_key.go
174 lines (156 loc) · 3.78 KB
/
fixed_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
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
package types
import (
"github.com/zhangsifeng92/geos/common"
. "github.com/zhangsifeng92/geos/common/eos_math"
. "github.com/zhangsifeng92/geos/exception"
. "github.com/zhangsifeng92/geos/exception/try"
"github.com/zhangsifeng92/geos/log"
"reflect"
)
func IsIntegral(T interface{}) bool {
switch T.(type) {
case int:
return true
case int8:
return true
case int16:
return true
case int32:
return true
case int64:
return true
case uint:
return true
case uint8:
return true
case uint16:
return true
case uint32:
return true
case uint64:
return true
case bool:
return true
default:
return false
}
}
func ConvertToWordT(T interface{}) WordT {
switch v := T.(type) {
case int:
if v >= 0 {
return Uint128{Low: uint64(v), High: 0}
} else {
return Uint128{Low: 0, High: 0}
}
case int8:
if v >= 0 {
return Uint128{Low: uint64(v), High: 0}
} else {
return Uint128{Low: 0, High: 0}
}
case int16:
if v >= 0 {
return Uint128{Low: uint64(v), High: 0}
} else {
return Uint128{Low: 0, High: 0}
}
case int32:
if v >= 0 {
return Uint128{Low: uint64(v), High: 0}
} else {
return Uint128{Low: 0, High: 0}
}
case int64:
if v >= 0 {
return Uint128{Low: uint64(v), High: 0}
} else {
return Uint128{Low: 0, High: 0}
}
case uint:
return Uint128{Low: uint64(v), High: 0}
case uint8:
return Uint128{Low: uint64(v), High: 0}
case uint16:
return Uint128{Low: uint64(v), High: 0}
case uint32:
return Uint128{Low: uint64(v), High: 0}
case uint64:
return Uint128{Low: uint64(v), High: 0}
case bool:
if v == true {
return Uint128{Low: 1, High: 0}
} else {
return Uint128{Low: 0, High: 0}
}
default:
return Uint128{Low: 0, High: 0}
}
}
func SizeOf(T interface{}) int {
return int(reflect.TypeOf(T).Size())
}
func IsSame(T interface{}, compType interface{}) bool {
return reflect.TypeOf(T) == reflect.TypeOf(compType)
}
type WordT = Uint128
var wordTSize = SizeOf(WordT{})
var SizeOfWordT = SizeOf(WordT{})
type FixedKey struct {
Size common.SizeT
data []WordT
}
func (f FixedKey) numWords() common.SizeT {
return (f.Size + SizeOfWordT - 1) / SizeOfWordT
}
func (f FixedKey) EnableFirstWord(first interface{}) bool {
var boolType bool
return IsIntegral(first) && !IsSame(first, boolType) && SizeOf(first) <= wordTSize
}
func (f *FixedKey) SetFromWordSequence(arr []interface{}) {
wordSize := SizeOf(reflect.TypeOf(arr[0]))
tempWord := CreateUint128(int(0))
subWordShift := 8 * wordSize
numSubWords := wordTSize / wordSize
subWordsLeft := numSubWords
f.data = make([]WordT, f.numWords())
i := 0
for _, w := range arr {
if subWordsLeft > 1 {
tempWord = tempWord.Or(ConvertToWordT(w))
tempWord.LeftShifts(subWordShift)
subWordsLeft--
continue
}
EosAssert(subWordsLeft == 1, &FixedKeyTypeException{}, "unexpected error in fixed_key constructor")
tempWord = tempWord.Or(ConvertToWordT(w))
subWordsLeft = numSubWords
f.data[i] = tempWord
tempWord = Uint128{Low: 0, High: 0}
i++
}
if subWordsLeft != numSubWords {
if subWordsLeft > 1 {
tempWord.LeftShifts(8 * int(subWordsLeft-1))
}
f.data[i] = tempWord
}
}
func (f *FixedKey) MakeFromWordSequence(first interface{}, rest ...interface{}) *FixedKey {
Try(func() {
FcAssert(f.EnableFirstWord(first), "The first word is invalid")
FcAssert(wordTSize == wordTSize/SizeOf(first)*SizeOf(first),
"size of the backing word size is not divisible by the size of the words supplied as arguments")
FcAssert(SizeOf(first)*(1+len(rest)) <= f.Size, "too many words supplied to make_from_word_sequence")
var arr []interface{}
arr = append(arr, first)
arr = append(arr, rest...)
f.SetFromWordSequence(arr)
}).Catch(func(e Exception) {
log.Error(e.DetailMessage())
}).End()
return f
}
func (f *FixedKey) GetArray() []WordT {
return f.data
}