1 /* $NetBSD: ltablib.c,v 1.2 2014/07/19 18:38:34 lneto Exp $ */ 2 3 /* 4 ** $Id: ltablib.c,v 1.2 2014/07/19 18:38:34 lneto Exp $ 5 ** Library for Table Manipulation 6 ** See Copyright Notice in lua.h 7 */ 8 9 10 #ifndef _KERNEL 11 #include <limits.h> 12 #include <stddef.h> 13 #endif 14 15 #define ltablib_c 16 #define LUA_LIB 17 18 #include "lua.h" 19 20 #include "lauxlib.h" 21 #include "lualib.h" 22 23 24 #define aux_getn(L,n) (luaL_checktype(L, n, LUA_TTABLE), luaL_len(L, n)) 25 26 27 28 #if defined(LUA_COMPAT_MAXN) 29 static int maxn (lua_State *L) { 30 lua_Number max = 0; 31 luaL_checktype(L, 1, LUA_TTABLE); 32 lua_pushnil(L); /* first key */ 33 while (lua_next(L, 1)) { 34 lua_pop(L, 1); /* remove value */ 35 if (lua_type(L, -1) == LUA_TNUMBER) { 36 lua_Number v = lua_tonumber(L, -1); 37 if (v > max) max = v; 38 } 39 } 40 lua_pushnumber(L, max); 41 return 1; 42 } 43 #endif 44 45 46 static int tinsert (lua_State *L) { 47 lua_Integer e = aux_getn(L, 1) + 1; /* first empty element */ 48 lua_Integer pos; /* where to insert new element */ 49 switch (lua_gettop(L)) { 50 case 2: { /* called with only 2 arguments */ 51 pos = e; /* insert new element at the end */ 52 break; 53 } 54 case 3: { 55 lua_Integer i; 56 pos = luaL_checkinteger(L, 2); /* 2nd argument is the position */ 57 luaL_argcheck(L, 1 <= pos && pos <= e, 2, "position out of bounds"); 58 for (i = e; i > pos; i--) { /* move up elements */ 59 lua_rawgeti(L, 1, i-1); 60 lua_rawseti(L, 1, i); /* t[i] = t[i-1] */ 61 } 62 break; 63 } 64 default: { 65 return luaL_error(L, "wrong number of arguments to " LUA_QL("insert")); 66 } 67 } 68 lua_rawseti(L, 1, pos); /* t[pos] = v */ 69 return 0; 70 } 71 72 73 static int tremove (lua_State *L) { 74 lua_Integer size = aux_getn(L, 1); 75 lua_Integer pos = luaL_optinteger(L, 2, size); 76 if (pos != size) /* validate 'pos' if given */ 77 luaL_argcheck(L, 1 <= pos && pos <= size + 1, 1, "position out of bounds"); 78 lua_rawgeti(L, 1, pos); /* result = t[pos] */ 79 for ( ; pos < size; pos++) { 80 lua_rawgeti(L, 1, pos+1); 81 lua_rawseti(L, 1, pos); /* t[pos] = t[pos+1] */ 82 } 83 lua_pushnil(L); 84 lua_rawseti(L, 1, pos); /* t[pos] = nil */ 85 return 1; 86 } 87 88 89 static void addfield (lua_State *L, luaL_Buffer *b, lua_Integer i) { 90 lua_rawgeti(L, 1, i); 91 if (!lua_isstring(L, -1)) 92 luaL_error(L, "invalid value (%s) at index %d in table for " 93 LUA_QL("concat"), luaL_typename(L, -1), i); 94 luaL_addvalue(b); 95 } 96 97 98 static int tconcat (lua_State *L) { 99 luaL_Buffer b; 100 size_t lsep; 101 lua_Integer i, last; 102 const char *sep = luaL_optlstring(L, 2, "", &lsep); 103 luaL_checktype(L, 1, LUA_TTABLE); 104 i = luaL_optinteger(L, 3, 1); 105 last = luaL_opt(L, luaL_checkinteger, 4, luaL_len(L, 1)); 106 luaL_buffinit(L, &b); 107 for (; i < last; i++) { 108 addfield(L, &b, i); 109 luaL_addlstring(&b, sep, lsep); 110 } 111 if (i == last) /* add last value (if interval was not empty) */ 112 addfield(L, &b, i); 113 luaL_pushresult(&b); 114 return 1; 115 } 116 117 118 /* 119 ** {====================================================== 120 ** Pack/unpack 121 ** ======================================================= 122 */ 123 124 static int pack (lua_State *L) { 125 int i; 126 int n = lua_gettop(L); /* number of elements to pack */ 127 lua_createtable(L, n, 1); /* create result table */ 128 lua_insert(L, 1); /* put it at index 1 */ 129 for (i = n; i >= 1; i--) /* assign elements */ 130 lua_rawseti(L, 1, i); 131 lua_pushinteger(L, n); 132 lua_setfield(L, 1, "n"); /* t.n = number of elements */ 133 return 1; /* return table */ 134 } 135 136 137 static int unpack (lua_State *L) { 138 lua_Integer i, e; 139 lua_Unsigned n; 140 luaL_checktype(L, 1, LUA_TTABLE); 141 i = luaL_optinteger(L, 2, 1); 142 e = luaL_opt(L, luaL_checkinteger, 3, luaL_len(L, 1)); 143 if (i > e) return 0; /* empty range */ 144 n = (lua_Unsigned)e - i; /* number of elements minus 1 (avoid overflows) */ 145 if (n >= (unsigned int)INT_MAX || !lua_checkstack(L, ++n)) 146 return luaL_error(L, "too many results to unpack"); 147 do { /* must have at least one element */ 148 lua_rawgeti(L, 1, i); /* push arg[i..e] */ 149 } while (i++ < e); 150 151 return n; 152 } 153 154 /* }====================================================== */ 155 156 157 158 /* 159 ** {====================================================== 160 ** Quicksort 161 ** (based on `Algorithms in MODULA-3', Robert Sedgewick; 162 ** Addison-Wesley, 1993.) 163 ** ======================================================= 164 */ 165 166 167 static void set2 (lua_State *L, int i, int j) { 168 lua_rawseti(L, 1, i); 169 lua_rawseti(L, 1, j); 170 } 171 172 static int sort_comp (lua_State *L, int a, int b) { 173 if (!lua_isnil(L, 2)) { /* function? */ 174 int res; 175 lua_pushvalue(L, 2); 176 lua_pushvalue(L, a-1); /* -1 to compensate function */ 177 lua_pushvalue(L, b-2); /* -2 to compensate function and `a' */ 178 lua_call(L, 2, 1); 179 res = lua_toboolean(L, -1); 180 lua_pop(L, 1); 181 return res; 182 } 183 else /* a < b? */ 184 return lua_compare(L, a, b, LUA_OPLT); 185 } 186 187 static void auxsort (lua_State *L, int l, int u) { 188 while (l < u) { /* for tail recursion */ 189 int i, j; 190 /* sort elements a[l], a[(l+u)/2] and a[u] */ 191 lua_rawgeti(L, 1, l); 192 lua_rawgeti(L, 1, u); 193 if (sort_comp(L, -1, -2)) /* a[u] < a[l]? */ 194 set2(L, l, u); /* swap a[l] - a[u] */ 195 else 196 lua_pop(L, 2); 197 if (u-l == 1) break; /* only 2 elements */ 198 i = (l+u)/2; 199 lua_rawgeti(L, 1, i); 200 lua_rawgeti(L, 1, l); 201 if (sort_comp(L, -2, -1)) /* a[i]<a[l]? */ 202 set2(L, i, l); 203 else { 204 lua_pop(L, 1); /* remove a[l] */ 205 lua_rawgeti(L, 1, u); 206 if (sort_comp(L, -1, -2)) /* a[u]<a[i]? */ 207 set2(L, i, u); 208 else 209 lua_pop(L, 2); 210 } 211 if (u-l == 2) break; /* only 3 elements */ 212 lua_rawgeti(L, 1, i); /* Pivot */ 213 lua_pushvalue(L, -1); 214 lua_rawgeti(L, 1, u-1); 215 set2(L, i, u-1); 216 /* a[l] <= P == a[u-1] <= a[u], only need to sort from l+1 to u-2 */ 217 i = l; j = u-1; 218 for (;;) { /* invariant: a[l..i] <= P <= a[j..u] */ 219 /* repeat ++i until a[i] >= P */ 220 while (lua_rawgeti(L, 1, ++i), sort_comp(L, -1, -2)) { 221 if (i>=u) luaL_error(L, "invalid order function for sorting"); 222 lua_pop(L, 1); /* remove a[i] */ 223 } 224 /* repeat --j until a[j] <= P */ 225 while (lua_rawgeti(L, 1, --j), sort_comp(L, -3, -1)) { 226 if (j<=l) luaL_error(L, "invalid order function for sorting"); 227 lua_pop(L, 1); /* remove a[j] */ 228 } 229 if (j<i) { 230 lua_pop(L, 3); /* pop pivot, a[i], a[j] */ 231 break; 232 } 233 set2(L, i, j); 234 } 235 lua_rawgeti(L, 1, u-1); 236 lua_rawgeti(L, 1, i); 237 set2(L, u-1, i); /* swap pivot (a[u-1]) with a[i] */ 238 /* a[l..i-1] <= a[i] == P <= a[i+1..u] */ 239 /* adjust so that smaller half is in [j..i] and larger one in [l..u] */ 240 if (i-l < u-i) { 241 j=l; i=i-1; l=i+2; 242 } 243 else { 244 j=i+1; i=u; u=j-2; 245 } 246 auxsort(L, j, i); /* call recursively the smaller one */ 247 } /* repeat the routine for the larger one */ 248 } 249 250 static int sort (lua_State *L) { 251 int n = aux_getn(L, 1); 252 luaL_checkstack(L, 40, ""); /* assume array is smaller than 2^40 */ 253 if (!lua_isnoneornil(L, 2)) /* is there a 2nd argument? */ 254 luaL_checktype(L, 2, LUA_TFUNCTION); 255 lua_settop(L, 2); /* make sure there is two arguments */ 256 auxsort(L, 1, n); 257 return 0; 258 } 259 260 /* }====================================================== */ 261 262 263 static const luaL_Reg tab_funcs[] = { 264 {"concat", tconcat}, 265 #if defined(LUA_COMPAT_MAXN) 266 {"maxn", maxn}, 267 #endif 268 {"insert", tinsert}, 269 {"pack", pack}, 270 {"unpack", unpack}, 271 {"remove", tremove}, 272 {"sort", sort}, 273 {NULL, NULL} 274 }; 275 276 277 LUAMOD_API int luaopen_table (lua_State *L) { 278 luaL_newlib(L, tab_funcs); 279 #if defined(LUA_COMPAT_UNPACK) 280 /* _G.unpack = table.unpack */ 281 lua_getfield(L, -1, "unpack"); 282 lua_setglobal(L, "unpack"); 283 #endif 284 return 1; 285 } 286 287