pycore_hamt.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #ifndef Py_INTERNAL_HAMT_H
  2. #define Py_INTERNAL_HAMT_H
  3. #ifndef Py_BUILD_CORE
  4. # error "this header requires Py_BUILD_CORE define"
  5. #endif
  6. /*
  7. HAMT tree is shaped by hashes of keys. Every group of 5 bits of a hash denotes
  8. the exact position of the key in one level of the tree. Since we're using
  9. 32 bit hashes, we can have at most 7 such levels. Although if there are
  10. two distinct keys with equal hashes, they will have to occupy the same
  11. cell in the 7th level of the tree -- so we'd put them in a "collision" node.
  12. Which brings the total possible tree depth to 8. Read more about the actual
  13. layout of the HAMT tree in `hamt.c`.
  14. This constant is used to define a datastucture for storing iteration state.
  15. */
  16. #define _Py_HAMT_MAX_TREE_DEPTH 8
  17. extern PyTypeObject _PyHamt_Type;
  18. extern PyTypeObject _PyHamt_ArrayNode_Type;
  19. extern PyTypeObject _PyHamt_BitmapNode_Type;
  20. extern PyTypeObject _PyHamt_CollisionNode_Type;
  21. extern PyTypeObject _PyHamtKeys_Type;
  22. extern PyTypeObject _PyHamtValues_Type;
  23. extern PyTypeObject _PyHamtItems_Type;
  24. /* other API */
  25. #define PyHamt_Check(o) Py_IS_TYPE((o), &_PyHamt_Type)
  26. /* Abstract tree node. */
  27. typedef struct {
  28. PyObject_HEAD
  29. } PyHamtNode;
  30. /* An HAMT immutable mapping collection. */
  31. typedef struct {
  32. PyObject_HEAD
  33. PyHamtNode *h_root;
  34. PyObject *h_weakreflist;
  35. Py_ssize_t h_count;
  36. } PyHamtObject;
  37. typedef struct {
  38. PyObject_VAR_HEAD
  39. uint32_t b_bitmap;
  40. PyObject *b_array[1];
  41. } PyHamtNode_Bitmap;
  42. /* A struct to hold the state of depth-first traverse of the tree.
  43. HAMT is an immutable collection. Iterators will hold a strong reference
  44. to it, and every node in the HAMT has strong references to its children.
  45. So for iterators, we can implement zero allocations and zero reference
  46. inc/dec depth-first iteration.
  47. - i_nodes: an array of seven pointers to tree nodes
  48. - i_level: the current node in i_nodes
  49. - i_pos: an array of positions within nodes in i_nodes.
  50. */
  51. typedef struct {
  52. PyHamtNode *i_nodes[_Py_HAMT_MAX_TREE_DEPTH];
  53. Py_ssize_t i_pos[_Py_HAMT_MAX_TREE_DEPTH];
  54. int8_t i_level;
  55. } PyHamtIteratorState;
  56. /* Base iterator object.
  57. Contains the iteration state, a pointer to the HAMT tree,
  58. and a pointer to the 'yield function'. The latter is a simple
  59. function that returns a key/value tuple for the 'Items' iterator,
  60. just a key for the 'Keys' iterator, and a value for the 'Values'
  61. iterator.
  62. */
  63. typedef struct {
  64. PyObject_HEAD
  65. PyHamtObject *hi_obj;
  66. PyHamtIteratorState hi_iter;
  67. binaryfunc hi_yield;
  68. } PyHamtIterator;
  69. /* Create a new HAMT immutable mapping. */
  70. PyHamtObject * _PyHamt_New(void);
  71. /* Return a new collection based on "o", but with an additional
  72. key/val pair. */
  73. PyHamtObject * _PyHamt_Assoc(PyHamtObject *o, PyObject *key, PyObject *val);
  74. /* Return a new collection based on "o", but without "key". */
  75. PyHamtObject * _PyHamt_Without(PyHamtObject *o, PyObject *key);
  76. /* Find "key" in the "o" collection.
  77. Return:
  78. - -1: An error occurred.
  79. - 0: "key" wasn't found in "o".
  80. - 1: "key" is in "o"; "*val" is set to its value (a borrowed ref).
  81. */
  82. int _PyHamt_Find(PyHamtObject *o, PyObject *key, PyObject **val);
  83. /* Check if "v" is equal to "w".
  84. Return:
  85. - 0: v != w
  86. - 1: v == w
  87. - -1: An error occurred.
  88. */
  89. int _PyHamt_Eq(PyHamtObject *v, PyHamtObject *w);
  90. /* Return the size of "o"; equivalent of "len(o)". */
  91. Py_ssize_t _PyHamt_Len(PyHamtObject *o);
  92. /* Return a Keys iterator over "o". */
  93. PyObject * _PyHamt_NewIterKeys(PyHamtObject *o);
  94. /* Return a Values iterator over "o". */
  95. PyObject * _PyHamt_NewIterValues(PyHamtObject *o);
  96. /* Return a Items iterator over "o". */
  97. PyObject * _PyHamt_NewIterItems(PyHamtObject *o);
  98. #endif /* !Py_INTERNAL_HAMT_H */