pycore_hamt.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. #define _Py_HAMT_MAX_TREE_DEPTH 7
  7. #define PyHamt_Check(o) (Py_TYPE(o) == &_PyHamt_Type)
  8. /* Abstract tree node. */
  9. typedef struct {
  10. PyObject_HEAD
  11. } PyHamtNode;
  12. /* An HAMT immutable mapping collection. */
  13. typedef struct {
  14. PyObject_HEAD
  15. PyHamtNode *h_root;
  16. PyObject *h_weakreflist;
  17. Py_ssize_t h_count;
  18. } PyHamtObject;
  19. /* A struct to hold the state of depth-first traverse of the tree.
  20. HAMT is an immutable collection. Iterators will hold a strong reference
  21. to it, and every node in the HAMT has strong references to its children.
  22. So for iterators, we can implement zero allocations and zero reference
  23. inc/dec depth-first iteration.
  24. - i_nodes: an array of seven pointers to tree nodes
  25. - i_level: the current node in i_nodes
  26. - i_pos: an array of positions within nodes in i_nodes.
  27. */
  28. typedef struct {
  29. PyHamtNode *i_nodes[_Py_HAMT_MAX_TREE_DEPTH];
  30. Py_ssize_t i_pos[_Py_HAMT_MAX_TREE_DEPTH];
  31. int8_t i_level;
  32. } PyHamtIteratorState;
  33. /* Base iterator object.
  34. Contains the iteration state, a pointer to the HAMT tree,
  35. and a pointer to the 'yield function'. The latter is a simple
  36. function that returns a key/value tuple for the 'Items' iterator,
  37. just a key for the 'Keys' iterator, and a value for the 'Values'
  38. iterator.
  39. */
  40. typedef struct {
  41. PyObject_HEAD
  42. PyHamtObject *hi_obj;
  43. PyHamtIteratorState hi_iter;
  44. binaryfunc hi_yield;
  45. } PyHamtIterator;
  46. PyAPI_DATA(PyTypeObject) _PyHamt_Type;
  47. PyAPI_DATA(PyTypeObject) _PyHamt_ArrayNode_Type;
  48. PyAPI_DATA(PyTypeObject) _PyHamt_BitmapNode_Type;
  49. PyAPI_DATA(PyTypeObject) _PyHamt_CollisionNode_Type;
  50. PyAPI_DATA(PyTypeObject) _PyHamtKeys_Type;
  51. PyAPI_DATA(PyTypeObject) _PyHamtValues_Type;
  52. PyAPI_DATA(PyTypeObject) _PyHamtItems_Type;
  53. /* Create a new HAMT immutable mapping. */
  54. PyHamtObject * _PyHamt_New(void);
  55. /* Return a new collection based on "o", but with an additional
  56. key/val pair. */
  57. PyHamtObject * _PyHamt_Assoc(PyHamtObject *o, PyObject *key, PyObject *val);
  58. /* Return a new collection based on "o", but without "key". */
  59. PyHamtObject * _PyHamt_Without(PyHamtObject *o, PyObject *key);
  60. /* Find "key" in the "o" collection.
  61. Return:
  62. - -1: An error occurred.
  63. - 0: "key" wasn't found in "o".
  64. - 1: "key" is in "o"; "*val" is set to its value (a borrowed ref).
  65. */
  66. int _PyHamt_Find(PyHamtObject *o, PyObject *key, PyObject **val);
  67. /* Check if "v" is equal to "w".
  68. Return:
  69. - 0: v != w
  70. - 1: v == w
  71. - -1: An error occurred.
  72. */
  73. int _PyHamt_Eq(PyHamtObject *v, PyHamtObject *w);
  74. /* Return the size of "o"; equivalent of "len(o)". */
  75. Py_ssize_t _PyHamt_Len(PyHamtObject *o);
  76. /* Return a Keys iterator over "o". */
  77. PyObject * _PyHamt_NewIterKeys(PyHamtObject *o);
  78. /* Return a Values iterator over "o". */
  79. PyObject * _PyHamt_NewIterValues(PyHamtObject *o);
  80. /* Return a Items iterator over "o". */
  81. PyObject * _PyHamt_NewIterItems(PyHamtObject *o);
  82. int _PyHamt_Init(void);
  83. void _PyHamt_Fini(void);
  84. #endif /* !Py_INTERNAL_HAMT_H */