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>
This commit is contained in:
Xiao Guangrong 2012-03-23 15:02:15 -07:00 committed by Linus Torvalds
parent 742245d5c2
commit 97e834c504

View file

@ -85,6 +85,17 @@ static inline unsigned long prio_tree_maxindex(unsigned int bits)
return index_bits_to_maxindex[bits - 1]; return index_bits_to_maxindex[bits - 1];
} }
static void prio_set_parent(struct prio_tree_node *parent,
struct prio_tree_node *child, bool left)
{
if (left)
parent->left = child;
else
parent->right = child;
child->parent = parent;
}
/* /*
* Extend a priority search tree so that it can store a node with heap_index * Extend a priority search tree so that it can store a node with heap_index
* max_heap_index. In the worst case, this algorithm takes O((log n)^2). * max_heap_index. In the worst case, this algorithm takes O((log n)^2).
@ -113,15 +124,12 @@ static struct prio_tree_node *prio_tree_expand(struct prio_tree_root *root,
prio_tree_remove(root, root->prio_tree_node); prio_tree_remove(root, root->prio_tree_node);
INIT_PRIO_TREE_NODE(tmp); INIT_PRIO_TREE_NODE(tmp);
prev->left = tmp; prio_set_parent(prev, tmp, true);
tmp->parent = prev;
prev = tmp; prev = tmp;
} }
if (!prio_tree_empty(root)) { if (!prio_tree_empty(root))
prev->left = root->prio_tree_node; prio_set_parent(prev, root->prio_tree_node, true);
prev->left->parent = prev;
}
root->prio_tree_node = node; root->prio_tree_node = node;
return node; return node;
@ -142,23 +150,14 @@ struct prio_tree_node *prio_tree_replace(struct prio_tree_root *root,
* and does not help much to improve performance (IMO). * and does not help much to improve performance (IMO).
*/ */
root->prio_tree_node = node; root->prio_tree_node = node;
} else { } else
node->parent = old->parent; prio_set_parent(old->parent, node, old->parent->left == old);
if (old->parent->left == old)
old->parent->left = node;
else
old->parent->right = node;
}
if (!prio_tree_left_empty(old)) { if (!prio_tree_left_empty(old))
node->left = old->left; prio_set_parent(node, old->left, true);
old->left->parent = node;
}
if (!prio_tree_right_empty(old)) { if (!prio_tree_right_empty(old))
node->right = old->right; prio_set_parent(node, old->right, false);
old->right->parent = node;
}
return old; return old;
} }
@ -218,16 +217,14 @@ struct prio_tree_node *prio_tree_insert(struct prio_tree_root *root,
if (index & mask) { if (index & mask) {
if (prio_tree_right_empty(cur)) { if (prio_tree_right_empty(cur)) {
INIT_PRIO_TREE_NODE(node); INIT_PRIO_TREE_NODE(node);
cur->right = node; prio_set_parent(cur, node, false);
node->parent = cur;
return res; return res;
} else } else
cur = cur->right; cur = cur->right;
} else { } else {
if (prio_tree_left_empty(cur)) { if (prio_tree_left_empty(cur)) {
INIT_PRIO_TREE_NODE(node); INIT_PRIO_TREE_NODE(node);
cur->left = node; prio_set_parent(cur, node, true);
node->parent = cur;
return res; return res;
} else } else
cur = cur->left; cur = cur->left;