mirror of
https://github.com/duanhf2012/origin.git
synced 2026-02-04 06:54:45 +08:00
新增对连续buff内存中指定索引位打标记函数
This commit is contained in:
61
util/algorithms/BitwiseOperation.go
Normal file
61
util/algorithms/BitwiseOperation.go
Normal file
@@ -0,0 +1,61 @@
|
|||||||
|
package algorithms
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"unsafe"
|
||||||
|
)
|
||||||
|
|
||||||
|
type BitNumber interface {
|
||||||
|
int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64 | uintptr
|
||||||
|
}
|
||||||
|
|
||||||
|
type UnsignedNumber interface {
|
||||||
|
uint | uint8 | uint16 | uint32 | uint64 | uintptr
|
||||||
|
}
|
||||||
|
|
||||||
|
func getBitTagIndex[Number BitNumber, UNumber UnsignedNumber](bitBuff []Number, bitPositionIndex UNumber) (uintptr, uintptr, bool) {
|
||||||
|
sliceIndex := uintptr(bitPositionIndex) / (8 * unsafe.Sizeof(bitBuff[0]))
|
||||||
|
sliceBitIndex := uintptr(bitPositionIndex) % (8 * unsafe.Sizeof(bitBuff[0]))
|
||||||
|
|
||||||
|
//位index不能越界
|
||||||
|
if uintptr(bitPositionIndex) >= uintptr(len(bitBuff))*unsafe.Sizeof(bitBuff[0])*8 {
|
||||||
|
return 0, 0, false
|
||||||
|
}
|
||||||
|
return sliceIndex, sliceBitIndex, true
|
||||||
|
}
|
||||||
|
|
||||||
|
func setBitTagByIndex[Number BitNumber, UNumber UnsignedNumber](bitBuff []Number, bitPositionIndex UNumber, setTag bool) bool {
|
||||||
|
sliceIndex, sliceBitIndex, ret := getBitTagIndex(bitBuff, bitPositionIndex)
|
||||||
|
if ret == false {
|
||||||
|
return ret
|
||||||
|
}
|
||||||
|
|
||||||
|
if setTag {
|
||||||
|
bitBuff[sliceIndex] = bitBuff[sliceIndex] | 1<<sliceBitIndex
|
||||||
|
} else {
|
||||||
|
bitBuff[sliceIndex] = bitBuff[sliceIndex] &^ (1 << sliceBitIndex)
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetBitwiseTag[Number BitNumber, UNumber UnsignedNumber](bitBuff []Number, bitPositionIndex UNumber) (bool, error) {
|
||||||
|
sliceIndex, sliceBitIndex, ret := getBitTagIndex(bitBuff, bitPositionIndex)
|
||||||
|
if ret == false {
|
||||||
|
return false, errors.New("Invalid parameter")
|
||||||
|
}
|
||||||
|
|
||||||
|
return (bitBuff[sliceIndex] & (1 << sliceBitIndex)) > 0, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func SetBitwiseTag[Number BitNumber, UNumber UnsignedNumber](bitBuff []Number, bitPositionIndex UNumber) bool {
|
||||||
|
return setBitTagByIndex(bitBuff, bitPositionIndex, true)
|
||||||
|
}
|
||||||
|
|
||||||
|
func ClearBitwiseTag[Number BitNumber, UNumber UnsignedNumber](bitBuff []Number, bitPositionIndex UNumber) bool {
|
||||||
|
return setBitTagByIndex(bitBuff, bitPositionIndex, false)
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetBitwiseNum[Number BitNumber](bitBuff []Number) int {
|
||||||
|
return len(bitBuff) * int(unsafe.Sizeof(bitBuff[0])*8)
|
||||||
|
}
|
||||||
37
util/algorithms/BitwiseOperation_test.go
Normal file
37
util/algorithms/BitwiseOperation_test.go
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
package algorithms
|
||||||
|
|
||||||
|
import "testing"
|
||||||
|
|
||||||
|
func Test_Bitwise(t *testing.T) {
|
||||||
|
//1.预分配10个byte切片,用于存储位标识
|
||||||
|
byteBuff := make([]byte, 10)
|
||||||
|
|
||||||
|
//2.获取buff总共位数
|
||||||
|
bitNum := GetBitwiseNum(byteBuff)
|
||||||
|
t.Log(bitNum)
|
||||||
|
|
||||||
|
//3..对索引79位打标记,注意是从0开始,79即为最后一个位
|
||||||
|
idx := uint(79)
|
||||||
|
|
||||||
|
//4.对byteBuff索引idx位置打上标记
|
||||||
|
SetBitwiseTag(byteBuff, idx)
|
||||||
|
|
||||||
|
//5.获取索引idx位置标记
|
||||||
|
isTag, ret := GetBitwiseTag(byteBuff, idx)
|
||||||
|
t.Log("set index ", idx, " :", isTag, ret)
|
||||||
|
if isTag != true {
|
||||||
|
t.Fatal("error")
|
||||||
|
}
|
||||||
|
|
||||||
|
//6.清除掉索引idx位标记
|
||||||
|
ClearBitwiseTag(byteBuff, idx)
|
||||||
|
|
||||||
|
//7.获取索引idx位置标记
|
||||||
|
isTag, ret = GetBitwiseTag(byteBuff, idx)
|
||||||
|
t.Log("get index ", idx, " :", isTag, ret)
|
||||||
|
|
||||||
|
if isTag != false {
|
||||||
|
t.Fatal("error")
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user