new function lua_rotate
Closed this issue · 3 comments
http://www.lua.org/manual/5.3/manual.html#lua_rotate
void lua_rotate (lua_State *L, int idx, int n);
Rotates the stack elements from idx to the top n positions in the direction of the top, for a positive n, or -n positions in the direction of the bottom, for a negative n. The absolute value of n must not be greater than the size of the slice being rotated.
First shot available now.
This might be better implemented using lua_insert
or lua_remove
?
I don't think so. The current implementation will copy the n
elements two times regardless of the shift width m
, while an implementation using lua_insert
/lua_remove
will copy them m
times which can be up to n/2
in the worst case. But you are welcome to try. The current approach is also the same algorithm that Lua 5.3 uses. I first tried to copy the elements to their final destination directly, but that is tricky if the number of elements and the shift width share common prime factors. In the end the approach taken by Lua 5.3 was a lot simpler and as fast, so I used that.