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