diff --git a/readme.md b/readme.md index 3d5fb38..f24c68e 100644 --- a/readme.md +++ b/readme.md @@ -81,6 +81,8 @@ 2021/1/4 :reflectM包添加indirect处理指针和非指针,获取type、value +2020/02/18:bitStore新增特殊档位获取方法,修改档位数目的参数标识 + # mod vendor模式加载包 通过go mod的方式加载的github上面的包会有报红的问题,但是包本身是可以运行的,这样就是会有一个问题,如果你想要点击去看方法的内容,没办法做到 diff --git a/util/bitStore/BitStoreTool.go b/util/bitStore/BitStoreTool.go index 3a316ab..c848c17 100644 --- a/util/bitStore/BitStoreTool.go +++ b/util/bitStore/BitStoreTool.go @@ -12,13 +12,13 @@ package bitStore import "fmt" const ( - bit = 32 - realBit = 31 + bit = 32 //作为标识,没有实际作用 + realBit = 31 //真实参与计算的每个数字代表的档位数量 ) type BitStore struct { - MaxGear int //存储的最大挡位 - GearPickList []int + MaxGear int //存储的最大挡位 + GearPickList []int //用来表示档位的数组(每一个数表示【下标 * bit】位的档位存储结果) } //初始化方法,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 func (bitStore *BitStore) FindAllGearMap() map[int]bool { length := bitStore.getGearListLength(bitStore.MaxGear) //获取最大档位的长度 @@ -73,12 +86,12 @@ func (bitStore *BitStore) checkGearRight(gear int) { //获取当前档位对应的数组长度(这边的length是从1开始的.) 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 { - return (gear - 1) % (bit - 1) + return gear - realBit*(length-1) - 1 } 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 { resultMap := make(map[int]bool, 0) for i := 0; i < length; i++ { - for j := 0; j < bit-1 && bitStore.MaxGear > i*(bit-1)+j; j++ { - resultMap[i*(bit-1)+j+1] = (bitStore.GearPickList[i] & (1 << uint(j))) > 0 + for j := 0; j < realBit && bitStore.MaxGear > i*realBit+j; j++ { + resultMap[i*realBit+j+1] = (bitStore.GearPickList[i] & (1 << uint(j))) > 0 } } 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 { resultMap := make(map[int]bool, 0) for i := 0; i < listSize; i++ { - for j := 0; j < (bit - 1); j++ { - resultMap[i*(bit-1)+j+1] = (bitStore.GearPickList[i] & (1 << uint(j))) > 0 + for j := 0; j < realBit; j++ { + resultMap[i*realBit+j+1] = (bitStore.GearPickList[i] & (1 << uint(j))) > 0 } } for i := listSize; i < length; i++ { - for j := 0; j < (bit-1) && bitStore.MaxGear > i*(bit-1)+j; j++ { - resultMap[i*(bit-1)+j+1] = false + for j := 0; j < realBit && bitStore.MaxGear > i*realBit+j; j++ { + resultMap[i*realBit+j+1] = false } } return resultMap @@ -131,3 +144,29 @@ func (bitStore *BitStore) checkIfReceive(gear int) { 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< 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< 0) + } + } + return gearList +} diff --git a/util/bitStore/BitStoreTool_test.go b/util/bitStore/BitStoreTool_test.go index 5075976..00e15a1 100644 --- a/util/bitStore/BitStoreTool_test.go +++ b/util/bitStore/BitStoreTool_test.go @@ -7,12 +7,74 @@ package bitStore import ( - "Go-Tool/util/sort" "fmt" "math" "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) { gearMap := bitStore.FindAllGearMap() falseList := make([]int, 0) @@ -34,22 +96,21 @@ func getReceiveResult(bitStore *BitStore) { keyList = append(keyList, key) } } - sort.BubbleSort(keyList) fmt.Println("已经领取的档位信息如下") for value := range keyList { fmt.Print(fmt.Sprintf("%d,", keyList[value])) } } -func TestOneThousand(t *testing.T) { +func testOneThousand(t *testing.T) { bitStore := NewBitStore(1000, nil) newStoreMap(bitStore) //初始化的校验 fmt.Println() - bitStore.ReceiveByGear(1) + bitStore.ReceiveByGear(3) 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) fmt.Println(fmt.Sprintf("%b", bitStore.GearPickList[0])) 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))) bitStore.ReceiveByGear(48) 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("这个时候领取结果 48应该返回true,结果是:%v", bitStore.IsGearReceive(48))) bitStore.ReceiveByGear(123) fmt.Println(fmt.Sprintf("这个时候领取结果 123应该返回true,结果是:%v", bitStore.IsGearReceive(123))) @@ -100,7 +161,7 @@ func TestOneThousand(t *testing.T) { getReceiveResult(bitStore) } -func TestFiftyEight(t *testing.T) { +func testFiftyEight() { bitStore := NewBitStore(58, nil) newStoreMap(bitStore) //初始化的校验 @@ -133,7 +194,7 @@ func TestFiftyEight(t *testing.T) { } } -func TestThirtyThree(t *testing.T) { +func testThirtyThree() { bitStore := NewBitStore(33, nil) newStoreMap(bitStore) //初始化的校验 @@ -160,7 +221,7 @@ func TestThirtyThree(t *testing.T) { } } -func TestEighteen(t *testing.T) { +func testEighteen() { bitStore := NewBitStore(18, nil) newStoreMap(bitStore) //初始化的校验 @@ -181,7 +242,7 @@ func TestEighteen(t *testing.T) { } } -func TestTen(t *testing.T) { +func testTen() { bitStore := NewBitStore(10, nil) newStoreMap(bitStore) //初始化的校验 @@ -200,7 +261,7 @@ func TestTen(t *testing.T) { } } -func TestTwo(t *testing.T) { +func testTwo() { bitStore := NewBitStore(2, nil) newStoreMap(bitStore) //初始化的校验