ext/template/google_hash.cpp.erb in google_hash-0.5.1 vs ext/template/google_hash.cpp.erb in google_hash-0.6.0

- old
+ new

@@ -71,14 +71,13 @@ }; <% end %> <% if key_type == 'VALUE' %> -static hash<const char*> H; +// static hash<const char*> H; // unused currently... +// using it is like hash<const char*> -// hashing it is like hash<const char*> - struct eqrb { bool operator()(const VALUE s1, const VALUE s2) const { // speeds up populate int 18/11 @@ -211,22 +210,25 @@ } <% end %> <% if assert_value_type %> if(!(TYPE(to_this) == <%= assert_value_type %>)) { + <%= "if(!(TYPE(to_this) == #{assert_value_type2}))" if assert_value_type2 %> rb_raise(rb_eTypeError, "not valid value <%=assert_value_type%>"); } <% end %> <%= options[:extra_set_code] %> + <%= options[:extra_set_code2] %> + RCallback* cbs = GetCallbackStruct(cb); (*cbs->hash_map)[ <%= convert_keys_from_ruby %>(set_this)] = <%= convert_values_from_ruby %>(to_this); return to_this; // ltodo test that it returns value... } -static VALUE rb_ghash_get(VALUE cb, VALUE get_this) { +static VALUE rb_ghash_get(VALUE cb, VALUE get_this, int just_check_for_presence) { // TODO optionally not type check assert anywhere [?] <% if assert_key_type %> if(!(TYPE(get_this) == <%= assert_key_type %>)) { <%= "if(!(TYPE(get_this) == #{assert_key_type2}))" if assert_key_type2 %> rb_raise(rb_eTypeError, "not valid get key (expected <%=assert_key_type%>)"); @@ -235,17 +237,38 @@ RCallback* cbs = GetCallbackStruct(cb); <%= options[:extra_get_code] %> <%= type %>_hash_map< <%= key_type %>, <%= value_type %> <%= extra_hash_params %> >::iterator out = cbs->hash_map->find(<%= convert_keys_from_ruby %>(get_this)); - if(out == cbs->hash_map->end()) { // not found...hmm...is this False, though? - return Qnil; - } else { - return <%= convert_values_to_ruby %>(out->second); + if(out == cbs->hash_map->end()) { // not found...hmm...is this False, though? + if(just_check_for_presence) + return Qfalse; + else + return Qnil; + } else { + if(just_check_for_presence) + return Qtrue; + else + return <%= convert_values_to_ruby %>(out->second); } + } + +static VALUE rb_ghash_get_value(VALUE cb, VALUE get_this) { + return rb_ghash_get(cb, get_this, 0); +} + +static VALUE rb_ghash_get_present(VALUE cb, VALUE get_this) { + return rb_ghash_get(cb, get_this, 1); +} +static VALUE rb_ghash_size(VALUE cb) { + RCallback* incoming = GetCallbackStruct(cb); + return INT2FIX(incoming->hash_map->size()); +} + + static VALUE rb_ghash_each(VALUE cb) { RCallback* incoming = GetCallbackStruct(cb); for(<%= type %>_hash_map< <%= key_type %>, <%= value_type %> <%= extra_hash_params %> >::iterator it = incoming->hash_map->begin(); it != incoming->hash_map->end(); ++it) { rb_yield_values(2, <%= convert_keys_to_ruby %>(it->first), <%= convert_values_to_ruby %>(it->second)); } @@ -297,14 +320,19 @@ rb_define_alloc_func(rb_cGoogleHashLocal, callback_alloc); // I guess it calls this for us, pre initialize... rb_define_method(rb_cGoogleHashLocal, "initialize", RUBY_METHOD_FUNC(rb_mri_hash_new), 0); rb_define_method(rb_cGoogleHashLocal, "[]=", RUBY_METHOD_FUNC(rb_ghash_set), 2); - rb_define_method(rb_cGoogleHashLocal, "[]", RUBY_METHOD_FUNC(rb_ghash_get), 1); + rb_define_method(rb_cGoogleHashLocal, "[]", RUBY_METHOD_FUNC(rb_ghash_get_value), 1); rb_define_method(rb_cGoogleHashLocal, "each", RUBY_METHOD_FUNC(rb_ghash_each), 0); rb_define_method(rb_cGoogleHashLocal, "values", RUBY_METHOD_FUNC(rb_ghash_values), 0); rb_define_method(rb_cGoogleHashLocal, "keys", RUBY_METHOD_FUNC(rb_ghash_keys), 0); + rb_define_method(rb_cGoogleHashLocal, "has_key?", RUBY_METHOD_FUNC(rb_ghash_get_present), 1); + rb_define_method(rb_cGoogleHashLocal, "key?", RUBY_METHOD_FUNC(rb_ghash_get_present), 1); + rb_define_method(rb_cGoogleHashLocal, "member?", RUBY_METHOD_FUNC(rb_ghash_get_present), 1); + rb_define_method(rb_cGoogleHashLocal, "include?", RUBY_METHOD_FUNC(rb_ghash_get_present), 1); rb_define_method(rb_cGoogleHashLocal, "keys_combination_2", RUBY_METHOD_FUNC(rb_ghash_combination_2), 0); + rb_define_method(rb_cGoogleHashLocal, "length", RUBY_METHOD_FUNC(rb_ghash_size), 0); id_eql = rb_intern("eql?"); id_hash = rb_intern("hash"); } }