hamt.h 3.0 KB

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