|
|
|
@ -133,14 +133,13 @@ static upb_Map* Map_GetMutable(VALUE _self) { |
|
|
|
|
VALUE Map_CreateHash(const upb_Map* map, upb_CType key_type, |
|
|
|
|
TypeInfo val_info) { |
|
|
|
|
VALUE hash = rb_hash_new(); |
|
|
|
|
size_t iter = kUpb_Map_Begin; |
|
|
|
|
TypeInfo key_info = TypeInfo_from_type(key_type); |
|
|
|
|
|
|
|
|
|
if (!map) return hash; |
|
|
|
|
|
|
|
|
|
while (upb_MapIterator_Next(map, &iter)) { |
|
|
|
|
upb_MessageValue key = upb_MapIterator_Key(map, iter); |
|
|
|
|
upb_MessageValue val = upb_MapIterator_Value(map, iter); |
|
|
|
|
size_t iter = kUpb_Map_Begin; |
|
|
|
|
upb_MessageValue key, val; |
|
|
|
|
while (upb_Map_Next(map, &key, &val, &iter)) { |
|
|
|
|
VALUE key_val = Convert_UpbToRuby(key, key_info, Qnil); |
|
|
|
|
VALUE val_val = Scalar_CreateHash(val, val_info); |
|
|
|
|
rb_hash_aset(hash, key_val, val_val); |
|
|
|
@ -156,9 +155,8 @@ VALUE Map_deep_copy(VALUE obj) { |
|
|
|
|
upb_Map* new_map = |
|
|
|
|
upb_Map_New(arena, self->key_type, self->value_type_info.type); |
|
|
|
|
size_t iter = kUpb_Map_Begin; |
|
|
|
|
while (upb_MapIterator_Next(self->map, &iter)) { |
|
|
|
|
upb_MessageValue key = upb_MapIterator_Key(self->map, iter); |
|
|
|
|
upb_MessageValue val = upb_MapIterator_Value(self->map, iter); |
|
|
|
|
upb_MessageValue key, val; |
|
|
|
|
while (upb_Map_Next(self->map, &key, &val, &iter)) { |
|
|
|
|
upb_MessageValue val_copy = |
|
|
|
|
Msgval_DeepCopy(val, self->value_type_info, arena); |
|
|
|
|
upb_Map_Set(new_map, key, val_copy, arena); |
|
|
|
@ -202,9 +200,8 @@ void Map_Inspect(StringBuilder* b, const upb_Map* map, upb_CType key_type, |
|
|
|
|
StringBuilder_Printf(b, "{"); |
|
|
|
|
if (map) { |
|
|
|
|
size_t iter = kUpb_Map_Begin; |
|
|
|
|
while (upb_MapIterator_Next(map, &iter)) { |
|
|
|
|
upb_MessageValue key = upb_MapIterator_Key(map, iter); |
|
|
|
|
upb_MessageValue val = upb_MapIterator_Value(map, iter); |
|
|
|
|
upb_MessageValue key, val; |
|
|
|
|
while (upb_Map_Next(map, &key, &val, &iter)) { |
|
|
|
|
if (first) { |
|
|
|
|
first = false; |
|
|
|
|
} else { |
|
|
|
@ -239,7 +236,6 @@ static VALUE Map_merge_into_self(VALUE _self, VALUE hashmap) { |
|
|
|
|
Map* other = ruby_to_Map(hashmap); |
|
|
|
|
upb_Arena* arena = Arena_get(self->arena); |
|
|
|
|
upb_Message* self_msg = Map_GetMutable(_self); |
|
|
|
|
size_t iter = kUpb_Map_Begin; |
|
|
|
|
|
|
|
|
|
Arena_fuse(other->arena, arena); |
|
|
|
|
|
|
|
|
@ -249,9 +245,9 @@ static VALUE Map_merge_into_self(VALUE _self, VALUE hashmap) { |
|
|
|
|
rb_raise(rb_eArgError, "Attempt to merge Map with mismatching types"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
while (upb_MapIterator_Next(other->map, &iter)) { |
|
|
|
|
upb_MessageValue key = upb_MapIterator_Key(other->map, iter); |
|
|
|
|
upb_MessageValue val = upb_MapIterator_Value(other->map, iter); |
|
|
|
|
size_t iter = kUpb_Map_Begin; |
|
|
|
|
upb_MessageValue key, val; |
|
|
|
|
while (upb_Map_Next(other->map, &key, &val, &iter)) { |
|
|
|
|
upb_Map_Set(self_msg, key, val, arena); |
|
|
|
|
} |
|
|
|
|
} else { |
|
|
|
@ -343,10 +339,9 @@ static VALUE Map_init(int argc, VALUE* argv, VALUE _self) { |
|
|
|
|
static VALUE Map_each(VALUE _self) { |
|
|
|
|
Map* self = ruby_to_Map(_self); |
|
|
|
|
size_t iter = kUpb_Map_Begin; |
|
|
|
|
upb_MessageValue key, val; |
|
|
|
|
|
|
|
|
|
while (upb_MapIterator_Next(self->map, &iter)) { |
|
|
|
|
upb_MessageValue key = upb_MapIterator_Key(self->map, iter); |
|
|
|
|
upb_MessageValue val = upb_MapIterator_Value(self->map, iter); |
|
|
|
|
while (upb_Map_Next(self->map, &key, &val, &iter)) { |
|
|
|
|
VALUE key_val = Convert_UpbToRuby(key, Map_keyinfo(self), self->arena); |
|
|
|
|
VALUE val_val = Convert_UpbToRuby(val, self->value_type_info, self->arena); |
|
|
|
|
rb_yield_values(2, key_val, val_val); |
|
|
|
@ -365,9 +360,9 @@ static VALUE Map_keys(VALUE _self) { |
|
|
|
|
Map* self = ruby_to_Map(_self); |
|
|
|
|
size_t iter = kUpb_Map_Begin; |
|
|
|
|
VALUE ret = rb_ary_new(); |
|
|
|
|
upb_MessageValue key, val; |
|
|
|
|
|
|
|
|
|
while (upb_MapIterator_Next(self->map, &iter)) { |
|
|
|
|
upb_MessageValue key = upb_MapIterator_Key(self->map, iter); |
|
|
|
|
while (upb_Map_Next(self->map, &key, &val, &iter)) { |
|
|
|
|
VALUE key_val = Convert_UpbToRuby(key, Map_keyinfo(self), self->arena); |
|
|
|
|
rb_ary_push(ret, key_val); |
|
|
|
|
} |
|
|
|
@ -385,9 +380,9 @@ static VALUE Map_values(VALUE _self) { |
|
|
|
|
Map* self = ruby_to_Map(_self); |
|
|
|
|
size_t iter = kUpb_Map_Begin; |
|
|
|
|
VALUE ret = rb_ary_new(); |
|
|
|
|
upb_MessageValue key, val; |
|
|
|
|
|
|
|
|
|
while (upb_MapIterator_Next(self->map, &iter)) { |
|
|
|
|
upb_MessageValue val = upb_MapIterator_Value(self->map, iter); |
|
|
|
|
while (upb_Map_Next(self->map, &key, &val, &iter)) { |
|
|
|
|
VALUE val_val = Convert_UpbToRuby(val, self->value_type_info, self->arena); |
|
|
|
|
rb_ary_push(ret, val_val); |
|
|
|
|
} |
|
|
|
@ -523,9 +518,8 @@ static VALUE Map_dup(VALUE _self) { |
|
|
|
|
|
|
|
|
|
Arena_fuse(self->arena, arena); |
|
|
|
|
|
|
|
|
|
while (upb_MapIterator_Next(self->map, &iter)) { |
|
|
|
|
upb_MessageValue key = upb_MapIterator_Key(self->map, iter); |
|
|
|
|
upb_MessageValue val = upb_MapIterator_Value(self->map, iter); |
|
|
|
|
upb_MessageValue key, val; |
|
|
|
|
while (upb_Map_Next(self->map, &key, &val, &iter)) { |
|
|
|
|
upb_Map_Set(new_map, key, val, arena); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -574,9 +568,8 @@ VALUE Map_eq(VALUE _self, VALUE _other) { |
|
|
|
|
// For each member of self, check that an equal member exists at the same key
|
|
|
|
|
// in other.
|
|
|
|
|
size_t iter = kUpb_Map_Begin; |
|
|
|
|
while (upb_MapIterator_Next(self->map, &iter)) { |
|
|
|
|
upb_MessageValue key = upb_MapIterator_Key(self->map, iter); |
|
|
|
|
upb_MessageValue val = upb_MapIterator_Value(self->map, iter); |
|
|
|
|
upb_MessageValue key, val; |
|
|
|
|
while (upb_Map_Next(self->map, &key, &val, &iter)) { |
|
|
|
|
upb_MessageValue other_val; |
|
|
|
|
if (!upb_Map_Get(other->map, key, &other_val)) { |
|
|
|
|
// Not present in other map.
|
|
|
|
@ -619,9 +612,8 @@ VALUE Map_hash(VALUE _self) { |
|
|
|
|
|
|
|
|
|
size_t iter = kUpb_Map_Begin; |
|
|
|
|
TypeInfo key_info = {self->key_type}; |
|
|
|
|
while (upb_MapIterator_Next(self->map, &iter)) { |
|
|
|
|
upb_MessageValue key = upb_MapIterator_Key(self->map, iter); |
|
|
|
|
upb_MessageValue val = upb_MapIterator_Value(self->map, iter); |
|
|
|
|
upb_MessageValue key, val; |
|
|
|
|
while (upb_Map_Next(self->map, &key, &val, &iter)) { |
|
|
|
|
hash = Msgval_GetHash(key, key_info, hash); |
|
|
|
|
hash = Msgval_GetHash(val, self->value_type_info, hash); |
|
|
|
|
} |
|
|
|
|