ยปCore Development>Code coverage>Modules/rotatingtree.c

Python code coverage for Modules/rotatingtree.c

#countcontent
1n/a#include "rotatingtree.h"
2n/a
3n/a#define KEY_LOWER_THAN(key1, key2) ((char*)(key1) < (char*)(key2))
4n/a
5n/a/* The randombits() function below is a fast-and-dirty generator that
6n/a * is probably irregular enough for our purposes. Note that it's biased:
7n/a * I think that ones are slightly more probable than zeroes. It's not
8n/a * important here, though.
9n/a */
10n/a
11n/astatic unsigned int random_value = 1;
12n/astatic unsigned int random_stream = 0;
13n/a
14n/astatic int
15n/arandombits(int bits)
16n/a{
17n/a int result;
18n/a if (random_stream < (1U << bits)) {
19n/a random_value *= 1082527;
20n/a random_stream = random_value;
21n/a }
22n/a result = random_stream & ((1<<bits)-1);
23n/a random_stream >>= bits;
24n/a return result;
25n/a}
26n/a
27n/a
28n/a/* Insert a new node into the tree.
29n/a (*root) is modified to point to the new root. */
30n/avoid
31n/aRotatingTree_Add(rotating_node_t **root, rotating_node_t *node)
32n/a{
33n/a while (*root != NULL) {
34n/a if (KEY_LOWER_THAN(node->key, (*root)->key))
35n/a root = &((*root)->left);
36n/a else
37n/a root = &((*root)->right);
38n/a }
39n/a node->left = NULL;
40n/a node->right = NULL;
41n/a *root = node;
42n/a}
43n/a
44n/a/* Locate the node with the given key. This is the most complicated
45n/a function because it occasionally rebalances the tree to move the
46n/a resulting node closer to the root. */
47n/arotating_node_t *
48n/aRotatingTree_Get(rotating_node_t **root, void *key)
49n/a{
50n/a if (randombits(3) != 4) {
51n/a /* Fast path, no rebalancing */
52n/a rotating_node_t *node = *root;
53n/a while (node != NULL) {
54n/a if (node->key == key)
55n/a return node;
56n/a if (KEY_LOWER_THAN(key, node->key))
57n/a node = node->left;
58n/a else
59n/a node = node->right;
60n/a }
61n/a return NULL;
62n/a }
63n/a else {
64n/a rotating_node_t **pnode = root;
65n/a rotating_node_t *node = *pnode;
66n/a rotating_node_t *next;
67n/a int rotate;
68n/a if (node == NULL)
69n/a return NULL;
70n/a while (1) {
71n/a if (node->key == key)
72n/a return node;
73n/a rotate = !randombits(1);
74n/a if (KEY_LOWER_THAN(key, node->key)) {
75n/a next = node->left;
76n/a if (next == NULL)
77n/a return NULL;
78n/a if (rotate) {
79n/a node->left = next->right;
80n/a next->right = node;
81n/a *pnode = next;
82n/a }
83n/a else
84n/a pnode = &(node->left);
85n/a }
86n/a else {
87n/a next = node->right;
88n/a if (next == NULL)
89n/a return NULL;
90n/a if (rotate) {
91n/a node->right = next->left;
92n/a next->left = node;
93n/a *pnode = next;
94n/a }
95n/a else
96n/a pnode = &(node->right);
97n/a }
98n/a node = next;
99n/a }
100n/a }
101n/a}
102n/a
103n/a/* Enumerate all nodes in the tree. The callback enumfn() should return
104n/a zero to continue the enumeration, or non-zero to interrupt it.
105n/a A non-zero value is directly returned by RotatingTree_Enum(). */
106n/aint
107n/aRotatingTree_Enum(rotating_node_t *root, rotating_tree_enum_fn enumfn,
108n/a void *arg)
109n/a{
110n/a int result;
111n/a rotating_node_t *node;
112n/a while (root != NULL) {
113n/a result = RotatingTree_Enum(root->left, enumfn, arg);
114n/a if (result != 0) return result;
115n/a node = root->right;
116n/a result = enumfn(root, arg);
117n/a if (result != 0) return result;
118n/a root = node;
119n/a }
120n/a return 0;
121n/a}