Skip to content

Commit

Permalink
a push for imac
Browse files Browse the repository at this point in the history
  • Loading branch information
RidaEn-nasry committed Oct 26, 2022
1 parent b6f1193 commit 69bcc5b
Show file tree
Hide file tree
Showing 31 changed files with 293 additions and 222 deletions.
4 changes: 2 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
"name": "(lldb) Launch",
"type": "lldb",
"request": "launch",
"program": "/Users/ren-nasr/Reimplementing-STL-Containers./test",
"program": "/Users/wa5ina/42/rsc/test",
"args": [
"benchmark"
"main"
],
"cwd": "${cwd}",

Expand Down
70 changes: 51 additions & 19 deletions containers/map/map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: ren-nasr <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/04 11:48:11 by ren-nasr #+# #+# */
/* Updated: 2022/10/24 18:21:16 by ren-nasr ### ########.fr */
/* Updated: 2022/10/26 10:28:02 by ren-nasr ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -30,7 +30,6 @@
#include <lexico_compare.hpp>
#include <equal.hpp>


namespace ft
{

Expand Down Expand Up @@ -238,7 +237,12 @@ namespace ft
_min = NULL;
_max = NULL;
_size = 0;
insert(x.begin(), x.end());
*this = x;
// _root = NULL;
// _min = NULL;
// _max = NULL;
// _size = 0;
// insert(x.begin(), x.end());
};

/* destructor */
Expand All @@ -250,8 +254,10 @@ namespace ft
/* operator= */
map& operator=(const map& x)
{
if (this == &x)
return *this;
clear();
insert(x.begin(), x.end());
insert(x.begin(), x.end());
return *this;
};

Expand Down Expand Up @@ -507,7 +513,6 @@ namespace ft
++first;
}

// else insert a new element
for (; first != last; ++first)
{
pair<iterator, bool> ret = _insert(*first);
Expand All @@ -534,7 +539,6 @@ namespace ft
// if tree is empty, do nothing
if (_root == NULL)
return 0;

// if key is not in the tree, do nothing
value_type val = value_type(erase_key, mapped_type());
node_type* node = _find(val);
Expand Down Expand Up @@ -619,7 +623,7 @@ namespace ft
{
return !(lhs == rhs);
};

friend bool operator<(const map& lhs, const map& rhs)
{
return lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
Expand All @@ -639,13 +643,13 @@ namespace ft
{
return !(lhs < rhs);
};

// swap
friend void swap(map& lhs, map& rhs)
{
lhs.swap(rhs);
};


private:
allocator_type _alloc;
Expand Down Expand Up @@ -678,7 +682,19 @@ namespace ft

// checking balance starting from node and going up to the root
node_type* tmp = node;
while (tmp != _root && tmp != NULL)
if (tmp == NULL)
return;

node_type* roof = tmp;
// checking for balance at 3 levels
for (int i = 0; i < 2; i++)
{
if (roof->parent() == NULL)
break;
roof = roof->parent();
}

while (tmp != roof && tmp != NULL)
{
int balance = tmp->balance();
// if subtree is balanced, continue
Expand Down Expand Up @@ -800,6 +816,13 @@ namespace ft
// if it's NULL, don't segfault yourself
if (tmp)
tmp->setParent(node->parent());
if (node->parent())
{
if (node == _min)
_min = node->parent();
if (node == _max)
_max = node->parent();
}
_node_alloc.destroy(node);
_node_alloc.deallocate(node, 1);
// return tmp;
Expand Down Expand Up @@ -867,13 +890,14 @@ namespace ft
node->setRight(new_node);
// check tree balance
_balance(node);
// update min and max
_max = _maximum();
_min = _minimum();

// updating dummy end
// _update_dummy_end();
// return iterator to the new node
if (!_min || _comp(val, _min->data()))
_min = new_node;
if (!_max || _comp(_max->data(), val))
_max = new_node;

// updating dummy end
// _update_dummy_end();
// return iterator to the new node
return make_pair(iterator(new_node, _min, _max), true);
};

Expand Down Expand Up @@ -919,10 +943,18 @@ namespace ft
_splice(min);
_balance(node);
}
_max = _maximum();
_min = _minimum();
// _max = _maximum();
// _min = _minimum();
};

// advance
void _advance(const_iterator& it, size_type n)
{
while (n--)
++it;
};



}; // map class

Expand Down
8 changes: 4 additions & 4 deletions containers/vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: ren-nasr <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/07/30 13:56:03 by ren-nasr #+# #+# */
/* Updated: 2022/10/24 18:15:40 by ren-nasr ### ########.fr */
/* Updated: 2022/10/25 12:12:50 by ren-nasr ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -440,12 +440,12 @@ namespace ft
_size++;
return position;
}
else if (this->_size == this->_capacity)
else if (this->_size >= this->_capacity)
{
// get position index
size_type index = position - this->begin();
reserve(this->_capacity == 0 ? 1 : this->_capacity * 1.5);
reserve(_capacity * 1.5);
// reserve(this->_capacity == 0 ? 1 : this->_capacity * 1.5);
reserve(_capacity * 2);
// reassign position
position = this->begin() + index;
}
Expand Down
6 changes: 6 additions & 0 deletions s.bash
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

# launch an executable and time it

# usage: timeit <executable> <args>
# we gonna use the time command to time the execution of the executable
time $1
Binary file added test
Binary file not shown.
Binary file added tests/avlnode.o
Binary file not shown.
Loading

0 comments on commit 69bcc5b

Please sign in to comment.