Skip to content

Commit

Permalink
adding and complying to main tests
Browse files Browse the repository at this point in the history
  • Loading branch information
RidaEn-nasry committed Oct 24, 2022
1 parent e4cdb2d commit c1b6a83
Show file tree
Hide file tree
Showing 29 changed files with 311 additions and 91 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"request": "launch",
"program": "/Users/wa5ina/42/Reimplementing-STL-Containers./test",
"args": [
"benchmark"
"main"
],
"cwd": "${cwd}",

Expand Down
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ TESTS=./tests/vector.cpp\
./tests/equal.cpp\
./tests/avlnode.cpp\
./tests/map.cpp\
./tests/benchmarking.cpp
./tests/benchmarking.cpp\
./tests/main_test.cpp

DEP=./others/choose_type.hpp\
./containers/vector.hpp\
Expand Down
32 changes: 19 additions & 13 deletions adapters/stack.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/02 12:47:18 by ren-nasr #+# #+# */
/* Updated: 2022/10/21 09:55:35 by ren-nasr ### ########.fr */
/* Updated: 2022/10/24 18:29:33 by ren-nasr ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -19,6 +19,10 @@

#ifndef STACK_HPP
#define STACK_HPP
#include <equal.hpp>
#include <lexico_compare.hpp>
#include <stack.hpp>
#include <vector.hpp>

namespace ft {

Expand All @@ -32,33 +36,33 @@ namespace ft {
typedef size_t size_type;

/************************** Member functions **************************/
explicit stack(const container_type& cntr = container_type()) : _cntr(cntr) {}
explicit stack(const container_type& cntr = container_type()) : c(cntr) {}

bool empty() const { return _cntr.empty(); }
size_type size() const { return _cntr.size(); }
bool empty() const { return c.empty(); }
size_type size() const { return c.size(); }

value_type& top() { return _cntr.back(); }
const value_type& top() const { return _cntr.back(); }
value_type& top() { return c.back(); }
const value_type& top() const { return c.back(); }

void push(const value_type& val) {
_cntr.push_back(val);
c.push_back(val);
}

void pop() { _cntr.pop_back(); }
void pop() { c.pop_back(); }

void swap(stack& x) { _cntr.swap(x._cntr); }
void swap(stack& x) { c.swap(x.c); }
/************************* non-member functions *************************/

friend bool operator== (const stack<T, Container>& lhs, const stack<T, Container>& rhs) {
return lhs._cntr == rhs._cntr;
return lhs.c == rhs.c;
}

friend bool operator!= (const stack<T, Container>& lhs, const stack<T, Container>& rhs) {
return !(lhs == rhs);
}

friend bool operator< (const stack<T, Container>& lhs, const stack<T, Container>& rhs) {
return lhs._cntr < rhs._cntr;
return lhs.c < rhs.c;
}

friend bool operator<= (const stack<T, Container>& lhs, const stack<T, Container>& rhs) {
Expand All @@ -76,8 +80,10 @@ namespace ft {
x.swap(y);
};

private:
container_type _cntr;

// member objects
protected:
container_type c;
};

} // namespace ft
Expand Down
20 changes: 2 additions & 18 deletions containers/map/avlnode.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/12 16:03:35 by ren-nasr #+# #+# */
/* Updated: 2022/10/23 20:56:48 by ren-nasr ### ########.fr */
/* Updated: 2022/10/24 17:45:37 by ren-nasr ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -36,25 +36,9 @@ namespace ft
AVLNode(key_compare& comp) : _comp(comp), _right(NULL), _left(NULL), _parent(NULL), _data(value_type()) {}


// (2) constructor with comparison function
// AVLNode(key_compare &comp) : _comp(comp), _right(NULL), _left(NULL), _parent(NULL), _data(value_type()) {}
// {
// _right = NULL;
// _left = NULL;
// _parent = NULL;
// _data = value_type();
// _comp = comp;
// };

// (3) constructor with data and compare and optional parent
AVLNode(value_type data, key_compare& comp, self* parent = NULL) : _data(data), _right(NULL), _left(NULL), _parent(parent), _comp(comp) {}
// {
// _right = NULL;
// _left = NULL;
// _parent = parent;
// _data = data;
// _comp = key_compare();
// };

// getters
inline reference data() { return _data; }
Expand Down Expand Up @@ -95,7 +79,7 @@ namespace ft
// calculate the balance of the node
int balance() const
{

// the balance is the height of the left subtree - the height of the right subtree
// a tree is unbalanced if the balance is greater tha
int left_height = _left == NULL ? 0 : _left->height();
Expand Down
90 changes: 45 additions & 45 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 15:19:49 by ren-nasr ### ########.fr */
/* Updated: 2022/10/24 18:21:16 by ren-nasr ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -27,6 +27,9 @@
#include <reverse_iterator.hpp>
#include <avlnode.hpp>
#include <make_pair.hpp>
#include <lexico_compare.hpp>
#include <equal.hpp>


namespace ft
{
Expand Down Expand Up @@ -603,6 +606,47 @@ namespace ft
return _comp;
};


/************** non-member functions **************/

// relational operators
friend bool operator==(const map& lhs, const map& rhs)
{
return lhs.size() == rhs.size() && equal(lhs.begin(), lhs.end(), rhs.begin());
};

friend bool operator!=(const map& lhs, const map& rhs)
{
return !(lhs == rhs);
};

friend bool operator<(const map& lhs, const map& rhs)
{
return lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
};

friend bool operator<=(const map& lhs, const map& rhs)
{
return !(rhs < lhs);
};

friend bool operator>(const map& lhs, const map& rhs)
{
return rhs < lhs;
};

friend bool operator>=(const map& lhs, const map& rhs)
{
return !(lhs < rhs);
};

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


private:
allocator_type _alloc;
value_compare _comp;
Expand Down Expand Up @@ -882,50 +926,6 @@ namespace ft

}; // map class

// non-member function overloads
template <class Key, class T, class Compare, class Alloc>
bool operator==(const map<Key, T, Compare, Alloc>& lhs, const map<Key, T, Compare, Alloc>& rhs)
{
return lhs.size() == rhs.size() && ft::equal(lhs.begin(), lhs.end(), rhs.begin());
}

template <class Key, class T, class Compare, class Alloc>
bool operator!=(const map<Key, T, Compare, Alloc>& lhs, const map<Key, T, Compare, Alloc>& rhs)
{
return !(lhs == rhs);
}

template <class Key, class T, class Compare, class Alloc>
bool operator<(const map<Key, T, Compare, Alloc>& lhs, const map<Key, T, Compare, Alloc>& rhs)
{
return ft::lexicographical_compare(lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
}

template <class Key, class T, class Compare, class Alloc>
bool operator<=(const map<Key, T, Compare, Alloc>& lhs, const map<Key, T, Compare, Alloc>& rhs)
{
return !(rhs < lhs);
}

template <class Key, class T, class Compare, class Alloc>
bool operator>(const map<Key, T, Compare, Alloc>& lhs, const map<Key, T, Compare, Alloc>& rhs)
{
return rhs < lhs;
}

template <class Key, class T, class Compare, class Alloc>
bool operator>=(const map<Key, T, Compare, Alloc>& lhs, const map<Key, T, Compare, Alloc>& rhs)
{
return !(lhs < rhs);
}

template <class Key, class T, class Compare, class Alloc>
void swap(map<Key, T, Compare, Alloc>& x, map<Key, T, Compare, Alloc>& y)
{
x.swap(y);
}


}; // ft namespace

#endif
61 changes: 61 additions & 0 deletions containers/set/rbtnode.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* rbtnode.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ren-nasr <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/24 17:19:57 by ren-nasr #+# #+# */
/* Updated: 2022/10/24 17:22:42 by ren-nasr ### ########.fr */
/* */
/* ************************************************************************** */

#ifndef RBTNODE_HPP
#define RBTNODE_HPP


// red-black tree node

template <class T, class Compare>
class RBTNode
{
public:
typedef RBTNode<T, Compare> self;
typedef T value_type;
typedef value_type& reference;
typedef const value_type& const_reference;
typedef value_type* pointer;
typedef const value_type* const_pointer;
typedef Compare key_compare;


// (1) default constructor
RBTNode(key_compare& comp) : _comp(comp), _right(NULL), _left(NULL), _parent(NULL), _data(value_type()) {}


// (3) constructor with data and compare and optional parent
RBTNode(value_type data, key_compare& comp, self* parent = NULL) : _data(data), _right(NULL), _left(NULL), _parent(parent), _comp(comp) {}

// getters
inline reference data() { return _data; }

inline pointer data_ptr() { return &_data; }

inline self* right() const { return _right; }

inline self* left() { return _left; }

inline self* parent() { return _parent; }



private:
value_type _data;
self* _right;
self* _left;
self* _parent;
bool _color;
key_compare _comp;
};

#endif
29 changes: 29 additions & 0 deletions containers/set/set.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* set.hpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ren-nasr <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/10/24 16:07:39 by ren-nasr #+# #+# */
/* Updated: 2022/10/24 16:52:45 by ren-nasr ### ########.fr */
/* */
/* ************************************************************************** */


#ifndef SET_HPP
#define SET_HPP


#include <memory>
#include <less.hpp>


namespace ft {
template <class key, class Compare = ft::less<key>, class Alloc = std::allocator<key> >
class set {

};
}

#endif
5 changes: 3 additions & 2 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/23 22:06:27 by ren-nasr ### ########.fr */
/* Updated: 2022/10/24 18:15:40 by ren-nasr ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -25,7 +25,8 @@
#include <memory>
#include <reverse_iterator.hpp>
#include <choose_type.hpp>

#include <equal.hpp>
#include <lexico_compare.hpp>
namespace ft
{
template <typename T, typename Alloc = std::allocator<T> >
Expand Down
File renamed without changes.
Binary file modified lab/a.out
Binary file not shown.
14 changes: 8 additions & 6 deletions lab/lab.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
#include <iostream>
#include <map>
// #include <bits/stdc++.h>
#include <set>

using namespace std;

int main()
{
map<int, char> m;
for (int i = 0; i < 10; i++)
m[i] = 'a' + i;

std::cout << m[1000] << std::endl;
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;
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.
Loading

0 comments on commit c1b6a83

Please sign in to comment.