Commit 97e834c5040b85e133d8d922111a62b2b853a406
Committed by
Linus Torvalds
1 parent
742245d5c2
Exists in
smarc-l5.0.0_1.0.0-ga
and in
5 other branches
prio_tree: introduce prio_set_parent()
Introduce prio_set_parent() to abstract the operation which is used to attach the node to its parent. Signed-off-by: Xiao Guangrong <xiaoguangrong@linux.vnet.ibm.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
Showing 1 changed file with 22 additions and 25 deletions Side-by-side Diff
lib/prio_tree.c
... | ... | @@ -85,6 +85,17 @@ |
85 | 85 | return index_bits_to_maxindex[bits - 1]; |
86 | 86 | } |
87 | 87 | |
88 | +static void prio_set_parent(struct prio_tree_node *parent, | |
89 | + struct prio_tree_node *child, bool left) | |
90 | +{ | |
91 | + if (left) | |
92 | + parent->left = child; | |
93 | + else | |
94 | + parent->right = child; | |
95 | + | |
96 | + child->parent = parent; | |
97 | +} | |
98 | + | |
88 | 99 | /* |
89 | 100 | * Extend a priority search tree so that it can store a node with heap_index |
90 | 101 | * max_heap_index. In the worst case, this algorithm takes O((log n)^2). |
91 | 102 | |
... | ... | @@ -113,15 +124,12 @@ |
113 | 124 | prio_tree_remove(root, root->prio_tree_node); |
114 | 125 | INIT_PRIO_TREE_NODE(tmp); |
115 | 126 | |
116 | - prev->left = tmp; | |
117 | - tmp->parent = prev; | |
127 | + prio_set_parent(prev, tmp, true); | |
118 | 128 | prev = tmp; |
119 | 129 | } |
120 | 130 | |
121 | - if (!prio_tree_empty(root)) { | |
122 | - prev->left = root->prio_tree_node; | |
123 | - prev->left->parent = prev; | |
124 | - } | |
131 | + if (!prio_tree_empty(root)) | |
132 | + prio_set_parent(prev, root->prio_tree_node, true); | |
125 | 133 | |
126 | 134 | root->prio_tree_node = node; |
127 | 135 | return node; |
128 | 136 | |
129 | 137 | |
... | ... | @@ -142,23 +150,14 @@ |
142 | 150 | * and does not help much to improve performance (IMO). |
143 | 151 | */ |
144 | 152 | root->prio_tree_node = node; |
145 | - } else { | |
146 | - node->parent = old->parent; | |
147 | - if (old->parent->left == old) | |
148 | - old->parent->left = node; | |
149 | - else | |
150 | - old->parent->right = node; | |
151 | - } | |
153 | + } else | |
154 | + prio_set_parent(old->parent, node, old->parent->left == old); | |
152 | 155 | |
153 | - if (!prio_tree_left_empty(old)) { | |
154 | - node->left = old->left; | |
155 | - old->left->parent = node; | |
156 | - } | |
156 | + if (!prio_tree_left_empty(old)) | |
157 | + prio_set_parent(node, old->left, true); | |
157 | 158 | |
158 | - if (!prio_tree_right_empty(old)) { | |
159 | - node->right = old->right; | |
160 | - old->right->parent = node; | |
161 | - } | |
159 | + if (!prio_tree_right_empty(old)) | |
160 | + prio_set_parent(node, old->right, false); | |
162 | 161 | |
163 | 162 | return old; |
164 | 163 | } |
165 | 164 | |
... | ... | @@ -218,16 +217,14 @@ |
218 | 217 | if (index & mask) { |
219 | 218 | if (prio_tree_right_empty(cur)) { |
220 | 219 | INIT_PRIO_TREE_NODE(node); |
221 | - cur->right = node; | |
222 | - node->parent = cur; | |
220 | + prio_set_parent(cur, node, false); | |
223 | 221 | return res; |
224 | 222 | } else |
225 | 223 | cur = cur->right; |
226 | 224 | } else { |
227 | 225 | if (prio_tree_left_empty(cur)) { |
228 | 226 | INIT_PRIO_TREE_NODE(node); |
229 | - cur->left = node; | |
230 | - node->parent = cur; | |
227 | + prio_set_parent(cur, node, true); | |
231 | 228 | return res; |
232 | 229 | } else |
233 | 230 | cur = cur->left; |