| OLD | NEW |
| 1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
| 2 // Copyright 2014 Google Inc. All rights reserved. | 2 // Copyright 2014 Google Inc. All rights reserved. |
| 3 // https://developers.google.com/protocol-buffers/ | 3 // https://developers.google.com/protocol-buffers/ |
| 4 // | 4 // |
| 5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
| 6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
| 7 // met: | 7 // met: |
| 8 // | 8 // |
| 9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
| 10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
| (...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 237 self->size++; | 237 self->size++; |
| 238 } | 238 } |
| 239 | 239 |
| 240 void* RepeatedField_index_native(VALUE _self, int index) { | 240 void* RepeatedField_index_native(VALUE _self, int index) { |
| 241 RepeatedField* self = ruby_to_RepeatedField(_self); | 241 RepeatedField* self = ruby_to_RepeatedField(_self); |
| 242 upb_fieldtype_t field_type = self->field_type; | 242 upb_fieldtype_t field_type = self->field_type; |
| 243 int element_size = native_slot_size(field_type); | 243 int element_size = native_slot_size(field_type); |
| 244 return RepeatedField_memoryat(self, index, element_size); | 244 return RepeatedField_memoryat(self, index, element_size); |
| 245 } | 245 } |
| 246 | 246 |
| 247 int RepeatedField_size(VALUE _self) { | |
| 248 RepeatedField* self = ruby_to_RepeatedField(_self); | |
| 249 return self->size; | |
| 250 } | |
| 251 | |
| 252 /* | 247 /* |
| 253 * Private ruby method, used by RepeatedField.pop | 248 * Private ruby method, used by RepeatedField.pop |
| 254 */ | 249 */ |
| 255 VALUE RepeatedField_pop_one(VALUE _self) { | 250 VALUE RepeatedField_pop_one(VALUE _self) { |
| 256 RepeatedField* self = ruby_to_RepeatedField(_self); | 251 RepeatedField* self = ruby_to_RepeatedField(_self); |
| 257 upb_fieldtype_t field_type = self->field_type; | 252 upb_fieldtype_t field_type = self->field_type; |
| 258 VALUE field_type_class = self->field_type_class; | 253 VALUE field_type_class = self->field_type_class; |
| 259 int element_size = native_slot_size(field_type); | 254 int element_size = native_slot_size(field_type); |
| 260 int index; | 255 int index; |
| 261 void* memory; | 256 void* memory; |
| (...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 440 } | 435 } |
| 441 | 436 |
| 442 /* | 437 /* |
| 443 * call-seq: | 438 * call-seq: |
| 444 * RepeatedField.hash => hash_value | 439 * RepeatedField.hash => hash_value |
| 445 * | 440 * |
| 446 * Returns a hash value computed from this repeated field's elements. | 441 * Returns a hash value computed from this repeated field's elements. |
| 447 */ | 442 */ |
| 448 VALUE RepeatedField_hash(VALUE _self) { | 443 VALUE RepeatedField_hash(VALUE _self) { |
| 449 RepeatedField* self = ruby_to_RepeatedField(_self); | 444 RepeatedField* self = ruby_to_RepeatedField(_self); |
| 450 st_index_t h = rb_hash_start(0); | 445 |
| 451 VALUE hash_sym = rb_intern("hash"); | 446 VALUE hash = LL2NUM(0); |
| 447 |
| 452 upb_fieldtype_t field_type = self->field_type; | 448 upb_fieldtype_t field_type = self->field_type; |
| 453 VALUE field_type_class = self->field_type_class; | 449 VALUE field_type_class = self->field_type_class; |
| 454 size_t elem_size = native_slot_size(field_type); | 450 size_t elem_size = native_slot_size(field_type); |
| 455 size_t off = 0; | 451 size_t off = 0; |
| 456 for (int i = 0; i < self->size; i++, off += elem_size) { | 452 for (int i = 0; i < self->size; i++, off += elem_size) { |
| 457 void* mem = ((uint8_t *)self->elements) + off; | 453 void* mem = ((uint8_t *)self->elements) + off; |
| 458 VALUE elem = native_slot_get(field_type, field_type_class, mem); | 454 VALUE elem = native_slot_get(field_type, field_type_class, mem); |
| 459 h = rb_hash_uint(h, NUM2LONG(rb_funcall(elem, hash_sym, 0))); | 455 hash = rb_funcall(hash, rb_intern("<<"), 1, INT2NUM(2)); |
| 456 hash = rb_funcall(hash, rb_intern("^"), 1, |
| 457 rb_funcall(elem, rb_intern("hash"), 0)); |
| 460 } | 458 } |
| 461 h = rb_hash_end(h); | |
| 462 | 459 |
| 463 return INT2FIX(h); | 460 return hash; |
| 464 } | 461 } |
| 465 | 462 |
| 466 /* | 463 /* |
| 467 * call-seq: | 464 * call-seq: |
| 468 * RepeatedField.+(other) => repeated field | 465 * RepeatedField.+(other) => repeated field |
| 469 * | 466 * |
| 470 * Returns a new repeated field that contains the concatenated list of this | 467 * Returns a new repeated field that contains the concatenated list of this |
| 471 * repeated field's elements and other's elements. The other (second) list may | 468 * repeated field's elements and other's elements. The other (second) list may |
| 472 * be either another repeated field or a Ruby array. | 469 * be either another repeated field or a Ruby array. |
| 473 */ | 470 */ |
| (...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 645 rb_define_method(klass, "dup", RepeatedField_dup, 0); | 642 rb_define_method(klass, "dup", RepeatedField_dup, 0); |
| 646 // Also define #clone so that we don't inherit Object#clone. | 643 // Also define #clone so that we don't inherit Object#clone. |
| 647 rb_define_method(klass, "clone", RepeatedField_dup, 0); | 644 rb_define_method(klass, "clone", RepeatedField_dup, 0); |
| 648 rb_define_method(klass, "==", RepeatedField_eq, 1); | 645 rb_define_method(klass, "==", RepeatedField_eq, 1); |
| 649 rb_define_method(klass, "to_ary", RepeatedField_to_ary, 0); | 646 rb_define_method(klass, "to_ary", RepeatedField_to_ary, 0); |
| 650 rb_define_method(klass, "hash", RepeatedField_hash, 0); | 647 rb_define_method(klass, "hash", RepeatedField_hash, 0); |
| 651 rb_define_method(klass, "+", RepeatedField_plus, 1); | 648 rb_define_method(klass, "+", RepeatedField_plus, 1); |
| 652 rb_define_method(klass, "concat", RepeatedField_concat, 1); | 649 rb_define_method(klass, "concat", RepeatedField_concat, 1); |
| 653 rb_include_module(klass, rb_mEnumerable); | 650 rb_include_module(klass, rb_mEnumerable); |
| 654 } | 651 } |
| OLD | NEW |