perf(structure-value): perf set bit
This commit is contained in:
parent
b6dedfa384
commit
0f1142d434
|
@ -147,26 +147,28 @@ func (v *Value) ChangeValueLength(f func()) UpdateLength {
|
|||
|
||||
func (v *Value) SetByte(offset int, val bool) {
|
||||
v.onType = DynamicNull // 位图使用无类型
|
||||
b := byte(0)
|
||||
if val {
|
||||
b = byte(1)
|
||||
}
|
||||
|
||||
if v.length >= offset {
|
||||
v.val[offset] = b
|
||||
// 扩容
|
||||
if len(v.val) <= offset/8 {
|
||||
newByte := make([]byte, (offset/8)+1)
|
||||
copy(newByte, v.val[:len(v.val)])
|
||||
v.val = newByte
|
||||
v.length = len(v.val)
|
||||
}
|
||||
if val {
|
||||
// true 位
|
||||
v.val[offset/8] |= (0b1 << (offset % 8))
|
||||
return
|
||||
}
|
||||
|
||||
newByte := make([]byte, offset+1)
|
||||
newByte[offset] = b
|
||||
copy(newByte, v.val[:v.length])
|
||||
v.val = newByte
|
||||
v.length = len(newByte)
|
||||
// false 位
|
||||
v.val[offset/8] ^= (0b1 << (offset % 8))
|
||||
}
|
||||
|
||||
func (v *Value) GetByte(offset int) (bool, error) {
|
||||
if v.length >= offset {
|
||||
return v.val[offset] == byte(1), nil
|
||||
if len(v.val) >= offset/8 {
|
||||
// 采用 & 来运算 是否为 true
|
||||
return v.val[offset/8]&(0b1<<(offset%8)) != 0, nil
|
||||
}
|
||||
|
||||
return false, errorx.New("the maximum length is exceeded")
|
||||
|
|
Loading…
Reference in New Issue