Description
Proposal Details
// Shift return a slice with n zero values of E inserted at the given position.
func Shift[S ~[]E, E any](s S, at, n int) S
// Rotate right-rotates the slice by n places. To left-rotate use -n.
func Rotate[S ~[]E, E any](s S, n int) S
Judging from the source code of the slices
package there already is an internal "rotateLeft/Right" function. I am unsure if there is a good reason not to export this useful function (especially as it is not trivial to implement without allocations).
A "Shift" function is not trivial to implement efficiently either, I think (without intermediate allocations, ie. making an zero slice of length and inserting it) . Maybe this can be done using other functions in the package that I am unaware of. In that case just an example would be nice.
(Looking at the implementation of Insert
I think a Shift function could simplify its implementation a little. Also Insert
checks for overlap but this seems covered by copy
already? Although maybe there's more to it then I can see by glancing at it).
Updates after feedback
Apparently shift
is commonly used to refer to a different operation on arrays/slices (especially in scripting languages) and bits. Alternative names for Shift
such as InsertN
or InsertZeros
should be considered.
Metadata
Metadata
Assignees
Type
Projects
Status