feat(Go-Tool):2020/02/18:bitStore新增特殊档位获取方法,修改档位数目的参数标识

This commit is contained in:
Huangzj
2021-02-18 11:17:19 +08:00
parent 75cba05f49
commit 39cee0ce6a
3 changed files with 125 additions and 23 deletions
+2
View File
@@ -81,6 +81,8 @@
2021/1/4 reflectM包添加indirect处理指针和非指针,获取type、value 2021/1/4 reflectM包添加indirect处理指针和非指针,获取type、value
2020/02/18bitStore新增特殊档位获取方法,修改档位数目的参数标识
# mod vendor模式加载包 # mod vendor模式加载包
通过go mod的方式加载的github上面的包会有报红的问题,但是包本身是可以运行的,这样就是会有一个问题,如果你想要点击去看方法的内容,没办法做到 通过go mod的方式加载的github上面的包会有报红的问题,但是包本身是可以运行的,这样就是会有一个问题,如果你想要点击去看方法的内容,没办法做到
+50 -11
View File
@@ -12,13 +12,13 @@ package bitStore
import "fmt" import "fmt"
const ( const (
bit = 32 bit = 32 //作为标识,没有实际作用
realBit = 31 realBit = 31 //真实参与计算的每个数字代表的档位数量
) )
type BitStore struct { type BitStore struct {
MaxGear int //存储的最大挡位 MaxGear int //存储的最大挡位
GearPickList []int GearPickList []int //用来表示档位的数组(每一个数表示【下标 * bit】位的档位存储结果)
} }
//初始化方法,gear表示最大的领取档位是多少.(档位刚开始计算,没有值的情况) //初始化方法,gear表示最大的领取档位是多少.(档位刚开始计算,没有值的情况)
@@ -34,6 +34,19 @@ func NewBitStore(gear int, gearPickList []int) *BitStore {
} }
} }
//返回指定档位长度的领取状态数组
func (bitStore *BitStore) FindSpecialGearList(gear int) []bool {
bitStore.checkGearRight(gear) //校验传进来的长度是否正确
bitStore.getSizeOrInit() //如果数组没有初始化,进行初始化
return bitStore.getGearListByGear(gear) //返回指定长度的领取状态数组
}
//获取当前有记录的档位的Map
func (bitStore *BitStore) FindGearMap() map[int]bool {
listSize := bitStore.getSizeOrInit() //获取当前数组长度,如果数组没有初始化,进行初始化
return bitStore.shortListDeal(listSize, listSize) //短数组的处理(没有达到最大大小的数组长度)
}
//获取所有档位对应的Map //获取所有档位对应的Map
func (bitStore *BitStore) FindAllGearMap() map[int]bool { func (bitStore *BitStore) FindAllGearMap() map[int]bool {
length := bitStore.getGearListLength(bitStore.MaxGear) //获取最大档位的长度 length := bitStore.getGearListLength(bitStore.MaxGear) //获取最大档位的长度
@@ -73,12 +86,12 @@ func (bitStore *BitStore) checkGearRight(gear int) {
//获取当前档位对应的数组长度(这边的length是从1开始的.) //获取当前档位对应的数组长度(这边的length是从1开始的.)
func (bitStore *BitStore) getGearListLength(gear int) int { func (bitStore *BitStore) getGearListLength(gear int) int {
return ((gear - 1) + (bit - 1)) / (bit - 1) return ((gear - 1) + realBit) / realBit
} }
//获取档位所在的二进制位置 //获取档位所在的二进制位置
func (bitStore *BitStore) getGearPosition(gear, length int) int { func (bitStore *BitStore) getGearPosition(gear, length int) int {
return (gear - 1) % (bit - 1) return gear - realBit*(length-1) - 1
} }
func (bitStore *BitStore) getSizeOrInit() int { func (bitStore *BitStore) getSizeOrInit() int {
@@ -91,8 +104,8 @@ func (bitStore *BitStore) getSizeOrInit() int {
func (bitStore *BitStore) fullListDeal(listSize int, length int) map[int]bool { func (bitStore *BitStore) fullListDeal(listSize int, length int) map[int]bool {
resultMap := make(map[int]bool, 0) resultMap := make(map[int]bool, 0)
for i := 0; i < length; i++ { for i := 0; i < length; i++ {
for j := 0; j < bit-1 && bitStore.MaxGear > i*(bit-1)+j; j++ { for j := 0; j < realBit && bitStore.MaxGear > i*realBit+j; j++ {
resultMap[i*(bit-1)+j+1] = (bitStore.GearPickList[i] & (1 << uint(j))) > 0 resultMap[i*realBit+j+1] = (bitStore.GearPickList[i] & (1 << uint(j))) > 0
} }
} }
return resultMap return resultMap
@@ -101,13 +114,13 @@ func (bitStore *BitStore) fullListDeal(listSize int, length int) map[int]bool {
func (bitStore *BitStore) shortListDeal(listSize, length int) map[int]bool { func (bitStore *BitStore) shortListDeal(listSize, length int) map[int]bool {
resultMap := make(map[int]bool, 0) resultMap := make(map[int]bool, 0)
for i := 0; i < listSize; i++ { for i := 0; i < listSize; i++ {
for j := 0; j < (bit - 1); j++ { for j := 0; j < realBit; j++ {
resultMap[i*(bit-1)+j+1] = (bitStore.GearPickList[i] & (1 << uint(j))) > 0 resultMap[i*realBit+j+1] = (bitStore.GearPickList[i] & (1 << uint(j))) > 0
} }
} }
for i := listSize; i < length; i++ { for i := listSize; i < length; i++ {
for j := 0; j < (bit-1) && bitStore.MaxGear > i*(bit-1)+j; j++ { for j := 0; j < realBit && bitStore.MaxGear > i*realBit+j; j++ {
resultMap[i*(bit-1)+j+1] = false resultMap[i*realBit+j+1] = false
} }
} }
return resultMap return resultMap
@@ -131,3 +144,29 @@ func (bitStore *BitStore) checkIfReceive(gear int) {
panic("当前档位已被领取,不可重复领取") panic("当前档位已被领取,不可重复领取")
} }
} }
func (bitStore *BitStore) getGearListByGear(gear int) []bool {
gearList := make([]bool, 0)
listAllGear := len(bitStore.GearPickList) * realBit //当前已领取数组可表示的总档位
//如果指定的长度大于gear已有数组长度,需要特殊处理
if listAllGear <= gear {
for i := 0; i < (listAllGear-1)/realBit+1; i++ {
for j := 0; j < realBit && realBit*i+j < gear; j++ {
gearList = append(gearList, (bitStore.GearPickList[i]&(1<<uint(j))) > 0)
}
}
for i := 0; i < gear-listAllGear; i++ {
for j := 0; j < realBit && realBit*i+j < gear-listAllGear; j++ {
gearList = append(gearList, false)
}
}
return gearList
}
//如果获取的档位比实际记录的档位小
for i := 0; i < (gear-1)/realBit+1; i++ {
for j := 0; j < realBit && realBit*i+j < gear; j++ {
gearList = append(gearList, (bitStore.GearPickList[i]&(1<<uint(j))) > 0)
}
}
return gearList
}
+72 -11
View File
@@ -7,12 +7,74 @@
package bitStore package bitStore
import ( import (
"Go-Tool/util/sort"
"fmt" "fmt"
"math" "math"
"testing" "testing"
) )
func TestBitStore(t *testing.T) {
fmt.Println("测试2档的情况")
testTwo()
fmt.Println()
fmt.Println("测试10档的情况")
testTen()
fmt.Println()
fmt.Println("测试18档的情况")
testEighteen()
fmt.Println()
fmt.Println("测试33档的情况")
testThirtyThree()
fmt.Println()
fmt.Println("测试58档的情况")
testFiftyEight()
fmt.Println()
fmt.Println("测试1000档的情况")
testOneThousand(t)
fmt.Println()
}
func TestBitStoreFind(t *testing.T) {
bitStore := NewBitStore(120, nil)
bitStore.ReceiveByGear(1)
bitStore.ReceiveByGear(3)
bitStore.ReceiveByGear(5)
bitStore.ReceiveByGear(7)
bitStore.ReceiveByGear(13)
bitStore.ReceiveByGear(10)
bitStore.ReceiveByGear(16)
bitStore.ReceiveByGear(33)
bitStore.ReceiveByGear(35)
fmt.Println(bitStore.FindGearMap())
fmt.Println(bitStore.FindSpecialGearList(1))
fmt.Println(len(bitStore.FindSpecialGearList(1)))
fmt.Println(bitStore.FindSpecialGearList(4))
fmt.Println(len(bitStore.FindSpecialGearList(4)))
fmt.Println(bitStore.FindSpecialGearList(2))
fmt.Println(len(bitStore.FindSpecialGearList(2)))
fmt.Println(bitStore.FindSpecialGearList(10))
fmt.Println(len(bitStore.FindSpecialGearList(10)))
fmt.Println(bitStore.FindSpecialGearList(13))
fmt.Println(len(bitStore.FindSpecialGearList(13)))
fmt.Println(bitStore.FindSpecialGearList(14))
fmt.Println(len(bitStore.FindSpecialGearList(14)))
fmt.Println(bitStore.FindSpecialGearList(20))
fmt.Println(len(bitStore.FindSpecialGearList(20)))
fmt.Println(bitStore.FindSpecialGearList(32))
fmt.Println(len(bitStore.FindSpecialGearList(32)))
fmt.Println(bitStore.FindSpecialGearList(63))
fmt.Println(len(bitStore.FindSpecialGearList(63)))
fmt.Println(bitStore.FindSpecialGearList(64))
fmt.Println(len(bitStore.FindSpecialGearList(64)))
fmt.Println(bitStore.FindSpecialGearList(65))
fmt.Println(len(bitStore.FindSpecialGearList(65)))
}
func newStoreMap(bitStore *BitStore) { func newStoreMap(bitStore *BitStore) {
gearMap := bitStore.FindAllGearMap() gearMap := bitStore.FindAllGearMap()
falseList := make([]int, 0) falseList := make([]int, 0)
@@ -34,22 +96,21 @@ func getReceiveResult(bitStore *BitStore) {
keyList = append(keyList, key) keyList = append(keyList, key)
} }
} }
sort.BubbleSort(keyList)
fmt.Println("已经领取的档位信息如下") fmt.Println("已经领取的档位信息如下")
for value := range keyList { for value := range keyList {
fmt.Print(fmt.Sprintf("%d,", keyList[value])) fmt.Print(fmt.Sprintf("%d,", keyList[value]))
} }
} }
func TestOneThousand(t *testing.T) { func testOneThousand(t *testing.T) {
bitStore := NewBitStore(1000, nil) bitStore := NewBitStore(1000, nil)
newStoreMap(bitStore) //初始化的校验 newStoreMap(bitStore) //初始化的校验
fmt.Println() fmt.Println()
bitStore.ReceiveByGear(1) bitStore.ReceiveByGear(3)
fmt.Println(fmt.Sprintf("%b", bitStore.GearPickList[0])) fmt.Println(fmt.Sprintf("%b", bitStore.GearPickList[0]))
fmt.Println(fmt.Sprintf("这个时候领取结果 1应该返回true,结果是:%v", bitStore.IsGearReceive(3))) fmt.Println(fmt.Sprintf("这个时候领取结果 3应该返回true,结果是:%v", bitStore.IsGearReceive(3)))
bitStore.ReceiveByGear(8) bitStore.ReceiveByGear(8)
fmt.Println(fmt.Sprintf("%b", bitStore.GearPickList[0])) fmt.Println(fmt.Sprintf("%b", bitStore.GearPickList[0]))
fmt.Println(fmt.Sprintf("这个时候领取结果 8应该返回true,结果是:%v", bitStore.IsGearReceive(8))) fmt.Println(fmt.Sprintf("这个时候领取结果 8应该返回true,结果是:%v", bitStore.IsGearReceive(8)))
@@ -76,10 +137,10 @@ func TestOneThousand(t *testing.T) {
fmt.Println(fmt.Sprintf("这个时候领取结果 44应该返回true,结果是:%v", bitStore.IsGearReceive(44))) fmt.Println(fmt.Sprintf("这个时候领取结果 44应该返回true,结果是:%v", bitStore.IsGearReceive(44)))
bitStore.ReceiveByGear(48) bitStore.ReceiveByGear(48)
fmt.Println(fmt.Sprintf("%b", bitStore.GearPickList[1])) fmt.Println(fmt.Sprintf("%b", bitStore.GearPickList[1]))
fmt.Println(fmt.Sprintf("这个时候领取结果 48应该返回true,结果是:%v", bitStore.IsGearReceive(48)))
fmt.Println(fmt.Sprintf("判断当前数组是否动态增长,当前数组长度为:%d", len(bitStore.GearPickList))) fmt.Println(fmt.Sprintf("判断当前数组是否动态增长,当前数组长度为:%d", len(bitStore.GearPickList)))
fmt.Println(fmt.Sprintf("这个时候领取结果 48应该返回true,结果是:%v", bitStore.IsGearReceive(48)))
bitStore.ReceiveByGear(123) bitStore.ReceiveByGear(123)
fmt.Println(fmt.Sprintf("这个时候领取结果 123应该返回true,结果是:%v", bitStore.IsGearReceive(123))) fmt.Println(fmt.Sprintf("这个时候领取结果 123应该返回true,结果是:%v", bitStore.IsGearReceive(123)))
@@ -100,7 +161,7 @@ func TestOneThousand(t *testing.T) {
getReceiveResult(bitStore) getReceiveResult(bitStore)
} }
func TestFiftyEight(t *testing.T) { func testFiftyEight() {
bitStore := NewBitStore(58, nil) bitStore := NewBitStore(58, nil)
newStoreMap(bitStore) //初始化的校验 newStoreMap(bitStore) //初始化的校验
@@ -133,7 +194,7 @@ func TestFiftyEight(t *testing.T) {
} }
} }
func TestThirtyThree(t *testing.T) { func testThirtyThree() {
bitStore := NewBitStore(33, nil) bitStore := NewBitStore(33, nil)
newStoreMap(bitStore) //初始化的校验 newStoreMap(bitStore) //初始化的校验
@@ -160,7 +221,7 @@ func TestThirtyThree(t *testing.T) {
} }
} }
func TestEighteen(t *testing.T) { func testEighteen() {
bitStore := NewBitStore(18, nil) bitStore := NewBitStore(18, nil)
newStoreMap(bitStore) //初始化的校验 newStoreMap(bitStore) //初始化的校验
@@ -181,7 +242,7 @@ func TestEighteen(t *testing.T) {
} }
} }
func TestTen(t *testing.T) { func testTen() {
bitStore := NewBitStore(10, nil) bitStore := NewBitStore(10, nil)
newStoreMap(bitStore) //初始化的校验 newStoreMap(bitStore) //初始化的校验
@@ -200,7 +261,7 @@ func TestTen(t *testing.T) {
} }
} }
func TestTwo(t *testing.T) { func testTwo() {
bitStore := NewBitStore(2, nil) bitStore := NewBitStore(2, nil)
newStoreMap(bitStore) //初始化的校验 newStoreMap(bitStore) //初始化的校验