map.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2014 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. #include "protobuf.h"
  31. // -----------------------------------------------------------------------------
  32. // Basic map operations on top of upb's strtable.
  33. //
  34. // Note that we roll our own `Map` container here because, as for
  35. // `RepeatedField`, we want a strongly-typed container. This is so that any user
  36. // errors due to incorrect map key or value types are raised as close as
  37. // possible to the error site, rather than at some deferred point (e.g.,
  38. // serialization).
  39. //
  40. // We build our `Map` on top of upb_strtable so that we're able to take
  41. // advantage of the native_slot storage abstraction, as RepeatedField does.
  42. // (This is not quite a perfect mapping -- see the key conversions below -- but
  43. // gives us full support and error-checking for all value types for free.)
  44. // -----------------------------------------------------------------------------
  45. // Map values are stored using the native_slot abstraction (as with repeated
  46. // field values), but keys are a bit special. Since we use a strtable, we need
  47. // to store keys as sequences of bytes such that equality of those bytes maps
  48. // one-to-one to equality of keys. We store strings directly (i.e., they map to
  49. // their own bytes) and integers as native integers (using the native_slot
  50. // abstraction).
  51. // Note that there is another tradeoff here in keeping string keys as native
  52. // strings rather than Ruby strings: traversing the Map requires conversion to
  53. // Ruby string values on every traversal, potentially creating more garbage. We
  54. // should consider ways to cache a Ruby version of the key if this becomes an
  55. // issue later.
  56. // Forms a key to use with the underlying strtable from a Ruby key value. |buf|
  57. // must point to TABLE_KEY_BUF_LENGTH bytes of temporary space, used to
  58. // construct a key byte sequence if needed. |out_key| and |out_length| provide
  59. // the resulting key data/length.
  60. #define TABLE_KEY_BUF_LENGTH 8 // sizeof(uint64_t)
  61. static void table_key(Map* self, VALUE key,
  62. char* buf,
  63. const char** out_key,
  64. size_t* out_length) {
  65. switch (self->key_type) {
  66. case UPB_TYPE_BYTES:
  67. case UPB_TYPE_STRING:
  68. // Strings: use string content directly.
  69. Check_Type(key, T_STRING);
  70. native_slot_validate_string_encoding(self->key_type, key);
  71. *out_key = RSTRING_PTR(key);
  72. *out_length = RSTRING_LEN(key);
  73. break;
  74. case UPB_TYPE_BOOL:
  75. case UPB_TYPE_INT32:
  76. case UPB_TYPE_INT64:
  77. case UPB_TYPE_UINT32:
  78. case UPB_TYPE_UINT64:
  79. native_slot_set(self->key_type, Qnil, buf, key);
  80. *out_key = buf;
  81. *out_length = native_slot_size(self->key_type);
  82. break;
  83. default:
  84. // Map constructor should not allow a Map with another key type to be
  85. // constructed.
  86. assert(false);
  87. break;
  88. }
  89. }
  90. static VALUE table_key_to_ruby(Map* self, const char* buf, size_t length) {
  91. switch (self->key_type) {
  92. case UPB_TYPE_BYTES:
  93. case UPB_TYPE_STRING: {
  94. VALUE ret = rb_str_new(buf, length);
  95. rb_enc_associate(ret,
  96. (self->key_type == UPB_TYPE_BYTES) ?
  97. kRubyString8bitEncoding : kRubyStringUtf8Encoding);
  98. return ret;
  99. }
  100. case UPB_TYPE_BOOL:
  101. case UPB_TYPE_INT32:
  102. case UPB_TYPE_INT64:
  103. case UPB_TYPE_UINT32:
  104. case UPB_TYPE_UINT64:
  105. return native_slot_get(self->key_type, Qnil, buf);
  106. default:
  107. assert(false);
  108. return Qnil;
  109. }
  110. }
  111. static void* value_memory(upb_value* v) {
  112. return (void*)(&v->val);
  113. }
  114. // -----------------------------------------------------------------------------
  115. // Map container type.
  116. // -----------------------------------------------------------------------------
  117. const rb_data_type_t Map_type = {
  118. "Google::Protobuf::Map",
  119. { Map_mark, Map_free, NULL },
  120. };
  121. VALUE cMap;
  122. Map* ruby_to_Map(VALUE _self) {
  123. Map* self;
  124. TypedData_Get_Struct(_self, Map, &Map_type, self);
  125. return self;
  126. }
  127. void Map_mark(void* _self) {
  128. Map* self = _self;
  129. rb_gc_mark(self->value_type_class);
  130. if (self->value_type == UPB_TYPE_STRING ||
  131. self->value_type == UPB_TYPE_BYTES ||
  132. self->value_type == UPB_TYPE_MESSAGE) {
  133. upb_strtable_iter it;
  134. for (upb_strtable_begin(&it, &self->table);
  135. !upb_strtable_done(&it);
  136. upb_strtable_next(&it)) {
  137. upb_value v = upb_strtable_iter_value(&it);
  138. void* mem = value_memory(&v);
  139. native_slot_mark(self->value_type, mem);
  140. }
  141. }
  142. }
  143. void Map_free(void* _self) {
  144. Map* self = _self;
  145. upb_strtable_uninit(&self->table);
  146. xfree(self);
  147. }
  148. VALUE Map_alloc(VALUE klass) {
  149. Map* self = ALLOC(Map);
  150. memset(self, 0, sizeof(Map));
  151. self->value_type_class = Qnil;
  152. return TypedData_Wrap_Struct(klass, &Map_type, self);
  153. }
  154. static bool needs_typeclass(upb_fieldtype_t type) {
  155. switch (type) {
  156. case UPB_TYPE_MESSAGE:
  157. case UPB_TYPE_ENUM:
  158. return true;
  159. default:
  160. return false;
  161. }
  162. }
  163. /*
  164. * call-seq:
  165. * Map.new(key_type, value_type, value_typeclass = nil, init_hashmap = {})
  166. * => new map
  167. *
  168. * Allocates a new Map container. This constructor may be called with 2, 3, or 4
  169. * arguments. The first two arguments are always present and are symbols (taking
  170. * on the same values as field-type symbols in message descriptors) that
  171. * indicate the type of the map key and value fields.
  172. *
  173. * The supported key types are: :int32, :int64, :uint32, :uint64, :bool,
  174. * :string, :bytes.
  175. *
  176. * The supported value types are: :int32, :int64, :uint32, :uint64, :bool,
  177. * :string, :bytes, :enum, :message.
  178. *
  179. * The third argument, value_typeclass, must be present if value_type is :enum
  180. * or :message. As in RepeatedField#new, this argument must be a message class
  181. * (for :message) or enum module (for :enum).
  182. *
  183. * The last argument, if present, provides initial content for map. Note that
  184. * this may be an ordinary Ruby hashmap or another Map instance with identical
  185. * key and value types. Also note that this argument may be present whether or
  186. * not value_typeclass is present (and it is unambiguously separate from
  187. * value_typeclass because value_typeclass's presence is strictly determined by
  188. * value_type). The contents of this initial hashmap or Map instance are
  189. * shallow-copied into the new Map: the original map is unmodified, but
  190. * references to underlying objects will be shared if the value type is a
  191. * message type.
  192. */
  193. VALUE Map_init(int argc, VALUE* argv, VALUE _self) {
  194. Map* self = ruby_to_Map(_self);
  195. int init_value_arg;
  196. // We take either two args (:key_type, :value_type), three args (:key_type,
  197. // :value_type, "ValueMessageType"), or four args (the above plus an initial
  198. // hashmap).
  199. if (argc < 2 || argc > 4) {
  200. rb_raise(rb_eArgError, "Map constructor expects 2, 3 or 4 arguments.");
  201. }
  202. self->key_type = ruby_to_fieldtype(argv[0]);
  203. self->value_type = ruby_to_fieldtype(argv[1]);
  204. // Check that the key type is an allowed type.
  205. switch (self->key_type) {
  206. case UPB_TYPE_INT32:
  207. case UPB_TYPE_INT64:
  208. case UPB_TYPE_UINT32:
  209. case UPB_TYPE_UINT64:
  210. case UPB_TYPE_BOOL:
  211. case UPB_TYPE_STRING:
  212. case UPB_TYPE_BYTES:
  213. // These are OK.
  214. break;
  215. default:
  216. rb_raise(rb_eArgError, "Invalid key type for map.");
  217. }
  218. init_value_arg = 2;
  219. if (needs_typeclass(self->value_type) && argc > 2) {
  220. self->value_type_class = argv[2];
  221. validate_type_class(self->value_type, self->value_type_class);
  222. init_value_arg = 3;
  223. }
  224. // Table value type is always UINT64: this ensures enough space to store the
  225. // native_slot value.
  226. if (!upb_strtable_init(&self->table, UPB_CTYPE_UINT64)) {
  227. rb_raise(rb_eRuntimeError, "Could not allocate table.");
  228. }
  229. if (argc > init_value_arg) {
  230. Map_merge_into_self(_self, argv[init_value_arg]);
  231. }
  232. return Qnil;
  233. }
  234. /*
  235. * call-seq:
  236. * Map.each(&block)
  237. *
  238. * Invokes &block on each |key, value| pair in the map, in unspecified order.
  239. * Note that Map also includes Enumerable; map thus acts like a normal Ruby
  240. * sequence.
  241. */
  242. VALUE Map_each(VALUE _self) {
  243. Map* self = ruby_to_Map(_self);
  244. upb_strtable_iter it;
  245. for (upb_strtable_begin(&it, &self->table);
  246. !upb_strtable_done(&it);
  247. upb_strtable_next(&it)) {
  248. VALUE key = table_key_to_ruby(
  249. self, upb_strtable_iter_key(&it), upb_strtable_iter_keylength(&it));
  250. upb_value v = upb_strtable_iter_value(&it);
  251. void* mem = value_memory(&v);
  252. VALUE value = native_slot_get(self->value_type,
  253. self->value_type_class,
  254. mem);
  255. rb_yield_values(2, key, value);
  256. }
  257. return Qnil;
  258. }
  259. /*
  260. * call-seq:
  261. * Map.keys => [list_of_keys]
  262. *
  263. * Returns the list of keys contained in the map, in unspecified order.
  264. */
  265. VALUE Map_keys(VALUE _self) {
  266. Map* self = ruby_to_Map(_self);
  267. VALUE ret = rb_ary_new();
  268. upb_strtable_iter it;
  269. for (upb_strtable_begin(&it, &self->table);
  270. !upb_strtable_done(&it);
  271. upb_strtable_next(&it)) {
  272. VALUE key = table_key_to_ruby(
  273. self, upb_strtable_iter_key(&it), upb_strtable_iter_keylength(&it));
  274. rb_ary_push(ret, key);
  275. }
  276. return ret;
  277. }
  278. /*
  279. * call-seq:
  280. * Map.values => [list_of_values]
  281. *
  282. * Returns the list of values contained in the map, in unspecified order.
  283. */
  284. VALUE Map_values(VALUE _self) {
  285. Map* self = ruby_to_Map(_self);
  286. VALUE ret = rb_ary_new();
  287. upb_strtable_iter it;
  288. for (upb_strtable_begin(&it, &self->table);
  289. !upb_strtable_done(&it);
  290. upb_strtable_next(&it)) {
  291. upb_value v = upb_strtable_iter_value(&it);
  292. void* mem = value_memory(&v);
  293. VALUE value = native_slot_get(self->value_type,
  294. self->value_type_class,
  295. mem);
  296. rb_ary_push(ret, value);
  297. }
  298. return ret;
  299. }
  300. /*
  301. * call-seq:
  302. * Map.[](key) => value
  303. *
  304. * Accesses the element at the given key. Throws an exception if the key type is
  305. * incorrect. Returns nil when the key is not present in the map.
  306. */
  307. VALUE Map_index(VALUE _self, VALUE key) {
  308. Map* self = ruby_to_Map(_self);
  309. char keybuf[TABLE_KEY_BUF_LENGTH];
  310. const char* keyval = NULL;
  311. size_t length = 0;
  312. upb_value v;
  313. table_key(self, key, keybuf, &keyval, &length);
  314. if (upb_strtable_lookup2(&self->table, keyval, length, &v)) {
  315. void* mem = value_memory(&v);
  316. return native_slot_get(self->value_type, self->value_type_class, mem);
  317. } else {
  318. return Qnil;
  319. }
  320. }
  321. /*
  322. * call-seq:
  323. * Map.[]=(key, value) => value
  324. *
  325. * Inserts or overwrites the value at the given key with the given new value.
  326. * Throws an exception if the key type is incorrect. Returns the new value that
  327. * was just inserted.
  328. */
  329. VALUE Map_index_set(VALUE _self, VALUE key, VALUE value) {
  330. Map* self = ruby_to_Map(_self);
  331. char keybuf[TABLE_KEY_BUF_LENGTH];
  332. const char* keyval = NULL;
  333. size_t length = 0;
  334. upb_value v;
  335. void* mem;
  336. table_key(self, key, keybuf, &keyval, &length);
  337. mem = value_memory(&v);
  338. native_slot_set(self->value_type, self->value_type_class, mem, value);
  339. // Replace any existing value by issuing a 'remove' operation first.
  340. upb_strtable_remove2(&self->table, keyval, length, NULL);
  341. if (!upb_strtable_insert2(&self->table, keyval, length, v)) {
  342. rb_raise(rb_eRuntimeError, "Could not insert into table");
  343. }
  344. // Ruby hashmap's :[]= method also returns the inserted value.
  345. return value;
  346. }
  347. /*
  348. * call-seq:
  349. * Map.has_key?(key) => bool
  350. *
  351. * Returns true if the given key is present in the map. Throws an exception if
  352. * the key has the wrong type.
  353. */
  354. VALUE Map_has_key(VALUE _self, VALUE key) {
  355. Map* self = ruby_to_Map(_self);
  356. char keybuf[TABLE_KEY_BUF_LENGTH];
  357. const char* keyval = NULL;
  358. size_t length = 0;
  359. table_key(self, key, keybuf, &keyval, &length);
  360. if (upb_strtable_lookup2(&self->table, keyval, length, NULL)) {
  361. return Qtrue;
  362. } else {
  363. return Qfalse;
  364. }
  365. }
  366. /*
  367. * call-seq:
  368. * Map.delete(key) => old_value
  369. *
  370. * Deletes the value at the given key, if any, returning either the old value or
  371. * nil if none was present. Throws an exception if the key is of the wrong type.
  372. */
  373. VALUE Map_delete(VALUE _self, VALUE key) {
  374. Map* self = ruby_to_Map(_self);
  375. char keybuf[TABLE_KEY_BUF_LENGTH];
  376. const char* keyval = NULL;
  377. size_t length = 0;
  378. upb_value v;
  379. table_key(self, key, keybuf, &keyval, &length);
  380. if (upb_strtable_remove2(&self->table, keyval, length, &v)) {
  381. void* mem = value_memory(&v);
  382. return native_slot_get(self->value_type, self->value_type_class, mem);
  383. } else {
  384. return Qnil;
  385. }
  386. }
  387. /*
  388. * call-seq:
  389. * Map.clear
  390. *
  391. * Removes all entries from the map.
  392. */
  393. VALUE Map_clear(VALUE _self) {
  394. Map* self = ruby_to_Map(_self);
  395. // Uninit and reinit the table -- this is faster than iterating and doing a
  396. // delete-lookup on each key.
  397. upb_strtable_uninit(&self->table);
  398. if (!upb_strtable_init(&self->table, UPB_CTYPE_INT64)) {
  399. rb_raise(rb_eRuntimeError, "Unable to re-initialize table");
  400. }
  401. return Qnil;
  402. }
  403. /*
  404. * call-seq:
  405. * Map.length
  406. *
  407. * Returns the number of entries (key-value pairs) in the map.
  408. */
  409. VALUE Map_length(VALUE _self) {
  410. Map* self = ruby_to_Map(_self);
  411. return ULL2NUM(upb_strtable_count(&self->table));
  412. }
  413. static VALUE Map_new_this_type(VALUE _self) {
  414. Map* self = ruby_to_Map(_self);
  415. VALUE new_map = Qnil;
  416. VALUE key_type = fieldtype_to_ruby(self->key_type);
  417. VALUE value_type = fieldtype_to_ruby(self->value_type);
  418. if (self->value_type_class != Qnil) {
  419. new_map = rb_funcall(CLASS_OF(_self), rb_intern("new"), 3,
  420. key_type, value_type, self->value_type_class);
  421. } else {
  422. new_map = rb_funcall(CLASS_OF(_self), rb_intern("new"), 2,
  423. key_type, value_type);
  424. }
  425. return new_map;
  426. }
  427. /*
  428. * call-seq:
  429. * Map.dup => new_map
  430. *
  431. * Duplicates this map with a shallow copy. References to all non-primitive
  432. * element objects (e.g., submessages) are shared.
  433. */
  434. VALUE Map_dup(VALUE _self) {
  435. Map* self = ruby_to_Map(_self);
  436. VALUE new_map = Map_new_this_type(_self);
  437. Map* new_self = ruby_to_Map(new_map);
  438. upb_strtable_iter it;
  439. for (upb_strtable_begin(&it, &self->table);
  440. !upb_strtable_done(&it);
  441. upb_strtable_next(&it)) {
  442. upb_value v = upb_strtable_iter_value(&it);
  443. void* mem = value_memory(&v);
  444. upb_value dup;
  445. void* dup_mem = value_memory(&dup);
  446. native_slot_dup(self->value_type, dup_mem, mem);
  447. if (!upb_strtable_insert2(&new_self->table,
  448. upb_strtable_iter_key(&it),
  449. upb_strtable_iter_keylength(&it),
  450. dup)) {
  451. rb_raise(rb_eRuntimeError, "Error inserting value into new table");
  452. }
  453. }
  454. return new_map;
  455. }
  456. // Used by Google::Protobuf.deep_copy but not exposed directly.
  457. VALUE Map_deep_copy(VALUE _self) {
  458. Map* self = ruby_to_Map(_self);
  459. VALUE new_map = Map_new_this_type(_self);
  460. Map* new_self = ruby_to_Map(new_map);
  461. upb_strtable_iter it;
  462. for (upb_strtable_begin(&it, &self->table);
  463. !upb_strtable_done(&it);
  464. upb_strtable_next(&it)) {
  465. upb_value v = upb_strtable_iter_value(&it);
  466. void* mem = value_memory(&v);
  467. upb_value dup;
  468. void* dup_mem = value_memory(&dup);
  469. native_slot_deep_copy(self->value_type, dup_mem, mem);
  470. if (!upb_strtable_insert2(&new_self->table,
  471. upb_strtable_iter_key(&it),
  472. upb_strtable_iter_keylength(&it),
  473. dup)) {
  474. rb_raise(rb_eRuntimeError, "Error inserting value into new table");
  475. }
  476. }
  477. return new_map;
  478. }
  479. /*
  480. * call-seq:
  481. * Map.==(other) => boolean
  482. *
  483. * Compares this map to another. Maps are equal if they have identical key sets,
  484. * and for each key, the values in both maps compare equal. Elements are
  485. * compared as per normal Ruby semantics, by calling their :== methods (or
  486. * performing a more efficient comparison for primitive types).
  487. *
  488. * Maps with dissimilar key types or value types/typeclasses are never equal,
  489. * even if value comparison (for example, between integers and floats) would
  490. * have otherwise indicated that every element has equal value.
  491. */
  492. VALUE Map_eq(VALUE _self, VALUE _other) {
  493. Map* self = ruby_to_Map(_self);
  494. Map* other;
  495. upb_strtable_iter it;
  496. // Allow comparisons to Ruby hashmaps by converting to a temporary Map
  497. // instance. Slow, but workable.
  498. if (TYPE(_other) == T_HASH) {
  499. VALUE other_map = Map_new_this_type(_self);
  500. Map_merge_into_self(other_map, _other);
  501. _other = other_map;
  502. }
  503. other = ruby_to_Map(_other);
  504. if (self == other) {
  505. return Qtrue;
  506. }
  507. if (self->key_type != other->key_type ||
  508. self->value_type != other->value_type ||
  509. self->value_type_class != other->value_type_class) {
  510. return Qfalse;
  511. }
  512. if (upb_strtable_count(&self->table) != upb_strtable_count(&other->table)) {
  513. return Qfalse;
  514. }
  515. // For each member of self, check that an equal member exists at the same key
  516. // in other.
  517. for (upb_strtable_begin(&it, &self->table);
  518. !upb_strtable_done(&it);
  519. upb_strtable_next(&it)) {
  520. upb_value v = upb_strtable_iter_value(&it);
  521. void* mem = value_memory(&v);
  522. upb_value other_v;
  523. void* other_mem = value_memory(&other_v);
  524. if (!upb_strtable_lookup2(&other->table,
  525. upb_strtable_iter_key(&it),
  526. upb_strtable_iter_keylength(&it),
  527. &other_v)) {
  528. // Not present in other map.
  529. return Qfalse;
  530. }
  531. if (!native_slot_eq(self->value_type, mem, other_mem)) {
  532. // Present, but value not equal.
  533. return Qfalse;
  534. }
  535. }
  536. return Qtrue;
  537. }
  538. /*
  539. * call-seq:
  540. * Map.hash => hash_value
  541. *
  542. * Returns a hash value based on this map's contents.
  543. */
  544. VALUE Map_hash(VALUE _self) {
  545. Map* self = ruby_to_Map(_self);
  546. st_index_t h = rb_hash_start(0);
  547. VALUE hash_sym = rb_intern("hash");
  548. upb_strtable_iter it;
  549. for (upb_strtable_begin(&it, &self->table);
  550. !upb_strtable_done(&it);
  551. upb_strtable_next(&it)) {
  552. VALUE key = table_key_to_ruby(
  553. self, upb_strtable_iter_key(&it), upb_strtable_iter_keylength(&it));
  554. upb_value v = upb_strtable_iter_value(&it);
  555. void* mem = value_memory(&v);
  556. VALUE value = native_slot_get(self->value_type,
  557. self->value_type_class,
  558. mem);
  559. h = rb_hash_uint(h, NUM2LONG(rb_funcall(key, hash_sym, 0)));
  560. h = rb_hash_uint(h, NUM2LONG(rb_funcall(value, hash_sym, 0)));
  561. }
  562. return INT2FIX(h);
  563. }
  564. /*
  565. * call-seq:
  566. * Map.inspect => string
  567. *
  568. * Returns a string representing this map's elements. It will be formatted as
  569. * "{key => value, key => value, ...}", with each key and value string
  570. * representation computed by its own #inspect method.
  571. */
  572. VALUE Map_inspect(VALUE _self) {
  573. Map* self = ruby_to_Map(_self);
  574. VALUE str = rb_str_new2("{");
  575. bool first = true;
  576. VALUE inspect_sym = rb_intern("inspect");
  577. upb_strtable_iter it;
  578. for (upb_strtable_begin(&it, &self->table);
  579. !upb_strtable_done(&it);
  580. upb_strtable_next(&it)) {
  581. VALUE key = table_key_to_ruby(
  582. self, upb_strtable_iter_key(&it), upb_strtable_iter_keylength(&it));
  583. upb_value v = upb_strtable_iter_value(&it);
  584. void* mem = value_memory(&v);
  585. VALUE value = native_slot_get(self->value_type,
  586. self->value_type_class,
  587. mem);
  588. if (!first) {
  589. str = rb_str_cat2(str, ", ");
  590. } else {
  591. first = false;
  592. }
  593. str = rb_str_append(str, rb_funcall(key, inspect_sym, 0));
  594. str = rb_str_cat2(str, "=>");
  595. str = rb_str_append(str, rb_funcall(value, inspect_sym, 0));
  596. }
  597. str = rb_str_cat2(str, "}");
  598. return str;
  599. }
  600. /*
  601. * call-seq:
  602. * Map.merge(other_map) => map
  603. *
  604. * Copies key/value pairs from other_map into a copy of this map. If a key is
  605. * set in other_map and this map, the value from other_map overwrites the value
  606. * in the new copy of this map. Returns the new copy of this map with merged
  607. * contents.
  608. */
  609. VALUE Map_merge(VALUE _self, VALUE hashmap) {
  610. VALUE dupped = Map_dup(_self);
  611. return Map_merge_into_self(dupped, hashmap);
  612. }
  613. static int merge_into_self_callback(VALUE key, VALUE value, VALUE self) {
  614. Map_index_set(self, key, value);
  615. return ST_CONTINUE;
  616. }
  617. // Used only internally -- shared by #merge and #initialize.
  618. VALUE Map_merge_into_self(VALUE _self, VALUE hashmap) {
  619. if (TYPE(hashmap) == T_HASH) {
  620. rb_hash_foreach(hashmap, merge_into_self_callback, _self);
  621. } else if (RB_TYPE_P(hashmap, T_DATA) && RTYPEDDATA_P(hashmap) &&
  622. RTYPEDDATA_TYPE(hashmap) == &Map_type) {
  623. Map* self = ruby_to_Map(_self);
  624. Map* other = ruby_to_Map(hashmap);
  625. upb_strtable_iter it;
  626. if (self->key_type != other->key_type ||
  627. self->value_type != other->value_type ||
  628. self->value_type_class != other->value_type_class) {
  629. rb_raise(rb_eArgError, "Attempt to merge Map with mismatching types");
  630. }
  631. for (upb_strtable_begin(&it, &other->table);
  632. !upb_strtable_done(&it);
  633. upb_strtable_next(&it)) {
  634. // Replace any existing value by issuing a 'remove' operation first.
  635. upb_value v;
  636. upb_value oldv;
  637. upb_strtable_remove2(&self->table,
  638. upb_strtable_iter_key(&it),
  639. upb_strtable_iter_keylength(&it),
  640. &oldv);
  641. v = upb_strtable_iter_value(&it);
  642. upb_strtable_insert2(&self->table,
  643. upb_strtable_iter_key(&it),
  644. upb_strtable_iter_keylength(&it),
  645. v);
  646. }
  647. } else {
  648. rb_raise(rb_eArgError, "Unknown type merging into Map");
  649. }
  650. return _self;
  651. }
  652. // Internal method: map iterator initialization (used for serialization).
  653. void Map_begin(VALUE _self, Map_iter* iter) {
  654. Map* self = ruby_to_Map(_self);
  655. iter->self = self;
  656. upb_strtable_begin(&iter->it, &self->table);
  657. }
  658. void Map_next(Map_iter* iter) {
  659. upb_strtable_next(&iter->it);
  660. }
  661. bool Map_done(Map_iter* iter) {
  662. return upb_strtable_done(&iter->it);
  663. }
  664. VALUE Map_iter_key(Map_iter* iter) {
  665. return table_key_to_ruby(
  666. iter->self,
  667. upb_strtable_iter_key(&iter->it),
  668. upb_strtable_iter_keylength(&iter->it));
  669. }
  670. VALUE Map_iter_value(Map_iter* iter) {
  671. upb_value v = upb_strtable_iter_value(&iter->it);
  672. void* mem = value_memory(&v);
  673. return native_slot_get(iter->self->value_type,
  674. iter->self->value_type_class,
  675. mem);
  676. }
  677. void Map_register(VALUE module) {
  678. VALUE klass = rb_define_class_under(module, "Map", rb_cObject);
  679. rb_define_alloc_func(klass, Map_alloc);
  680. cMap = klass;
  681. rb_gc_register_address(&cMap);
  682. rb_define_method(klass, "initialize", Map_init, -1);
  683. rb_define_method(klass, "each", Map_each, 0);
  684. rb_define_method(klass, "keys", Map_keys, 0);
  685. rb_define_method(klass, "values", Map_values, 0);
  686. rb_define_method(klass, "[]", Map_index, 1);
  687. rb_define_method(klass, "[]=", Map_index_set, 2);
  688. rb_define_method(klass, "has_key?", Map_has_key, 1);
  689. rb_define_method(klass, "delete", Map_delete, 1);
  690. rb_define_method(klass, "clear", Map_clear, 0);
  691. rb_define_method(klass, "length", Map_length, 0);
  692. rb_define_method(klass, "dup", Map_dup, 0);
  693. rb_define_method(klass, "==", Map_eq, 1);
  694. rb_define_method(klass, "hash", Map_hash, 0);
  695. rb_define_method(klass, "inspect", Map_inspect, 0);
  696. rb_define_method(klass, "merge", Map_merge, 1);
  697. rb_include_module(klass, rb_mEnumerable);
  698. }