-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
finally my very slow map container past the test happy slappy
- Loading branch information
1 parent
69bcc5b
commit cfcea99
Showing
20 changed files
with
109 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
@@ -29,6 +29,7 @@ | |
#include <make_pair.hpp> | ||
#include <lexico_compare.hpp> | ||
#include <equal.hpp> | ||
#include <stack.hpp> | ||
|
||
namespace ft | ||
{ | ||
|
@@ -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; | ||
}; | ||
|
||
|
@@ -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> | ||
|
@@ -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; | ||
|
@@ -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) | ||
{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
@@ -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; | ||
} | ||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
@@ -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) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.