-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathteds_stricttreemap.h
177 lines (151 loc) · 5.15 KB
/
teds_stricttreemap.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
+----------------------------------------------------------------------+
| teds extension for PHP |
| See COPYING file for further copyright information |
+----------------------------------------------------------------------+
| Author: Tyson Andre <[email protected]> |
+----------------------------------------------------------------------+
*/
#ifndef TEDS_STRICTTREEMAP_H
#define TEDS_STRICTTREEMAP_H
PHP_MINIT_FUNCTION(teds_stricttreemap);
typedef struct _teds_stricttreemap_node {
zval key;
zval value;
union {
struct {
struct _teds_stricttreemap_node *left;
struct _teds_stricttreemap_node *right;
};
struct _teds_stricttreemap_node *children[2];
};
struct _teds_stricttreemap_node *parent;
} teds_stricttreemap_node;
#define TEDS_STRICTTREEMAP_NODE_COLOR(node) Z_EXTRA((node)->value)
#define TEDS_STRICTTREEMAP_NODE_COLOR_NULLABLE(node) ((node) != NULL ? TEDS_STRICTTREEMAP_NODE_COLOR(node) : TEDS_NODE_BLACK)
typedef struct _teds_stricttreemap_tree {
struct _teds_stricttreemap_node *root;
teds_intrusive_dllist active_iterators;
uint32_t nNumOfElements;
#if PHP_VERSION_ID < 80300
bool should_rebuild_properties;
#endif
bool initialized;
} teds_stricttreemap_tree;
typedef struct _teds_stricttreemap {
teds_stricttreemap_tree tree;
zend_object std;
} teds_stricttreemap;
void teds_stricttreemap_tree_init_from_array(teds_stricttreemap_tree *tree, zend_array *values);
void teds_stricttreemap_tree_init_from_traversable(teds_stricttreemap_tree *tree, zend_object *obj);
void teds_stricttreemap_tree_dtor(teds_stricttreemap_tree *tree);
void teds_stricttreemap_tree_dtor_range(teds_stricttreemap_node *start, size_t from, size_t to);
static zend_always_inline teds_stricttreemap_node *teds_stricttreemap_node_get_leftmost(teds_stricttreemap_node *node)
{
ZEND_ASSERT(node != NULL);
while (node->left) {
node = node->left;
}
return node;
}
static zend_always_inline teds_stricttreemap_node *teds_stricttreemap_node_get_rightmost(teds_stricttreemap_node *node)
{
ZEND_ASSERT(node != NULL);
while (node->right) {
node = node->right;
}
return node;
}
static zend_always_inline teds_stricttreemap_node *teds_stricttreemap_node_get_next(const teds_stricttreemap_node *node)
{
/**
* The next node of "a" is "b". The next node of "b" is "c".
* b
* / \_
* a d
* /
* c
*/
if (node->right) {
return teds_stricttreemap_node_get_leftmost(node->right);
}
while (true) {
teds_stricttreemap_node *parent = node->parent;
if (!parent) {
return NULL;
}
ZEND_ASSERT(node != parent);
if (parent->right != node) {
ZEND_ASSERT(parent->left == node);
return parent;
}
node = parent;
}
}
static zend_always_inline teds_stricttreemap_node *teds_stricttreemap_node_get_prev(const teds_stricttreemap_node *node)
{
if (node->left) {
return teds_stricttreemap_node_get_rightmost(node->left);
}
while (true) {
teds_stricttreemap_node *parent = node->parent;
if (!parent) {
return NULL;
}
ZEND_ASSERT(node != parent);
if (parent->left != node) {
ZEND_ASSERT(parent->right == node);
return parent;
}
node = parent;
}
}
static zend_always_inline teds_stricttreemap_node *teds_stricttreemap_tree_get_first(const teds_stricttreemap_tree *tree)
{
teds_stricttreemap_node *it = tree->root;
if (it == NULL) {
return NULL;
}
return teds_stricttreemap_node_get_leftmost(it);
}
static zend_always_inline teds_stricttreemap_node *teds_stricttreemap_tree_get_last(const teds_stricttreemap_tree *tree)
{
teds_stricttreemap_node *it = tree->root;
if (it == NULL) {
return NULL;
}
return teds_stricttreemap_node_get_rightmost(it);
}
#define TEDS_STRICTTREEMAP_FOREACH(_stricttreemap) do { \
const teds_stricttreemap_tree *const __stricttreemap = (_stricttreemap); \
teds_stricttreemap_node *_p = teds_stricttreemap_tree_get_first(__stricttreemap); \
if (_p) { ZEND_ASSERT(__stricttreemap->nNumOfElements > 0); } else { ZEND_ASSERT(__stricttreemap->nNumOfElements == 0); } \
while (_p != NULL) {
#define TEDS_STRICTTREEMAP_FOREACH_END() \
_p = teds_stricttreemap_node_get_next(_p); \
} \
} while (0)
#define TEDS_STRICTTREEMAP_FOREACH_KEY_VAL(stricttreemap, k, v) TEDS_STRICTTREEMAP_FOREACH(stricttreemap) \
k = &_p->key; \
v = &_p->value;
#define TEDS_STRICTTREEMAP_FOREACH_KEY(stricttreemap, k) TEDS_STRICTTREEMAP_FOREACH(stricttreemap) \
k = &_p->key;
#define TEDS_STRICTTREEMAP_FOREACH_VAL(stricttreemap, v) TEDS_STRICTTREEMAP_FOREACH(stricttreemap) \
v = &_p->value;
#define TEDS_STRICTTREEMAP_FOREACH_NODE(stricttreemap, node) TEDS_STRICTTREEMAP_FOREACH(stricttreemap) \
node = _p;
/* Helps enforce the invariants in debug mode:
* - if capacity == 0, then entries == NULL
* - if capacity > 0, then entries != NULL
*/
static zend_always_inline bool teds_stricttreemap_tree_empty_size(const teds_stricttreemap_tree *tree)
{
if (tree->nNumOfElements > 0) {
ZEND_ASSERT(tree->root != NULL);
ZEND_ASSERT(tree->initialized);
return false;
}
ZEND_ASSERT(tree->root == NULL);
return true;
}
#endif /* TEDS_STRICTTREEMAP_H */