Exemple #1
0
static int tinsert (lv_State *L) {
    lv_clearFirstTableValue(L);
    int e = aux_getn(L, 1) + 1;  /* first empty element */
    int pos;  /* where to insert new element */
    switch (lv_gettop(L)) {
    case 2: {  /* called with only 2 arguments */
        pos = e;  /* insert new element at the end */
        break;
    }
    case 3: {
        int i;
        pos = lvL_checkint(L, 2);  /* 2nd argument is the position */
        if (pos > e) e = pos;  /* `grow' array if necessary */
        for (i = e; i > pos; i--) {  /* move up elements */
            lv_rawgeti(L, 1, i-1);
            lv_rawseti(L, 1, i);  /* t[i] = t[i-1] */
        }
        break;
    }
    default: {
        return lvL_error(L, "wrong number of arguments to " LV_QL("insert"));
    }
    }
    lvL_setn(L, 1, e);  /* new size */
    lv_rawseti(L, 1, pos);  /* t[pos] = v */
    return 0;
}
Exemple #2
0
static void addfield (lv_State *L, lvL_Buffer *b, int i) {
    lv_rawgeti(L, 1, i);
    if (!lv_isstring(L, -1))
        lvL_error(L, "invalid value (%s) at index %d in table for "
                  LV_QL("concat"), lvL_typename(L, -1), i);
    lvL_addvalue(b);
}
Exemple #3
0
static int db_setfenv (lv_State *L) {
    lvL_checktype(L, 2, LV_TTABLE);
    lv_settop(L, 2);
    if (lv_setfenv(L, 1) == 0)
        lvL_error(L, LV_QL("setfenv")
                  " cannot change environment of given object");
    return 1;
}
Exemple #4
0
static int setn (lv_State *L) {
    lv_clearFirstTableValue(L);
    lvL_checktype(L, 1, LV_TTABLE);
#ifndef lvL_setn
    lvL_setn(L, 1, lvL_checkint(L, 2));
#else
    lvL_error(L, LV_QL("setn") " is obsolete");
#endif
    lv_pushvalue(L, 1);
    return 1;
}
Exemple #5
0
static int getfield (lv_State *L, const char *key, int d) {
  int res;
  lv_getfield(L, -1, key);
  if (lv_isnumber(L, -1))
    res = (int)lv_tointeger(L, -1);
  else {
    if (d < 0)
      return lvL_error(L, "field " LV_QS " missing in date table", key);
    res = d;
  }
  lv_pop(L, 1);
  return res;
}
Exemple #6
0
static void auxsort (lv_State *L, int l, int u) {
    while (l < u) {  /* for tail recursion */
        int i, j;
        /* sort elements a[l], a[(l+u)/2] and a[u] */
        lv_rawgeti(L, 1, l);
        lv_rawgeti(L, 1, u);
        if (sort_comp(L, -1, -2))  /* a[u] < a[l]? */
            set2(L, l, u);  /* swap a[l] - a[u] */
        else
            lv_pop(L, 2);
        if (u-l == 1) break;  /* only 2 elements */
        i = (l+u)/2;
        lv_rawgeti(L, 1, i);
        lv_rawgeti(L, 1, l);
        if (sort_comp(L, -2, -1))  /* a[i]<a[l]? */
            set2(L, i, l);
        else {
            lv_pop(L, 1);  /* remove a[l] */
            lv_rawgeti(L, 1, u);
            if (sort_comp(L, -1, -2))  /* a[u]<a[i]? */
                set2(L, i, u);
            else
                lv_pop(L, 2);
        }
        if (u-l == 2) break;  /* only 3 elements */
        lv_rawgeti(L, 1, i);  /* Pivot */
        lv_pushvalue(L, -1);
        lv_rawgeti(L, 1, u-1);
        set2(L, i, u-1);
        /* a[l] <= P == a[u-1] <= a[u], only need to sort from l+1 to u-2 */
        i = l;
        j = u-1;
        for (;;) {  /* invariant: a[l..i] <= P <= a[j..u] */
            /* repeat ++i until a[i] >= P */
            while (lv_rawgeti(L, 1, ++i), sort_comp(L, -1, -2)) {
                if (i>u) lvL_error(L, "invalid order function for sorting");
                lv_pop(L, 1);  /* remove a[i] */
            }
            /* repeat --j until a[j] <= P */
            while (lv_rawgeti(L, 1, --j), sort_comp(L, -3, -1)) {
                if (j<l) lvL_error(L, "invalid order function for sorting");
                lv_pop(L, 1);  /* remove a[j] */
            }
            if (j<i) {
                lv_pop(L, 3);  /* pop pivot, a[i], a[j] */
                break;
            }
            set2(L, i, j);
        }
        lv_rawgeti(L, 1, u-1);
        lv_rawgeti(L, 1, i);
        set2(L, u-1, i);  /* swap pivot (a[u-1]) with a[i] */
        /* a[l..i-1] <= a[i] == P <= a[i+1..u] */
        /* adjust so that smaller half is in [j..i] and larger one in [l..u] */
        if (i-l < u-i) {
            j=l;
            i=i-1;
            l=i+2;
        }
        else {
            j=i+1;
            i=u;
            u=j-2;
        }
        auxsort(L, j, i);  /* call recursively the smaller one */
    }  /* repeat the routine for the larger one */
}