refactor user defined keypad
This commit is contained in:
@@ -10,14 +10,14 @@ import (
|
||||
|
||||
type UserInterface struct {
|
||||
IdxInterface m.IdxInterface
|
||||
KeypadSize m.KeypadSize
|
||||
kp *m.KeypadDimension
|
||||
}
|
||||
|
||||
func NewUserInterface(keypadSize m.KeypadSize) (*UserInterface, error) {
|
||||
idxInterface := util.IdentityArray(keypadSize.TotalAttrs())
|
||||
func NewUserInterface(kp *m.KeypadDimension) (*UserInterface, error) {
|
||||
idxInterface := util.IdentityArray(kp.TotalAttrs())
|
||||
userInterface := UserInterface{
|
||||
IdxInterface: idxInterface,
|
||||
KeypadSize: keypadSize,
|
||||
kp: kp,
|
||||
}
|
||||
err := userInterface.RandomShuffle()
|
||||
if err != nil {
|
||||
@@ -55,7 +55,7 @@ func (u *UserInterface) RandomShuffle() error {
|
||||
}
|
||||
|
||||
func (u *UserInterface) InterfaceMatrix() ([][]int, error) {
|
||||
return util.ListToMatrix(u.IdxInterface, u.KeypadSize.AttrsPerKey)
|
||||
return util.ListToMatrix(u.IdxInterface, u.kp.AttrsPerKey)
|
||||
}
|
||||
|
||||
func (u *UserInterface) SetViewMatrix() ([][]int, error) {
|
||||
@@ -67,8 +67,8 @@ func (u *UserInterface) SetViewMatrix() ([][]int, error) {
|
||||
}
|
||||
|
||||
func (u *UserInterface) DisperseInterface() error {
|
||||
if !u.KeypadSize.IsDispersable() {
|
||||
return errors.New("interface is not dispersable")
|
||||
if !u.kp.IsDispersable() {
|
||||
panic("interface is not dispersable")
|
||||
}
|
||||
|
||||
err := u.shuffleKeys()
|
||||
@@ -84,7 +84,7 @@ func (u *UserInterface) DisperseInterface() error {
|
||||
}
|
||||
|
||||
func (u *UserInterface) shuffleKeys() error {
|
||||
userInterfaceMatrix, err := util.ListToMatrix(u.IdxInterface, u.KeypadSize.AttrsPerKey)
|
||||
userInterfaceMatrix, err := util.ListToMatrix(u.IdxInterface, u.kp.AttrsPerKey)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -144,11 +144,11 @@ func (u *UserInterface) PartialInterfaceShuffle() error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
numbOfSelectedSets := u.KeypadSize.AttrsPerKey / 2
|
||||
if u.KeypadSize.AttrsPerKey&1 == 1 {
|
||||
numbOfSelectedSets := u.kp.AttrsPerKey / 2
|
||||
if u.kp.AttrsPerKey&1 == 1 {
|
||||
numbOfSelectedSets += util.Choice[int]([]int{0, 1})
|
||||
}
|
||||
setIdxs, err := util.RandomPermutation(u.KeypadSize.AttrsPerKey)
|
||||
setIdxs, err := util.RandomPermutation(u.kp.AttrsPerKey)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -158,7 +158,7 @@ func (u *UserInterface) PartialInterfaceShuffle() error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
interfaceBySet := make([][]int, u.KeypadSize.AttrsPerKey)
|
||||
interfaceBySet := make([][]int, u.kp.AttrsPerKey)
|
||||
for idx, attrs := range keypadSetView {
|
||||
if selectedSets.Contains(idx) {
|
||||
err = util.FisherYatesShuffle[int](&attrs)
|
||||
@@ -177,12 +177,12 @@ func (u *UserInterface) PartialInterfaceShuffle() error {
|
||||
}
|
||||
|
||||
func (u *UserInterface) GetAttrIdxByKeyNumbSetIdx(setIdx int, keyNumb int) (int, error) {
|
||||
if keyNumb < 0 || u.KeypadSize.NumbOfKeys <= keyNumb {
|
||||
return -1, errors.New(fmt.Sprintf("keyNumb %d is out of range 0-%d", keyNumb, u.KeypadSize.NumbOfKeys))
|
||||
if keyNumb < 0 || u.kp.NumbOfKeys <= keyNumb {
|
||||
return -1, errors.New(fmt.Sprintf("keyNumb %d is out of range 0-%d", keyNumb, u.kp.NumbOfKeys))
|
||||
}
|
||||
|
||||
if setIdx < 0 || u.KeypadSize.AttrsPerKey <= setIdx {
|
||||
return -1, errors.New(fmt.Sprintf("setIdx %d is out of range 0-%d", setIdx, u.KeypadSize.AttrsPerKey))
|
||||
if setIdx < 0 || u.kp.AttrsPerKey <= setIdx {
|
||||
return -1, errors.New(fmt.Sprintf("setIdx %d is out of range 0-%d", setIdx, u.kp.AttrsPerKey))
|
||||
}
|
||||
keypadView, err := u.InterfaceMatrix()
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user