diff --git a/Makefile b/Makefile index b5dbccf..ecafa0c 100644 --- a/Makefile +++ b/Makefile @@ -39,8 +39,8 @@ EXT = cpp CFLAGS = -Wall -Wextra -Werror $(INCLUDES) CFLAGS += -MMD -MP #see end-page note on header dependencie -#CFLAGS += -std=c++98 -#CFLAGS += -g3 +CFLAGS += -std=c++98 +CFLAGS += -g3 CFLAGS_STL = $(CFLAGS) CFLAGS_STL += -D STL diff --git a/headers/map.hpp b/headers/map.hpp index 48bf5ce..c20142f 100644 --- a/headers/map.hpp +++ b/headers/map.hpp @@ -175,9 +175,10 @@ private: // BBST enum {INSERT, ERASE}; - void _balance(node* n, bool action); + void _balance(node* n); short _compute_height(node* n); short _balance_factor(node* n); + short _balance_f(node* n); node* _rotate_left(node* n); node* _rotate_right(node* n); }; diff --git a/templates/map.tpp b/templates/map.tpp index e0e088c..69e1b17 100644 --- a/templates/map.tpp +++ b/templates/map.tpp @@ -176,7 +176,7 @@ MP_TPL pair MP:: next->up = n; _size++; - _balance(n, INSERT); + _balance(n); return (ft::make_pair(iterator(next, _sentinel), true)); } MP_TPL typename MP::iterator MP:: @@ -238,7 +238,7 @@ MP_TPL void MP:: _allocator_node.deallocate(n, 1); _size--; - _balance(replace, ERASE); + _balance(replace); } MP_TPL void MP:: erase(iterator first, iterator last) { @@ -417,7 +417,7 @@ MP_TPL typename MP::allocator_type MP:: * private functions : *********************/ MP_TPL void MP:: - _balance(node* n, bool action) { + _balance(node* n) { node* old_n; node* parent = NULL; @@ -425,20 +425,25 @@ MP_TPL void MP:: while (n) { n->height = _compute_height(n); +// n->height = _balance_f(n); if (_balance_factor(n) > 1) // Left Heavy +// if (n->height > 1) // Left Heavy { parent = n->up; if (_balance_factor(n->left) < 0) // Left-Right Case (BF == -1) +// if (n->left->height < 0) // Left-Right Case (BF == -1) n->left = _rotate_left(n->left); // Left-Left Case n = _rotate_right(n); old_n = n->right; } - else if (_balance_factor(n) < -1) // Right Heavy +// else if (_balance_factor(n) < -1) // Right Heavy + else if (n->height < -1) // Right Heavy { parent = n->up; if (_balance_factor(n->right) > 0) // Right-Left Case (BF == 1) +// if (n->right->height > 0) // Right-Left Case (BF == 1) n->right = _rotate_right(n->right); // Right-Right Case n = _rotate_left(n); @@ -451,23 +456,11 @@ MP_TPL void MP:: parent->left = n; else parent->right = n; - if (action == INSERT) - break; - else if (action == ERASE) - parent = NULL; + parent = NULL; } n = n->up; } - - if (action == INSERT) - { - while (n) - { - n->height = _compute_height(n); - n = n->up; - } - } } MP_TPL short MP:: _compute_height(node* n) { @@ -481,6 +474,18 @@ MP_TPL short MP:: else return 1; } +MP_TPL short MP:: + _balance_f(node* n) { + + if (n->left && n->right) + return n->left->height - n->right->height; + else if (n->left) + return (n->left->height + 1); + else if (n->right) + return (-1 - (n->right->height)); + else + return 0; +} MP_TPL short MP:: _balance_factor(node* n) { @@ -508,6 +513,8 @@ MP_TPL node* MP:: n->height = _compute_height(n); ori_right->height = _compute_height(ori_right); +// n->height = _balance_f(n); +// ori_right->height = _balance_f(ori_right); if (n == _root) { @@ -532,6 +539,8 @@ MP_TPL node* MP:: n->height = _compute_height(n); ori_left->height = _compute_height(ori_left); +// n->height = _balance_f(n); +// ori_left->height = _balance_f(ori_left); if (n == _root) { diff --git a/tests/main.cpp b/tests/main.cpp index ac6c4c4..06f15e6 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -4,35 +4,35 @@ int main() { // VECTOR - tests_vector_constructor(); - tests_vector_operator_assignation(); - tests_vector_begin(); - tests_vector_end(); - tests_vector_rbegin(); - tests_vector_rend(); - tests_vector_size(); - tests_vector_max_size(); - tests_vector_resize(); - tests_vector_capacity(); - tests_vector_empty(); - tests_vector_reserve(); - tests_vector_operator_access(); - tests_vector_at(); - tests_vector_front(); - tests_vector_back(); - tests_vector_assign(); - tests_vector_push_back(); - tests_vector_pop_back(); - tests_vector_insert(); - tests_vector_erase(); - tests_vector_swap(); - tests_vector_clear(); - tests_vector_get_allocator(); - tests_vector_swap_non_member(); - tests_vector_reverse_iterators(); - tests_vector_relational_operators(); - - // MAP +// tests_vector_constructor(); +// tests_vector_operator_assignation(); +// tests_vector_begin(); +// tests_vector_end(); +// tests_vector_rbegin(); +// tests_vector_rend(); +// tests_vector_size(); +// tests_vector_max_size(); +// tests_vector_resize(); +// tests_vector_capacity(); +// tests_vector_empty(); +// tests_vector_reserve(); +// tests_vector_operator_access(); +// tests_vector_at(); +// tests_vector_front(); +// tests_vector_back(); +// tests_vector_assign(); +// tests_vector_push_back(); +// tests_vector_pop_back(); +// tests_vector_insert(); +// tests_vector_erase(); +// tests_vector_swap(); +// tests_vector_clear(); +// tests_vector_get_allocator(); +// tests_vector_swap_non_member(); +// tests_vector_reverse_iterators(); +// tests_vector_relational_operators(); +// +// // MAP tests_map_simple(); tests_map_constructor(); tests_map_operator_assignation(); @@ -58,14 +58,14 @@ int main() { tests_map_get_allocator(); tests_map_swap_non_member(); tests_map_relational_operators(); - - // STACK - tests_stack_constructor(); - tests_stack_empty(); - tests_stack_size(); - tests_stack_top(); - tests_stack_push(); - tests_stack_pop(); +// +// // STACK +// tests_stack_constructor(); +// tests_stack_empty(); +// tests_stack_size(); +// tests_stack_top(); +// tests_stack_push(); +// tests_stack_pop(); // execute tests and print them : int size = test_list.size();