diff options
Diffstat (limited to 'ltablib.c')
| -rw-r--r-- | ltablib.c | 378 |
1 files changed, 235 insertions, 143 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: ltablib.c,v 1.79 2014/11/02 19:19:04 roberto Exp $ | 2 | ** $Id: ltablib.c,v 1.90 2015/11/25 12:48:57 roberto Exp $ |
| 3 | ** Library for Table Manipulation | 3 | ** Library for Table Manipulation |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -12,6 +12,7 @@ | |||
| 12 | 12 | ||
| 13 | #include <limits.h> | 13 | #include <limits.h> |
| 14 | #include <stddef.h> | 14 | #include <stddef.h> |
| 15 | #include <string.h> | ||
| 15 | 16 | ||
| 16 | #include "lua.h" | 17 | #include "lua.h" |
| 17 | 18 | ||
| @@ -19,42 +20,44 @@ | |||
| 19 | #include "lualib.h" | 20 | #include "lualib.h" |
| 20 | 21 | ||
| 21 | 22 | ||
| 22 | |||
| 23 | /* | 23 | /* |
| 24 | ** Structure with table-access functions | 24 | ** Operations that an object must define to mimic a table |
| 25 | ** (some functions only need some of them) | ||
| 25 | */ | 26 | */ |
| 26 | typedef struct { | 27 | #define TAB_R 1 /* read */ |
| 27 | int (*geti) (lua_State *L, int idx, lua_Integer n); | 28 | #define TAB_W 2 /* write */ |
| 28 | void (*seti) (lua_State *L, int idx, lua_Integer n); | 29 | #define TAB_L 4 /* length */ |
| 29 | } TabA; | 30 | #define TAB_RW (TAB_R | TAB_W) /* read/write */ |
| 31 | |||
| 32 | |||
| 33 | #define aux_getn(L,n,w) (checktab(L, n, (w) | TAB_L), luaL_len(L, n)) | ||
| 34 | |||
| 35 | |||
| 36 | static int checkfield (lua_State *L, const char *key, int n) { | ||
| 37 | lua_pushstring(L, key); | ||
| 38 | return (lua_rawget(L, -n) != LUA_TNIL); | ||
| 39 | } | ||
| 30 | 40 | ||
| 31 | 41 | ||
| 32 | /* | 42 | /* |
| 33 | ** Check that 'arg' has a table and set access functions in 'ta' to raw | 43 | ** Check that 'arg' either is a table or can behave like one (that is, |
| 34 | ** or non-raw according to the presence of corresponding metamethods. | 44 | ** has a metatable with the required metamethods) |
| 35 | */ | 45 | */ |
| 36 | static void checktab (lua_State *L, int arg, TabA *ta) { | 46 | static void checktab (lua_State *L, int arg, int what) { |
| 37 | ta->geti = NULL; ta->seti = NULL; | 47 | if (lua_type(L, arg) != LUA_TTABLE) { /* is it not a table? */ |
| 38 | if (lua_getmetatable(L, arg)) { | 48 | int n = 1; /* number of elements to pop */ |
| 39 | lua_pushliteral(L, "__index"); /* 'index' metamethod */ | 49 | if (lua_getmetatable(L, arg) && /* must have metatable */ |
| 40 | if (lua_rawget(L, -2) != LUA_TNIL) | 50 | (!(what & TAB_R) || checkfield(L, "__index", ++n)) && |
| 41 | ta->geti = lua_geti; | 51 | (!(what & TAB_W) || checkfield(L, "__newindex", ++n)) && |
| 42 | lua_pushliteral(L, "__newindex"); /* 'newindex' metamethod */ | 52 | (!(what & TAB_L) || checkfield(L, "__len", ++n))) { |
| 43 | if (lua_rawget(L, -3) != LUA_TNIL) | 53 | lua_pop(L, n); /* pop metatable and tested metamethods */ |
| 44 | ta->seti = lua_seti; | 54 | } |
| 45 | lua_pop(L, 3); /* pop metatable plus both metamethods */ | 55 | else |
| 46 | } | 56 | luaL_argerror(L, arg, "table expected"); /* force an error */ |
| 47 | if (ta->geti == NULL || ta->seti == NULL) { | ||
| 48 | luaL_checktype(L, arg, LUA_TTABLE); /* must be table for raw methods */ | ||
| 49 | if (ta->geti == NULL) ta->geti = lua_rawgeti; | ||
| 50 | if (ta->seti == NULL) ta->seti = lua_rawseti; | ||
| 51 | } | 57 | } |
| 52 | } | 58 | } |
| 53 | 59 | ||
| 54 | 60 | ||
| 55 | #define aux_getn(L,n,ta) (checktab(L, n, ta), luaL_len(L, n)) | ||
| 56 | |||
| 57 | |||
| 58 | #if defined(LUA_COMPAT_MAXN) | 61 | #if defined(LUA_COMPAT_MAXN) |
| 59 | static int maxn (lua_State *L) { | 62 | static int maxn (lua_State *L) { |
| 60 | lua_Number max = 0; | 63 | lua_Number max = 0; |
| @@ -74,8 +77,7 @@ static int maxn (lua_State *L) { | |||
| 74 | 77 | ||
| 75 | 78 | ||
| 76 | static int tinsert (lua_State *L) { | 79 | static int tinsert (lua_State *L) { |
| 77 | TabA ta; | 80 | lua_Integer e = aux_getn(L, 1, TAB_RW) + 1; /* first empty element */ |
| 78 | lua_Integer e = aux_getn(L, 1, &ta) + 1; /* first empty element */ | ||
| 79 | lua_Integer pos; /* where to insert new element */ | 81 | lua_Integer pos; /* where to insert new element */ |
| 80 | switch (lua_gettop(L)) { | 82 | switch (lua_gettop(L)) { |
| 81 | case 2: { /* called with only 2 arguments */ | 83 | case 2: { /* called with only 2 arguments */ |
| @@ -87,8 +89,8 @@ static int tinsert (lua_State *L) { | |||
| 87 | pos = luaL_checkinteger(L, 2); /* 2nd argument is the position */ | 89 | pos = luaL_checkinteger(L, 2); /* 2nd argument is the position */ |
| 88 | luaL_argcheck(L, 1 <= pos && pos <= e, 2, "position out of bounds"); | 90 | luaL_argcheck(L, 1 <= pos && pos <= e, 2, "position out of bounds"); |
| 89 | for (i = e; i > pos; i--) { /* move up elements */ | 91 | for (i = e; i > pos; i--) { /* move up elements */ |
| 90 | (*ta.geti)(L, 1, i - 1); | 92 | lua_geti(L, 1, i - 1); |
| 91 | (*ta.seti)(L, 1, i); /* t[i] = t[i - 1] */ | 93 | lua_seti(L, 1, i); /* t[i] = t[i - 1] */ |
| 92 | } | 94 | } |
| 93 | break; | 95 | break; |
| 94 | } | 96 | } |
| @@ -96,55 +98,57 @@ static int tinsert (lua_State *L) { | |||
| 96 | return luaL_error(L, "wrong number of arguments to 'insert'"); | 98 | return luaL_error(L, "wrong number of arguments to 'insert'"); |
| 97 | } | 99 | } |
| 98 | } | 100 | } |
| 99 | (*ta.seti)(L, 1, pos); /* t[pos] = v */ | 101 | lua_seti(L, 1, pos); /* t[pos] = v */ |
| 100 | return 0; | 102 | return 0; |
| 101 | } | 103 | } |
| 102 | 104 | ||
| 103 | 105 | ||
| 104 | static int tremove (lua_State *L) { | 106 | static int tremove (lua_State *L) { |
| 105 | TabA ta; | 107 | lua_Integer size = aux_getn(L, 1, TAB_RW); |
| 106 | lua_Integer size = aux_getn(L, 1, &ta); | ||
| 107 | lua_Integer pos = luaL_optinteger(L, 2, size); | 108 | lua_Integer pos = luaL_optinteger(L, 2, size); |
| 108 | if (pos != size) /* validate 'pos' if given */ | 109 | if (pos != size) /* validate 'pos' if given */ |
| 109 | luaL_argcheck(L, 1 <= pos && pos <= size + 1, 1, "position out of bounds"); | 110 | luaL_argcheck(L, 1 <= pos && pos <= size + 1, 1, "position out of bounds"); |
| 110 | (*ta.geti)(L, 1, pos); /* result = t[pos] */ | 111 | lua_geti(L, 1, pos); /* result = t[pos] */ |
| 111 | for ( ; pos < size; pos++) { | 112 | for ( ; pos < size; pos++) { |
| 112 | (*ta.geti)(L, 1, pos + 1); | 113 | lua_geti(L, 1, pos + 1); |
| 113 | (*ta.seti)(L, 1, pos); /* t[pos] = t[pos + 1] */ | 114 | lua_seti(L, 1, pos); /* t[pos] = t[pos + 1] */ |
| 114 | } | 115 | } |
| 115 | lua_pushnil(L); | 116 | lua_pushnil(L); |
| 116 | (*ta.seti)(L, 1, pos); /* t[pos] = nil */ | 117 | lua_seti(L, 1, pos); /* t[pos] = nil */ |
| 117 | return 1; | 118 | return 1; |
| 118 | } | 119 | } |
| 119 | 120 | ||
| 120 | 121 | ||
| 122 | /* | ||
| 123 | ** Copy elements (1[f], ..., 1[e]) into (tt[t], tt[t+1], ...). Whenever | ||
| 124 | ** possible, copy in increasing order, which is better for rehashing. | ||
| 125 | ** "possible" means destination after original range, or smaller | ||
| 126 | ** than origin, or copying to another table. | ||
| 127 | */ | ||
| 121 | static int tmove (lua_State *L) { | 128 | static int tmove (lua_State *L) { |
| 122 | TabA ta; | ||
| 123 | lua_Integer f = luaL_checkinteger(L, 2); | 129 | lua_Integer f = luaL_checkinteger(L, 2); |
| 124 | lua_Integer e = luaL_checkinteger(L, 3); | 130 | lua_Integer e = luaL_checkinteger(L, 3); |
| 125 | lua_Integer t = luaL_checkinteger(L, 4); | 131 | lua_Integer t = luaL_checkinteger(L, 4); |
| 126 | int tt = !lua_isnoneornil(L, 5) ? 5 : 1; /* destination table */ | 132 | int tt = !lua_isnoneornil(L, 5) ? 5 : 1; /* destination table */ |
| 127 | /* the following restriction avoids several problems with overflows */ | 133 | checktab(L, 1, TAB_R); |
| 128 | luaL_argcheck(L, f > 0, 2, "initial position must be positive"); | 134 | checktab(L, tt, TAB_W); |
| 129 | if (e >= f) { /* otherwise, nothing to move */ | 135 | if (e >= f) { /* otherwise, nothing to move */ |
| 130 | lua_Integer n, i; | 136 | lua_Integer n, i; |
| 131 | ta.geti = (luaL_getmetafield(L, 1, "__index") == LUA_TNIL) | 137 | luaL_argcheck(L, f > 0 || e < LUA_MAXINTEGER + f, 3, |
| 132 | ? (luaL_checktype(L, 1, LUA_TTABLE), lua_rawgeti) | 138 | "too many elements to move"); |
| 133 | : lua_geti; | ||
| 134 | ta.seti = (luaL_getmetafield(L, tt, "__newindex") == LUA_TNIL) | ||
| 135 | ? (luaL_checktype(L, tt, LUA_TTABLE), lua_rawseti) | ||
| 136 | : lua_seti; | ||
| 137 | n = e - f + 1; /* number of elements to move */ | 139 | n = e - f + 1; /* number of elements to move */ |
| 138 | if (t > f) { | 140 | luaL_argcheck(L, t <= LUA_MAXINTEGER - n + 1, 4, |
| 139 | for (i = n - 1; i >= 0; i--) { | 141 | "destination wrap around"); |
| 140 | (*ta.geti)(L, 1, f + i); | 142 | if (t > e || t <= f || tt != 1) { |
| 141 | (*ta.seti)(L, tt, t + i); | 143 | for (i = 0; i < n; i++) { |
| 144 | lua_geti(L, 1, f + i); | ||
| 145 | lua_seti(L, tt, t + i); | ||
| 142 | } | 146 | } |
| 143 | } | 147 | } |
| 144 | else { | 148 | else { |
| 145 | for (i = 0; i < n; i++) { | 149 | for (i = n - 1; i >= 0; i--) { |
| 146 | (*ta.geti)(L, 1, f + i); | 150 | lua_geti(L, 1, f + i); |
| 147 | (*ta.seti)(L, tt, t + i); | 151 | lua_seti(L, tt, t + i); |
| 148 | } | 152 | } |
| 149 | } | 153 | } |
| 150 | } | 154 | } |
| @@ -153,8 +157,8 @@ static int tmove (lua_State *L) { | |||
| 153 | } | 157 | } |
| 154 | 158 | ||
| 155 | 159 | ||
| 156 | static void addfield (lua_State *L, luaL_Buffer *b, TabA *ta, lua_Integer i) { | 160 | static void addfield (lua_State *L, luaL_Buffer *b, lua_Integer i) { |
| 157 | (*ta->geti)(L, 1, i); | 161 | lua_geti(L, 1, i); |
| 158 | if (!lua_isstring(L, -1)) | 162 | if (!lua_isstring(L, -1)) |
| 159 | luaL_error(L, "invalid value (%s) at index %d in table for 'concat'", | 163 | luaL_error(L, "invalid value (%s) at index %d in table for 'concat'", |
| 160 | luaL_typename(L, -1), i); | 164 | luaL_typename(L, -1), i); |
| @@ -163,21 +167,19 @@ static void addfield (lua_State *L, luaL_Buffer *b, TabA *ta, lua_Integer i) { | |||
| 163 | 167 | ||
| 164 | 168 | ||
| 165 | static int tconcat (lua_State *L) { | 169 | static int tconcat (lua_State *L) { |
| 166 | TabA ta; | ||
| 167 | luaL_Buffer b; | 170 | luaL_Buffer b; |
| 171 | lua_Integer last = aux_getn(L, 1, TAB_R); | ||
| 168 | size_t lsep; | 172 | size_t lsep; |
| 169 | lua_Integer i, last; | ||
| 170 | const char *sep = luaL_optlstring(L, 2, "", &lsep); | 173 | const char *sep = luaL_optlstring(L, 2, "", &lsep); |
| 171 | checktab(L, 1, &ta); | 174 | lua_Integer i = luaL_optinteger(L, 3, 1); |
| 172 | i = luaL_optinteger(L, 3, 1); | 175 | last = luaL_opt(L, luaL_checkinteger, 4, last); |
| 173 | last = luaL_opt(L, luaL_checkinteger, 4, luaL_len(L, 1)); | ||
| 174 | luaL_buffinit(L, &b); | 176 | luaL_buffinit(L, &b); |
| 175 | for (; i < last; i++) { | 177 | for (; i < last; i++) { |
| 176 | addfield(L, &b, &ta, i); | 178 | addfield(L, &b, i); |
| 177 | luaL_addlstring(&b, sep, lsep); | 179 | luaL_addlstring(&b, sep, lsep); |
| 178 | } | 180 | } |
| 179 | if (i == last) /* add last value (if interval was not empty) */ | 181 | if (i == last) /* add last value (if interval was not empty) */ |
| 180 | addfield(L, &b, &ta, i); | 182 | addfield(L, &b, i); |
| 181 | luaL_pushresult(&b); | 183 | luaL_pushresult(&b); |
| 182 | return 1; | 184 | return 1; |
| 183 | } | 185 | } |
| @@ -195,7 +197,7 @@ static int pack (lua_State *L) { | |||
| 195 | lua_createtable(L, n, 1); /* create result table */ | 197 | lua_createtable(L, n, 1); /* create result table */ |
| 196 | lua_insert(L, 1); /* put it at index 1 */ | 198 | lua_insert(L, 1); /* put it at index 1 */ |
| 197 | for (i = n; i >= 1; i--) /* assign elements */ | 199 | for (i = n; i >= 1; i--) /* assign elements */ |
| 198 | lua_rawseti(L, 1, i); | 200 | lua_seti(L, 1, i); |
| 199 | lua_pushinteger(L, n); | 201 | lua_pushinteger(L, n); |
| 200 | lua_setfield(L, 1, "n"); /* t.n = number of elements */ | 202 | lua_setfield(L, 1, "n"); /* t.n = number of elements */ |
| 201 | return 1; /* return table */ | 203 | return 1; /* return table */ |
| @@ -203,20 +205,17 @@ static int pack (lua_State *L) { | |||
| 203 | 205 | ||
| 204 | 206 | ||
| 205 | static int unpack (lua_State *L) { | 207 | static int unpack (lua_State *L) { |
| 206 | TabA ta; | ||
| 207 | lua_Integer i, e; | ||
| 208 | lua_Unsigned n; | 208 | lua_Unsigned n; |
| 209 | checktab(L, 1, &ta); | 209 | lua_Integer i = luaL_optinteger(L, 2, 1); |
| 210 | i = luaL_optinteger(L, 2, 1); | 210 | lua_Integer e = luaL_opt(L, luaL_checkinteger, 3, luaL_len(L, 1)); |
| 211 | e = luaL_opt(L, luaL_checkinteger, 3, luaL_len(L, 1)); | ||
| 212 | if (i > e) return 0; /* empty range */ | 211 | if (i > e) return 0; /* empty range */ |
| 213 | n = (lua_Unsigned)e - i; /* number of elements minus 1 (avoid overflows) */ | 212 | n = (lua_Unsigned)e - i; /* number of elements minus 1 (avoid overflows) */ |
| 214 | if (n >= (unsigned int)INT_MAX || !lua_checkstack(L, (int)(++n))) | 213 | if (n >= (unsigned int)INT_MAX || !lua_checkstack(L, (int)(++n))) |
| 215 | return luaL_error(L, "too many results to unpack"); | 214 | return luaL_error(L, "too many results to unpack"); |
| 216 | do { /* must have at least one element */ | 215 | for (; i < e; i++) { /* push arg[i..e - 1] (to avoid overflows) */ |
| 217 | (*ta.geti)(L, 1, i); /* push arg[i..e] */ | 216 | lua_geti(L, 1, i); |
| 218 | } while (i++ < e); | 217 | } |
| 219 | 218 | lua_geti(L, 1, e); /* push last element */ | |
| 220 | return (int)n; | 219 | return (int)n; |
| 221 | } | 220 | } |
| 222 | 221 | ||
| @@ -233,97 +232,190 @@ static int unpack (lua_State *L) { | |||
| 233 | */ | 232 | */ |
| 234 | 233 | ||
| 235 | 234 | ||
| 236 | static void set2 (lua_State *L, TabA *ta, int i, int j) { | 235 | /* |
| 237 | (*ta->seti)(L, 1, i); | 236 | ** Produce a "random" 'unsigned int' to randomize pivot choice. This |
| 238 | (*ta->seti)(L, 1, j); | 237 | ** macro is used only when 'sort' detects a big imbalance in the result |
| 238 | ** of a partition. (If you don't want/need this "randomness", ~0 is a | ||
| 239 | ** good choice.) | ||
| 240 | */ | ||
| 241 | #if !defined(l_randomizePivot) /* { */ | ||
| 242 | |||
| 243 | #include <time.h> | ||
| 244 | |||
| 245 | /* size of 'e' measured in number of 'unsigned int's */ | ||
| 246 | #define sof(e) (sizeof(e) / sizeof(unsigned int)) | ||
| 247 | |||
| 248 | /* | ||
| 249 | ** Use 'time' and 'clock' as sources of "randomness". Because we don't | ||
| 250 | ** know the types 'clock_t' and 'time_t', we cannot cast them to | ||
| 251 | ** anything without risking overflows. A safe way to use their values | ||
| 252 | ** is to copy them to an array of a known type and use the array values. | ||
| 253 | */ | ||
| 254 | static unsigned int l_randomizePivot (void) { | ||
| 255 | clock_t c = clock(); | ||
| 256 | time_t t = time(NULL); | ||
| 257 | unsigned int buff[sof(c) + sof(t)]; | ||
| 258 | unsigned int i, rnd = 0; | ||
| 259 | memcpy(buff, &c, sof(c) * sizeof(unsigned int)); | ||
| 260 | memcpy(buff + sof(c), &t, sof(t) * sizeof(unsigned int)); | ||
| 261 | for (i = 0; i < sof(buff); i++) | ||
| 262 | rnd += buff[i]; | ||
| 263 | return rnd; | ||
| 264 | } | ||
| 265 | |||
| 266 | #endif /* } */ | ||
| 267 | |||
| 268 | |||
| 269 | /* arrays larger than 'RANLIMIT' may use randomized pivots */ | ||
| 270 | #define RANLIMIT 100u | ||
| 271 | |||
| 272 | |||
| 273 | static void set2 (lua_State *L, unsigned int i, unsigned int j) { | ||
| 274 | lua_seti(L, 1, i); | ||
| 275 | lua_seti(L, 1, j); | ||
| 239 | } | 276 | } |
| 240 | 277 | ||
| 278 | |||
| 279 | /* | ||
| 280 | ** Return true iff value at stack index 'a' is less than the value at | ||
| 281 | ** index 'b' (according to the order of the sort). | ||
| 282 | */ | ||
| 241 | static int sort_comp (lua_State *L, int a, int b) { | 283 | static int sort_comp (lua_State *L, int a, int b) { |
| 242 | if (!lua_isnil(L, 2)) { /* function? */ | 284 | if (lua_isnil(L, 2)) /* no function? */ |
| 285 | return lua_compare(L, a, b, LUA_OPLT); /* a < b */ | ||
| 286 | else { /* function */ | ||
| 243 | int res; | 287 | int res; |
| 244 | lua_pushvalue(L, 2); | 288 | lua_pushvalue(L, 2); /* push function */ |
| 245 | lua_pushvalue(L, a-1); /* -1 to compensate function */ | 289 | lua_pushvalue(L, a-1); /* -1 to compensate function */ |
| 246 | lua_pushvalue(L, b-2); /* -2 to compensate function and 'a' */ | 290 | lua_pushvalue(L, b-2); /* -2 to compensate function and 'a' */ |
| 247 | lua_call(L, 2, 1); | 291 | lua_call(L, 2, 1); /* call function */ |
| 248 | res = lua_toboolean(L, -1); | 292 | res = lua_toboolean(L, -1); /* get result */ |
| 249 | lua_pop(L, 1); | 293 | lua_pop(L, 1); /* pop result */ |
| 250 | return res; | 294 | return res; |
| 251 | } | 295 | } |
| 252 | else /* a < b? */ | ||
| 253 | return lua_compare(L, a, b, LUA_OPLT); | ||
| 254 | } | 296 | } |
| 255 | 297 | ||
| 256 | static void auxsort (lua_State *L, TabA *ta, int l, int u) { | 298 | |
| 257 | while (l < u) { /* for tail recursion */ | 299 | /* |
| 258 | int i, j; | 300 | ** Does the partition: Pivot P is at the top of the stack. |
| 259 | /* sort elements a[l], a[(l+u)/2] and a[u] */ | 301 | ** precondition: a[lo] <= P == a[up-1] <= a[up], |
| 260 | (*ta->geti)(L, 1, l); | 302 | ** so it only needs to do the partition from lo + 1 to up - 2. |
| 261 | (*ta->geti)(L, 1, u); | 303 | ** Pos-condition: a[lo .. i - 1] <= a[i] == P <= a[i + 1 .. up] |
| 262 | if (sort_comp(L, -1, -2)) /* a[u] < a[l]? */ | 304 | ** returns 'i'. |
| 263 | set2(L, ta, l, u); /* swap a[l] - a[u] */ | 305 | */ |
| 306 | static unsigned int partition (lua_State *L, unsigned int lo, | ||
| 307 | unsigned int up) { | ||
| 308 | unsigned int i = lo; /* will be incremented before first use */ | ||
| 309 | unsigned int j = up - 1; /* will be decremented before first use */ | ||
| 310 | /* loop invariant: a[lo .. i] <= P <= a[j .. up] */ | ||
| 311 | for (;;) { | ||
| 312 | /* next loop: repeat ++i while a[i] < P */ | ||
| 313 | while (lua_geti(L, 1, ++i), sort_comp(L, -1, -2)) { | ||
| 314 | if (i == up - 1) /* a[i] < P but a[up - 1] == P ?? */ | ||
| 315 | luaL_error(L, "invalid order function for sorting"); | ||
| 316 | lua_pop(L, 1); /* remove a[i] */ | ||
| 317 | } | ||
| 318 | /* after the loop, a[i] >= P and a[lo .. i - 1] < P */ | ||
| 319 | /* next loop: repeat --j while P < a[j] */ | ||
| 320 | while (lua_geti(L, 1, --j), sort_comp(L, -3, -1)) { | ||
| 321 | if (j < i) /* j < i but a[j] > P ?? */ | ||
| 322 | luaL_error(L, "invalid order function for sorting"); | ||
| 323 | lua_pop(L, 1); /* remove a[j] */ | ||
| 324 | } | ||
| 325 | /* after the loop, a[j] <= P and a[j + 1 .. up] >= P */ | ||
| 326 | if (j < i) { /* no elements out of place? */ | ||
| 327 | /* a[lo .. i - 1] <= P <= a[j + 1 .. i .. up] */ | ||
| 328 | lua_pop(L, 1); /* pop a[j] */ | ||
| 329 | /* swap pivot (a[up - 1]) with a[i] to satisfy pos-condition */ | ||
| 330 | set2(L, up - 1, i); | ||
| 331 | return i; | ||
| 332 | } | ||
| 333 | /* otherwise, swap a[i] - a[j] to restore invariant and repeat */ | ||
| 334 | set2(L, i, j); | ||
| 335 | } | ||
| 336 | } | ||
| 337 | |||
| 338 | |||
| 339 | /* | ||
| 340 | ** Choose an element in the middle (2nd-3th quarters) of [lo,up] | ||
| 341 | ** "randomized" by 'rnd' | ||
| 342 | */ | ||
| 343 | static unsigned int choosePivot (unsigned int lo, unsigned int up, | ||
| 344 | unsigned int rnd) { | ||
| 345 | unsigned int r4 = (unsigned int)(up - lo) / 4u; /* range/4 */ | ||
| 346 | unsigned int p = rnd % (r4 * 2) + (lo + r4); | ||
| 347 | lua_assert(lo + r4 <= p && p <= up - r4); | ||
| 348 | return p; | ||
| 349 | } | ||
| 350 | |||
| 351 | |||
| 352 | /* | ||
| 353 | ** QuickSort algorithm (recursive function) | ||
| 354 | */ | ||
| 355 | static void auxsort (lua_State *L, unsigned int lo, unsigned int up, | ||
| 356 | unsigned int rnd) { | ||
| 357 | while (lo < up) { /* loop for tail recursion */ | ||
| 358 | unsigned int p; /* Pivot index */ | ||
| 359 | unsigned int n; /* to be used later */ | ||
| 360 | /* sort elements 'lo', 'p', and 'up' */ | ||
| 361 | lua_geti(L, 1, lo); | ||
| 362 | lua_geti(L, 1, up); | ||
| 363 | if (sort_comp(L, -1, -2)) /* a[up] < a[lo]? */ | ||
| 364 | set2(L, lo, up); /* swap a[lo] - a[up] */ | ||
| 264 | else | 365 | else |
| 265 | lua_pop(L, 2); | 366 | lua_pop(L, 2); /* remove both values */ |
| 266 | if (u-l == 1) break; /* only 2 elements */ | 367 | if (up - lo == 1) /* only 2 elements? */ |
| 267 | i = (l+u)/2; | 368 | return; /* already sorted */ |
| 268 | (*ta->geti)(L, 1, i); | 369 | if (up - lo < RANLIMIT || rnd == 0) /* small interval or no randomize? */ |
| 269 | (*ta->geti)(L, 1, l); | 370 | p = (lo + up)/2; /* middle element is a good pivot */ |
| 270 | if (sort_comp(L, -2, -1)) /* a[i]<a[l]? */ | 371 | else /* for larger intervals, it is worth a random pivot */ |
| 271 | set2(L, ta, i, l); | 372 | p = choosePivot(lo, up, rnd); |
| 373 | lua_geti(L, 1, p); | ||
| 374 | lua_geti(L, 1, lo); | ||
| 375 | if (sort_comp(L, -2, -1)) /* a[p] < a[lo]? */ | ||
| 376 | set2(L, p, lo); /* swap a[p] - a[lo] */ | ||
| 272 | else { | 377 | else { |
| 273 | lua_pop(L, 1); /* remove a[l] */ | 378 | lua_pop(L, 1); /* remove a[lo] */ |
| 274 | (*ta->geti)(L, 1, u); | 379 | lua_geti(L, 1, up); |
| 275 | if (sort_comp(L, -1, -2)) /* a[u]<a[i]? */ | 380 | if (sort_comp(L, -1, -2)) /* a[up] < a[p]? */ |
| 276 | set2(L, ta, i, u); | 381 | set2(L, p, up); /* swap a[up] - a[p] */ |
| 277 | else | 382 | else |
| 278 | lua_pop(L, 2); | 383 | lua_pop(L, 2); |
| 279 | } | 384 | } |
| 280 | if (u-l == 2) break; /* only 3 elements */ | 385 | if (up - lo == 2) /* only 3 elements? */ |
| 281 | (*ta->geti)(L, 1, i); /* Pivot */ | 386 | return; /* already sorted */ |
| 282 | lua_pushvalue(L, -1); | 387 | lua_geti(L, 1, p); /* get middle element (Pivot) */ |
| 283 | (*ta->geti)(L, 1, u-1); | 388 | lua_pushvalue(L, -1); /* push Pivot */ |
| 284 | set2(L, ta, i, u-1); | 389 | lua_geti(L, 1, up - 1); /* push a[up - 1] */ |
| 285 | /* a[l] <= P == a[u-1] <= a[u], only need to sort from l+1 to u-2 */ | 390 | set2(L, p, up - 1); /* swap Pivot (a[p]) with a[up - 1] */ |
| 286 | i = l; j = u-1; | 391 | p = partition(L, lo, up); |
| 287 | for (;;) { /* invariant: a[l..i] <= P <= a[j..u] */ | 392 | /* a[lo .. p - 1] <= a[p] == P <= a[p + 1 .. up] */ |
| 288 | /* repeat ++i until a[i] >= P */ | 393 | if (p - lo < up - p) { /* lower interval is smaller? */ |
| 289 | while ((*ta->geti)(L, 1, ++i), sort_comp(L, -1, -2)) { | 394 | auxsort(L, lo, p - 1, rnd); /* call recursively for lower interval */ |
| 290 | if (i>=u) luaL_error(L, "invalid order function for sorting"); | 395 | n = p - lo; /* size of smaller interval */ |
| 291 | lua_pop(L, 1); /* remove a[i] */ | 396 | lo = p + 1; /* tail call for [p + 1 .. up] (upper interval) */ |
| 292 | } | ||
| 293 | /* repeat --j until a[j] <= P */ | ||
| 294 | while ((*ta->geti)(L, 1, --j), sort_comp(L, -3, -1)) { | ||
| 295 | if (j<=l) luaL_error(L, "invalid order function for sorting"); | ||
| 296 | lua_pop(L, 1); /* remove a[j] */ | ||
| 297 | } | ||
| 298 | if (j<i) { | ||
| 299 | lua_pop(L, 3); /* pop pivot, a[i], a[j] */ | ||
| 300 | break; | ||
| 301 | } | ||
| 302 | set2(L, ta, i, j); | ||
| 303 | } | ||
| 304 | (*ta->geti)(L, 1, u-1); | ||
| 305 | (*ta->geti)(L, 1, i); | ||
| 306 | set2(L, ta, u-1, i); /* swap pivot (a[u-1]) with a[i] */ | ||
| 307 | /* a[l..i-1] <= a[i] == P <= a[i+1..u] */ | ||
| 308 | /* adjust so that smaller half is in [j..i] and larger one in [l..u] */ | ||
| 309 | if (i-l < u-i) { | ||
| 310 | j=l; i=i-1; l=i+2; | ||
| 311 | } | 397 | } |
| 312 | else { | 398 | else { |
| 313 | j=i+1; i=u; u=j-2; | 399 | auxsort(L, p + 1, up, rnd); /* call recursively for upper interval */ |
| 400 | n = up - p; /* size of smaller interval */ | ||
| 401 | up = p - 1; /* tail call for [lo .. p - 1] (lower interval) */ | ||
| 314 | } | 402 | } |
| 315 | auxsort(L, ta, j, i); /* call recursively the smaller one */ | 403 | if ((up - lo) / 128u > n) /* partition too imbalanced? */ |
| 316 | } /* repeat the routine for the larger one */ | 404 | rnd = l_randomizePivot(); /* try a new randomization */ |
| 405 | } /* tail call auxsort(L, lo, up, rnd) */ | ||
| 317 | } | 406 | } |
| 318 | 407 | ||
| 408 | |||
| 319 | static int sort (lua_State *L) { | 409 | static int sort (lua_State *L) { |
| 320 | TabA ta; | 410 | lua_Integer n = aux_getn(L, 1, TAB_RW); |
| 321 | int n = (int)aux_getn(L, 1, &ta); | 411 | if (n > 1) { /* non-trivial interval? */ |
| 322 | luaL_checkstack(L, 50, ""); /* assume array is smaller than 2^50 */ | 412 | luaL_argcheck(L, n < INT_MAX, 1, "array too big"); |
| 323 | if (!lua_isnoneornil(L, 2)) /* is there a 2nd argument? */ | 413 | luaL_checkstack(L, 40, ""); /* assume array is smaller than 2^40 */ |
| 324 | luaL_checktype(L, 2, LUA_TFUNCTION); | 414 | if (!lua_isnoneornil(L, 2)) /* is there a 2nd argument? */ |
| 325 | lua_settop(L, 2); /* make sure there are two arguments */ | 415 | luaL_checktype(L, 2, LUA_TFUNCTION); /* must be a function */ |
| 326 | auxsort(L, &ta, 1, n); | 416 | lua_settop(L, 2); /* make sure there are two arguments */ |
| 417 | auxsort(L, 1, (unsigned int)n, 0u); | ||
| 418 | } | ||
| 327 | return 0; | 419 | return 0; |
| 328 | } | 420 | } |
| 329 | 421 | ||
