Skip to content

Commit

Permalink
finally my very slow map container past the test happy slappy
Browse files Browse the repository at this point in the history
  • Loading branch information
RidaEn-nasry committed Oct 26, 2022
1 parent 69bcc5b commit cfcea99
Show file tree
Hide file tree
Showing 20 changed files with 109 additions and 26 deletions.
102 changes: 88 additions & 14 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/26 10:28:02 by ren-nasr ### ########.fr */
/* Updated: 2022/10/26 11:24:00 by ren-nasr ### ########.fr */
/* */
/* ************************************************************************** */

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

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

/* destructor */
~map()
{
clear();
// clear();
};

/* operator= */
// feeew
map& operator=(const map& x)
{
if (this == &x)
return *this;
clear();
insert(x.begin(), x.end());
// treversing the tree at the node level and inserting the nodes
stack<node_type*> s;
node_type* current = x._root;
_root = _new_node(current->data(), NULL);
node_type* tmp = _root;
// push the root and set current to left until we reach the left most node
while (current)
{
s.push(current);
current = current->left();
if (current)
{
tmp->setLeft(_new_node(current->data(), tmp));
tmp = tmp->left();
}
}
// pop the stack and set current to right
while (!s.empty())
{
// pop the stack
current = s.top();
s.pop();
// set current to right
current = current->right();
// create a new node and set the parent
if (current)
{
tmp->setRight(_new_node(current->data(), tmp));
tmp = tmp->right();
}
// push the right node and set current to left until we reach the left most node
while (current)
{
s.push(current);
current = current->left();
if (current)
{
tmp->setLeft(_new_node(current->data(), tmp));
tmp = tmp->left();
}
}

}
_size = x._size;
_min = x._min;
_max = x._max;



// insert(x.begin(), x.end());
return *this;
};

Expand Down Expand Up @@ -490,8 +536,12 @@ namespace ft
node_type* node = _find(*position);
if (node == NULL)
return insert(val).first;
return _insert(node, val).first;
}

pair<iterator, bool> ret = _insert(val);
if (ret.second)
_size++;
return ret.first;
};

// insert range (3)
template <class InputIterator>
Expand Down Expand Up @@ -687,7 +737,7 @@ namespace ft

node_type* roof = tmp;
// checking for balance at 3 levels
for (int i = 0; i < 2; i++)
for (int i = 0; i < 3; i++)
{
if (roof->parent() == NULL)
break;
Expand Down Expand Up @@ -895,12 +945,36 @@ namespace ft
if (!_max || _comp(_max->data(), val))
_max = new_node;

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

// insrting with hint
iterator _insert(node_type* node, value_type& val) {
// if node exist return an iterator to it.
if (!_comp(node->data(), val) && !_comp(val, node->data()))
{
return iterator(node, _min, _max);
}
node_type* new_node = _new_node(val, node);
if (_comp(val, node->data()))
// if smaller than it's parent, insert it to the left
node->setLeft(new_node);
else
// if greater insert to the right
node->setRight(new_node);
// check tree balance
_balance(node);
if (!_min || _comp(val, _min->data()))
_min = new_node;
if (!_max || _comp(_max->data(), val))
_max = new_node;
return iterator(new_node, _min, _max);
};


// _removing
void _remove(const value_type& data)
{
Expand Down
Binary file modified lab/a.out
Binary file not shown.
22 changes: 15 additions & 7 deletions lab/lab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,20 @@ using namespace std;

int main()
{
set<int> s;
s.insert(1);
s.insert(2);
s.insert(3);
s.insert(2);
for (set<int>::iterator it = s.begin(); it != s.end(); it++)
cout << *it << endl;
map<string, int> m;
m.insert(pair<string, int>("a", 1));
m.insert(pair<string, int>("b", 2));
m.insert(pair<string, int>("c", 3));


map<string, int>::iterator it = m.begin();
map<string, int>::iterator it2 = m.end();
map<string, int> m2;
m2.insert(it, it2);

for (map<string, int>::iterator it = m2.begin(); it != m2.end(); ++it)
std::cout << it->first << " => " << it->second << '\n';


return 0;
}
Binary file removed test
Binary file not shown.
Binary file removed tests/avlnode.o
Binary file not shown.
Binary file removed tests/benchmarking.o
Binary file not shown.
Binary file removed tests/enable_if.o
Binary file not shown.
Binary file removed tests/equal.o
Binary file not shown.
Binary file removed tests/is_integral.o
Binary file not shown.
Binary file removed tests/iterator_traits.o
Binary file not shown.
Binary file removed tests/lexico_compare.o
Binary file not shown.
3 changes: 1 addition & 2 deletions tests/main_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: ren-nasr <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/24 17:49:04 by ren-nasr #+# #+# */
/* Updated: 2022/10/26 10:31:07 by ren-nasr ### ########.fr */
/* Updated: 2022/10/26 11:24:14 by ren-nasr ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -112,7 +112,6 @@ int main_test(char* argv) {
}
std::cout << "should be constant with the same seed: " << sum << std::endl;

// std::cout << COUNT << std::endl;
{
ft::map<int, int> copy = map_int;
}
Expand Down
Binary file removed tests/main_test.o
Binary file not shown.
Binary file removed tests/map.o
Binary file not shown.
Binary file removed tests/pair.o
Binary file not shown.
Binary file removed tests/stack.o
Binary file not shown.
6 changes: 4 additions & 2 deletions tests/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: ren-nasr <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/09/30 11:49:06 by ren-nasr #+# #+# */
/* Updated: 2022/10/25 15:55:05 by ren-nasr ### ########.fr */
/* Updated: 2022/10/26 11:25:06 by ren-nasr ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -21,11 +21,13 @@ void ft_run_all_tests()
ft_enable_if_tests_suite();
ft_is_integral_tests_suite();
ft_equal_tests_suite();

ft_map_tests_suite();
ft_benchmark_vec_tests_suite();
ft_benchmark_map_tests_suite();
ft_benchmark_stack_tests_suite();
char* seed = new char[10];
snprintf(seed, 10, "%ld", (time(NULL) % 100));
main_test(seed);
}

int main(int argc, char** argv)
Expand Down
2 changes: 1 addition & 1 deletion tests/test.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
#include <typeinfo>

#define VEC_TEST_SIZE 1000000
#define MAP_TEST_SIZE 1000000
#define MAP_TEST_SIZE 10000
#define STACK_TEST_SIZE 1000000

#define INFO_ERR() std::cout << "\033[31m[ERROR]\033[0m " << __FILE__ << ":" << __LINE__ << " " << __FUNCTION__ << "(): " << std::endl;
Expand Down
Binary file removed tests/test.o
Binary file not shown.
Binary file removed tests/vector.o
Binary file not shown.

0 comments on commit cfcea99

Please sign in to comment.