# class BDB::Recnum

### Public Class Methods

[](*args) click to toggle source

    static VALUE
    bdb_sary_s_create(int argc, VALUE *argv, VALUE obj)
    {
        VALUE res;

        res = rb_funcall2(obj, rb_intern("new"), 0, 0);
        if (argc < 0) {
            rb_raise(rb_eArgError, "negative number of arguments");
        }
        if (argc > 0) {
            bdb_sary_push_m(argc, argv, res);
        }
        return res;
    }

new(*args) click to toggle source

    static VALUE
    bdb_recnum_init(int argc, VALUE *argv, VALUE obj)
    {
        VALUE *nargv;
        VALUE array = rb_str_new2("array_base");
        VALUE sarray = rb_str_new2("set_array_base");

        if (!argc || TYPE(argv[argc - 1]) != T_HASH) {
            nargv = ALLOCA_N(VALUE, argc + 1);
            MEMCPY(nargv, argv, VALUE, argc);
            nargv[argc] = rb_hash_new();
            argv = nargv;
            argc++;
        }
        rb_hash_aset(argv[argc - 1], array, INT2FIX(0));
        if (rb_hash_aref(argv[argc - 1], sarray) != RHASH_IFNONE(argv[argc - 1])) {
            rb_hash_aset(argv[argc - 1], sarray, INT2FIX(0));
        }
        rb_hash_aset(argv[argc - 1], rb_str_new2("set_flags"), INT2FIX(DB_RENUMBER));
        return bdb_init(argc, argv, obj);
    }

### Public Instance Methods

&(p1) click to toggle source

    static VALUE
    bdb_sary_and(VALUE obj, VALUE y)
    {
        return rb_funcall(bdb_sary_to_a(obj), rb_intern("&"), 1, y);
    }

*(p1) click to toggle source

    static VALUE
    bdb_sary_times(VALUE obj, VALUE y)
    {
        return rb_funcall(bdb_sary_to_a(obj), rb_intern("*"), 1, y);
    }

+(p1) click to toggle source

    static VALUE
    bdb_sary_plus(VALUE obj, VALUE y)
    {
        return rb_ary_plus(bdb_sary_to_a(obj), y);
    }

-(p1) click to toggle source

    static VALUE
    bdb_sary_diff(VALUE obj, VALUE y)
    {
        return rb_funcall(bdb_sary_to_a(obj), rb_intern("-"), 1, y);
    }

<<(p1) click to toggle source

    static VALUE
    bdb_sary_push(VALUE obj, VALUE y)
    {
        bdb_DB *dbst;
        VALUE tmp[2];

        GetDB(obj, dbst);
        tmp[0] = INT2NUM(dbst->len);
        tmp[1] = y;
        bdb_put(2, tmp, obj);
        dbst->len++;
        return obj;
    }

<=>(p1) click to toggle source

    static VALUE
    bdb_sary_cmp(VALUE obj, VALUE obj2)
    {
        bdb_DB *dbst, *dbst2 = 0;
        VALUE a, a2, tmp, ary;
        long i, len;

        if (obj == obj2) return INT2FIX(0);
        GetDB(obj, dbst);
        len = dbst->len;
        if (!rb_obj_is_kind_of(obj2, bdb_cRecnum)) {
            obj2 = rb_convert_type(obj2, T_ARRAY, "Array", "to_ary");
            if (len > RARRAY_LEN(obj2)) {
                len = RARRAY_LEN(obj2);
            }
            ary = Qtrue;
        }
        else {
            GetDB(obj2, dbst2);
            len = dbst->len;
            if (len > dbst2->len) {
                len = dbst2->len;
            }
            ary = Qfalse;
        }
        for (i = 0; i < len; i++) {
            tmp = INT2NUM(i);
            a = bdb_get(1, &tmp, obj);
            if (ary) {
                a2 = RARRAY_PTR(obj2)[i];
            }
            else {
                a2 = bdb_get(1, &tmp, obj2);
            }
            tmp = rb_funcall(a, id_cmp, 1, a2);
            if (tmp != INT2FIX(0)) {
                return tmp;
            }
        }
        len = dbst->len - ary?RARRAY_LEN(obj2):dbst2->len;
        if (len == 0) return INT2FIX(0);
        if (len > 0) return INT2FIX(1);
        return INT2FIX(-1);
    }

[](p1, p2 = v2) click to toggle source

    static VALUE
    bdb_sary_aref(int argc, VALUE *argv, VALUE obj)
    {
        VALUE arg1, arg2;
        long beg, len;
        bdb_DB *dbst;

        GetDB(obj, dbst);
        if (rb_scan_args(argc, argv, "11", &arg1, &arg2) == 2) {
            beg = NUM2LONG(arg1);
            len = NUM2LONG(arg2);
            if (beg < 0) {
                beg = dbst->len + beg;
            }
            return bdb_sary_subseq(obj, beg, len);
        }

        if (FIXNUM_P(arg1)) {
            return bdb_sary_entry(obj, arg1);
        }
        else if (TYPE(arg1) == T_BIGNUM) {
            rb_raise(rb_eIndexError, "index too big");
        }
        else {
            switch (rb_range_beg_len(arg1, &beg, &len, dbst->len, 0)) {
              case Qfalse:
                break;
              case Qnil:
                return Qnil;
              default:
                return bdb_sary_subseq(obj, beg, len);
            }
        }
        return bdb_sary_entry(obj, arg1);
    }

[]=(*args) click to toggle source

    static VALUE
    bdb_sary_aset(int argc, VALUE *argv, VALUE obj)
    {
        long  beg, len;
        bdb_DB *dbst;

        GetDB(obj, dbst);
        if (argc == 3) {
            bdb_sary_replace(obj, NUM2LONG(argv[0]), NUM2LONG(argv[1]), argv[2]);
            return argv[2];
        }
        if (argc != 2) {
            rb_raise(rb_eArgError, "wrong # of arguments(%d for 2)", argc);
        }
        if (FIXNUM_P(argv[0])) {
            beg = FIX2LONG(argv[0]);
            goto fixnum;
        }
        else if (rb_range_beg_len(argv[0], &beg, &len, dbst->len, 1)) {
            bdb_sary_replace(obj, beg, len, argv[1]);
            return argv[1];
        }
        if (TYPE(argv[0]) == T_BIGNUM) {
            rb_raise(rb_eIndexError, "index too big");
        }

        beg = NUM2LONG(argv[0]);
      fixnum:
        if (beg < 0) {
            beg += dbst->len;
            if (beg < 0) {
                rb_raise(rb_eIndexError, "index %ld out of array",
                         beg - dbst->len);
            }
        }
        if (beg > dbst->len) {
            VALUE nargv[2];
            int i;

            nargv[1] = Qnil;
            for (i = dbst->len; i < beg; i++) {
                nargv[0] = INT2NUM(i);
                bdb_put(2, nargv, obj);
                dbst->len++;
            }
        }
        argv[0] = INT2NUM(beg);
        bdb_put(2, argv, obj);
        dbst->len++;
        return argv[1];
    }

at(p1) click to toggle source

    static VALUE
    bdb_sary_at(VALUE obj, VALUE pos)
    {
        return bdb_sary_entry(obj, pos);
    }

clear(*args) click to toggle source

    static VALUE
    bdb_sary_clear(int argc, VALUE *argv, VALUE obj)
    {
        bdb_DB *dbst;
        VALUE g;
        int flags = 0;

        if (argc && TYPE(argv[argc - 1]) == T_HASH) {
            VALUE f = argv[argc - 1];
            if ((g = rb_hash_aref(f, rb_intern("flags"))) != RHASH_IFNONE(f) ||
                (g = rb_hash_aref(f, rb_str_new2("flags"))) != RHASH_IFNONE(f)) {
                flags = NUM2INT(g);
            }
            argc--;
        }
        if (argc == 1) {
            flags = NUM2INT(argv[0]);
        }
        g = INT2FIX(flags);
        bdb_clear(1, &g, obj);
        GetDB(obj, dbst);
        dbst->len = 0;
        return obj;
    }

collect(*args) click to toggle source

    static VALUE
    bdb_sary_collect(int argc, VALUE *argv, VALUE obj)
    {
        if (!rb_block_given_p()) {
            return bdb_sary_to_a(obj);
        }
        return bdb_each_kvc(argc, argv, obj, DB_NEXT, rb_ary_new(), BDB_ST_VALUE);
    }

collect!(*args) click to toggle source

    static VALUE
    bdb_sary_collect_bang(int argc, VALUE *argv, VALUE obj)
    {
        return bdb_each_kvc(argc, argv, obj, DB_NEXT, Qtrue, BDB_ST_VALUE);
    }

compact() click to toggle source

    static VALUE
    bdb_sary_compact(VALUE obj)
    {
        return rb_funcall(bdb_sary_to_a(obj), rb_intern("compact!"), 0, 0);
    }

compact!() click to toggle source

    static VALUE
    bdb_sary_compact_bang(VALUE obj)
    {
        bdb_DB *dbst;
        long i, j;
        VALUE tmp;

        GetDB(obj, dbst);
        j = dbst->len;
        for (i = 0; i < dbst->len; ) {
            tmp = INT2NUM(i);
            tmp = bdb_get(1, &tmp, obj);
            if (NIL_P(tmp)) {
                bdb_del(obj, INT2NUM(i));
                dbst->len--;
            }
            else {
                i++;
            }
        }
        if (dbst->len == j) return Qnil;
        return obj;
    }

concat(p1) click to toggle source

    static VALUE
    bdb_sary_concat(VALUE obj, VALUE y)
    {
        bdb_DB *dbst;
        long i;
        VALUE tmp[2];

        y = rb_convert_type(y, T_ARRAY, "Array", "to_ary");
        GetDB(obj, dbst);
        for (i = 0; i < RARRAY_LEN(y); i++) {
            tmp[0] = INT2NUM(dbst->len);
            tmp[1] = RARRAY_PTR(y)[i];
            bdb_put(2, tmp, obj);
            dbst->len++;
        }
        return obj;
    }

db_get(p1, p2 = v2) click to toggle source

    static VALUE
    bdb_sary_aref(int argc, VALUE *argv, VALUE obj)
    {
        VALUE arg1, arg2;
        long beg, len;
        bdb_DB *dbst;

        GetDB(obj, dbst);
        if (rb_scan_args(argc, argv, "11", &arg1, &arg2) == 2) {
            beg = NUM2LONG(arg1);
            len = NUM2LONG(arg2);
            if (beg < 0) {
                beg = dbst->len + beg;
            }
            return bdb_sary_subseq(obj, beg, len);
        }

        if (FIXNUM_P(arg1)) {
            return bdb_sary_entry(obj, arg1);
        }
        else if (TYPE(arg1) == T_BIGNUM) {
            rb_raise(rb_eIndexError, "index too big");
        }
        else {
            switch (rb_range_beg_len(arg1, &beg, &len, dbst->len, 0)) {
              case Qfalse:
                break;
              case Qnil:
                return Qnil;
              default:
                return bdb_sary_subseq(obj, beg, len);
            }
        }
        return bdb_sary_entry(obj, arg1);
    }

db_put(*args) click to toggle source

    static VALUE
    bdb_sary_aset(int argc, VALUE *argv, VALUE obj)
    {
        long  beg, len;
        bdb_DB *dbst;

        GetDB(obj, dbst);
        if (argc == 3) {
            bdb_sary_replace(obj, NUM2LONG(argv[0]), NUM2LONG(argv[1]), argv[2]);
            return argv[2];
        }
        if (argc != 2) {
            rb_raise(rb_eArgError, "wrong # of arguments(%d for 2)", argc);
        }
        if (FIXNUM_P(argv[0])) {
            beg = FIX2LONG(argv[0]);
            goto fixnum;
        }
        else if (rb_range_beg_len(argv[0], &beg, &len, dbst->len, 1)) {
            bdb_sary_replace(obj, beg, len, argv[1]);
            return argv[1];
        }
        if (TYPE(argv[0]) == T_BIGNUM) {
            rb_raise(rb_eIndexError, "index too big");
        }

        beg = NUM2LONG(argv[0]);
      fixnum:
        if (beg < 0) {
            beg += dbst->len;
            if (beg < 0) {
                rb_raise(rb_eIndexError, "index %ld out of array",
                         beg - dbst->len);
            }
        }
        if (beg > dbst->len) {
            VALUE nargv[2];
            int i;

            nargv[1] = Qnil;
            for (i = dbst->len; i < beg; i++) {
                nargv[0] = INT2NUM(i);
                bdb_put(2, nargv, obj);
                dbst->len++;
            }
        }
        argv[0] = INT2NUM(beg);
        bdb_put(2, argv, obj);
        dbst->len++;
        return argv[1];
    }

delete(p1) click to toggle source

    static VALUE
    bdb_sary_delete(VALUE obj, VALUE item)
    {
        bdb_DB *dbst;
        long i1, i2;
        VALUE tmp, a;

        GetDB(obj, dbst);
        i2 = dbst->len;
        for (i1 = 0; i1 < dbst->len;) {
            tmp = INT2NUM(i1);
            a = bdb_get(1, &tmp, obj);
            if (rb_equal(a, item)) {
                bdb_del(obj, INT2NUM(i1));
                dbst->len--;
            }
            else {
                i1++;
            }
        }
        if (dbst->len == i2) {
            if (rb_block_given_p()) {
                return rb_yield(item);
            }
            return Qnil;
        }
        return item;
    }

delete_at(p1) click to toggle source

    static VALUE
    bdb_sary_delete_at_m(VALUE obj, VALUE a)
    {
        bdb_DB *dbst;
        long pos;
        VALUE tmp;
        VALUE del = Qnil;

        GetDB(obj, dbst);
        pos = NUM2INT(a);
        if (pos >= dbst->len) return Qnil;
        if (pos < 0) pos += dbst->len;
        if (pos < 0) return Qnil;

        tmp = INT2NUM(pos);
        del = bdb_get(1, &tmp, obj);
        bdb_del(obj, tmp);
        dbst->len--;
        return del;
    }

delete_if() click to toggle source

    static VALUE
    bdb_sary_delete_if(VALUE obj)
    {
        bdb_sary_reject_bang(obj);
        return obj;
    }

empty?() click to toggle source

    static VALUE
    bdb_sary_empty_p(VALUE obj)
    {
        bdb_DB *dbst;

        GetDB(obj, dbst);
        if (dbst->len < 0) rb_raise(bdb_eFatal, "Invalid BDB::Recnum");
        return (dbst->len)?Qfalse:Qtrue;
    }

fetch(p1, p2 = v2) click to toggle source

    static VALUE
    bdb_sary_fetch(int argc, VALUE *argv, VALUE obj)
    {
        VALUE pos, ifnone;
        bdb_DB *dbst;
        long idx;

        GetDB(obj, dbst);
        rb_scan_args(argc, argv, "11", &pos, &ifnone);
        idx = NUM2LONG(pos);

        if (idx < 0) {
            idx +=  dbst->len;
        }
        if (idx < 0 || dbst->len <= idx) {
            return ifnone;
        }
        pos = INT2NUM(idx);
        return bdb_get(1, &pos, obj);
    }

fill(p1, p2 = v2, p3 = v3) click to toggle source

    static VALUE
    bdb_sary_fill(int argc, VALUE *argv, VALUE obj)
    {
        VALUE item, arg1, arg2, tmp[2];
        long beg, len, i;
        bdb_DB *dbst;

        GetDB(obj, dbst);
        rb_scan_args(argc, argv, "12", &item, &arg1, &arg2);
        switch (argc) {
          case 1:
              len = dbst->len;
              beg = 0;
              break;
          case 2:
            if (rb_range_beg_len(arg1, &beg, &len, dbst->len, 1)) {
                break;
            }
            /* fall through */
          case 3:
            beg = NIL_P(arg1)?0:NUM2LONG(arg1);
            if (beg < 0) {
                beg += dbst->len;
                if (beg < 0) beg = 0;
            }
            len = NIL_P(arg2)?dbst->len - beg:NUM2LONG(arg2);
            break;
        }
        tmp[1] = item;
        for (i = 0; i < len; i++) {
            tmp[0] = INT2NUM(i + beg);
            bdb_put(2, tmp, obj);
            if ((i + beg) >= dbst->len) dbst->len++;
        }
        return obj;
    }

filter(*args) click to toggle source

    static VALUE
    bdb_sary_filter(int argc, VALUE *argv, VALUE obj)
    {
        rb_warn("BDB::Recnum#filter is deprecated; use BDB::Recnum#collect!");
        return bdb_sary_collect_bang(argc, argv, obj);
    }

first() click to toggle source

    static VALUE
    bdb_sary_first(VALUE obj)
    {
        bdb_DB *dbst;
        VALUE tmp;

        GetDB(obj, dbst);
        tmp = INT2NUM(0);
        return bdb_get(1, &tmp, obj);
    }

get(p1, p2 = v2) click to toggle source

    static VALUE
    bdb_sary_aref(int argc, VALUE *argv, VALUE obj)
    {
        VALUE arg1, arg2;
        long beg, len;
        bdb_DB *dbst;

        GetDB(obj, dbst);
        if (rb_scan_args(argc, argv, "11", &arg1, &arg2) == 2) {
            beg = NUM2LONG(arg1);
            len = NUM2LONG(arg2);
            if (beg < 0) {
                beg = dbst->len + beg;
            }
            return bdb_sary_subseq(obj, beg, len);
        }

        if (FIXNUM_P(arg1)) {
            return bdb_sary_entry(obj, arg1);
        }
        else if (TYPE(arg1) == T_BIGNUM) {
            rb_raise(rb_eIndexError, "index too big");
        }
        else {
            switch (rb_range_beg_len(arg1, &beg, &len, dbst->len, 0)) {
              case Qfalse:
                break;
              case Qnil:
                return Qnil;
              default:
                return bdb_sary_subseq(obj, beg, len);
            }
        }
        return bdb_sary_entry(obj, arg1);
    }

indexes(*args) click to toggle source

    static VALUE
    bdb_sary_indexes(int argc, VALUE *argv, VALUE obj)
    {
    #if HAVE_RB_ARY_VALUES_AT
        rb_warn("Recnum#%s is deprecated; use Recnum#values_at",
    #if HAVE_RB_FRAME_THIS_FUNC
                rb_id2name(rb_frame_this_func()));
    #else
                rb_id2name(rb_frame_last_func()));
    #endif
    #endif
        return bdb_sary_values_at(argc, argv, obj);
    }

indices(*args) click to toggle source

    static VALUE
    bdb_sary_indexes(int argc, VALUE *argv, VALUE obj)
    {
    #if HAVE_RB_ARY_VALUES_AT
        rb_warn("Recnum#%s is deprecated; use Recnum#values_at",
    #if HAVE_RB_FRAME_THIS_FUNC
                rb_id2name(rb_frame_this_func()));
    #else
                rb_id2name(rb_frame_last_func()));
    #endif
    #endif
        return bdb_sary_values_at(argc, argv, obj);
    }

insert(*args) click to toggle source

    static VALUE
    bdb_sary_insert(int argc, VALUE *argv, VALUE obj)
    {
        long pos;

        if (argc < 2) {
            rb_raise(rb_eArgError, "wrong number of arguments(at least 2)");
        }
        pos = NUM2LONG(argv[0]);
        if (pos == -1) {
            bdb_DB *dbst;

            GetDB(obj, dbst);
            pos = dbst->len;
        }
        else if (pos < 0) {
            pos++;
        }

        bdb_sary_replace(obj, pos, 0, rb_ary_new4(argc-1, argv+1));
        return obj;
    }

last() click to toggle source

    static VALUE
    bdb_sary_last(VALUE obj)
    {
        bdb_DB *dbst;
        VALUE tmp;

        GetDB(obj, dbst);
        if (!dbst->len) return Qnil;
        tmp = INT2NUM(dbst->len);
        return bdb_get(1, &tmp, obj);
    }

length() click to toggle source

    static VALUE
    bdb_sary_length(VALUE obj)
    {
        bdb_DB *dbst;

        GetDB(obj, dbst);
        if (dbst->len < 0) rb_raise(bdb_eFatal, "Invalid BDB::Recnum");
        return INT2NUM(dbst->len);
    }

Also aliased as: size
map() click to toggle source

    static VALUE
    bdb_sary_collect(int argc, VALUE *argv, VALUE obj)
    {
        if (!rb_block_given_p()) {
            return bdb_sary_to_a(obj);
        }
        return bdb_each_kvc(argc, argv, obj, DB_NEXT, rb_ary_new(), BDB_ST_VALUE);
    }

map!(*args) click to toggle source

    static VALUE
    bdb_sary_collect_bang(int argc, VALUE *argv, VALUE obj)
    {
        return bdb_each_kvc(argc, argv, obj, DB_NEXT, Qtrue, BDB_ST_VALUE);
    }

nitems() click to toggle source

    static VALUE
    bdb_sary_nitems(VALUE obj)
    {
        bdb_DB *dbst;
        long i, j;
        VALUE tmp;

        GetDB(obj, dbst);
        j = 0;
        for (i = 0; i < dbst->len; ) {
            tmp = INT2NUM(i);
            tmp = bdb_get(1, &tmp, obj);
            if (!NIL_P(tmp)) j++;
        }
        return INT2NUM(j);
    }

pop() click to toggle source

    static VALUE
    bdb_sary_pop(VALUE obj)
    {
        VALUE res;
        bdb_DB *dbst;

        GetDB(obj, dbst);
        if (dbst->len == 0) return Qnil;
        res = bdb_intern_shift_pop(obj, DB_LAST, 1);
        return res;
    }

push(*args) click to toggle source

    static VALUE
    bdb_sary_push_m(int argc, VALUE *argv, VALUE obj)
    {
        bdb_DB *dbst;
        long i;
        VALUE tmp[2];

        if (argc == 0) {
            rb_raise(rb_eArgError, "wrong # of arguments(at least 1)");
        }
        if (argc > 0) {
            GetDB(obj, dbst);
            for (i = 0; i < argc; i++) {
                tmp[0] = INT2NUM(dbst->len);
                tmp[1] = argv[i];
                bdb_put(2, tmp, obj);
                dbst->len++;
            }
        }
        return obj;
    }

put(*args) click to toggle source

    static VALUE
    bdb_sary_aset(int argc, VALUE *argv, VALUE obj)
    {
        long  beg, len;
        bdb_DB *dbst;

        GetDB(obj, dbst);
        if (argc == 3) {
            bdb_sary_replace(obj, NUM2LONG(argv[0]), NUM2LONG(argv[1]), argv[2]);
            return argv[2];
        }
        if (argc != 2) {
            rb_raise(rb_eArgError, "wrong # of arguments(%d for 2)", argc);
        }
        if (FIXNUM_P(argv[0])) {
            beg = FIX2LONG(argv[0]);
            goto fixnum;
        }
        else if (rb_range_beg_len(argv[0], &beg, &len, dbst->len, 1)) {
            bdb_sary_replace(obj, beg, len, argv[1]);
            return argv[1];
        }
        if (TYPE(argv[0]) == T_BIGNUM) {
            rb_raise(rb_eIndexError, "index too big");
        }

        beg = NUM2LONG(argv[0]);
      fixnum:
        if (beg < 0) {
            beg += dbst->len;
            if (beg < 0) {
                rb_raise(rb_eIndexError, "index %ld out of array",
                         beg - dbst->len);
            }
        }
        if (beg > dbst->len) {
            VALUE nargv[2];
            int i;

            nargv[1] = Qnil;
            for (i = dbst->len; i < beg; i++) {
                nargv[0] = INT2NUM(i);
                bdb_put(2, nargv, obj);
                dbst->len++;
            }
        }
        argv[0] = INT2NUM(beg);
        bdb_put(2, argv, obj);
        dbst->len++;
        return argv[1];
    }

reject!() click to toggle source

    static VALUE
    bdb_sary_reject_bang(VALUE obj)
    {
        bdb_DB *dbst;
        long i1, i2;
        VALUE tmp, a;

        GetDB(obj, dbst);
        i2 = dbst->len;
        for (i1 = 0; i1 < dbst->len;) {
            tmp = INT2NUM(i1);
            a = bdb_get(1, &tmp, obj);
            if (!RTEST(rb_yield(a))) {
                i1++;
                continue;
            }
            bdb_del(obj, tmp);
            dbst->len--;
        }
        if (dbst->len == i2) return Qnil;
        return obj;
    }

replace(p1) click to toggle source

    static VALUE
    bdb_sary_replace_m(VALUE obj, VALUE obj2)
    {
        bdb_DB *dbst;

        GetDB(obj, dbst);
        obj2 = rb_convert_type(obj2, T_ARRAY, "Array", "to_ary");
        bdb_sary_replace(obj, 0, dbst->len, obj2);
        return obj;
    }

reverse() click to toggle source

    static VALUE
    bdb_sary_reverse_m(VALUE obj)
    {
        return bdb_to_type(obj, rb_ary_new(), Qnil);
    }

reverse!() click to toggle source

    static VALUE
    bdb_sary_reverse_bang(VALUE obj)
    {
        long i, j;
        bdb_DB *dbst;
        VALUE tmp[2], interm;

        GetDB(obj, dbst);
        if (dbst->len <= 1) return obj;
        i = 0;
        j = dbst->len - 1;
        while (i < j) {
            tmp[0] = INT2NUM(i);
            interm = bdb_get(1, tmp, obj);
            tmp[0] = INT2NUM(j);
            tmp[1] = bdb_get(1, tmp, obj);
            tmp[0] = INT2NUM(i);
            bdb_put(2, tmp, obj);
            tmp[0] = INT2NUM(j);
            tmp[1] = interm;
            bdb_put(2, tmp, obj);
            i++; j--;
        }
        return obj;
    }

rindex(p1) click to toggle source

    static VALUE
    bdb_sary_rindex(VALUE obj, VALUE a)
    {
        return bdb_internal_value(obj, a, Qtrue, DB_PREV);
    }

select(*args) click to toggle source

    static VALUE
    bdb_sary_select(int argc, VALUE *argv, VALUE obj)
    {
        VALUE result;
        long i;

        if (rb_block_given_p()) {
            if (argc > 0) {
                rb_raise(rb_eArgError, "wrong number arguments(%d for 0)", argc);
            }
            return bdb_each_kvc(argc, argv, obj, DB_NEXT, rb_ary_new(), BDB_ST_SELECT);
        }
    #if HAVE_RB_ARY_VALUES_AT
            rb_warn("Recnum#select(index..) is deprecated; use Recnum#values_at");
    #endif
        result = rb_ary_new();
        for (i = 0; i < argc; i++) {
            rb_ary_push(result, bdb_sary_fetch(1, argv + i, obj));
        }
        return result;
    }

shift() click to toggle source

    static VALUE
    bdb_sary_shift(VALUE obj)
    {
        VALUE res;
        bdb_DB *dbst;

        GetDB(obj, dbst);
        if (dbst->len == 0) return Qnil;
        res = bdb_intern_shift_pop(obj, DB_FIRST, 1);
        return res;
    }

size()
Alias for: length
slice(p1, p2 = v2) click to toggle source

    static VALUE
    bdb_sary_aref(int argc, VALUE *argv, VALUE obj)
    {
        VALUE arg1, arg2;
        long beg, len;
        bdb_DB *dbst;

        GetDB(obj, dbst);
        if (rb_scan_args(argc, argv, "11", &arg1, &arg2) == 2) {
            beg = NUM2LONG(arg1);
            len = NUM2LONG(arg2);
            if (beg < 0) {
                beg = dbst->len + beg;
            }
            return bdb_sary_subseq(obj, beg, len);
        }

        if (FIXNUM_P(arg1)) {
            return bdb_sary_entry(obj, arg1);
        }
        else if (TYPE(arg1) == T_BIGNUM) {
            rb_raise(rb_eIndexError, "index too big");
        }
        else {
            switch (rb_range_beg_len(arg1, &beg, &len, dbst->len, 0)) {
              case Qfalse:
                break;
              case Qnil:
                return Qnil;
              default:
                return bdb_sary_subseq(obj, beg, len);
            }
        }
        return bdb_sary_entry(obj, arg1);
    }

slice!(p1, p2 = v2) click to toggle source

    static VALUE
    bdb_sary_slice_bang(int argc, VALUE *argv, VALUE obj)
    {
        VALUE arg1, arg2;
        long pos, len;
        bdb_DB *dbst;

        GetDB(obj, dbst);
        if (rb_scan_args(argc, argv, "11", &arg1, &arg2) == 2) {
            pos = NUM2LONG(arg1);
            len = NUM2LONG(arg2);
          delete_pos_len:
            if (pos < 0) {
                pos = dbst->len + pos;
            }
            arg2 = bdb_sary_subseq(obj, pos, len);
            bdb_sary_replace(obj, pos, len, Qnil);
            return arg2;
        }

        if (!FIXNUM_P(arg1) && rb_range_beg_len(arg1, &pos, &len, dbst->len, 1)) {
            goto delete_pos_len;
        }

        pos = NUM2LONG(arg1);
        if (pos >= dbst->len) return Qnil;
        if (pos < 0) pos += dbst->len;
        if (pos < 0) return Qnil;

        arg1 = INT2NUM(pos);
        arg2 = bdb_sary_at(obj, arg1);
        if (bdb_del(obj, arg1) != Qnil) dbst->len--;
        return arg2;
    }

store(*args) click to toggle source

    static VALUE
    bdb_sary_aset(int argc, VALUE *argv, VALUE obj)
    {
        long  beg, len;
        bdb_DB *dbst;

        GetDB(obj, dbst);
        if (argc == 3) {
            bdb_sary_replace(obj, NUM2LONG(argv[0]), NUM2LONG(argv[1]), argv[2]);
            return argv[2];
        }
        if (argc != 2) {
            rb_raise(rb_eArgError, "wrong # of arguments(%d for 2)", argc);
        }
        if (FIXNUM_P(argv[0])) {
            beg = FIX2LONG(argv[0]);
            goto fixnum;
        }
        else if (rb_range_beg_len(argv[0], &beg, &len, dbst->len, 1)) {
            bdb_sary_replace(obj, beg, len, argv[1]);
            return argv[1];
        }
        if (TYPE(argv[0]) == T_BIGNUM) {
            rb_raise(rb_eIndexError, "index too big");
        }

        beg = NUM2LONG(argv[0]);
      fixnum:
        if (beg < 0) {
            beg += dbst->len;
            if (beg < 0) {
                rb_raise(rb_eIndexError, "index %ld out of array",
                         beg - dbst->len);
            }
        }
        if (beg > dbst->len) {
            VALUE nargv[2];
            int i;

            nargv[1] = Qnil;
            for (i = dbst->len; i < beg; i++) {
                nargv[0] = INT2NUM(i);
                bdb_put(2, nargv, obj);
                dbst->len++;
            }
        }
        argv[0] = INT2NUM(beg);
        bdb_put(2, argv, obj);
        dbst->len++;
        return argv[1];
    }

to_a() click to toggle source

    static VALUE
    bdb_sary_to_a(VALUE obj)
    {
        return bdb_to_type(obj, rb_ary_new(), Qfalse);
    }

to_ary() click to toggle source

    static VALUE
    bdb_sary_to_a(VALUE obj)
    {
        return bdb_to_type(obj, rb_ary_new(), Qfalse);
    }

unshift(*args) click to toggle source

    static VALUE
    bdb_sary_unshift_m(int argc, VALUE *argv, VALUE obj)
    {
        bdb_DB *dbst;
        VALUE tmp[2];
        long i;

        if (argc == 0) {
            rb_raise(rb_eArgError, "wrong # of arguments(at least 1)");
        }
        if (argc > 0) {
    /* ++ */
            GetDB(obj, dbst);
            for (i = dbst->len - 1; i >= 0; i++) {
                tmp[0] = INT2NUM(i);
                tmp[1] = bdb_get(1, tmp, obj);
                tmp[0] = INT2NUM(i + argc);
                bdb_put(2, tmp, obj);
            }
            for (i = 0; i < argc; i++) {
                tmp[0] = INT2NUM(i);
                tmp[1] = argv[i];
                bdb_put(2, tmp, obj);
                dbst->len++;
            }
        }
        return obj;
    }

values_at(*args) click to toggle source

    static VALUE
    bdb_sary_values_at(int argc, VALUE *argv, VALUE obj)
    {
        VALUE result;
        long i;

        result = rb_ary_new();
        for (i = 0; i < argc; i++) {
            rb_ary_push(result, bdb_sary_fetch(1, argv + i, obj));
        }
        return result;
    }

|(p1) click to toggle source

    static VALUE
    bdb_sary_or(VALUE obj, VALUE y)
    {
        return rb_funcall(bdb_sary_to_a(obj), rb_intern("|"), 1, y);
    }