| 1 |
|
| #define PERL_NO_GET_CONTEXT |
| 2 |
|
| #include "EXTERN.h" |
| 3 |
|
| #include "perl.h" |
| 4 |
|
| #include "XSUB.h" |
| 5 |
|
| |
| 6 |
|
| #include "ppport.h" |
| 7 |
|
| |
| 8 |
|
| // #define ENABLE_DEBUG 1 |
| 9 |
|
| |
| 10 |
|
| #define debug(fmt, ...) \ |
| 11 |
|
| do { if (ENABLE_DEBUG) fprintf(stderr, "DEBUG: " fmt "\n", ##__VA_ARGS__); } while (0) |
| 12 |
|
| |
| 13 |
|
| typedef struct _btree_node BinaryTreeNode; |
| 14 |
|
| typedef struct _btree_pad BinaryTreePad; |
| 15 |
|
| |
| 16 |
|
| struct _btree_pad { |
| 17 |
|
| BinaryTreeNode *root; |
| 18 |
|
| HV * options; |
| 19 |
|
| }; |
| 20 |
|
| |
| 21 |
|
| struct _btree_node { |
| 22 |
|
| IV key; |
| 23 |
|
| // unsigned long size; |
| 24 |
|
| BinaryTreeNode* parent; |
| 25 |
|
| BinaryTreeNode* left; |
| 26 |
|
| BinaryTreeNode* right; |
| 27 |
|
| HV * payload; |
| 28 |
|
| }; |
| 29 |
|
| |
| 30 |
|
| |
| 31 |
|
| BinaryTreePad * btree_pad_new(); |
| 32 |
|
| |
| 33 |
|
| |
| 34 |
|
| BinaryTreeNode * btree_find_leftmost_node(BinaryTreeNode * n); |
| 35 |
|
| |
| 36 |
|
| BinaryTreeNode * btree_node_create(IV key, HV *payload); |
| 37 |
|
| |
| 38 |
|
| |
| 39 |
2 |
| BinaryTreePad * btree_pad_new() |
| 40 |
|
| { |
| 41 |
2 |
| BinaryTreePad * pad = NULL; |
| 42 |
2 |
| Newx(pad, sizeof(BinaryTreePad), BinaryTreePad); |
| 43 |
2 |
| pad->root = NULL; |
| 44 |
2 |
| pad->options = NULL; |
| 45 |
2 |
| return pad; |
| 46 |
|
| } |
| 47 |
|
| |
| 48 |
9 |
| BinaryTreeNode * btree_node_create(IV key, HV *payload) |
| 49 |
|
| { |
| 50 |
9 |
| BinaryTreeNode * new_node = NULL; |
| 51 |
9 |
| Newx(new_node, sizeof(BinaryTreeNode), BinaryTreeNode); |
| 52 |
9 |
| new_node->payload = payload; |
| 53 |
9 |
| new_node->key = key; |
| 54 |
9 |
| new_node->left = NULL; |
| 55 |
9 |
| new_node->right = NULL; |
| 56 |
9 |
| return new_node; |
| 57 |
|
| } |
| 58 |
|
| |
| 59 |
|
| |
| 60 |
|
| bool btree_update(BinaryTreeNode * node, IV key, HV * payload); |
| 61 |
|
| BinaryTreeNode * btree_search(BinaryTreeNode * node, IV key); |
| 62 |
|
| inline bool btree_insert_node_hash_by_key(BinaryTreeNode * node, IV key, HV * payload); |
| 63 |
|
| inline bool btree_pad_insert_node(BinaryTreePad *pad, IV key, HV *node_hash); |
| 64 |
|
| inline bool btree_pad_insert_node_by_key_field(BinaryTreePad *pad, char * key_field, int key_field_len, HV *node_hash); |
| 65 |
|
| inline AV * btree_pad_insert_av_nodes_by_key_field(BinaryTreePad * pad, char * key_field, unsigned int key_field_len, AV* new_nodes); |
| 66 |
|
| bool btree_node_exists(BinaryTreeNode * node, IV key); |
| 67 |
|
| |
| 68 |
|
| void btree_preorder_traverse(BinaryTreeNode * node, SV *callback); |
| 69 |
|
| void btree_inorder_traverse(BinaryTreeNode * node, SV *callback); |
| 70 |
|
| void btree_postorder_traverse(BinaryTreeNode * node, SV *callback); |
| 71 |
|
| |
| 72 |
0 |
| BinaryTreeNode * btree_find_leftmost_node(BinaryTreeNode * n) |
| 73 |
|
| { |
| 74 |
0 |
▶ | if (n->left) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (n->left) { |
0 |
0 |
0 |
|
| 75 |
0 |
| return btree_find_leftmost_node(n->left); |
| 76 |
|
| } |
| 77 |
0 |
| return n; |
| 78 |
|
| } |
| 79 |
|
| |
| 80 |
|
| bool btree_delete(BinaryTreeNode * node, IV key); |
| 81 |
|
| |
| 82 |
8 |
| void btree_node_free(BinaryTreeNode * node) |
| 83 |
|
| { |
| 84 |
8 |
▶ | if (node) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (node) { |
8 |
0 |
8 |
|
| 85 |
8 |
▶ | if (node->left) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (node->left) { |
3 |
5 |
8 |
|
| 86 |
3 |
| btree_node_free(node->left); |
| 87 |
|
| } |
| 88 |
8 |
▶ | if (node->right) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (node->right) { |
0 |
8 |
8 |
|
| 89 |
0 |
| btree_node_free(node->right); |
| 90 |
|
| } |
| 91 |
8 |
| if (node->payload) { |
| 92 |
|
| // SvREFCNT_dec(node->payload); |
| 93 |
|
| } |
| 94 |
8 |
| Safefree(node); |
| 95 |
|
| } |
| 96 |
8 |
| } |
| 97 |
|
| |
| 98 |
13 |
| bool btree_delete(BinaryTreeNode * node, IV key) |
| 99 |
|
| { |
| 100 |
13 |
▶ | if (key < node->key) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (key < node->key) { |
2 |
11 |
13 |
|
| 101 |
2 |
▶ | if (node->left) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (node->left) { |
2 |
0 |
2 |
|
| 102 |
2 |
| return btree_delete(node->left, key); |
| 103 |
|
| } else { |
| 104 |
0 |
| return FALSE; |
| 105 |
|
| } |
| 106 |
11 |
▶ | } else if (key > node->key) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (key > node->key) { |
7 |
4 |
11 |
|
| 107 |
7 |
▶ | if (node->right) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (node->right) { |
4 |
3 |
7 |
|
| 108 |
4 |
| return btree_delete(node->right, key); |
| 109 |
|
| } else { |
| 110 |
3 |
| return FALSE; |
| 111 |
|
| } |
| 112 |
4 |
▶ | } else if (key == node->key) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (key == node->key) { |
4 |
0 |
4 |
|
| 113 |
|
| |
| 114 |
4 |
▶ | if (node->left && node->right) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (node->left && node->right) { |
1 |
3 |
4 |
| if (node->left && node->right) { |
0 |
1 |
1 |
|
| 115 |
0 |
| BinaryTreeNode* leftmost = btree_find_leftmost_node(node->right); |
| 116 |
0 |
| node->key = leftmost->key; |
| 117 |
0 |
| node->payload = leftmost->payload; |
| 118 |
0 |
| btree_node_free(leftmost); |
| 119 |
4 |
▶ | } else if (node->left) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (node->left) { |
1 |
3 |
4 |
|
| 120 |
1 |
| BinaryTreeNode *to_free = node->left; |
| 121 |
1 |
| node->key = to_free->key; |
| 122 |
1 |
| node->payload = to_free->payload; |
| 123 |
1 |
| node->left = to_free->left; |
| 124 |
1 |
| Safefree(to_free); |
| 125 |
3 |
▶ | } else if (node->right) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (node->right) { |
0 |
3 |
3 |
|
| 126 |
0 |
| BinaryTreeNode *to_free = node->right; |
| 127 |
0 |
| node->key = to_free->key; |
| 128 |
0 |
| node->payload = to_free->payload; |
| 129 |
0 |
| node->right = to_free->right; |
| 130 |
0 |
| Safefree(to_free); |
| 131 |
|
| } else { |
| 132 |
3 |
▶ | if (node->parent && node->parent->left == node) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (node->parent && node->parent->left == node) { |
3 |
0 |
3 |
| if (node->parent && node->parent->left == node) { |
1 |
2 |
3 |
|
| 133 |
1 |
| node->parent->left = NULL; |
| 134 |
2 |
▶ | } else if (node->parent && node->parent->right == node) { |
Branch: 2 branches
| Branch | True | False | Total |
| } else if (node->parent && node->parent->right == node) { |
2 |
0 |
2 |
| } else if (node->parent && node->parent->right == node) { |
2 |
0 |
2 |
|
| 135 |
2 |
| node->parent->right = NULL; |
| 136 |
|
| } |
| 137 |
3 |
| btree_node_free(node); |
| 138 |
|
| } |
| 139 |
4 |
| return TRUE; |
| 140 |
|
| } |
| 141 |
0 |
| return FALSE; |
| 142 |
|
| } |
| 143 |
|
| |
| 144 |
|
| |
| 145 |
|
| static |
| 146 |
|
| inline |
| 147 |
2 |
| void callback_value(IV key, SV* node_sv, SV* callback) |
| 148 |
|
| { |
| 149 |
|
| int ret; |
| 150 |
|
| |
| 151 |
2 |
| dSP; |
| 152 |
2 |
▶ | PUSHMARK(SP); |
Branch: 1 branch
| Branch | True | False | Total |
| PUSHMARK(SP); |
0 |
2 |
2 |
|
| 153 |
2 |
▶ | XPUSHs(sv_2mortal(newSViv(key))); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(sv_2mortal(newSViv(key))); |
0 |
2 |
2 |
|
| 154 |
2 |
▶ | XPUSHs(node_sv); |
Branch: 1 branch
| Branch | True | False | Total |
| XPUSHs(node_sv); |
0 |
2 |
2 |
|
| 155 |
2 |
| PUTBACK; |
| 156 |
|
| |
| 157 |
2 |
| ret = call_sv(callback, G_SCALAR); |
| 158 |
2 |
| SPAGAIN; |
| 159 |
|
| |
| 160 |
|
| /* |
| 161 |
|
| if (!ret) |
| 162 |
|
| croak("callback did not return a value"); |
| 163 |
|
| */ |
| 164 |
|
| // IV value = POPi; |
| 165 |
2 |
| PUTBACK; |
| 166 |
|
| // return value; |
| 167 |
2 |
| } |
| 168 |
|
| |
| 169 |
0 |
| void btree_postorder_traverse(BinaryTreeNode * node, SV *callback) |
| 170 |
|
| { |
| 171 |
0 |
▶ | if (node->left) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (node->left) { |
0 |
0 |
0 |
|
| 172 |
0 |
| btree_preorder_traverse(node->left, callback); |
| 173 |
|
| } |
| 174 |
0 |
▶ | if (node->right) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (node->right) { |
0 |
0 |
0 |
|
| 175 |
0 |
| btree_preorder_traverse(node->right, callback); |
| 176 |
|
| } |
| 177 |
0 |
▶ | if (node->payload) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (node->payload) { |
0 |
0 |
0 |
|
| 178 |
0 |
| callback_value(node->key, (SV*) newRV_noinc((SV*) node->payload), callback); |
| 179 |
|
| } |
| 180 |
0 |
| } |
| 181 |
|
| |
| 182 |
0 |
| void btree_inorder_traverse(BinaryTreeNode * node, SV *callback) |
| 183 |
|
| { |
| 184 |
0 |
▶ | if (node->left) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (node->left) { |
0 |
0 |
0 |
|
| 185 |
0 |
| btree_preorder_traverse(node->left, callback); |
| 186 |
|
| } |
| 187 |
0 |
▶ | if (node->payload) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (node->payload) { |
0 |
0 |
0 |
|
| 188 |
0 |
| callback_value(node->key, (SV*) newRV_noinc((SV*) node->payload), callback); |
| 189 |
|
| } |
| 190 |
0 |
▶ | if (node->right) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (node->right) { |
0 |
0 |
0 |
|
| 191 |
0 |
| btree_preorder_traverse(node->right, callback); |
| 192 |
|
| } |
| 193 |
0 |
| } |
| 194 |
|
| |
| 195 |
2 |
| void btree_preorder_traverse(BinaryTreeNode * node, SV *callback) |
| 196 |
|
| { |
| 197 |
2 |
▶ | if (node->payload) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (node->payload) { |
2 |
0 |
2 |
|
| 198 |
2 |
| callback_value(node->key, (SV*) newRV_noinc((SV*) node->payload), callback); |
| 199 |
|
| } |
| 200 |
2 |
▶ | if (node->left) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (node->left) { |
1 |
1 |
2 |
|
| 201 |
1 |
| btree_preorder_traverse(node->left, callback); |
| 202 |
|
| } |
| 203 |
2 |
▶ | if (node->right) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (node->right) { |
0 |
2 |
2 |
|
| 204 |
0 |
| btree_preorder_traverse(node->right, callback); |
| 205 |
|
| } |
| 206 |
2 |
| } |
| 207 |
|
| |
| 208 |
1 |
| bool btree_update(BinaryTreeNode * node, IV key, HV * payload) |
| 209 |
|
| { |
| 210 |
1 |
▶ | if (key < node->key) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (key < node->key) { |
0 |
1 |
1 |
|
| 211 |
0 |
▶ | if (node->left) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (node->left) { |
0 |
0 |
0 |
|
| 212 |
0 |
| return btree_update(node->left, key, payload); |
| 213 |
|
| } |
| 214 |
0 |
| return FALSE; |
| 215 |
1 |
▶ | } else if (key > node->key) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (key > node->key) { |
1 |
0 |
1 |
|
| 216 |
1 |
▶ | if (node->right) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (node->right) { |
0 |
1 |
1 |
|
| 217 |
0 |
| return btree_update(node->right, key, payload); |
| 218 |
|
| } |
| 219 |
1 |
| return FALSE; |
| 220 |
0 |
▶ | } else if (key == node->key) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (key == node->key) { |
0 |
0 |
0 |
|
| 221 |
0 |
▶ | if (node->payload) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (node->payload) { |
0 |
0 |
0 |
|
| 222 |
0 |
| SvREFCNT_dec(node->payload); |
| 223 |
|
| } |
| 224 |
0 |
| node->payload = payload; |
| 225 |
0 |
| return TRUE; |
| 226 |
|
| } |
| 227 |
0 |
| return FALSE; |
| 228 |
|
| } |
| 229 |
|
| |
| 230 |
|
| |
| 231 |
3 |
| bool btree_node_exists(BinaryTreeNode * node, IV key) |
| 232 |
|
| { |
| 233 |
3 |
▶ | if (key < node->key) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (key < node->key) { |
0 |
3 |
3 |
|
| 234 |
0 |
▶ | if (node->left) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (node->left) { |
0 |
0 |
0 |
|
| 235 |
0 |
| return btree_node_exists(node->left, key); |
| 236 |
|
| } |
| 237 |
0 |
| return false; |
| 238 |
3 |
▶ | } else if (key > node->key) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (key > node->key) { |
1 |
2 |
3 |
|
| 239 |
1 |
▶ | if (node->right) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (node->right) { |
0 |
1 |
1 |
|
| 240 |
0 |
| return btree_node_exists(node->right, key); |
| 241 |
|
| } |
| 242 |
1 |
| return false; |
| 243 |
2 |
▶ | } else if (key == node->key) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (key == node->key) { |
2 |
0 |
2 |
|
| 244 |
2 |
| return true; |
| 245 |
|
| } |
| 246 |
0 |
| return false; |
| 247 |
|
| } |
| 248 |
|
| |
| 249 |
7 |
| BinaryTreeNode * btree_search(BinaryTreeNode * node, IV key) |
| 250 |
|
| { |
| 251 |
7 |
▶ | if (key < node->key) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (key < node->key) { |
3 |
4 |
7 |
|
| 252 |
3 |
▶ | if (node->left) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (node->left) { |
2 |
1 |
3 |
|
| 253 |
2 |
| return btree_search(node->left, key); |
| 254 |
|
| } |
| 255 |
1 |
| return NULL; |
| 256 |
4 |
▶ | } else if (key > node->key) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (key > node->key) { |
2 |
2 |
4 |
|
| 257 |
2 |
▶ | if (node->right) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (node->right) { |
0 |
2 |
2 |
|
| 258 |
0 |
| return btree_search(node->right, key); |
| 259 |
|
| } |
| 260 |
2 |
| return NULL; |
| 261 |
2 |
▶ | } else if (key == node->key) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (key == node->key) { |
2 |
0 |
2 |
|
| 262 |
2 |
| return node; |
| 263 |
|
| } |
| 264 |
0 |
| return NULL; |
| 265 |
|
| } |
| 266 |
|
| |
| 267 |
10 |
| bool btree_insert_node_hash_by_key(BinaryTreeNode * node, IV key, HV * payload) |
| 268 |
|
| { |
| 269 |
10 |
▶ | if (key < node->key) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (key < node->key) { |
7 |
3 |
10 |
|
| 270 |
7 |
▶ | if (node->left) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (node->left) { |
2 |
5 |
7 |
|
| 271 |
2 |
| return btree_insert_node_hash_by_key(node->left, key, payload); |
| 272 |
|
| } else { |
| 273 |
5 |
| BinaryTreeNode * new_node = btree_node_create(key, payload); |
| 274 |
5 |
| new_node->parent = node; |
| 275 |
5 |
| node->left = new_node; |
| 276 |
|
| } |
| 277 |
5 |
| return TRUE; |
| 278 |
3 |
▶ | } else if (key > node->key) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (key > node->key) { |
3 |
0 |
3 |
|
| 279 |
3 |
▶ | if (node->right) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (node->right) { |
1 |
2 |
3 |
|
| 280 |
1 |
| return btree_insert_node_hash_by_key(node->right, key, payload); |
| 281 |
|
| } else { |
| 282 |
2 |
| BinaryTreeNode * new_node = btree_node_create(key, payload); |
| 283 |
2 |
| new_node->parent = node; |
| 284 |
2 |
| node->right = new_node; |
| 285 |
|
| } |
| 286 |
2 |
| return TRUE; |
| 287 |
0 |
▶ | } else if (key == node->key) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (key == node->key) { |
0 |
0 |
0 |
|
| 288 |
0 |
| croak("the key already exists in the tree."); |
| 289 |
|
| return FALSE; |
| 290 |
|
| } |
| 291 |
0 |
| return FALSE; |
| 292 |
|
| } |
| 293 |
|
| |
| 294 |
|
| |
| 295 |
|
| void btree_dump(BinaryTreeNode* node, uint indent); |
| 296 |
|
| |
| 297 |
3 |
| void btree_dump(BinaryTreeNode* node, uint indent) |
| 298 |
|
| { |
| 299 |
9 |
▶ | for (int i = 0 ; i < indent; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (int i = 0 ; i < indent; i++) { |
6 |
3 |
9 |
|
| 300 |
6 |
| fprintf(stderr, " "); |
| 301 |
|
| } |
| 302 |
3 |
| fprintf(stderr, "(o) key: %lu", node->key); |
| 303 |
3 |
▶ | if (node->payload) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (node->payload) { |
3 |
0 |
3 |
|
| 304 |
3 |
| fprintf(stderr, ", hash: %s", Perl_sv_peek((SV*) node->payload) ); |
| 305 |
|
| } else { |
| 306 |
0 |
| fprintf(stderr, ", hash: (empty)"); |
| 307 |
|
| } |
| 308 |
3 |
| fprintf(stderr, "\n"); |
| 309 |
|
| |
| 310 |
3 |
▶ | if (node->left) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (node->left) { |
2 |
1 |
3 |
|
| 311 |
4 |
▶ | for (int i = 0 ; i < indent; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (int i = 0 ; i < indent; i++) { |
2 |
2 |
4 |
|
| 312 |
2 |
| fprintf(stderr, " "); |
| 313 |
|
| } |
| 314 |
2 |
| fprintf(stderr, " ->left:\n"); |
| 315 |
2 |
| btree_dump(node->left, indent+2); |
| 316 |
|
| } |
| 317 |
3 |
▶ | if (node->right) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (node->right) { |
0 |
3 |
3 |
|
| 318 |
0 |
▶ | for (int i = 0 ; i < indent; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (int i = 0 ; i < indent; i++) { |
0 |
0 |
0 |
|
| 319 |
0 |
| fprintf(stderr, " "); |
| 320 |
|
| } |
| 321 |
0 |
| fprintf(stderr, " ->right:\n"); |
| 322 |
0 |
| btree_dump(node->right, indent+2); |
| 323 |
|
| } |
| 324 |
3 |
| } |
| 325 |
|
| |
| 326 |
|
| |
| 327 |
|
| IV hv_fetch_key_must(HV * hash, char *field, uint field_len); |
| 328 |
|
| |
| 329 |
10 |
| IV hv_fetch_key_must(HV * hash, char *field, uint field_len) |
| 330 |
|
| { |
| 331 |
10 |
| SV** ret = hv_fetch(hash, field, field_len, FALSE); |
| 332 |
10 |
▶ | if (ret == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (ret == NULL) { |
0 |
10 |
10 |
|
| 333 |
0 |
| croak("key field %s does not exist", field); |
| 334 |
|
| } |
| 335 |
10 |
▶ | if (!SvIOK(*ret)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!SvIOK(*ret)) { |
0 |
10 |
10 |
|
| 336 |
0 |
| croak("The value of %s is invalid", field); |
| 337 |
|
| } |
| 338 |
10 |
| return SvIV(*ret); |
| 339 |
|
| } |
| 340 |
|
| |
| 341 |
|
| |
| 342 |
|
| |
| 343 |
9 |
| char * get_options_key_field(HV * options) |
| 344 |
|
| { |
| 345 |
9 |
▶ | if (!hv_exists(options, "by_key", sizeof("by_key") - 1)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!hv_exists(options, "by_key", sizeof("by_key") - 1)) { |
0 |
9 |
9 |
|
| 346 |
0 |
| return NULL; |
| 347 |
|
| } |
| 348 |
|
| |
| 349 |
9 |
| SV** field_sv = hv_fetch(options, "by_key", 6, 0); |
| 350 |
9 |
▶ | if (field_sv == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (field_sv == NULL) { |
0 |
9 |
9 |
|
| 351 |
0 |
| return NULL; |
| 352 |
|
| } |
| 353 |
|
| |
| 354 |
9 |
▶ | if (SvTYPE(*field_sv) != SVt_PV) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvTYPE(*field_sv) != SVt_PV) { |
0 |
9 |
9 |
|
| 355 |
0 |
| return NULL; |
| 356 |
|
| } |
| 357 |
|
| |
| 358 |
9 |
| char * key_field = (char *)SvPV_nolen(*field_sv); |
| 359 |
9 |
▶ | if (key_field == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (key_field == NULL) { |
0 |
9 |
9 |
|
| 360 |
0 |
| return NULL; |
| 361 |
|
| } |
| 362 |
9 |
| return key_field; |
| 363 |
|
| } |
| 364 |
|
| |
| 365 |
|
| |
| 366 |
|
| |
| 367 |
9 |
| bool btree_pad_insert_node(BinaryTreePad *pad, IV key, HV *node_hash) |
| 368 |
|
| { |
| 369 |
9 |
| SvREFCNT_inc(node_hash); |
| 370 |
9 |
▶ | if (pad->root) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (pad->root) { |
7 |
2 |
9 |
|
| 371 |
7 |
| return btree_insert_node_hash_by_key(pad->root, key, node_hash); |
| 372 |
|
| } |
| 373 |
2 |
| pad->root = btree_node_create(key, node_hash); |
| 374 |
2 |
| return TRUE; |
| 375 |
|
| } |
| 376 |
|
| |
| 377 |
3 |
| bool btree_pad_insert_node_by_key_field(BinaryTreePad *pad, char * key_field, int key_field_len, HV *node_hash) |
| 378 |
|
| { |
| 379 |
3 |
| IV key = hv_fetch_key_must(node_hash, key_field, key_field_len); |
| 380 |
3 |
| return btree_pad_insert_node(pad, key, node_hash); |
| 381 |
|
| } |
| 382 |
|
| |
| 383 |
1 |
| AV * btree_pad_insert_av_nodes_by_key_field(BinaryTreePad * pad, char * key_field, unsigned int key_field_len, AV* new_nodes) |
| 384 |
|
| { |
| 385 |
|
| bool ret; |
| 386 |
1 |
| AV * av_result = newAV(); |
| 387 |
1 |
| SSize_t top_index = av_top_index(new_nodes); |
| 388 |
4 |
▶ | for (int i = 0; i <= top_index; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (int i = 0; i <= top_index; i++) { |
3 |
1 |
4 |
|
| 389 |
3 |
| SV ** new_node_ref_p = av_fetch(new_nodes, i, FALSE); |
| 390 |
3 |
| HV * new_node = (HV*) SvRV(*new_node_ref_p); |
| 391 |
3 |
| ret = btree_pad_insert_node_by_key_field(pad, key_field, key_field_len, new_node); |
| 392 |
3 |
| av_push(av_result, newSViv(ret)); |
| 393 |
|
| } |
| 394 |
1 |
| return av_result; |
| 395 |
|
| } |
| 396 |
|
| |
| 397 |
|
| |
| 398 |
|
| MODULE = Tree::Binary::XS PACKAGE = Tree::Binary::XS |
| 399 |
|
| |
| 400 |
|
| TYPEMAP: <<END; |
| 401 |
|
| |
| 402 |
|
| END |
| 403 |
|
| |
| 404 |
|
| void |
| 405 |
|
| new(...) |
| 406 |
|
| PPCODE: |
| 407 |
2 |
| BinaryTreePad *pad = btree_pad_new(); |
| 408 |
|
| |
| 409 |
|
| // printf("pad: %x\n", pad); |
| 410 |
|
| |
| 411 |
|
| // newHV(); |
| 412 |
2 |
| SV* ret = newSV(0); |
| 413 |
2 |
▶ | SvUPGRADE(ret, SVt_RV); |
Branch: 1 branch
| Branch | True | False | Total |
| SvUPGRADE(ret, SVt_RV); |
2 |
0 |
2 |
|
| 414 |
2 |
| SvROK_on(ret); |
| 415 |
2 |
| SvRV(ret) = (SV*)pad; |
| 416 |
|
| |
| 417 |
2 |
| SV * obj = newRV_noinc(ret); |
| 418 |
|
| STRLEN classname_len; |
| 419 |
2 |
| char * classname = SvPVbyte(ST(0), classname_len); |
| 420 |
2 |
| HV * stash = gv_stashpvn(classname, classname_len, 0); |
| 421 |
2 |
| sv_bless(obj, stash); |
| 422 |
|
| |
| 423 |
2 |
| SV * options_ref = ST(1); |
| 424 |
2 |
▶ | if (options_ref) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (options_ref) { |
2 |
0 |
2 |
|
| 425 |
2 |
| HV * options_hv = (HV*) SvRV(options_ref); |
| 426 |
2 |
| SvREFCNT_inc(options_hv); |
| 427 |
2 |
| pad->options = options_hv; |
| 428 |
|
| } |
| 429 |
2 |
▶ | EXTEND(SP, 1); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 1); |
0 |
2 |
2 |
|
| 430 |
2 |
| PUSHs(sv_2mortal(obj)); |
| 431 |
|
| |
| 432 |
|
| |
| 433 |
|
| HV* |
| 434 |
|
| options(self_sv) |
| 435 |
|
| SV* self_sv |
| 436 |
|
| CODE: |
| 437 |
1 |
| BinaryTreePad* pad = (BinaryTreePad*) SvRV(SvRV(self_sv)); |
| 438 |
1 |
▶ | if (pad->options) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (pad->options) { |
1 |
0 |
1 |
|
| 439 |
1 |
| RETVAL = pad->options; |
| 440 |
|
| } else { |
| 441 |
0 |
| RETVAL = newHV(); |
| 442 |
|
| } |
| 443 |
|
| OUTPUT: |
| 444 |
|
| RETVAL |
| 445 |
|
| |
| 446 |
|
| |
| 447 |
|
| |
| 448 |
|
| SV* |
| 449 |
|
| delete(self_sv, ...) |
| 450 |
|
| SV* self_sv |
| 451 |
|
| CODE: |
| 452 |
|
| |
| 453 |
5 |
| BinaryTreePad* pad = (BinaryTreePad*) SvRV(SvRV(self_sv)); |
| 454 |
5 |
▶ | if (!pad->root) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!pad->root) { |
0 |
5 |
5 |
|
| 455 |
|
| // Empty tree |
| 456 |
0 |
| XSRETURN_UNDEF; |
| 457 |
|
| } |
| 458 |
|
| |
| 459 |
5 |
▶ | if (items > 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (items > 1) { |
5 |
0 |
5 |
|
| 460 |
5 |
| bool deleted_any = false; |
| 461 |
|
| int i; |
| 462 |
12 |
▶ | for (i = 1; i < items; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 1; i < items; i++) { |
7 |
5 |
12 |
|
| 463 |
7 |
| SV * arg_sv = ST(i); |
| 464 |
7 |
▶ | if (!SvIOK(arg_sv)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!SvIOK(arg_sv)) { |
0 |
7 |
7 |
|
| 465 |
0 |
| croak("Invalid key: it should be an integer."); |
| 466 |
|
| } |
| 467 |
7 |
| IV key = SvIV(arg_sv); |
| 468 |
7 |
▶ | if (btree_delete(pad->root, key)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (btree_delete(pad->root, key)) { |
4 |
3 |
7 |
|
| 469 |
4 |
| deleted_any = true; |
| 470 |
|
| } |
| 471 |
|
| } |
| 472 |
|
| |
| 473 |
5 |
▶ | if (deleted_any) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (deleted_any) { |
3 |
2 |
5 |
|
| 474 |
3 |
| XSRETURN_YES; |
| 475 |
|
| } else { |
| 476 |
2 |
| XSRETURN_NO; |
| 477 |
|
| } |
| 478 |
|
| } |
| 479 |
0 |
| XSRETURN_NO; |
| 480 |
|
| |
| 481 |
|
| |
| 482 |
|
| void |
| 483 |
|
| dump(self_sv) |
| 484 |
|
| SV* self_sv |
| 485 |
|
| CODE: |
| 486 |
1 |
| BinaryTreePad* pad = (BinaryTreePad*) SvRV(SvRV(self_sv)); |
| 487 |
1 |
| btree_dump(pad->root, 0); |
| 488 |
|
| |
| 489 |
|
| |
| 490 |
|
| |
| 491 |
|
| SV * |
| 492 |
|
| search(self_sv, key_sv) |
| 493 |
|
| SV * self_sv |
| 494 |
|
| SV * key_sv |
| 495 |
|
| CODE: |
| 496 |
5 |
| BinaryTreePad* pad = (BinaryTreePad*) SvRV(SvRV(self_sv)); |
| 497 |
|
| |
| 498 |
5 |
▶ | if (!pad->root) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!pad->root) { |
0 |
5 |
5 |
|
| 499 |
0 |
| XSRETURN_UNDEF; |
| 500 |
|
| } |
| 501 |
|
| |
| 502 |
5 |
▶ | if (!SvIOK(key_sv)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!SvIOK(key_sv)) { |
0 |
5 |
5 |
|
| 503 |
0 |
| croak("The search key must be IV"); |
| 504 |
|
| } |
| 505 |
|
| |
| 506 |
5 |
| IV key = SvIV(key_sv); |
| 507 |
5 |
| BinaryTreeNode * node = btree_search(pad->root, key); |
| 508 |
|
| |
| 509 |
5 |
▶ | if (node == NULL || node->payload == NULL) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (node == NULL || node->payload == NULL) { |
2 |
3 |
5 |
| if (node == NULL || node->payload == NULL) { |
0 |
2 |
2 |
|
| 510 |
3 |
| XSRETURN_UNDEF; |
| 511 |
|
| } |
| 512 |
|
| |
| 513 |
|
| // Perl_sv_dump(node->payload); |
| 514 |
2 |
| RETVAL = newRV_inc((SV*) node->payload); |
| 515 |
|
| OUTPUT: |
| 516 |
|
| RETVAL |
| 517 |
|
| |
| 518 |
|
| |
| 519 |
|
| SV* |
| 520 |
|
| update(self_sv, ...) |
| 521 |
|
| SV* self_sv |
| 522 |
|
| CODE: |
| 523 |
|
| |
| 524 |
1 |
| BinaryTreePad* pad = (BinaryTreePad*) SvRV(SvRV(self_sv)); |
| 525 |
|
| |
| 526 |
1 |
| char *key_field = "key"; |
| 527 |
|
| |
| 528 |
1 |
▶ | if (!pad || !pad->options) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!pad || !pad->options) { |
1 |
0 |
1 |
| if (!pad || !pad->options) { |
0 |
1 |
1 |
|
| 529 |
0 |
| XSRETURN_UNDEF; |
| 530 |
|
| } |
| 531 |
1 |
▶ | if (pad->options) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (pad->options) { |
1 |
0 |
1 |
|
| 532 |
1 |
| key_field = get_options_key_field(pad->options); |
| 533 |
|
| } |
| 534 |
|
| |
| 535 |
|
| IV key; |
| 536 |
1 |
| HV * node_hash = NULL; |
| 537 |
|
| |
| 538 |
|
| // if there is only one argument (items == 2 including $self) |
| 539 |
1 |
▶ | if (items == 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (items == 2) { |
1 |
0 |
1 |
|
| 540 |
1 |
▶ | if (SvIOK(ST(1))) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvIOK(ST(1))) { |
0 |
1 |
1 |
|
| 541 |
0 |
| key = SvIV( ST(1) ); |
| 542 |
0 |
| node_hash = newHV(); |
| 543 |
1 |
▶ | } else if (SvROK( ST(1) ) && SvTYPE(SvRV(ST(1))) == SVt_PVHV ) { |
Branch: 2 branches
| Branch | True | False | Total |
| } else if (SvROK( ST(1) ) && SvTYPE(SvRV(ST(1))) == SVt_PVHV ) { |
1 |
0 |
1 |
| } else if (SvROK( ST(1) ) && SvTYPE(SvRV(ST(1))) == SVt_PVHV ) { |
1 |
0 |
1 |
|
| 544 |
1 |
| node_hash = (HV*) SvRV(ST(1)); |
| 545 |
|
| } |
| 546 |
0 |
▶ | } else if (items == 3) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (items == 3) { |
0 |
0 |
0 |
|
| 547 |
0 |
▶ | if (SvIOK(ST(1)) && SvROK(ST(2)) && SvTYPE(SvRV(ST(2))) == SVt_PVHV) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (SvIOK(ST(1)) && SvROK(ST(2)) && SvTYPE(SvRV(ST(2))) == SVt_PVHV) { |
0 |
0 |
0 |
| if (SvIOK(ST(1)) && SvROK(ST(2)) && SvTYPE(SvRV(ST(2))) == SVt_PVHV) { |
0 |
0 |
0 |
| if (SvIOK(ST(1)) && SvROK(ST(2)) && SvTYPE(SvRV(ST(2))) == SVt_PVHV) { |
0 |
0 |
0 |
|
| 548 |
0 |
| key = SvIV( ST(1) ); |
| 549 |
0 |
| node_hash = (HV*) SvRV(ST(2)); |
| 550 |
|
| } else { |
| 551 |
0 |
| croak("The BinaryTree::insert method can only accept either (key, hashref) or (hashref)"); |
| 552 |
|
| } |
| 553 |
|
| } |
| 554 |
|
| |
| 555 |
1 |
▶ | if (!key) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!key) { |
0 |
1 |
1 |
|
| 556 |
|
| // If the key does not exist |
| 557 |
0 |
| key = hv_fetch_key_must(node_hash, key_field, strlen(key_field)); |
| 558 |
|
| } |
| 559 |
|
| |
| 560 |
1 |
▶ | if (pad->root) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (pad->root) { |
1 |
0 |
1 |
|
| 561 |
1 |
| SvREFCNT_inc(node_hash); |
| 562 |
1 |
▶ | if (btree_update(pad->root, key, node_hash)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (btree_update(pad->root, key, node_hash)) { |
0 |
1 |
1 |
|
| 563 |
0 |
| XSRETURN_YES; |
| 564 |
|
| } |
| 565 |
|
| } |
| 566 |
1 |
| XSRETURN_NO; |
| 567 |
|
| |
| 568 |
|
| |
| 569 |
|
| void |
| 570 |
|
| preorder_traverse(self_sv, callback) |
| 571 |
|
| SV* self_sv |
| 572 |
|
| SV* callback |
| 573 |
|
| CODE: |
| 574 |
1 |
| BinaryTreePad* pad = (BinaryTreePad*) SvRV(SvRV(self_sv)); |
| 575 |
1 |
▶ | if (pad->root) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (pad->root) { |
1 |
0 |
1 |
|
| 576 |
1 |
| btree_preorder_traverse(pad->root, callback); |
| 577 |
|
| } |
| 578 |
|
| |
| 579 |
|
| void |
| 580 |
|
| inorder_traverse(self_sv, callback) |
| 581 |
|
| SV* self_sv |
| 582 |
|
| SV* callback |
| 583 |
|
| CODE: |
| 584 |
0 |
| BinaryTreePad* pad = (BinaryTreePad*) SvRV(SvRV(self_sv)); |
| 585 |
0 |
▶ | if (pad->root) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (pad->root) { |
0 |
0 |
0 |
|
| 586 |
0 |
| btree_inorder_traverse(pad->root, callback); |
| 587 |
|
| } |
| 588 |
|
| |
| 589 |
|
| void |
| 590 |
|
| postorder_traverse(self_sv, callback) |
| 591 |
|
| SV* self_sv |
| 592 |
|
| SV* callback |
| 593 |
|
| CODE: |
| 594 |
0 |
| BinaryTreePad* pad = (BinaryTreePad*) SvRV(SvRV(self_sv)); |
| 595 |
0 |
▶ | if (pad->root) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (pad->root) { |
0 |
0 |
0 |
|
| 596 |
0 |
| btree_postorder_traverse(pad->root, callback); |
| 597 |
|
| } |
| 598 |
|
| |
| 599 |
|
| |
| 600 |
|
| SV * |
| 601 |
|
| exists(self_sv, arg) |
| 602 |
|
| SV* self_sv |
| 603 |
|
| SV* arg |
| 604 |
|
| |
| 605 |
|
| CODE: |
| 606 |
3 |
| BinaryTreePad* pad = (BinaryTreePad*) SvRV(SvRV(self_sv)); |
| 607 |
|
| |
| 608 |
|
| IV key; |
| 609 |
|
| |
| 610 |
3 |
▶ | if (SvIOK(arg)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvIOK(arg)) { |
2 |
1 |
3 |
|
| 611 |
2 |
| key = SvIV(arg); |
| 612 |
2 |
▶ | } else if (SvROK(ST(1)) && SvTYPE(SvRV(ST(1))) == SVt_PVHV) { |
Branch: 2 branches
| Branch | True | False | Total |
| } else if (SvROK(ST(1)) && SvTYPE(SvRV(ST(1))) == SVt_PVHV) { |
1 |
0 |
1 |
| } else if (SvROK(ST(1)) && SvTYPE(SvRV(ST(1))) == SVt_PVHV) { |
1 |
0 |
1 |
|
| 613 |
1 |
| HV * node_hash = (HV*) SvRV(ST(1)); |
| 614 |
1 |
| char * key_field = "key"; |
| 615 |
1 |
▶ | if (!pad || !pad->options) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!pad || !pad->options) { |
1 |
0 |
1 |
| if (!pad || !pad->options) { |
0 |
1 |
1 |
|
| 616 |
0 |
| XSRETURN_UNDEF; |
| 617 |
|
| } |
| 618 |
1 |
▶ | if (pad->options) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (pad->options) { |
1 |
0 |
1 |
|
| 619 |
1 |
| key_field = get_options_key_field(pad->options); |
| 620 |
|
| } |
| 621 |
1 |
| key = hv_fetch_key_must(node_hash, key_field, strlen(key_field)); |
| 622 |
|
| |
| 623 |
|
| } else { |
| 624 |
0 |
| croak("The exists method can only accept exists(IV) or exists(HashRef)"); |
| 625 |
|
| } |
| 626 |
|
| |
| 627 |
3 |
▶ | if (pad->root) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (pad->root) { |
3 |
0 |
3 |
|
| 628 |
3 |
| bool ret = btree_node_exists(pad->root, key); |
| 629 |
3 |
| XSRETURN(ret); |
| 630 |
|
| } |
| 631 |
|
| |
| 632 |
|
| AV * |
| 633 |
|
| insert_those(self_sv, ...) |
| 634 |
|
| SV* self_sv |
| 635 |
|
| CODE: |
| 636 |
1 |
| BinaryTreePad* pad = (BinaryTreePad*) SvRV(SvRV(self_sv)); |
| 637 |
|
| |
| 638 |
1 |
| char * key_field = "key"; |
| 639 |
1 |
▶ | if (!pad || !pad->options) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!pad || !pad->options) { |
1 |
0 |
1 |
| if (!pad || !pad->options) { |
0 |
1 |
1 |
|
| 640 |
0 |
| XSRETURN_UNDEF; |
| 641 |
|
| } |
| 642 |
1 |
▶ | if (pad->options) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (pad->options) { |
1 |
0 |
1 |
|
| 643 |
1 |
| key_field = get_options_key_field(pad->options); |
| 644 |
|
| } |
| 645 |
1 |
▶ | if (SvROK(ST(1)) && SvTYPE(SvRV(ST(1))) == SVt_PVAV ) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (SvROK(ST(1)) && SvTYPE(SvRV(ST(1))) == SVt_PVAV ) { |
1 |
0 |
1 |
| if (SvROK(ST(1)) && SvTYPE(SvRV(ST(1))) == SVt_PVAV ) { |
1 |
0 |
1 |
|
| 646 |
1 |
| RETVAL = btree_pad_insert_av_nodes_by_key_field(pad, key_field, strlen(key_field), (AV*) SvRV(ST(1))); |
| 647 |
|
| } else { |
| 648 |
0 |
| XSRETURN_UNDEF; |
| 649 |
|
| } |
| 650 |
|
| OUTPUT: |
| 651 |
|
| RETVAL |
| 652 |
|
| |
| 653 |
|
| |
| 654 |
|
| SV * |
| 655 |
|
| insert(self_sv, ...) |
| 656 |
|
| SV* self_sv |
| 657 |
|
| CODE: |
| 658 |
|
| |
| 659 |
6 |
| BinaryTreePad* pad = (BinaryTreePad*) SvRV(SvRV(self_sv)); |
| 660 |
|
| |
| 661 |
6 |
| char * key_field = "key"; |
| 662 |
6 |
▶ | if (!pad || !pad->options) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!pad || !pad->options) { |
6 |
0 |
6 |
| if (!pad || !pad->options) { |
0 |
6 |
6 |
|
| 663 |
0 |
| XSRETURN_UNDEF; |
| 664 |
|
| } |
| 665 |
6 |
▶ | if (pad->options) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (pad->options) { |
6 |
0 |
6 |
|
| 666 |
6 |
| key_field = get_options_key_field(pad->options); |
| 667 |
|
| } |
| 668 |
|
| |
| 669 |
|
| IV key; |
| 670 |
6 |
| HV * node_hash = NULL; |
| 671 |
|
| |
| 672 |
|
| // if there is only one argument (items == 2 including $self) |
| 673 |
6 |
▶ | if (items == 2) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (items == 2) { |
6 |
0 |
6 |
|
| 674 |
|
| |
| 675 |
|
| // If the key is defined but without hash |
| 676 |
6 |
▶ | if (SvIOK(ST(1))) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvIOK(ST(1))) { |
0 |
6 |
6 |
|
| 677 |
0 |
| key = SvIV( ST(1) ); |
| 678 |
0 |
| node_hash = newHV(); |
| 679 |
6 |
▶ | } else if (SvROK(ST(1)) && SvTYPE(SvRV(ST(1))) == SVt_PVHV ) { |
Branch: 2 branches
| Branch | True | False | Total |
| } else if (SvROK(ST(1)) && SvTYPE(SvRV(ST(1))) == SVt_PVHV ) { |
6 |
0 |
6 |
| } else if (SvROK(ST(1)) && SvTYPE(SvRV(ST(1))) == SVt_PVHV ) { |
6 |
0 |
6 |
|
| 680 |
|
| // dereference the hashref |
| 681 |
6 |
| node_hash = (HV*) SvRV(ST(1)); |
| 682 |
0 |
▶ | } else if (SvROK(ST(1)) && SvTYPE(SvRV(ST(1))) == SVt_PVAV ) { |
Branch: 2 branches
| Branch | True | False | Total |
| } else if (SvROK(ST(1)) && SvTYPE(SvRV(ST(1))) == SVt_PVAV ) { |
0 |
0 |
0 |
| } else if (SvROK(ST(1)) && SvTYPE(SvRV(ST(1))) == SVt_PVAV ) { |
0 |
0 |
0 |
|
| 683 |
|
| |
| 684 |
0 |
| AV * result = btree_pad_insert_av_nodes_by_key_field(pad, key_field, strlen(key_field), (AV*) SvRV(ST(1))); |
| 685 |
0 |
| XSRETURN_YES; |
| 686 |
|
| // RETVAL = sv_2mortal(result); |
| 687 |
|
| |
| 688 |
|
| } else { |
| 689 |
0 |
| croak("The BinaryTree::insert method can only accept either (key, hashref) or (hashref)"); |
| 690 |
|
| } |
| 691 |
0 |
▶ | } else if (items == 3) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (items == 3) { |
0 |
0 |
0 |
|
| 692 |
0 |
▶ | if (SvIOK(ST(1)) && SvROK(ST(2)) && SvTYPE(SvRV(ST(2))) == SVt_PVHV) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (SvIOK(ST(1)) && SvROK(ST(2)) && SvTYPE(SvRV(ST(2))) == SVt_PVHV) { |
0 |
0 |
0 |
| if (SvIOK(ST(1)) && SvROK(ST(2)) && SvTYPE(SvRV(ST(2))) == SVt_PVHV) { |
0 |
0 |
0 |
| if (SvIOK(ST(1)) && SvROK(ST(2)) && SvTYPE(SvRV(ST(2))) == SVt_PVHV) { |
0 |
0 |
0 |
|
| 693 |
0 |
| key = SvIV( ST(1) ); |
| 694 |
0 |
| node_hash = (HV*) SvRV(ST(2)); |
| 695 |
|
| } else { |
| 696 |
0 |
| croak("The BinaryTree::insert method can only accept either (key, hashref) or (hashref)"); |
| 697 |
|
| } |
| 698 |
|
| } |
| 699 |
|
| |
| 700 |
6 |
▶ | if (!key) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!key) { |
6 |
0 |
6 |
|
| 701 |
|
| // If the key does not exist |
| 702 |
6 |
| key = hv_fetch_key_must(node_hash, key_field, strlen(key_field)); |
| 703 |
|
| } |
| 704 |
|
| |
| 705 |
6 |
| bool ret = btree_pad_insert_node(pad, key, node_hash); |
| 706 |
6 |
| XSRETURN(ret); |
| 707 |
|
| |
| 708 |
|
| void |
| 709 |
|
| DESTROY(self_sv) |
| 710 |
|
| SV* self_sv |
| 711 |
|
| PPCODE: |
| 712 |
2 |
| BinaryTreePad* pad = (BinaryTreePad*) SvRV(SvRV(self_sv)); |
| 713 |
|
| // printf("DESTORY pad: %x\n", pad); |
| 714 |
|
| // BinaryTreePad* pad = *(BinaryTreePad**) p; |
| 715 |
2 |
▶ | if (pad && pad->root) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (pad && pad->root) { |
2 |
0 |
2 |
| if (pad && pad->root) { |
2 |
0 |
2 |
|
| 716 |
2 |
| btree_node_free(pad->root); |
| 717 |
|
| } |
| 718 |
2 |
| Safefree(pad); |
| 719 |
2 |
| SvRV(SvRV(self_sv)) = 0; |
| 720 |
|
| |