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