Refactor source code (#32)

Refactor large functions into smaller functions.
This commit is contained in:
Bailey Thompson
2019-05-05 00:57:33 -04:00
committed by GitHub
parent e2d596e4bd
commit 14f4a03c92
4 changed files with 492 additions and 308 deletions

118
src/map.c
View File

@@ -150,14 +150,12 @@ static void map_rotate_right(map me,
}
/*
* Repairs the AVL tree on insert.
* Performs a left repair.
*/
static struct node *map_repair(map me,
static struct node *map_repair_left(map me,
struct node *const parent,
struct node *const child,
struct node *const grand_child)
struct node *const child)
{
if (parent->balance == 2 && child->balance >= 0) {
map_rotate_left(me, parent, child);
if (child->balance == 0) {
parent->balance = 1;
@@ -167,8 +165,15 @@ static struct node *map_repair(map me,
child->balance = 0;
}
return child;
}
if (parent->balance == -2 && child->balance <= 0) {
}
/*
* Performs a right repair.
*/
static struct node *map_repair_right(map me,
struct node *const parent,
struct node *const child)
{
map_rotate_right(me, parent, child);
if (child->balance == 0) {
parent->balance = -1;
@@ -178,8 +183,16 @@ static struct node *map_repair(map me,
child->balance = 0;
}
return child;
}
if (parent->balance == -2 && child->balance == 1) {
}
/*
* Performs a left-right repair.
*/
static struct node *map_repair_left_right(map me,
struct node *const parent,
struct node *const child,
struct node *const grand_child)
{
map_rotate_left(me, child, grand_child);
map_rotate_right(me, parent, grand_child);
if (grand_child->balance == 1) {
@@ -194,8 +207,16 @@ static struct node *map_repair(map me,
}
grand_child->balance = 0;
return grand_child;
}
if (parent->balance == 2 && child->balance == -1) {
}
/*
* Performs a right-left repair.
*/
static struct node *map_repair_right_left(map me,
struct node *const parent,
struct node *const child,
struct node *const grand_child)
{
map_rotate_right(me, child, grand_child);
map_rotate_left(me, parent, grand_child);
if (grand_child->balance == 1) {
@@ -210,8 +231,27 @@ static struct node *map_repair(map me,
}
grand_child->balance = 0;
return grand_child;
}
/*
* Repairs the AVL tree on insert. The only possible values of parent->balance
* are {-2, 2} and the only possible values of child->balance are {-1, 0, 1}.
*/
static struct node *map_repair(map me,
struct node *const parent,
struct node *const child,
struct node *const grand_child)
{
if (parent->balance == 2) {
if (child->balance == -1) {
return map_repair_right_left(me, parent, child, grand_child);
}
/* Impossible to get here. */
return map_repair_left(me, parent, child);
}
if (child->balance == 1) {
return map_repair_left_right(me, parent, child, grand_child);
}
return map_repair_right(me, parent, child);
}
/*
@@ -408,32 +448,12 @@ static struct node *map_repair_pivot(map me,
}
/*
* Balances the AVL tree on deletion.
* Goes back up the tree repairing it along the way.
*/
static void map_delete_balance(map me,
struct node *item,
const int is_left_deleted)
static void map_trace_ancestors(map me, struct node *item)
{
struct node *child;
struct node *parent;
if (is_left_deleted) {
item->balance++;
} else {
item->balance--;
}
/* If balance is -1 or +1 after modification, then the tree is balanced. */
if (item->balance == -1 || item->balance == 1) {
return;
}
/* Must re-balance if not in {-1, 0, 1} */
if (item->balance > 1 || item->balance < -1) {
item = map_repair_pivot(me, item, is_left_deleted);
if (!item->parent || item->balance == -1 || item->balance == 1) {
return;
}
}
child = item;
parent = item->parent;
struct node *child = item;
struct node *parent = item->parent;
while (parent) {
if (parent->left == child) {
parent->balance++;
@@ -460,6 +480,32 @@ static void map_delete_balance(map me,
}
}
/*
* Balances the AVL tree on deletion.
*/
static void map_delete_balance(map me,
struct node *item,
const int is_left_deleted)
{
if (is_left_deleted) {
item->balance++;
} else {
item->balance--;
}
/* If balance is -1 or +1 after modification, then the tree is balanced. */
if (item->balance == -1 || item->balance == 1) {
return;
}
/* Must re-balance if not in {-1, 0, 1} */
if (item->balance > 1 || item->balance < -1) {
item = map_repair_pivot(me, item, is_left_deleted);
if (!item->parent || item->balance == -1 || item->balance == 1) {
return;
}
}
map_trace_ancestors(me, item);
}
/*
* Removes traverse when it has no children.
*/

View File

@@ -166,14 +166,12 @@ static void multimap_rotate_right(multimap me,
}
/*
* Repairs the AVL tree on insert.
* Performs a left repair.
*/
static struct node *multimap_repair(multimap me,
static struct node *multimap_repair_left(multimap me,
struct node *const parent,
struct node *const child,
struct node *const grand_child)
struct node *const child)
{
if (parent->balance == 2 && child->balance >= 0) {
multimap_rotate_left(me, parent, child);
if (child->balance == 0) {
parent->balance = 1;
@@ -183,8 +181,15 @@ static struct node *multimap_repair(multimap me,
child->balance = 0;
}
return child;
}
if (parent->balance == -2 && child->balance <= 0) {
}
/*
* Performs a right repair.
*/
static struct node *multimap_repair_right(multimap me,
struct node *const parent,
struct node *const child)
{
multimap_rotate_right(me, parent, child);
if (child->balance == 0) {
parent->balance = -1;
@@ -194,8 +199,16 @@ static struct node *multimap_repair(multimap me,
child->balance = 0;
}
return child;
}
if (parent->balance == -2 && child->balance == 1) {
}
/*
* Performs a left-right repair.
*/
static struct node *multimap_repair_left_right(multimap me,
struct node *const parent,
struct node *const child,
struct node *const grand_child)
{
multimap_rotate_left(me, child, grand_child);
multimap_rotate_right(me, parent, grand_child);
if (grand_child->balance == 1) {
@@ -210,8 +223,16 @@ static struct node *multimap_repair(multimap me,
}
grand_child->balance = 0;
return grand_child;
}
if (parent->balance == 2 && child->balance == -1) {
}
/*
* Performs a right-left repair.
*/
static struct node *multimap_repair_right_left(multimap me,
struct node *const parent,
struct node *const child,
struct node *const grand_child)
{
multimap_rotate_right(me, child, grand_child);
multimap_rotate_left(me, parent, grand_child);
if (grand_child->balance == 1) {
@@ -226,8 +247,27 @@ static struct node *multimap_repair(multimap me,
}
grand_child->balance = 0;
return grand_child;
}
/*
* Repairs the AVL tree on insert. The only possible values of parent->balance
* are {-2, 2} and the only possible values of child->balance are {-1, 0, 1}.
*/
static struct node *multimap_repair(multimap me,
struct node *const parent,
struct node *const child,
struct node *const grand_child)
{
if (parent->balance == 2) {
if (child->balance == -1) {
return multimap_repair_right_left(me, parent, child, grand_child);
}
/* Impossible to get here. */
return multimap_repair_left(me, parent, child);
}
if (child->balance == 1) {
return multimap_repair_left_right(me, parent, child, grand_child);
}
return multimap_repair_right(me, parent, child);
}
/*
@@ -487,32 +527,12 @@ static struct node *multimap_repair_pivot(multimap me,
}
/*
* Balances the AVL tree on deletion.
* Goes back up the tree repairing it along the way.
*/
static void multimap_delete_balance(multimap me,
struct node *item,
const int is_left_deleted)
static void multimap_trace_ancestors(multimap me, struct node *item)
{
struct node *child;
struct node *parent;
if (is_left_deleted) {
item->balance++;
} else {
item->balance--;
}
/* If balance is -1 or +1 after modification, then the tree is balanced. */
if (item->balance == -1 || item->balance == 1) {
return;
}
/* Must re-balance if not in {-1, 0, 1} */
if (item->balance > 1 || item->balance < -1) {
item = multimap_repair_pivot(me, item, is_left_deleted);
if (!item->parent || item->balance == -1 || item->balance == 1) {
return;
}
}
child = item;
parent = item->parent;
struct node *child = item;
struct node *parent = item->parent;
while (parent) {
if (parent->left == child) {
parent->balance++;
@@ -539,6 +559,32 @@ static void multimap_delete_balance(multimap me,
}
}
/*
* Balances the AVL tree on deletion.
*/
static void multimap_delete_balance(multimap me,
struct node *item,
const int is_left_deleted)
{
if (is_left_deleted) {
item->balance++;
} else {
item->balance--;
}
/* If balance is -1 or +1 after modification, then the tree is balanced. */
if (item->balance == -1 || item->balance == 1) {
return;
}
/* Must re-balance if not in {-1, 0, 1} */
if (item->balance > 1 || item->balance < -1) {
item = multimap_repair_pivot(me, item, is_left_deleted);
if (!item->parent || item->balance == -1 || item->balance == 1) {
return;
}
}
multimap_trace_ancestors(me, item);
}
/*
* Removes traverse when it has no children.
*/

View File

@@ -148,14 +148,12 @@ static void multiset_rotate_right(multiset me,
}
/*
* Repairs the AVL tree on insert.
* Performs a left repair.
*/
static struct node *multiset_repair(multiset me,
static struct node *multiset_repair_left(multiset me,
struct node *const parent,
struct node *const child,
struct node *const grand_child)
struct node *const child)
{
if (parent->balance == 2 && child->balance >= 0) {
multiset_rotate_left(me, parent, child);
if (child->balance == 0) {
parent->balance = 1;
@@ -165,8 +163,15 @@ static struct node *multiset_repair(multiset me,
child->balance = 0;
}
return child;
}
if (parent->balance == -2 && child->balance <= 0) {
}
/*
* Performs a right repair.
*/
static struct node *multiset_repair_right(multiset me,
struct node *const parent,
struct node *const child)
{
multiset_rotate_right(me, parent, child);
if (child->balance == 0) {
parent->balance = -1;
@@ -176,8 +181,16 @@ static struct node *multiset_repair(multiset me,
child->balance = 0;
}
return child;
}
if (parent->balance == -2 && child->balance == 1) {
}
/*
* Performs a left-right repair.
*/
static struct node *multiset_repair_left_right(multiset me,
struct node *const parent,
struct node *const child,
struct node *const grand_child)
{
multiset_rotate_left(me, child, grand_child);
multiset_rotate_right(me, parent, grand_child);
if (grand_child->balance == 1) {
@@ -192,8 +205,16 @@ static struct node *multiset_repair(multiset me,
}
grand_child->balance = 0;
return grand_child;
}
if (parent->balance == 2 && child->balance == -1) {
}
/*
* Performs a right-left repair.
*/
static struct node *multiset_repair_right_left(multiset me,
struct node *const parent,
struct node *const child,
struct node *const grand_child)
{
multiset_rotate_right(me, child, grand_child);
multiset_rotate_left(me, parent, grand_child);
if (grand_child->balance == 1) {
@@ -208,8 +229,27 @@ static struct node *multiset_repair(multiset me,
}
grand_child->balance = 0;
return grand_child;
}
/*
* Repairs the AVL tree on insert. The only possible values of parent->balance
* are {-2, 2} and the only possible values of child->balance are {-1, 0, 1}.
*/
static struct node *multiset_repair(multiset me,
struct node *const parent,
struct node *const child,
struct node *const grand_child)
{
if (parent->balance == 2) {
if (child->balance == -1) {
return multiset_repair_right_left(me, parent, child, grand_child);
}
/* Impossible to get here. */
return multiset_repair_left(me, parent, child);
}
if (child->balance == 1) {
return multiset_repair_left_right(me, parent, child, grand_child);
}
return multiset_repair_right(me, parent, child);
}
/*
@@ -396,32 +436,12 @@ static struct node *multiset_repair_pivot(multiset me,
}
/*
* Balances the AVL tree on deletion.
* Goes back up the tree repairing it along the way.
*/
static void multiset_delete_balance(multiset me,
struct node *item,
const int is_left_deleted)
static void multiset_trace_ancestors(multiset me, struct node *item)
{
struct node *child;
struct node *parent;
if (is_left_deleted) {
item->balance++;
} else {
item->balance--;
}
/* If balance is -1 or +1 after modification, then the tree is balanced. */
if (item->balance == -1 || item->balance == 1) {
return;
}
/* Must re-balance if not in {-1, 0, 1} */
if (item->balance > 1 || item->balance < -1) {
item = multiset_repair_pivot(me, item, is_left_deleted);
if (!item->parent || item->balance == -1 || item->balance == 1) {
return;
}
}
child = item;
parent = item->parent;
struct node *child = item;
struct node *parent = item->parent;
while (parent) {
if (parent->left == child) {
parent->balance++;
@@ -448,6 +468,32 @@ static void multiset_delete_balance(multiset me,
}
}
/*
* Balances the AVL tree on deletion.
*/
static void multiset_delete_balance(multiset me,
struct node *item,
const int is_left_deleted)
{
if (is_left_deleted) {
item->balance++;
} else {
item->balance--;
}
/* If balance is -1 or +1 after modification, then the tree is balanced. */
if (item->balance == -1 || item->balance == 1) {
return;
}
/* Must re-balance if not in {-1, 0, 1} */
if (item->balance > 1 || item->balance < -1) {
item = multiset_repair_pivot(me, item, is_left_deleted);
if (!item->parent || item->balance == -1 || item->balance == 1) {
return;
}
}
multiset_trace_ancestors(me, item);
}
/*
* Removes traverse when it has no children.
*/

118
src/set.c
View File

@@ -145,14 +145,12 @@ static void set_rotate_right(set me,
}
/*
* Repairs the AVL tree on insert.
* Performs a left repair.
*/
static struct node *set_repair(set me,
static struct node *set_repair_left(set me,
struct node *const parent,
struct node *const child,
struct node *const grand_child)
struct node *const child)
{
if (parent->balance == 2 && child->balance >= 0) {
set_rotate_left(me, parent, child);
if (child->balance == 0) {
parent->balance = 1;
@@ -162,8 +160,15 @@ static struct node *set_repair(set me,
child->balance = 0;
}
return child;
}
if (parent->balance == -2 && child->balance <= 0) {
}
/*
* Performs a right repair.
*/
static struct node *set_repair_right(set me,
struct node *const parent,
struct node *const child)
{
set_rotate_right(me, parent, child);
if (child->balance == 0) {
parent->balance = -1;
@@ -173,8 +178,16 @@ static struct node *set_repair(set me,
child->balance = 0;
}
return child;
}
if (parent->balance == -2 && child->balance == 1) {
}
/*
* Performs a left-right repair.
*/
static struct node *set_repair_left_right(set me,
struct node *const parent,
struct node *const child,
struct node *const grand_child)
{
set_rotate_left(me, child, grand_child);
set_rotate_right(me, parent, grand_child);
if (grand_child->balance == 1) {
@@ -189,8 +202,16 @@ static struct node *set_repair(set me,
}
grand_child->balance = 0;
return grand_child;
}
if (parent->balance == 2 && child->balance == -1) {
}
/*
* Performs a right-left repair.
*/
static struct node *set_repair_right_left(set me,
struct node *const parent,
struct node *const child,
struct node *const grand_child)
{
set_rotate_right(me, child, grand_child);
set_rotate_left(me, parent, grand_child);
if (grand_child->balance == 1) {
@@ -205,8 +226,27 @@ static struct node *set_repair(set me,
}
grand_child->balance = 0;
return grand_child;
}
/*
* Repairs the AVL tree on insert. The only possible values of parent->balance
* are {-2, 2} and the only possible values of child->balance are {-1, 0, 1}.
*/
static struct node *set_repair(set me,
struct node *const parent,
struct node *const child,
struct node *const grand_child)
{
if (parent->balance == 2) {
if (child->balance == -1) {
return set_repair_right_left(me, parent, child, grand_child);
}
/* Impossible to get here. */
return set_repair_left(me, parent, child);
}
if (child->balance == 1) {
return set_repair_left_right(me, parent, child, grand_child);
}
return set_repair_right(me, parent, child);
}
/*
@@ -373,32 +413,12 @@ static struct node *set_repair_pivot(set me,
}
/*
* Balances the AVL tree on deletion.
* Goes back up the tree repairing it along the way.
*/
static void set_delete_balance(set me,
struct node *item,
const int is_left_deleted)
static void set_trace_ancestors(set me, struct node *item)
{
struct node *child;
struct node *parent;
if (is_left_deleted) {
item->balance++;
} else {
item->balance--;
}
/* If balance is -1 or +1 after modification, then the tree is balanced. */
if (item->balance == -1 || item->balance == 1) {
return;
}
/* Must re-balance if not in {-1, 0, 1} */
if (item->balance > 1 || item->balance < -1) {
item = set_repair_pivot(me, item, is_left_deleted);
if (!item->parent || item->balance == -1 || item->balance == 1) {
return;
}
}
child = item;
parent = item->parent;
struct node *child = item;
struct node *parent = item->parent;
while (parent) {
if (parent->left == child) {
parent->balance++;
@@ -425,6 +445,32 @@ static void set_delete_balance(set me,
}
}
/*
* Balances the AVL tree on deletion.
*/
static void set_delete_balance(set me,
struct node *item,
const int is_left_deleted)
{
if (is_left_deleted) {
item->balance++;
} else {
item->balance--;
}
/* If balance is -1 or +1 after modification, then the tree is balanced. */
if (item->balance == -1 || item->balance == 1) {
return;
}
/* Must re-balance if not in {-1, 0, 1} */
if (item->balance > 1 || item->balance < -1) {
item = set_repair_pivot(me, item, is_left_deleted);
if (!item->parent || item->balance == -1 || item->balance == 1) {
return;
}
}
set_trace_ancestors(me, item);
}
/*
* Removes traverse when it has no children.
*/