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 |
247 /* | 252 /* |
248 * Private ruby method, used by RepeatedField.pop | 253 * Private ruby method, used by RepeatedField.pop |
249 */ | 254 */ |
250 VALUE RepeatedField_pop_one(VALUE _self) { | 255 VALUE RepeatedField_pop_one(VALUE _self) { |
251 RepeatedField* self = ruby_to_RepeatedField(_self); | 256 RepeatedField* self = ruby_to_RepeatedField(_self); |
252 upb_fieldtype_t field_type = self->field_type; | 257 upb_fieldtype_t field_type = self->field_type; |
253 VALUE field_type_class = self->field_type_class; | 258 VALUE field_type_class = self->field_type_class; |
254 int element_size = native_slot_size(field_type); | 259 int element_size = native_slot_size(field_type); |
255 int index; | 260 int index; |
256 void* memory; | 261 void* memory; |
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
435 } | 440 } |
436 | 441 |
437 /* | 442 /* |
438 * call-seq: | 443 * call-seq: |
439 * RepeatedField.hash => hash_value | 444 * RepeatedField.hash => hash_value |
440 * | 445 * |
441 * Returns a hash value computed from this repeated field's elements. | 446 * Returns a hash value computed from this repeated field's elements. |
442 */ | 447 */ |
443 VALUE RepeatedField_hash(VALUE _self) { | 448 VALUE RepeatedField_hash(VALUE _self) { |
444 RepeatedField* self = ruby_to_RepeatedField(_self); | 449 RepeatedField* self = ruby_to_RepeatedField(_self); |
445 | 450 st_index_t h = rb_hash_start(0); |
446 VALUE hash = LL2NUM(0); | 451 VALUE hash_sym = rb_intern("hash"); |
447 | |
448 upb_fieldtype_t field_type = self->field_type; | 452 upb_fieldtype_t field_type = self->field_type; |
449 VALUE field_type_class = self->field_type_class; | 453 VALUE field_type_class = self->field_type_class; |
450 size_t elem_size = native_slot_size(field_type); | 454 size_t elem_size = native_slot_size(field_type); |
451 size_t off = 0; | 455 size_t off = 0; |
452 for (int i = 0; i < self->size; i++, off += elem_size) { | 456 for (int i = 0; i < self->size; i++, off += elem_size) { |
453 void* mem = ((uint8_t *)self->elements) + off; | 457 void* mem = ((uint8_t *)self->elements) + off; |
454 VALUE elem = native_slot_get(field_type, field_type_class, mem); | 458 VALUE elem = native_slot_get(field_type, field_type_class, mem); |
455 hash = rb_funcall(hash, rb_intern("<<"), 1, INT2NUM(2)); | 459 h = rb_hash_uint(h, NUM2LONG(rb_funcall(elem, hash_sym, 0))); |
456 hash = rb_funcall(hash, rb_intern("^"), 1, | |
457 rb_funcall(elem, rb_intern("hash"), 0)); | |
458 } | 460 } |
| 461 h = rb_hash_end(h); |
459 | 462 |
460 return hash; | 463 return INT2FIX(h); |
461 } | 464 } |
462 | 465 |
463 /* | 466 /* |
464 * call-seq: | 467 * call-seq: |
465 * RepeatedField.+(other) => repeated field | 468 * RepeatedField.+(other) => repeated field |
466 * | 469 * |
467 * Returns a new repeated field that contains the concatenated list of this | 470 * Returns a new repeated field that contains the concatenated list of this |
468 * repeated field's elements and other's elements. The other (second) list may | 471 * repeated field's elements and other's elements. The other (second) list may |
469 * be either another repeated field or a Ruby array. | 472 * be either another repeated field or a Ruby array. |
470 */ | 473 */ |
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
642 rb_define_method(klass, "dup", RepeatedField_dup, 0); | 645 rb_define_method(klass, "dup", RepeatedField_dup, 0); |
643 // Also define #clone so that we don't inherit Object#clone. | 646 // Also define #clone so that we don't inherit Object#clone. |
644 rb_define_method(klass, "clone", RepeatedField_dup, 0); | 647 rb_define_method(klass, "clone", RepeatedField_dup, 0); |
645 rb_define_method(klass, "==", RepeatedField_eq, 1); | 648 rb_define_method(klass, "==", RepeatedField_eq, 1); |
646 rb_define_method(klass, "to_ary", RepeatedField_to_ary, 0); | 649 rb_define_method(klass, "to_ary", RepeatedField_to_ary, 0); |
647 rb_define_method(klass, "hash", RepeatedField_hash, 0); | 650 rb_define_method(klass, "hash", RepeatedField_hash, 0); |
648 rb_define_method(klass, "+", RepeatedField_plus, 1); | 651 rb_define_method(klass, "+", RepeatedField_plus, 1); |
649 rb_define_method(klass, "concat", RepeatedField_concat, 1); | 652 rb_define_method(klass, "concat", RepeatedField_concat, 1); |
650 rb_include_module(klass, rb_mEnumerable); | 653 rb_include_module(klass, rb_mEnumerable); |
651 } | 654 } |
OLD | NEW |