map.c 25 KB

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