makefile really improved on header dependencies

This commit is contained in:
hugogogo
2022-06-29 14:10:17 +02:00
parent 26436c8d8a
commit b0b63d6238
27 changed files with 610 additions and 481 deletions

View File

@@ -43,8 +43,8 @@ MP_TPL MP::
~map() {
clear();
_allocator_node_sentinel.destroy(_sentinel);
_allocator_node_sentinel.deallocate(_sentinel, 1);
_allocator_sentinel.destroy(_sentinel);
_allocator_sentinel.deallocate(_sentinel, 1);
}
// operator= ---------------------------------
MP_TPL MP& MP::
@@ -141,12 +141,40 @@ MP_TPL typename MP::mapped_type& MP::
MP_TPL pair<typename MP::iterator, bool> MP::
insert(const value_type& value) {
pair<typename MP::iterator, bool> ret;
ret = _insert(value);
if (ret.second == true)
_insert_rebalancing(ret.first.getNode()->up);
return (ret);
node<value_type>* n = _root;
node<value_type>* next = n;
while (next != NULL)
{
if (value.first == n->value.first)
return ft::make_pair(iterator(n, _sentinel), false);
n = next;
if (value.first < n->value.first)
next = n->left;
else if (value.first > n->value.first)
next = n->right;
}
next = _allocator_node.allocate(1);
_allocator_node.construct(next, node<value_type>(value));
if (_root == NULL)
{
_root = next;
_sentinel->child = next;
}
else
{
if (value.first < n->value.first)
n->left = next;
else if (value.first > n->value.first)
n->right = next;
}
next->up = n;
_size++;
_balance(n, INSERT);
return (ft::make_pair(iterator(next, _sentinel), true));
}
MP_TPL typename MP::iterator MP::
insert(iterator hint, const value_type& value) {
@@ -157,19 +185,47 @@ MP_TPL typename MP::iterator MP::
MP_TPL template < typename InputIt > void MP::
insert(InputIt first, InputIt last) {
while (first != last)
{
for (; first != last; first++)
insert(*first);
++first;
}
}
// erase -------------------------------------
// https://www.geeksforgeeks.org/binary-search-tree-set-2-delete
MP_TPL void MP::
erase(iterator pos) {
node<value_type>* delete_point;
delete_point = _erase(pos);
_erase_rebalancing(delete_point);
node<value_type>* n = pos.getNode();
node<value_type>* n_del = NULL;
node<value_type>* next;
if (n->left && n->right)
{
next = n->right->min();
if (next->up != n)
{
_subtree_shift(next, next->right);
next->right = n->right;
next->right->up = next;
}
n_del = _subtree_shift(n, next);
next->left = n->left;
next->left->up = next;
}
else
{
if (n->left)
n_del = _subtree_shift(n, n->left);
else if (n->right)
n_del = _subtree_shift(n, n->right);
else
n_del = _subtree_shift(n, NULL);
}
_allocator_node.destroy(n);
_allocator_node.deallocate(n, 1);
_size--;
_balance(n_del, ERASE);
}
MP_TPL void MP::
erase(iterator first, iterator last) {
@@ -180,22 +236,21 @@ MP_TPL void MP::
MP_TPL typename MP::size_type MP::
erase(const Key& key) {
iterator pos = find(key);
iterator pos;
pos = find(key);
if (pos == end())
return (0);
else
{
erase(pos);
return (1);
}
erase(pos);
return (1);
}
// swap --------------------------------------
MP_TPL void MP::
swap(map& other) {
node<value_type>* tmp_root = _root;
node_sentinel<value_type>* tmp_sentinel = _sentinel;
size_type tmp_size = _size;
node<value_type>* tmp_root = _root;
sentinel<value_type>* tmp_sentinel = _sentinel;
size_type tmp_size = _size;
_root = other._root;
_sentinel = other._sentinel;
@@ -351,146 +406,35 @@ MP_TPL typename MP::allocator_type MP::
MP_TPL void MP::
_init_sentinel() {
_sentinel = _allocator_node_sentinel.allocate(1);
_allocator_node_sentinel.construct(_sentinel, node_sentinel<value_type>());
}
MP_TPL pair<typename MP::iterator, bool> MP::
_insert(const value_type& value) {
node<value_type>* n = _root;
node<value_type>* prev = NULL;
while (n)
{
prev = n;
if (_comp(value.first, n->value.first))
n = n->left;
else if (_comp(n->value.first, value.first))
n = n->right;
else
return ft::make_pair(iterator(n, _sentinel), false);
}
n = _allocator_node.allocate(1);
_allocator_node.construct(n, node<value_type>(value));
if (_root == NULL)
{
_root = n;
_sentinel->child = _root;
}
else if (_comp(value.first, prev->value.first))
prev->left = n;
else
prev->right = n;
n->up = prev;
++_size;
return ft::make_pair(iterator(n, _sentinel), true);
_sentinel = _allocator_sentinel.allocate(1);
_allocator_sentinel.construct(_sentinel, sentinel<value_type>());
}
MP_TPL node<typename MP::value_type>* MP::
_erase(iterator pos) {
_subtree_shift(node<value_type>* n_old, node<value_type>* n_new) {
node<value_type>* n = pos.getNode();
node<value_type>* delete_point = NULL;
node<value_type>* p = n_old->up;
if (n->left && n->right)
{
node<value_type>* next = n->right->min();
if (next->up != n)
{
_subtree_shift(next, next->right);
next->right = n->right;
next->right->up = next;
}
delete_point = _subtree_shift(n, next);
next->left = n->left;
next->left->up = next;
}
else if (!n->left && !n->right)
delete_point = _subtree_shift(n, NULL);
else if (n->left)
delete_point = _subtree_shift(n, n->left);
else if (n->right)
delete_point = _subtree_shift(n, n->right);
_allocator_node.destroy(n);
_allocator_node.deallocate(n, 1);
--_size;
return (delete_point);
}
MP_TPL node<typename MP::value_type>* MP::
_subtree_shift(node<value_type>* st_old, node<value_type>* st_new) {
node<value_type>* p = st_old->up;
if (st_old == _root)
if (n_old == _root)
{
_root = st_new;
_root = n_new;
_sentinel->child = _root;
}
else if (st_old == p->left)
p->left = st_new;
else if (n_old == p->left)
p->left = n_new;
else
p->right = st_new;
p->right = n_new;
if (st_new == NULL)
if (n_new == NULL)
return (p);
st_new->up = p;
return (st_new);
n_new->up = p;
return (n_new);
}
MP_TPL void MP::
_insert_rebalancing(node<value_type>* n) {
_balance(node<value_type>* n, bool action) {
node<value_type>* old_n;
node<value_type>* parent = NULL;
while (n)
{
n->height = _compute_height(n);
if (_balance_factor(n) > 1) // Left Heavy
{
parent = n->up;
if (_balance_factor(n->left) < 0) // Left-Right Case
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
{
parent = n->up;
if (_balance_factor(n->right) > 0) // Right-Left Case
n->right = _rotate_right(n->right);
// Right-Right Case
n = _rotate_left(n);
old_n = n->left;
}
if (parent)
{
if (parent->left == old_n)
parent->left = n;
else
parent->right = n;
break;
}
n = n->up;
}
while (n)
{
n->height = _compute_height(n);
n = n->up;
}
}
MP_TPL void MP::
_erase_rebalancing(node<value_type>* n) {
node<value_type>* old_n;
node<value_type>* parent = NULL;
while (n)
{
n->height = _compute_height(n);
@@ -520,11 +464,23 @@ MP_TPL void MP::
parent->left = n;
else
parent->right = n;
parent = NULL;
if (action == INSERT)
break;
else if (action == ERASE)
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<value_type>* n) {