ext/dartsclone/dartscloneext.hpp in dartsclone-0.1.0 vs ext/dartsclone/dartscloneext.hpp in dartsclone-0.2.0

- old
+ new

@@ -28,10 +28,12 @@ rb_define_alloc_func(rb_cDoubleArray, double_array_alloc); rb_define_method(rb_cDoubleArray, "initialize", RUBY_METHOD_FUNC(_double_array_init), 0); rb_define_method(rb_cDoubleArray, "build", RUBY_METHOD_FUNC(_double_array_build), -1); rb_define_method(rb_cDoubleArray, "open", RUBY_METHOD_FUNC(_double_array_open), -1); rb_define_method(rb_cDoubleArray, "save", RUBY_METHOD_FUNC(_double_array_save), -1); + rb_define_method(rb_cDoubleArray, "get_array", RUBY_METHOD_FUNC(_double_array_get_array), 0); + rb_define_method(rb_cDoubleArray, "set_array", RUBY_METHOD_FUNC(_double_array_set_array), 1); rb_define_method(rb_cDoubleArray, "exact_match_search", RUBY_METHOD_FUNC(_double_array_exact_match_search), -1); rb_define_method(rb_cDoubleArray, "common_prefix_search", RUBY_METHOD_FUNC(_double_array_common_prefix_search), -1); rb_define_method(rb_cDoubleArray, "traverse", RUBY_METHOD_FUNC(_double_array_traverse), -1); rb_define_method(rb_cDoubleArray, "unit_size", RUBY_METHOD_FUNC(_double_array_unit_size), 0); rb_define_method(rb_cDoubleArray, "size", RUBY_METHOD_FUNC(_double_array_size), 0); @@ -88,12 +90,12 @@ VALUE kwvalues[3] = { Qundef, Qundef, Qundef }; rb_get_kwargs(kwargs, kwtable, 0, 3, kwvalues); const char* filename = StringValueCStr(_filename); const char* mode = kwvalues[0] == Qundef ? "rb" : StringValueCStr(kwvalues[0]); - const size_t offset = kwvalues[1] == Qundef ? 0 : (size_t)NUM2INT(kwvalues[1]); - const size_t size = kwvalues[2] == Qundef ? 0 : (size_t)NUM2INT(kwvalues[2]); + const size_t offset = kwvalues[1] == Qundef ? 0 : NUM2SIZET(kwvalues[1]); + const size_t size = kwvalues[2] == Qundef ? 0 : NUM2SIZET(kwvalues[2]); if (get_double_array(self)->open(filename, mode, offset, size) != 0) { return Qfalse; } return Qtrue; @@ -108,18 +110,32 @@ VALUE kwvalues[2] = { Qundef, Qundef }; rb_get_kwargs(kwargs, kwtable, 0, 2, kwvalues); const char* filename = StringValueCStr(_filename); const char* mode = kwvalues[0] == Qundef ? "wb" : StringValueCStr(kwvalues[0]); - const size_t offset = kwvalues[1] == Qundef ? 0 : (size_t)NUM2INT(kwvalues[1]); + const size_t offset = kwvalues[1] == Qundef ? 0 : NUM2SIZET(kwvalues[1]); if (get_double_array(self)->save(filename, mode, offset) != 0) { return Qfalse; } return Qtrue; }; + static VALUE _double_array_get_array(VALUE self) { + const char* arr = (char*)get_double_array(self)->array(); + const size_t sz = get_double_array(self)->total_size(); + return rb_str_new(arr, sz); + }; + + static VALUE _double_array_set_array(VALUE self, VALUE bytes) { + const size_t sz = NUM2SIZET(rb_funcall(bytes, rb_intern("size"), 0)); + const size_t total_sz = sz / get_double_array(self)->unit_size(); + char* arr = StringValuePtr(bytes); + get_double_array(self)->set_array(arr, total_sz); + return Qnil; + }; + static VALUE _double_array_exact_match_search(int argc, VALUE* argv, VALUE self) { VALUE _key = Qnil; VALUE kwargs = Qnil; rb_scan_args(argc, argv, "1:", &_key, &kwargs); @@ -131,12 +147,12 @@ rb_raise(rb_eRuntimeError, "failed to search: dictionary is empty"); return Qnil; } const char* key = StringValueCStr(_key); - const size_t length = kwvalues[0] == Qundef ? 0 : (size_t)NUM2INT(kwvalues[0]); - const size_t node_pos = kwvalues[1] == Qundef ? 0 : (size_t)NUM2INT(kwvalues[1]); + const size_t length = kwvalues[0] == Qundef ? 0 : NUM2SIZET(kwvalues[0]); + const size_t node_pos = kwvalues[1] == Qundef ? 0 : NUM2SIZET(kwvalues[1]); Darts::DoubleArray::value_type value; get_double_array(self)->exactMatchSearch(key, value, length, node_pos); return INT2NUM(value); }; @@ -154,13 +170,13 @@ rb_raise(rb_eRuntimeError, "failed to search: dictionary is empty"); return Qnil; } const char* key = StringValueCStr(_key); - const size_t max_num_results = kwvalues[0] == Qundef ? (size_t)NUM2INT(rb_funcall(_key, rb_intern("size"), 0)) : (size_t)NUM2INT(kwvalues[0]); - const size_t length = kwvalues[1] == Qundef ? 0 : (size_t)NUM2INT(kwvalues[1]); - const size_t node_pos = kwvalues[2] == Qundef ? 0 : (size_t)NUM2INT(kwvalues[2]); + const size_t max_num_results = kwvalues[0] == Qundef ? NUM2SIZET(rb_funcall(_key, rb_intern("size"), 0)) : NUM2SIZET(kwvalues[0]); + const size_t length = kwvalues[1] == Qundef ? 0 : NUM2SIZET(kwvalues[1]); + const size_t node_pos = kwvalues[2] == Qundef ? 0 : NUM2SIZET(kwvalues[2]); Darts::DoubleArray::result_pair_type* results = (Darts::DoubleArray::result_pair_type*)ruby_xmalloc(max_num_results * sizeof(Darts::DoubleArray::result_pair_type)); const size_t num_matches = get_double_array(self)->commonPrefixSearch(key, results, max_num_results, length, node_pos); const int num_returns = (int)(max_num_results < num_matches ? max_num_results : num_matches); @@ -189,12 +205,12 @@ ID kwtable[3] = { rb_intern("node_pos"), rb_intern("key_pos"), rb_intern("length") }; VALUE kwvalues[3] = { Qundef, Qundef, Qundef }; rb_get_kwargs(kwargs, kwtable, 2, 1, kwvalues); const char* key = StringValueCStr(_key); - size_t node_pos = (size_t)NUM2INT(kwvalues[0]); - size_t key_pos = (size_t)NUM2INT(kwvalues[1]); - const size_t length = kwvalues[2] == Qundef ? 0 : (size_t)NUM2INT(kwvalues[2]); + size_t node_pos = NUM2SIZET(kwvalues[0]); + size_t key_pos = NUM2SIZET(kwvalues[1]); + const size_t length = kwvalues[2] == Qundef ? 0 : NUM2SIZET(kwvalues[2]); Darts::DoubleArray::value_type value = get_double_array(self)->traverse(key, node_pos, key_pos, length); VALUE ret = rb_hash_new(); rb_hash_aset(ret, ID2SYM(rb_intern("value")), INT2NUM(value));