ext/bdb1/recnum.c in bdb1-0.2.4 vs ext/bdb1/recnum.c in bdb1-0.2.5

- old
+ new

@@ -1,13 +1,11 @@ #include "bdb1.h" static ID id_cmp; static VALUE -bdb1_recnum_init(argc, argv, obj) - int argc; - VALUE *argv, obj; +bdb1_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"); @@ -24,13 +22,11 @@ } return bdb1_init(argc, argv, obj); } static VALUE -bdb1_sary_subseq(obj, beg, len) - VALUE obj; - long beg, len; +bdb1_sary_subseq(VALUE obj, long beg, long len) { VALUE ary2, a; bdb1_DB *dbst; long i; @@ -50,12 +46,11 @@ } return ary2; } static VALUE -bdb1_sary_entry(obj, position) - VALUE obj, position; +bdb1_sary_entry(VALUE obj, VALUE position) { bdb1_DB *dbst; long offset; GetDB(obj, dbst); @@ -79,27 +74,25 @@ * * * db[nth] * Retrieves the +nth+ item from an array. Index starts from zero. * If index is the negative, counts backward from the end of the * array. The index of the last element is -1. Returns +nil+, if - * the +nth+ element is not exist in the array. + * the +nth+ element does not exist in the array. * * * db[start..end] * Returns an array containing the objects from +start+ to +end+, * including both ends. if end is larger than the length of the * array, it will be rounded to the length. If +start+ is out of an * array range , returns +nil+. And if +start+ is larger than end * with in array range, returns empty array ([]). - + * * * db[start, length] * Returns an array containing +length+ items from +start+. Returns * +nil+ if +length+ is negative. */ static VALUE -bdb1_sary_aref(argc, argv, obj) - int argc; - VALUE *argv, obj; +bdb1_sary_aref(int argc, VALUE *argv, VALUE obj) { VALUE arg1, arg2; long beg, len; bdb1_DB *dbst; @@ -131,13 +124,11 @@ } return bdb1_sary_entry(obj, arg1); } static VALUE -bdb1_intern_shift_pop(obj, depart, len) - VALUE obj; - int depart, len; +bdb1_intern_shift_pop(VALUE obj, int depart, int len) { bdb1_DB *dbst; DBT key, data; int ret, i; db_recno_t recno; @@ -159,13 +150,11 @@ else if (RARRAY_LEN(res) == 1) return RARRAY_PTR(res)[0]; else return res; } static void -bdb1_sary_replace(obj, beg, len, rpl) - VALUE obj, rpl; - long beg, len; +bdb1_sary_replace(VALUE obj, long beg, long len, VALUE rpl) { long i, j, rlen; VALUE tmp[2]; bdb1_DB *dbst; @@ -238,30 +227,27 @@ * db[start..end] = val * db[start, length] = val * * Element assignment - with the following syntax: * - * * db[nth] + * * db[nth] = val * Changes the +nth+ element of the array into +val+. If +nth+ is * larger than array length, the array shall be extended * automatically. Extended region shall be initialized by +nil+. * - * * db[start..end] + * * db[start..end] = val * Replaces the items from +start+ to +end+ with +val+. If +val+ is * not an array, the type of +val+ will be converted into the Array * using +to_a+ method. * - * * db[start, length] + * * db[start, length] = val * Replaces the +length+ items from +start+ with +val+. If +val+ is * not an array, the type of +val+ will be converted into the Array * using +to_a+. */ static VALUE -bdb1_sary_aset(argc, argv, obj) - int argc; - VALUE *argv; - VALUE obj; +bdb1_sary_aset(int argc, VALUE *argv, VALUE obj) { long beg, len; bdb1_DB *dbst; GetDB(obj, dbst); @@ -308,17 +294,12 @@ bdb1_put(2, argv, obj); dbst->len++; return argv[1]; } -#if HAVE_RB_ARY_INSERT - static VALUE -bdb1_sary_insert(argc, argv, obj) - int argc; - VALUE *argv; - VALUE obj; +bdb1_sary_insert(int argc, VALUE *argv, VALUE obj) { long pos; if (argc < 2) { rb_raise(rb_eArgError, "wrong number of arguments(at least 2)"); @@ -336,34 +317,29 @@ bdb1_sary_replace(obj, pos, 0, rb_ary_new4(argc-1, argv+1)); return obj; } -#endif - static VALUE -bdb1_sary_at(obj, pos) - VALUE obj, pos; +bdb1_sary_at(VALUE obj, VALUE pos) { return bdb1_sary_entry(obj, pos); } static VALUE -bdb1_sary_first(obj) - VALUE obj; +bdb1_sary_first(VALUE obj) { bdb1_DB *dbst; VALUE tmp; GetDB(obj, dbst); tmp = INT2NUM(0); return bdb1_get(1, &tmp, obj); } static VALUE -bdb1_sary_last(obj) - VALUE obj; +bdb1_sary_last(VALUE obj) { bdb1_DB *dbst; VALUE tmp; GetDB(obj, dbst); @@ -371,13 +347,11 @@ tmp = INT2NUM(dbst->len - 1); return bdb1_get(1, &tmp, obj); } static VALUE -bdb1_sary_fetch(argc, argv, obj) - int argc; - VALUE *argv, obj; +bdb1_sary_fetch(int argc, VALUE *argv, VALUE obj) { VALUE pos, ifnone; bdb1_DB *dbst; long idx; @@ -395,12 +369,11 @@ return bdb1_get(1, &pos, obj); } static VALUE -bdb1_sary_concat(obj, y) - VALUE obj, y; +bdb1_sary_concat(VALUE obj, VALUE y) { bdb1_DB *dbst; long i; VALUE tmp[2]; @@ -414,12 +387,11 @@ } return obj; } static VALUE -bdb1_sary_push(obj, y) - VALUE obj, y; +bdb1_sary_push(VALUE obj, VALUE y) { bdb1_DB *dbst; VALUE tmp[2]; GetDB(obj, dbst); @@ -429,13 +401,11 @@ dbst->len++; return obj; } static VALUE -bdb1_sary_push_m(argc, argv, obj) - int argc; - VALUE obj, *argv; +bdb1_sary_push_m(int argc, VALUE *argv, VALUE obj) { bdb1_DB *dbst; long i; VALUE tmp[2]; @@ -453,14 +423,11 @@ } return obj; } static VALUE -bdb1_sary_s_create(argc, argv, obj) - int argc; - VALUE *argv; - VALUE obj; +bdb1_sary_s_create(int argc, VALUE *argv, VALUE obj) { VALUE res; int i; res = rb_funcall2(obj, rb_intern("new"), 0, 0); @@ -472,12 +439,11 @@ } return res; } static VALUE -bdb1_sary_shift(obj) - VALUE obj; +bdb1_sary_shift(VALUE obj) { VALUE res; bdb1_DB *dbst; GetDB(obj, dbst); @@ -485,12 +451,11 @@ res = bdb1_intern_shift_pop(obj, DB_FIRST, 1); return res; } static VALUE -bdb1_sary_pop(obj) - VALUE obj; +bdb1_sary_pop(VALUE obj) { VALUE res; bdb1_DB *dbst; GetDB(obj, dbst); @@ -498,14 +463,11 @@ res = bdb1_intern_shift_pop(obj, DB_LAST, 1); return res; } static VALUE -bdb1_sary_unshift_m(argc, argv, obj) - int argc; - VALUE *argv; - VALUE obj; +bdb1_sary_unshift_m(int argc, VALUE *argv, VALUE obj) { bdb1_DB *dbst; VALUE tmp[3]; long i; @@ -524,55 +486,49 @@ } return obj; } static VALUE -bdb1_sary_length(obj) - VALUE obj; +bdb1_sary_length(VALUE obj) { bdb1_DB *dbst; GetDB(obj, dbst); if (dbst->len < 0) rb_raise(bdb1_eFatal, "Invalid BDB::Recnum"); return INT2NUM(dbst->len); } static VALUE -bdb1_sary_empty_p(obj) - VALUE obj; +bdb1_sary_empty_p(VALUE obj) { bdb1_DB *dbst; GetDB(obj, dbst); if (dbst->len < 0) rb_raise(bdb1_eFatal, "Invalid BDB::Recnum"); - return (dbst->len)?Qfalse:Qtrue; + return dbst->len == 0 ? Qtrue : Qfalse; } static VALUE -bdb1_sary_rindex(obj, a) - VALUE obj, a; +bdb1_sary_rindex(VALUE obj, VALUE a) { return bdb1_internal_value(obj, a, Qtrue, DB_PREV); } static VALUE -bdb1_sary_to_a(obj) - VALUE obj; +bdb1_sary_to_a(VALUE obj) { return bdb1_to_type(obj, rb_ary_new(), Qfalse); } static VALUE -bdb1_sary_reverse_m(obj) - VALUE obj; +bdb1_sary_reverse_m(VALUE obj) { return bdb1_to_type(obj, rb_ary_new(), Qnil); } static VALUE -bdb1_sary_reverse_bang(obj) - VALUE obj; +bdb1_sary_reverse_bang(VALUE obj) { long i, j; bdb1_DB *dbst; VALUE tmp[2], interm; @@ -594,30 +550,26 @@ } return obj; } static VALUE -bdb1_sary_collect_bang(obj) - VALUE obj; +bdb1_sary_collect_bang(VALUE obj) { return bdb1_each_vc(obj, Qtrue, Qfalse); } static VALUE -bdb1_sary_collect(obj) - VALUE obj; +bdb1_sary_collect(VALUE obj) { if (!rb_block_given_p()) { return bdb1_sary_to_a(obj); } return bdb1_each_vc(obj, Qfalse, Qfalse); } static VALUE -bdb1_sary_values_at(argc, argv, obj) - int argc; - VALUE *argv, obj; +bdb1_sary_values_at(int argc, VALUE *argv, VALUE obj) { VALUE result; long i; result = rb_ary_new(); @@ -626,64 +578,27 @@ } return result; } static VALUE -bdb1_sary_select(argc, argv, obj) - int argc; - VALUE *argv, obj; +bdb1_sary_select(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 bdb1_each_vc(obj, Qfalse, Qtrue); } -#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 bdb1_sary_values_at(argc, argv, obj); + rb_raise(rb_eArgError, "block is not given"); } static VALUE -bdb1_sary_indexes(argc, argv, obj) - int argc; - VALUE obj, *argv; +bdb1_sary_filter(VALUE obj) { - VALUE indexes; - int i; - -#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 bdb1_sary_select(argc, argv, obj); -} - -static VALUE -bdb1_sary_filter(obj) - VALUE obj; -{ rb_warn("BDB1::Recnum#filter is deprecated; use BDB1::Recnum#collect!"); return bdb1_sary_collect_bang(obj); } static VALUE -bdb1_sary_delete(obj, item) - VALUE obj, item; +bdb1_sary_delete(VALUE obj, VALUE item) { bdb1_DB *dbst; long i1, i2; VALUE tmp, a; @@ -708,12 +623,11 @@ } return item; } static VALUE -bdb1_sary_delete_at_m(obj, a) - VALUE obj, a; +bdb1_sary_delete_at_m(VALUE obj, VALUE a) { bdb1_DB *dbst; long pos; VALUE tmp; VALUE del = Qnil; @@ -730,12 +644,11 @@ dbst->len--; return del; } static VALUE -bdb1_sary_reject_bang(obj) - VALUE obj; +bdb1_sary_reject_bang(VALUE obj) { bdb1_DB *dbst; long i1, i2; VALUE tmp, a; @@ -754,45 +667,39 @@ if (dbst->len == i2) return Qnil; return obj; } static VALUE -bdb1_sary_delete_if(obj) - VALUE obj; +bdb1_sary_delete_if(VALUE obj) { bdb1_sary_reject_bang(obj); return obj; } static VALUE -bdb1_sary_replace_m(obj, obj2) - VALUE obj, obj2; +bdb1_sary_replace_m(VALUE obj, VALUE obj2) { bdb1_DB *dbst; GetDB(obj, dbst); obj2 = rb_convert_type(obj2, T_ARRAY, "Array", "to_ary"); bdb1_sary_replace(obj, 0, dbst->len, obj2); return obj; } static VALUE -bdb1_sary_clear(obj) - VALUE obj; +bdb1_sary_clear(VALUE obj) { bdb1_DB *dbst; bdb1_clear(obj); GetDB(obj, dbst); dbst->len = 0; return obj; } static VALUE -bdb1_sary_fill(argc, argv, obj) - int argc; - VALUE *argv; - VALUE obj; +bdb1_sary_fill(int argc, VALUE *argv, VALUE obj) { VALUE item, arg1, arg2, tmp[2]; long beg, len, i; bdb1_DB *dbst; @@ -825,33 +732,33 @@ } return obj; } static VALUE -bdb1_sary_cmp(obj, obj2) - VALUE obj, obj2; +bdb1_sary_cmp(VALUE obj, VALUE obj2) { bdb1_DB *dbst, *dbst2; - VALUE a, a2, tmp, ary; + VALUE a, a2, tmp; + int ary; long i, len; if (obj == obj2) return INT2FIX(0); GetDB(obj, dbst); len = dbst->len; if (!rb_obj_is_kind_of(obj2, bdb1_cRecnum)) { obj2 = rb_convert_type(obj2, T_ARRAY, "Array", "to_ary"); if (len > RARRAY_LEN(obj2)) { len = RARRAY_LEN(obj2); } - ary = Qtrue; + ary = 1; } else { GetDB(obj2, dbst2); if (len > dbst2->len) { len = dbst2->len; } - ary = Qfalse; + ary = 0; } for (i = 0; i < len; i++) { tmp = INT2NUM(i); a = bdb1_get(1, &tmp, obj); if (ary) { @@ -863,21 +770,18 @@ tmp = rb_funcall(a, id_cmp, 1, a2); if (tmp != INT2FIX(0)) { return tmp; } } - len = dbst->len - (ary?RARRAY_LEN(obj2):dbst2->len); + len = dbst->len - (ary ? RARRAY_LEN(obj2) : dbst2->len); if (len == 0) return INT2FIX(0); if (len > 0) return INT2FIX(1); return INT2FIX(-1); } static VALUE -bdb1_sary_slice_bang(argc, argv, obj) - int argc; - VALUE *argv; - VALUE obj; +bdb1_sary_slice_bang(int argc, VALUE *argv, VALUE obj) { VALUE arg1, arg2; long pos, len; bdb1_DB *dbst; @@ -908,54 +812,47 @@ if (bdb1_del(obj, arg1) != Qnil) dbst->len--; return arg2; } static VALUE -bdb1_sary_plus(obj, y) - VALUE obj, y; +bdb1_sary_plus(VALUE obj, VALUE y) { return rb_ary_plus(bdb1_sary_to_a(obj), y); } static VALUE -bdb1_sary_times(obj, y) - VALUE obj, y; +bdb1_sary_times(VALUE obj, VALUE y) { return rb_funcall(bdb1_sary_to_a(obj), rb_intern("*"), 1, y); } static VALUE -bdb1_sary_diff(obj, y) - VALUE obj, y; +bdb1_sary_diff(VALUE obj, VALUE y) { return rb_funcall(bdb1_sary_to_a(obj), rb_intern("-"), 1, y); } static VALUE -bdb1_sary_and(obj, y) - VALUE obj, y; +bdb1_sary_and(VALUE obj, VALUE y) { return rb_funcall(bdb1_sary_to_a(obj), rb_intern("&"), 1, y); } static VALUE -bdb1_sary_or(obj, y) - VALUE obj, y; +bdb1_sary_or(VALUE obj, VALUE y) { return rb_funcall(bdb1_sary_to_a(obj), rb_intern("|"), 1, y); } static VALUE -bdb1_sary_compact(obj) - VALUE obj; +bdb1_sary_compact(VALUE obj) { return rb_funcall(bdb1_sary_to_a(obj), rb_intern("compact"), 0, 0); } static VALUE -bdb1_sary_compact_bang(obj) - VALUE obj; +bdb1_sary_compact_bang(VALUE obj) { bdb1_DB *dbst; long i, j; VALUE tmp; @@ -975,12 +872,11 @@ if (dbst->len == j) return Qnil; return obj; } static VALUE -bdb1_sary_nitems(obj) - VALUE obj; +bdb1_sary_nitems(VALUE obj) { bdb1_DB *dbst; long i, j; VALUE tmp; @@ -993,12 +889,11 @@ } return INT2NUM(j); } static VALUE -bdb1_sary_each_index(obj) - VALUE obj; +bdb1_sary_each_index(VALUE obj) { bdb1_DB *dbst; long i; GetDB(obj, dbst); @@ -1006,11 +901,12 @@ rb_yield(INT2NUM(i)); } return obj; } -void bdb1_init_recnum() +void +bdb1_init_recnum(void) { id_cmp = rb_intern("<=>"); bdb1_cRecnum = rb_define_class_under(bdb1_mDb, "Recnum", bdb1_cCommon); rb_define_singleton_method(bdb1_cRecnum, "[]", bdb1_sary_s_create, -1); rb_const_set(bdb1_mDb, rb_intern("Recno"), bdb1_cRecnum); @@ -1025,35 +921,26 @@ rb_define_method(bdb1_cRecnum, "<<", bdb1_sary_push, 1); rb_define_method(bdb1_cRecnum, "push", bdb1_sary_push_m, -1); rb_define_method(bdb1_cRecnum, "pop", bdb1_sary_pop, 0); rb_define_method(bdb1_cRecnum, "shift", bdb1_sary_shift, 0); rb_define_method(bdb1_cRecnum, "unshift", bdb1_sary_unshift_m, -1); -#if HAVE_RB_ARY_INSERT rb_define_method(bdb1_cRecnum, "insert", bdb1_sary_insert, -1); -#endif rb_define_method(bdb1_cRecnum, "each", bdb1_each_value, 0); rb_define_method(bdb1_cRecnum, "each_index", bdb1_sary_each_index, 0); rb_define_method(bdb1_cRecnum, "reverse_each", bdb1_each_eulav, 0); rb_define_method(bdb1_cRecnum, "length", bdb1_sary_length, 0); rb_define_alias(bdb1_cRecnum, "size", "length"); rb_define_method(bdb1_cRecnum, "empty?", bdb1_sary_empty_p, 0); - rb_define_method(bdb1_cRecnum, "index", bdb1_index, 1); + rb_define_method(bdb1_cRecnum, "key", bdb1_key, 1); + rb_define_method(bdb1_cRecnum, "index", bdb1_key, 1); rb_define_method(bdb1_cRecnum, "rindex", bdb1_sary_rindex, 1); - rb_define_method(bdb1_cRecnum, "indexes", bdb1_sary_indexes, -1); - rb_define_method(bdb1_cRecnum, "indices", bdb1_sary_indexes, -1); rb_define_method(bdb1_cRecnum, "reverse", bdb1_sary_reverse_m, 0); rb_define_method(bdb1_cRecnum, "reverse!", bdb1_sary_reverse_bang, 0); rb_define_method(bdb1_cRecnum, "collect", bdb1_sary_collect, 0); rb_define_method(bdb1_cRecnum, "collect!", bdb1_sary_collect_bang, 0); -#if HAVE_RB_ARY_MAP rb_define_method(bdb1_cRecnum, "map", bdb1_sary_collect, 0); -#endif -#if HAVE_RB_ARY_VALUES_AT rb_define_method(bdb1_cRecnum, "values_at", bdb1_sary_values_at, -1); -#endif -#if HAVE_RB_ARY_SELECT - rb_define_method(bdb1_cRecnum, "select", bdb1_sary_select, -1); -#endif + rb_define_method(bdb1_cRecnum, "select", bdb1_sary_select, 0); rb_define_method(bdb1_cRecnum, "map!", bdb1_sary_collect_bang, 0); rb_define_method(bdb1_cRecnum, "filter", bdb1_sary_filter, 0); rb_define_method(bdb1_cRecnum, "delete", bdb1_sary_delete, 1); rb_define_method(bdb1_cRecnum, "delete_at", bdb1_sary_delete_at_m, 1); rb_define_method(bdb1_cRecnum, "delete_if", bdb1_sary_delete_if, 0);