checkpoint: a more performant shuffle func
This commit is contained in:
parent
fb764ad6dc
commit
c5384ca719
2 changed files with 12 additions and 33 deletions
|
@ -212,7 +212,7 @@ func (b *Board) RepositionStatus(currentIndex, targetIndex int) error {
|
||||||
return fmt.Errorf("unable to get the list of statuses; %w", err)
|
return fmt.Errorf("unable to get the list of statuses; %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
statuses = moveAndShuffle(statuses, currentIndex, targetIndex)
|
statuses = shuffle(statuses, currentIndex, targetIndex)
|
||||||
|
|
||||||
if err := b.normaliseStatusesPositionValues(statuses); err != nil {
|
if err := b.normaliseStatusesPositionValues(statuses); err != nil {
|
||||||
return fmt.Errorf("unable to normalise the statuses position values; %w", err)
|
return fmt.Errorf("unable to normalise the statuses position values; %w", err)
|
||||||
|
|
|
@ -1,47 +1,26 @@
|
||||||
package board
|
package board
|
||||||
|
|
||||||
// moveAndShuffle creates a new list where the element specified at the index of the current
|
// shuffle takes a list of Statuses and swaps its position with the Status closer to its desired
|
||||||
// position is moved to the index of the target position. Elements within the range of the
|
// position until it reaches it.
|
||||||
// old and new positions will then shuffle forwards or backwards in the list depending on the
|
//
|
||||||
// direction of the move.
|
|
||||||
// This is currently used to move specified columns forwards or backwards on the Kanban board.
|
// This is currently used to move specified columns forwards or backwards on the Kanban board.
|
||||||
// When a column changes position the other columns shuffle forward or backwards as required.
|
// When a column changes position the other columns shuffle forward or backwards as required.
|
||||||
func moveAndShuffle(input []Status, oldIndex, newIndex int) []Status {
|
func shuffle(list []Status, oldIndex, newIndex int) []Status {
|
||||||
if newIndex == oldIndex {
|
if newIndex == oldIndex {
|
||||||
return input
|
return list
|
||||||
}
|
}
|
||||||
|
|
||||||
output := make([]Status, len(input))
|
|
||||||
|
|
||||||
// copy the original slice and assign the target element to the
|
|
||||||
// new index.
|
|
||||||
copy(output, input)
|
|
||||||
|
|
||||||
output[newIndex] = input[oldIndex]
|
|
||||||
|
|
||||||
// shuffle the elements in range down the slice if the target element moves
|
|
||||||
// up the list.
|
|
||||||
if newIndex < oldIndex {
|
if newIndex < oldIndex {
|
||||||
for index := newIndex; index <= oldIndex; index++ {
|
for i := oldIndex; i > newIndex; i-- {
|
||||||
if index == oldIndex {
|
list[i], list[i-1] = list[i-1], list[i]
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
output[index+1] = input[index]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return output
|
return list
|
||||||
}
|
}
|
||||||
|
|
||||||
// shuffle the elements in range up the slice if the target element moves
|
for i := oldIndex; i < newIndex; i++ {
|
||||||
// down the list.
|
list[i], list[i+1] = list[i+1], list[i]
|
||||||
for index := oldIndex; index <= newIndex; index++ {
|
|
||||||
if index == oldIndex {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
output[index-1] = input[index]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return output
|
return list
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue