** Let x = AB, where A is a prefix of length 'n'. Then, ** rotate x n == BA. But BA == (A^r . B^r)^r. */
| 205 | ** rotate x n == BA. But BA == (A^r . B^r)^r. |
| 206 | */ |
| 207 | LUA_API void lua_rotate (lua_State *L, int idx, int n) { |
| 208 | StkId p, t, m; |
| 209 | lua_lock(L); |
| 210 | t = L->top - 1; /* end of stack segment being rotated */ |
| 211 | p = index2addr(L, idx); /* start of segment */ |
| 212 | api_checkstackindex(L, idx, p); |
| 213 | api_check(L, (n >= 0 ? n : -n) <= (t - p + 1), "invalid 'n'"); |
| 214 | m = (n >= 0 ? t - n : p - n - 1); /* end of prefix */ |
| 215 | reverse(L, p, m); /* reverse the prefix with length 'n' */ |
| 216 | reverse(L, m + 1, t); /* reverse the suffix */ |
| 217 | reverse(L, p, t); /* reverse the entire segment */ |
| 218 | lua_unlock(L); |
| 219 | } |
| 220 | |
| 221 | |
| 222 | LUA_API void lua_copy (lua_State *L, int fromidx, int toidx) { |
no test coverage detected