You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

map.h 4.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * Copyright (c) 2009-2021, Google LLC
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of Google LLC nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. * ARE DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY DIRECT,
  20. * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #ifndef UPB_MAP_H_
  28. #define UPB_MAP_H_
  29. #include "google/protobuf/descriptor.upb.h"
  30. #include "upb/message_value.h"
  31. // Must be last.
  32. #include "upb/port_def.inc"
  33. #ifdef __cplusplus
  34. extern "C"
  35. {
  36. #endif
  37. /* Creates a new map on the given arena with the given key/value size. */
  38. upb_Map* upb_Map_New(upb_Arena* a, upb_CType key_type, upb_CType value_type);
  39. /* Returns the number of entries in the map. */
  40. size_t upb_Map_Size(const upb_Map* map);
  41. /* Stores a value for the given key into |*val| (or the zero value if the key is
  42. * not present). Returns whether the key was present. The |val| pointer may be
  43. * NULL, in which case the function tests whether the given key is present. */
  44. bool upb_Map_Get(const upb_Map* map, upb_MessageValue key, upb_MessageValue* val);
  45. /* Removes all entries in the map. */
  46. void upb_Map_Clear(upb_Map* map);
  47. typedef enum
  48. {
  49. // LINT.IfChange
  50. kUpb_MapInsertStatus_Inserted = 0,
  51. kUpb_MapInsertStatus_Replaced = 1,
  52. kUpb_MapInsertStatus_OutOfMemory = 2,
  53. // LINT.ThenChange(//depot/google3/third_party/upb/upb/msg_internal.h)
  54. } upb_MapInsertStatus;
  55. /* Sets the given key to the given value, returning whether the key was inserted
  56. * or replaced. If the key was inserted, then any existing iterators will be
  57. * invalidated. */
  58. upb_MapInsertStatus upb_Map_Insert(upb_Map* map, upb_MessageValue key, upb_MessageValue val, upb_Arena* arena);
  59. /* Sets the given key to the given value. Returns false if memory allocation
  60. * failed. If the key is newly inserted, then any existing iterators will be
  61. * invalidated. */
  62. UPB_INLINE bool upb_Map_Set(upb_Map* map, upb_MessageValue key, upb_MessageValue val, upb_Arena* arena)
  63. {
  64. return upb_Map_Insert(map, key, val, arena) !=
  65. kUpb_MapInsertStatus_OutOfMemory;
  66. }
  67. /* Deletes this key from the table. Returns true if the key was present. */
  68. bool upb_Map_Delete(upb_Map* map, upb_MessageValue key);
  69. /* Map iteration:
  70. *
  71. * size_t iter = kUpb_Map_Begin;
  72. * while (upb_MapIterator_Next(map, &iter)) {
  73. * upb_MessageValue key = upb_MapIterator_Key(map, iter);
  74. * upb_MessageValue val = upb_MapIterator_Value(map, iter);
  75. *
  76. * // If mutating is desired.
  77. * upb_MapIterator_SetValue(map, iter, value2);
  78. * }
  79. */
  80. /* Advances to the next entry. Returns false if no more entries are present. */
  81. bool upb_MapIterator_Next(const upb_Map* map, size_t* iter);
  82. /* Returns true if the iterator still points to a valid entry, or false if the
  83. * iterator is past the last element. It is an error to call this function with
  84. * kUpb_Map_Begin (you must call next() at least once first). */
  85. bool upb_MapIterator_Done(const upb_Map* map, size_t iter);
  86. /* Returns the key and value for this entry of the map. */
  87. upb_MessageValue upb_MapIterator_Key(const upb_Map* map, size_t iter);
  88. upb_MessageValue upb_MapIterator_Value(const upb_Map* map, size_t iter);
  89. /* Sets the value for this entry. The iterator must not be done, and the
  90. * iterator must not have been initialized const. */
  91. void upb_MapIterator_SetValue(upb_Map* map, size_t iter, upb_MessageValue value);
  92. #ifdef __cplusplus
  93. } /* extern "C" */
  94. #endif
  95. #include "upb/port_undef.inc"
  96. #endif /* UPB_MAP_H_ */