1
0

dict.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #pragma once
  2. #include <stdbool.h>
  3. #include "pocketpy/objects/base.h"
  4. #include "pocketpy/common/vector.h"
  5. #ifdef __cplusplus
  6. extern "C" {
  7. #endif
  8. /** @brief `pkpy_Dict` is the Dict type in Python */
  9. typedef struct {
  10. int count; /** number of elements in the dictionary */
  11. c11_vector _entries; /** contains `pkpy_DictEntry` (hidden type) */
  12. int _htcap; /** capacity of the hashtable, always a power of 2 */
  13. void* _hashtable; /** contains indecies, can be `u8`, `u16` or `u32` according to size*/
  14. } pkpy_Dict;
  15. /** @brief `pkpy_DictIter` is used to iterate over a `pkpy_Dict` */
  16. typedef struct {
  17. const pkpy_Dict* _dict;
  18. int _index;
  19. } pkpy_DictIter;
  20. /**
  21. * @brief `pkpy_Dict` constructor
  22. * @param self `pkpy_Dict` instance
  23. */
  24. void pkpy_Dict__ctor(pkpy_Dict* self);
  25. /**
  26. * @brief `pkpy_Dict` destructor
  27. * @param self `pkpy_Dict` instance
  28. */
  29. void pkpy_Dict__dtor(pkpy_Dict* self);
  30. /**
  31. * @brief Copy a `pkpy_Dict`
  32. * @param self `pkpy_Dict` instance
  33. * @return a new `pkpy_Dict` instance, must be destructed by the caller
  34. */
  35. pkpy_Dict pkpy_Dict__copy(const pkpy_Dict* self);
  36. /**
  37. * @brief Set a key-value pair into the `pkpy_Dict`
  38. * @param self `pkpy_Dict` instance
  39. * @param vm __eq__ and __hash__ context
  40. * @param key key to set
  41. * @param val value to set
  42. * @return `true` if the key is newly added, `false` if the key already exists
  43. */
  44. bool pkpy_Dict__set(pkpy_Dict* self, PyVar key, PyVar val);
  45. /**
  46. * @brief Check if a key exists in the `pkpy_Dict`
  47. * @param self `pkpy_Dict` instance
  48. * @param vm __eq__ and __hash__ context
  49. * @param key key to check
  50. * @return `true` if the key exists, `false` otherwise
  51. */
  52. bool pkpy_Dict__contains(const pkpy_Dict* self, PyVar key);
  53. /**
  54. * @brief Remove a key from the `pkpy_Dict`
  55. * @param self `pkpy_Dict` instance
  56. * @param vm __eq__ and __hash__ context
  57. * @param key key to remove
  58. * @return `true` if the key was found and removed, `false` if the key doesn't exist
  59. */
  60. bool pkpy_Dict__del(pkpy_Dict* self, PyVar key);
  61. /**
  62. * @brief Try to get a value from the `pkpy_Dict`
  63. * @param self `pkpy_Dict` instance
  64. * @param vm __eq__ and __hash__ context
  65. * @param key key to get
  66. * @return the value associated with the key, `NULL` if the key doesn't exist
  67. */
  68. const PyVar* pkpy_Dict__try_get(const pkpy_Dict* self, PyVar key);
  69. /**
  70. * @brief Update the `pkpy_Dict` with another one
  71. * @param self `pkpy_Dict` instance
  72. * @param vm __eq__ and __hash__ context
  73. * @param other `pkpy_Dict` instance to update with
  74. */
  75. void pkpy_Dict__update(pkpy_Dict* self, const pkpy_Dict* other);
  76. /**
  77. * @brief Clear the `pkpy_Dict`
  78. * @param self `pkpy_Dict` instance
  79. */
  80. void pkpy_Dict__clear(pkpy_Dict* self);
  81. /**
  82. * @brief Iterate over the `pkpy_Dict`
  83. * @param self `pkpy_Dict` instance
  84. * @return an iterator over the `pkpy_Dict`
  85. */
  86. pkpy_DictIter pkpy_Dict__iter(const pkpy_Dict* self);
  87. /**
  88. * @brief Iterate over the `pkpy_Dict`
  89. * @param self `pkpy_Dict` instance
  90. * @param key key will be filled with the current key, can be `NULL` if not needed
  91. * @param value value will be filled with the current value, can be `NULL` if not needed
  92. * @return `true` if the iteration is still valid, `false` otherwise
  93. */
  94. bool pkpy_DictIter__next(pkpy_DictIter* self, PyVar* key, PyVar* value);
  95. #ifdef __cplusplus
  96. }
  97. #endif