map.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849
  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 VALUE 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. key = native_slot_encode_and_freeze_string(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. return key;
  90. }
  91. static VALUE table_key_to_ruby(Map* self, const char* buf, size_t length) {
  92. switch (self->key_type) {
  93. case UPB_TYPE_BYTES:
  94. case UPB_TYPE_STRING: {
  95. VALUE ret = rb_str_new(buf, length);
  96. rb_enc_associate(ret,
  97. (self->key_type == UPB_TYPE_BYTES) ?
  98. kRubyString8bitEncoding : kRubyStringUtf8Encoding);
  99. return ret;
  100. }
  101. case UPB_TYPE_BOOL:
  102. case UPB_TYPE_INT32:
  103. case UPB_TYPE_INT64:
  104. case UPB_TYPE_UINT32:
  105. case UPB_TYPE_UINT64:
  106. return native_slot_get(self->key_type, Qnil, buf);
  107. default:
  108. assert(false);
  109. return Qnil;
  110. }
  111. }
  112. static void* value_memory(upb_value* v) {
  113. return (void*)(&v->val);
  114. }
  115. // -----------------------------------------------------------------------------
  116. // Map container type.
  117. // -----------------------------------------------------------------------------
  118. const rb_data_type_t Map_type = {
  119. "Google::Protobuf::Map",
  120. { Map_mark, Map_free, NULL },
  121. };
  122. VALUE cMap;
  123. Map* ruby_to_Map(VALUE _self) {
  124. Map* self;
  125. TypedData_Get_Struct(_self, Map, &Map_type, self);
  126. return self;
  127. }
  128. void Map_mark(void* _self) {
  129. Map* self = _self;
  130. rb_gc_mark(self->value_type_class);
  131. rb_gc_mark(self->parse_frame);
  132. if (self->value_type == UPB_TYPE_STRING ||
  133. self->value_type == UPB_TYPE_BYTES ||
  134. self->value_type == UPB_TYPE_MESSAGE) {
  135. upb_strtable_iter it;
  136. for (upb_strtable_begin(&it, &self->table);
  137. !upb_strtable_done(&it);
  138. upb_strtable_next(&it)) {
  139. upb_value v = upb_strtable_iter_value(&it);
  140. void* mem = value_memory(&v);
  141. native_slot_mark(self->value_type, mem);
  142. }
  143. }
  144. }
  145. void Map_free(void* _self) {
  146. Map* self = _self;
  147. upb_strtable_uninit(&self->table);
  148. xfree(self);
  149. }
  150. VALUE Map_alloc(VALUE klass) {
  151. Map* self = ALLOC(Map);
  152. memset(self, 0, sizeof(Map));
  153. self->value_type_class = Qnil;
  154. return TypedData_Wrap_Struct(klass, &Map_type, self);
  155. }
  156. VALUE Map_set_frame(VALUE map, VALUE val) {
  157. Map* self = ruby_to_Map(map);
  158. self->parse_frame = val;
  159. return val;
  160. }
  161. static bool needs_typeclass(upb_fieldtype_t type) {
  162. switch (type) {
  163. case UPB_TYPE_MESSAGE:
  164. case UPB_TYPE_ENUM:
  165. return true;
  166. default:
  167. return false;
  168. }
  169. }
  170. /*
  171. * call-seq:
  172. * Map.new(key_type, value_type, value_typeclass = nil, init_hashmap = {})
  173. * => new map
  174. *
  175. * Allocates a new Map container. This constructor may be called with 2, 3, or 4
  176. * arguments. The first two arguments are always present and are symbols (taking
  177. * on the same values as field-type symbols in message descriptors) that
  178. * indicate the type of the map key and value fields.
  179. *
  180. * The supported key types are: :int32, :int64, :uint32, :uint64, :bool,
  181. * :string, :bytes.
  182. *
  183. * The supported value types are: :int32, :int64, :uint32, :uint64, :bool,
  184. * :string, :bytes, :enum, :message.
  185. *
  186. * The third argument, value_typeclass, must be present if value_type is :enum
  187. * or :message. As in RepeatedField#new, this argument must be a message class
  188. * (for :message) or enum module (for :enum).
  189. *
  190. * The last argument, if present, provides initial content for map. Note that
  191. * this may be an ordinary Ruby hashmap or another Map instance with identical
  192. * key and value types. Also note that this argument may be present whether or
  193. * not value_typeclass is present (and it is unambiguously separate from
  194. * value_typeclass because value_typeclass's presence is strictly determined by
  195. * value_type). The contents of this initial hashmap or Map instance are
  196. * shallow-copied into the new Map: the original map is unmodified, but
  197. * references to underlying objects will be shared if the value type is a
  198. * message type.
  199. */
  200. VALUE Map_init(int argc, VALUE* argv, VALUE _self) {
  201. Map* self = ruby_to_Map(_self);
  202. int init_value_arg;
  203. // We take either two args (:key_type, :value_type), three args (:key_type,
  204. // :value_type, "ValueMessageType"), or four args (the above plus an initial
  205. // hashmap).
  206. if (argc < 2 || argc > 4) {
  207. rb_raise(rb_eArgError, "Map constructor expects 2, 3 or 4 arguments.");
  208. }
  209. self->key_type = ruby_to_fieldtype(argv[0]);
  210. self->value_type = ruby_to_fieldtype(argv[1]);
  211. self->parse_frame = Qnil;
  212. // Check that the key type is an allowed type.
  213. switch (self->key_type) {
  214. case UPB_TYPE_INT32:
  215. case UPB_TYPE_INT64:
  216. case UPB_TYPE_UINT32:
  217. case UPB_TYPE_UINT64:
  218. case UPB_TYPE_BOOL:
  219. case UPB_TYPE_STRING:
  220. case UPB_TYPE_BYTES:
  221. // These are OK.
  222. break;
  223. default:
  224. rb_raise(rb_eArgError, "Invalid key type for map.");
  225. }
  226. init_value_arg = 2;
  227. if (needs_typeclass(self->value_type) && argc > 2) {
  228. self->value_type_class = argv[2];
  229. validate_type_class(self->value_type, self->value_type_class);
  230. init_value_arg = 3;
  231. }
  232. // Table value type is always UINT64: this ensures enough space to store the
  233. // native_slot value.
  234. if (!upb_strtable_init(&self->table, UPB_CTYPE_UINT64)) {
  235. rb_raise(rb_eRuntimeError, "Could not allocate table.");
  236. }
  237. if (argc > init_value_arg) {
  238. Map_merge_into_self(_self, argv[init_value_arg]);
  239. }
  240. return Qnil;
  241. }
  242. /*
  243. * call-seq:
  244. * Map.each(&block)
  245. *
  246. * Invokes &block on each |key, value| pair in the map, in unspecified order.
  247. * Note that Map also includes Enumerable; map thus acts like a normal Ruby
  248. * sequence.
  249. */
  250. VALUE Map_each(VALUE _self) {
  251. Map* self = ruby_to_Map(_self);
  252. upb_strtable_iter it;
  253. for (upb_strtable_begin(&it, &self->table);
  254. !upb_strtable_done(&it);
  255. upb_strtable_next(&it)) {
  256. VALUE key = table_key_to_ruby(
  257. self, upb_strtable_iter_key(&it), upb_strtable_iter_keylength(&it));
  258. upb_value v = upb_strtable_iter_value(&it);
  259. void* mem = value_memory(&v);
  260. VALUE value = native_slot_get(self->value_type,
  261. self->value_type_class,
  262. mem);
  263. rb_yield_values(2, key, value);
  264. }
  265. return Qnil;
  266. }
  267. /*
  268. * call-seq:
  269. * Map.keys => [list_of_keys]
  270. *
  271. * Returns the list of keys contained in the map, in unspecified order.
  272. */
  273. VALUE Map_keys(VALUE _self) {
  274. Map* self = ruby_to_Map(_self);
  275. VALUE ret = rb_ary_new();
  276. upb_strtable_iter it;
  277. for (upb_strtable_begin(&it, &self->table);
  278. !upb_strtable_done(&it);
  279. upb_strtable_next(&it)) {
  280. VALUE key = table_key_to_ruby(
  281. self, upb_strtable_iter_key(&it), upb_strtable_iter_keylength(&it));
  282. rb_ary_push(ret, key);
  283. }
  284. return ret;
  285. }
  286. /*
  287. * call-seq:
  288. * Map.values => [list_of_values]
  289. *
  290. * Returns the list of values contained in the map, in unspecified order.
  291. */
  292. VALUE Map_values(VALUE _self) {
  293. Map* self = ruby_to_Map(_self);
  294. VALUE ret = rb_ary_new();
  295. upb_strtable_iter it;
  296. for (upb_strtable_begin(&it, &self->table);
  297. !upb_strtable_done(&it);
  298. upb_strtable_next(&it)) {
  299. upb_value v = upb_strtable_iter_value(&it);
  300. void* mem = value_memory(&v);
  301. VALUE value = native_slot_get(self->value_type,
  302. self->value_type_class,
  303. mem);
  304. rb_ary_push(ret, value);
  305. }
  306. return ret;
  307. }
  308. /*
  309. * call-seq:
  310. * Map.[](key) => value
  311. *
  312. * Accesses the element at the given key. Throws an exception if the key type is
  313. * incorrect. Returns nil when the key is not present in the map.
  314. */
  315. VALUE Map_index(VALUE _self, VALUE key) {
  316. Map* self = ruby_to_Map(_self);
  317. char keybuf[TABLE_KEY_BUF_LENGTH];
  318. const char* keyval = NULL;
  319. size_t length = 0;
  320. upb_value v;
  321. key = table_key(self, key, keybuf, &keyval, &length);
  322. if (upb_strtable_lookup2(&self->table, keyval, length, &v)) {
  323. void* mem = value_memory(&v);
  324. return native_slot_get(self->value_type, self->value_type_class, mem);
  325. } else {
  326. return Qnil;
  327. }
  328. }
  329. /*
  330. * call-seq:
  331. * Map.[]=(key, value) => value
  332. *
  333. * Inserts or overwrites the value at the given key with the given new value.
  334. * Throws an exception if the key type is incorrect. Returns the new value that
  335. * was just inserted.
  336. */
  337. VALUE Map_index_set(VALUE _self, VALUE key, VALUE value) {
  338. Map* self = ruby_to_Map(_self);
  339. char keybuf[TABLE_KEY_BUF_LENGTH];
  340. const char* keyval = NULL;
  341. size_t length = 0;
  342. upb_value v;
  343. void* mem;
  344. key = table_key(self, key, keybuf, &keyval, &length);
  345. mem = value_memory(&v);
  346. native_slot_set(self->value_type, self->value_type_class, mem, value);
  347. // Replace any existing value by issuing a 'remove' operation first.
  348. upb_strtable_remove2(&self->table, keyval, length, NULL);
  349. if (!upb_strtable_insert2(&self->table, keyval, length, v)) {
  350. rb_raise(rb_eRuntimeError, "Could not insert into table");
  351. }
  352. // Ruby hashmap's :[]= method also returns the inserted value.
  353. return value;
  354. }
  355. /*
  356. * call-seq:
  357. * Map.has_key?(key) => bool
  358. *
  359. * Returns true if the given key is present in the map. Throws an exception if
  360. * the key has the wrong type.
  361. */
  362. VALUE Map_has_key(VALUE _self, VALUE key) {
  363. Map* self = ruby_to_Map(_self);
  364. char keybuf[TABLE_KEY_BUF_LENGTH];
  365. const char* keyval = NULL;
  366. size_t length = 0;
  367. key = table_key(self, key, keybuf, &keyval, &length);
  368. if (upb_strtable_lookup2(&self->table, keyval, length, NULL)) {
  369. return Qtrue;
  370. } else {
  371. return Qfalse;
  372. }
  373. }
  374. /*
  375. * call-seq:
  376. * Map.delete(key) => old_value
  377. *
  378. * Deletes the value at the given key, if any, returning either the old value or
  379. * nil if none was present. Throws an exception if the key is of the wrong type.
  380. */
  381. VALUE Map_delete(VALUE _self, VALUE key) {
  382. Map* self = ruby_to_Map(_self);
  383. char keybuf[TABLE_KEY_BUF_LENGTH];
  384. const char* keyval = NULL;
  385. size_t length = 0;
  386. upb_value v;
  387. key = table_key(self, key, keybuf, &keyval, &length);
  388. if (upb_strtable_remove2(&self->table, keyval, length, &v)) {
  389. void* mem = value_memory(&v);
  390. return native_slot_get(self->value_type, self->value_type_class, mem);
  391. } else {
  392. return Qnil;
  393. }
  394. }
  395. /*
  396. * call-seq:
  397. * Map.clear
  398. *
  399. * Removes all entries from the map.
  400. */
  401. VALUE Map_clear(VALUE _self) {
  402. Map* self = ruby_to_Map(_self);
  403. // Uninit and reinit the table -- this is faster than iterating and doing a
  404. // delete-lookup on each key.
  405. upb_strtable_uninit(&self->table);
  406. if (!upb_strtable_init(&self->table, UPB_CTYPE_INT64)) {
  407. rb_raise(rb_eRuntimeError, "Unable to re-initialize table");
  408. }
  409. return Qnil;
  410. }
  411. /*
  412. * call-seq:
  413. * Map.length
  414. *
  415. * Returns the number of entries (key-value pairs) in the map.
  416. */
  417. VALUE Map_length(VALUE _self) {
  418. Map* self = ruby_to_Map(_self);
  419. return ULL2NUM(upb_strtable_count(&self->table));
  420. }
  421. static VALUE Map_new_this_type(VALUE _self) {
  422. Map* self = ruby_to_Map(_self);
  423. VALUE new_map = Qnil;
  424. VALUE key_type = fieldtype_to_ruby(self->key_type);
  425. VALUE value_type = fieldtype_to_ruby(self->value_type);
  426. if (self->value_type_class != Qnil) {
  427. new_map = rb_funcall(CLASS_OF(_self), rb_intern("new"), 3,
  428. key_type, value_type, self->value_type_class);
  429. } else {
  430. new_map = rb_funcall(CLASS_OF(_self), rb_intern("new"), 2,
  431. key_type, value_type);
  432. }
  433. return new_map;
  434. }
  435. /*
  436. * call-seq:
  437. * Map.dup => new_map
  438. *
  439. * Duplicates this map with a shallow copy. References to all non-primitive
  440. * element objects (e.g., submessages) are shared.
  441. */
  442. VALUE Map_dup(VALUE _self) {
  443. Map* self = ruby_to_Map(_self);
  444. VALUE new_map = Map_new_this_type(_self);
  445. Map* new_self = ruby_to_Map(new_map);
  446. upb_strtable_iter it;
  447. for (upb_strtable_begin(&it, &self->table);
  448. !upb_strtable_done(&it);
  449. upb_strtable_next(&it)) {
  450. upb_value v = upb_strtable_iter_value(&it);
  451. void* mem = value_memory(&v);
  452. upb_value dup;
  453. void* dup_mem = value_memory(&dup);
  454. native_slot_dup(self->value_type, dup_mem, mem);
  455. if (!upb_strtable_insert2(&new_self->table,
  456. upb_strtable_iter_key(&it),
  457. upb_strtable_iter_keylength(&it),
  458. dup)) {
  459. rb_raise(rb_eRuntimeError, "Error inserting value into new table");
  460. }
  461. }
  462. return new_map;
  463. }
  464. // Used by Google::Protobuf.deep_copy but not exposed directly.
  465. VALUE Map_deep_copy(VALUE _self) {
  466. Map* self = ruby_to_Map(_self);
  467. VALUE new_map = Map_new_this_type(_self);
  468. Map* new_self = ruby_to_Map(new_map);
  469. upb_strtable_iter it;
  470. for (upb_strtable_begin(&it, &self->table);
  471. !upb_strtable_done(&it);
  472. upb_strtable_next(&it)) {
  473. upb_value v = upb_strtable_iter_value(&it);
  474. void* mem = value_memory(&v);
  475. upb_value dup;
  476. void* dup_mem = value_memory(&dup);
  477. native_slot_deep_copy(self->value_type, dup_mem, mem);
  478. if (!upb_strtable_insert2(&new_self->table,
  479. upb_strtable_iter_key(&it),
  480. upb_strtable_iter_keylength(&it),
  481. dup)) {
  482. rb_raise(rb_eRuntimeError, "Error inserting value into new table");
  483. }
  484. }
  485. return new_map;
  486. }
  487. /*
  488. * call-seq:
  489. * Map.==(other) => boolean
  490. *
  491. * Compares this map to another. Maps are equal if they have identical key sets,
  492. * and for each key, the values in both maps compare equal. Elements are
  493. * compared as per normal Ruby semantics, by calling their :== methods (or
  494. * performing a more efficient comparison for primitive types).
  495. *
  496. * Maps with dissimilar key types or value types/typeclasses are never equal,
  497. * even if value comparison (for example, between integers and floats) would
  498. * have otherwise indicated that every element has equal value.
  499. */
  500. VALUE Map_eq(VALUE _self, VALUE _other) {
  501. Map* self = ruby_to_Map(_self);
  502. Map* other;
  503. upb_strtable_iter it;
  504. // Allow comparisons to Ruby hashmaps by converting to a temporary Map
  505. // instance. Slow, but workable.
  506. if (TYPE(_other) == T_HASH) {
  507. VALUE other_map = Map_new_this_type(_self);
  508. Map_merge_into_self(other_map, _other);
  509. _other = other_map;
  510. }
  511. other = ruby_to_Map(_other);
  512. if (self == other) {
  513. return Qtrue;
  514. }
  515. if (self->key_type != other->key_type ||
  516. self->value_type != other->value_type ||
  517. self->value_type_class != other->value_type_class) {
  518. return Qfalse;
  519. }
  520. if (upb_strtable_count(&self->table) != upb_strtable_count(&other->table)) {
  521. return Qfalse;
  522. }
  523. // For each member of self, check that an equal member exists at the same key
  524. // in other.
  525. for (upb_strtable_begin(&it, &self->table);
  526. !upb_strtable_done(&it);
  527. upb_strtable_next(&it)) {
  528. upb_value v = upb_strtable_iter_value(&it);
  529. void* mem = value_memory(&v);
  530. upb_value other_v;
  531. void* other_mem = value_memory(&other_v);
  532. if (!upb_strtable_lookup2(&other->table,
  533. upb_strtable_iter_key(&it),
  534. upb_strtable_iter_keylength(&it),
  535. &other_v)) {
  536. // Not present in other map.
  537. return Qfalse;
  538. }
  539. if (!native_slot_eq(self->value_type, mem, other_mem)) {
  540. // Present, but value not equal.
  541. return Qfalse;
  542. }
  543. }
  544. return Qtrue;
  545. }
  546. /*
  547. * call-seq:
  548. * Map.hash => hash_value
  549. *
  550. * Returns a hash value based on this map's contents.
  551. */
  552. VALUE Map_hash(VALUE _self) {
  553. Map* self = ruby_to_Map(_self);
  554. st_index_t h = rb_hash_start(0);
  555. VALUE hash_sym = rb_intern("hash");
  556. upb_strtable_iter it;
  557. for (upb_strtable_begin(&it, &self->table);
  558. !upb_strtable_done(&it);
  559. upb_strtable_next(&it)) {
  560. VALUE key = table_key_to_ruby(
  561. self, upb_strtable_iter_key(&it), upb_strtable_iter_keylength(&it));
  562. upb_value v = upb_strtable_iter_value(&it);
  563. void* mem = value_memory(&v);
  564. VALUE value = native_slot_get(self->value_type,
  565. self->value_type_class,
  566. mem);
  567. h = rb_hash_uint(h, NUM2LONG(rb_funcall(key, hash_sym, 0)));
  568. h = rb_hash_uint(h, NUM2LONG(rb_funcall(value, hash_sym, 0)));
  569. }
  570. return INT2FIX(h);
  571. }
  572. /*
  573. * call-seq:
  574. * Map.to_h => {}
  575. *
  576. * Returns a Ruby Hash object containing all the values within the map
  577. */
  578. VALUE Map_to_h(VALUE _self) {
  579. Map* self = ruby_to_Map(_self);
  580. VALUE hash = rb_hash_new();
  581. upb_strtable_iter it;
  582. for (upb_strtable_begin(&it, &self->table);
  583. !upb_strtable_done(&it);
  584. upb_strtable_next(&it)) {
  585. VALUE key = table_key_to_ruby(
  586. self, upb_strtable_iter_key(&it), upb_strtable_iter_keylength(&it));
  587. upb_value v = upb_strtable_iter_value(&it);
  588. void* mem = value_memory(&v);
  589. VALUE value = native_slot_get(self->value_type,
  590. self->value_type_class,
  591. mem);
  592. if (self->value_type == UPB_TYPE_MESSAGE) {
  593. value = Message_to_h(value);
  594. }
  595. rb_hash_aset(hash, key, value);
  596. }
  597. return hash;
  598. }
  599. /*
  600. * call-seq:
  601. * Map.inspect => string
  602. *
  603. * Returns a string representing this map's elements. It will be formatted as
  604. * "{key => value, key => value, ...}", with each key and value string
  605. * representation computed by its own #inspect method.
  606. */
  607. VALUE Map_inspect(VALUE _self) {
  608. Map* self = ruby_to_Map(_self);
  609. VALUE str = rb_str_new2("{");
  610. bool first = true;
  611. VALUE inspect_sym = rb_intern("inspect");
  612. upb_strtable_iter it;
  613. for (upb_strtable_begin(&it, &self->table);
  614. !upb_strtable_done(&it);
  615. upb_strtable_next(&it)) {
  616. VALUE key = table_key_to_ruby(
  617. self, upb_strtable_iter_key(&it), upb_strtable_iter_keylength(&it));
  618. upb_value v = upb_strtable_iter_value(&it);
  619. void* mem = value_memory(&v);
  620. VALUE value = native_slot_get(self->value_type,
  621. self->value_type_class,
  622. mem);
  623. if (!first) {
  624. str = rb_str_cat2(str, ", ");
  625. } else {
  626. first = false;
  627. }
  628. str = rb_str_append(str, rb_funcall(key, inspect_sym, 0));
  629. str = rb_str_cat2(str, "=>");
  630. str = rb_str_append(str, rb_funcall(value, inspect_sym, 0));
  631. }
  632. str = rb_str_cat2(str, "}");
  633. return str;
  634. }
  635. /*
  636. * call-seq:
  637. * Map.merge(other_map) => map
  638. *
  639. * Copies key/value pairs from other_map into a copy of this map. If a key is
  640. * set in other_map and this map, the value from other_map overwrites the value
  641. * in the new copy of this map. Returns the new copy of this map with merged
  642. * contents.
  643. */
  644. VALUE Map_merge(VALUE _self, VALUE hashmap) {
  645. VALUE dupped = Map_dup(_self);
  646. return Map_merge_into_self(dupped, hashmap);
  647. }
  648. static int merge_into_self_callback(VALUE key, VALUE value, VALUE self) {
  649. Map_index_set(self, key, value);
  650. return ST_CONTINUE;
  651. }
  652. // Used only internally -- shared by #merge and #initialize.
  653. VALUE Map_merge_into_self(VALUE _self, VALUE hashmap) {
  654. if (TYPE(hashmap) == T_HASH) {
  655. rb_hash_foreach(hashmap, merge_into_self_callback, _self);
  656. } else if (RB_TYPE_P(hashmap, T_DATA) && RTYPEDDATA_P(hashmap) &&
  657. RTYPEDDATA_TYPE(hashmap) == &Map_type) {
  658. Map* self = ruby_to_Map(_self);
  659. Map* other = ruby_to_Map(hashmap);
  660. upb_strtable_iter it;
  661. if (self->key_type != other->key_type ||
  662. self->value_type != other->value_type ||
  663. self->value_type_class != other->value_type_class) {
  664. rb_raise(rb_eArgError, "Attempt to merge Map with mismatching types");
  665. }
  666. for (upb_strtable_begin(&it, &other->table);
  667. !upb_strtable_done(&it);
  668. upb_strtable_next(&it)) {
  669. // Replace any existing value by issuing a 'remove' operation first.
  670. upb_value v;
  671. upb_value oldv;
  672. upb_strtable_remove2(&self->table,
  673. upb_strtable_iter_key(&it),
  674. upb_strtable_iter_keylength(&it),
  675. &oldv);
  676. v = upb_strtable_iter_value(&it);
  677. upb_strtable_insert2(&self->table,
  678. upb_strtable_iter_key(&it),
  679. upb_strtable_iter_keylength(&it),
  680. v);
  681. }
  682. } else {
  683. rb_raise(rb_eArgError, "Unknown type merging into Map");
  684. }
  685. return _self;
  686. }
  687. // Internal method: map iterator initialization (used for serialization).
  688. void Map_begin(VALUE _self, Map_iter* iter) {
  689. Map* self = ruby_to_Map(_self);
  690. iter->self = self;
  691. upb_strtable_begin(&iter->it, &self->table);
  692. }
  693. void Map_next(Map_iter* iter) {
  694. upb_strtable_next(&iter->it);
  695. }
  696. bool Map_done(Map_iter* iter) {
  697. return upb_strtable_done(&iter->it);
  698. }
  699. VALUE Map_iter_key(Map_iter* iter) {
  700. return table_key_to_ruby(
  701. iter->self,
  702. upb_strtable_iter_key(&iter->it),
  703. upb_strtable_iter_keylength(&iter->it));
  704. }
  705. VALUE Map_iter_value(Map_iter* iter) {
  706. upb_value v = upb_strtable_iter_value(&iter->it);
  707. void* mem = value_memory(&v);
  708. return native_slot_get(iter->self->value_type,
  709. iter->self->value_type_class,
  710. mem);
  711. }
  712. void Map_register(VALUE module) {
  713. VALUE klass = rb_define_class_under(module, "Map", rb_cObject);
  714. rb_define_alloc_func(klass, Map_alloc);
  715. rb_gc_register_address(&cMap);
  716. cMap = klass;
  717. rb_define_method(klass, "initialize", Map_init, -1);
  718. rb_define_method(klass, "each", Map_each, 0);
  719. rb_define_method(klass, "keys", Map_keys, 0);
  720. rb_define_method(klass, "values", Map_values, 0);
  721. rb_define_method(klass, "[]", Map_index, 1);
  722. rb_define_method(klass, "[]=", Map_index_set, 2);
  723. rb_define_method(klass, "has_key?", Map_has_key, 1);
  724. rb_define_method(klass, "delete", Map_delete, 1);
  725. rb_define_method(klass, "clear", Map_clear, 0);
  726. rb_define_method(klass, "length", Map_length, 0);
  727. rb_define_method(klass, "dup", Map_dup, 0);
  728. rb_define_method(klass, "==", Map_eq, 1);
  729. rb_define_method(klass, "hash", Map_hash, 0);
  730. rb_define_method(klass, "to_hash", Map_to_h, 0);
  731. rb_define_method(klass, "to_h", Map_to_h, 0);
  732. rb_define_method(klass, "inspect", Map_inspect, 0);
  733. rb_define_method(klass, "merge", Map_merge, 1);
  734. rb_include_module(klass, rb_mEnumerable);
  735. }