-/* ET-trees datastructure implementation.
+/* ET-trees data structure implementation.
Contributed by Pavel Nejedly
- Copyright (C) 2002 Free Software Foundation, Inc.
+ Copyright (C) 2002, 2003 Free Software Foundation, Inc.
This file is part of the libiberty library.
Libiberty is free software; you can redistribute it and/or
You should have received a copy of the GNU Library General Public
License along with libiberty; see the file COPYING.LIB. If
not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
-Boston, MA 02111-1307, USA.
+Boston, MA 02111-1307, USA.
The ET-forest structure is described in:
D. D. Sleator and R. E. Tarjan. A data structure for dynamic trees.
alloc_pool occur_pool;
};
-/* Single occurrence of node in ET-forest.
+/* Single occurrence of node in ET-forest.
A single node may have multiple occurrences.
*/
struct et_forest_occurrence
};
-static et_forest_occurrence_t splay PARAMS ((et_forest_occurrence_t));
-static void remove_all_occurrences PARAMS ((et_forest_t, et_forest_node_t));
-static inline et_forest_occurrence_t find_leftmost_node
- PARAMS ((et_forest_occurrence_t));
-static inline et_forest_occurrence_t find_rightmost_node
- PARAMS ((et_forest_occurrence_t));
-static int calculate_value PARAMS ((et_forest_occurrence_t));
+static et_forest_occurrence_t splay (et_forest_occurrence_t);
+static void remove_all_occurrences (et_forest_t, et_forest_node_t);
+static inline et_forest_occurrence_t find_leftmost_node
+ (et_forest_occurrence_t);
+static inline et_forest_occurrence_t find_rightmost_node
+ (et_forest_occurrence_t);
+static int calculate_value (et_forest_occurrence_t);
/* Return leftmost node present in the tree roted by OCC. */
static inline et_forest_occurrence_t
-find_leftmost_node (occ)
- et_forest_occurrence_t occ;
+find_leftmost_node (et_forest_occurrence_t occ)
{
while (occ->left)
occ = occ->left;
/* Return rightmost node present in the tree roted by OCC. */
static inline et_forest_occurrence_t
-find_rightmost_node (occ)
- et_forest_occurrence_t occ;
+find_rightmost_node (et_forest_occurrence_t occ)
{
while (occ->right)
occ = occ->right;
}
-/* Operation splay for splay tree structure representing ocuurences. */
+/* Operation splay for splay tree structure representing occurrences. */
static et_forest_occurrence_t
-splay (node)
- et_forest_occurrence_t node;
+splay (et_forest_occurrence_t node)
{
et_forest_occurrence_t parent;
et_forest_occurrence_t grandparent;
}
}
}
-
+
}
/* parent == root. */
{
et_forest_occurrence_t node1;
int count1;
-
+
node1 = node->right;
count1 = node->count_right;
else
node->parent->right = node;
}
- }
- else
+ }
+ else
{
/* node == parent->right. */
et_forest_occurrence_t node1;
int count1;
-
+
node1 = node->left;
count1 = node->count_left;
return node;
}
-/* Remove all occurences of the given node before destroying the node. */
+/* Remove all occurrences of the given node before destroying the node. */
static void
-remove_all_occurrences (forest, forest_node)
- et_forest_t forest;
- et_forest_node_t forest_node;
+remove_all_occurrences (et_forest_t forest, et_forest_node_t forest_node)
{
et_forest_occurrence_t first = forest_node->first;
et_forest_occurrence_t last = forest_node->last;
if (first->left)
first->left->parent = 0;
if (first->right)
- first->right->parent = 0;
+ first->right->parent = 0;
if (last != first)
{
prev_node = splay (find_rightmost_node (first->left));
next_node = splay (find_leftmost_node (last->right));
- /* prev_node and next_node are consecutive occurencies
+ /* prev_node and next_node are consecutive occurrences
of the same node. */
if (prev_node->next != next_node)
abort ();
/* Calculate ET value of the given node. */
static inline int
-calculate_value (node)
- et_forest_occurrence_t node;
+calculate_value (et_forest_occurrence_t node)
{
int value = node->count_left;
/* Create ET-forest structure. */
et_forest_t
-et_forest_create ()
+et_forest_create (void)
{
-
et_forest_t forest = xmalloc (sizeof (struct et_forest));
forest->nnodes = 0;
/* Deallocate the structure. */
-void
-et_forest_delete (forest)
- et_forest_t forest;
+void
+et_forest_delete (et_forest_t forest)
{
if (forest->nnodes)
abort ();
/* Create new node with VALUE and return the edge.
Return NULL when memory allocation failed. */
et_forest_node_t
-et_forest_add_node (forest, value)
- et_forest_t forest;
- void *value;
+et_forest_add_node (et_forest_t forest, void *value)
{
/* Create node with one occurrence. */
et_forest_node_t node;
return node;
}
-/* Add new edge to the tree, return 1 if succesfull.
+/* Add new edge to the tree, return 1 if successful.
0 indicates that creation of the edge will close the cycle in graph. */
int
-et_forest_add_edge (forest, parent_node, child_node)
- et_forest_t forest ATTRIBUTE_UNUSED;
- et_forest_node_t parent_node;
- et_forest_node_t child_node;
+et_forest_add_edge (et_forest_t forest ATTRIBUTE_UNUSED,
+ et_forest_node_t parent_node, et_forest_node_t child_node)
{
et_forest_occurrence_t new_occ, parent_occ, child_occ;
if (child_occ->left)
abort (); /* child must be root of its containing tree. */
-
+
new_occ = pool_alloc (forest->occur_pool);
new_occ->node = parent_node;
/* Remove NODE from the tree and all connected edges. */
void
-et_forest_remove_node (forest, node)
- et_forest_t forest;
- et_forest_node_t node;
+et_forest_remove_node (et_forest_t forest, et_forest_node_t node)
{
remove_all_occurrences (forest, node);
forest->nnodes--;
pool_free (forest->node_pool, node);
}
-/* Remove edge from the tree, return 1 if sucesfull,
+/* Remove edge from the tree, return 1 if successful,
0 indicates nonexisting edge. */
int
-et_forest_remove_edge (forest, parent_node, child_node)
- et_forest_t forest ATTRIBUTE_UNUSED;
- et_forest_node_t parent_node;
- et_forest_node_t child_node;
+et_forest_remove_edge (et_forest_t forest ATTRIBUTE_UNUSED,
+ et_forest_node_t parent_node,
+ et_forest_node_t child_node)
{
et_forest_occurrence_t parent_pre_occ, parent_post_occ;
splay (parent_pre_occ);
parent_pre_occ->right->parent = 0;
-
+
parent_post_occ = parent_pre_occ->next;
splay (parent_post_occ);
/* Return the parent of the NODE if any, NULL otherwise. */
et_forest_node_t
-et_forest_parent (forest, node)
- et_forest_t forest ATTRIBUTE_UNUSED;
- et_forest_node_t node;
+et_forest_parent (et_forest_t forest ATTRIBUTE_UNUSED, et_forest_node_t node)
{
splay (node->first);
/* Return nearest common ancestor of NODE1 and NODE2.
Return NULL of they are in different trees. */
et_forest_node_t
-et_forest_common_ancestor (forest, node1, node2)
- et_forest_t forest ATTRIBUTE_UNUSED;
- et_forest_node_t node1;
- et_forest_node_t node2;
+et_forest_common_ancestor (et_forest_t forest ATTRIBUTE_UNUSED,
+ et_forest_node_t node1, et_forest_node_t node2)
{
int value1, value2, max_value;
et_forest_node_t ancestor;
if (node1 == node2)
return node1;
-
+
if (! node1 || ! node2)
abort ();
ancestor = node2;
max_value = value1;
}
-
+
while (calculate_value (ancestor->last) < max_value)
{
/* Find parent node. */
/* Return the value pointer of node set during it's creation. */
void *
-et_forest_node_value (forest, node)
- et_forest_t forest ATTRIBUTE_UNUSED;
- et_forest_node_t node;
+et_forest_node_value (et_forest_t forest ATTRIBUTE_UNUSED,
+ et_forest_node_t node)
{
/* Alloc threading NULL as a special node of the forest. */
if (!node)
/* Find all sons of NODE and store them into ARRAY allocated by the caller.
Return number of nodes found. */
int
-et_forest_enumerate_sons (forest, node, array)
- et_forest_t forest ATTRIBUTE_UNUSED;
- et_forest_node_t node;
- et_forest_node_t *array;
+et_forest_enumerate_sons (et_forest_t forest ATTRIBUTE_UNUSED,
+ et_forest_node_t node, et_forest_node_t *array)
{
int n = 0;
et_forest_occurrence_t occ = node->first, stop = node->last, occ1;
/* Parent is the rightmost node of the left successor.
- Look for all occurences having no right succesor
+ Look for all occurrences having no right successor
and lookup the sons. */
while (occ != stop)
{