-
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.
- Loading branch information
1 parent
e4cdb2d
commit c1b6a83
Showing
29 changed files
with
311 additions
and
91 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
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
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/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 */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
@@ -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 { | ||
|
||
|
@@ -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) { | ||
|
@@ -76,8 +80,10 @@ namespace ft { | |
x.swap(y); | ||
}; | ||
|
||
private: | ||
container_type _cntr; | ||
|
||
// member objects | ||
protected: | ||
container_type c; | ||
}; | ||
|
||
} // namespace ft | ||
|
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/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 */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
@@ -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; } | ||
|
@@ -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(); | ||
|
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/24 15:19:49 by ren-nasr ### ########.fr */ | ||
/* Updated: 2022/10/24 18:21:16 by ren-nasr ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
@@ -27,6 +27,9 @@ | |
#include <reverse_iterator.hpp> | ||
#include <avlnode.hpp> | ||
#include <make_pair.hpp> | ||
#include <lexico_compare.hpp> | ||
#include <equal.hpp> | ||
|
||
|
||
namespace ft | ||
{ | ||
|
@@ -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; | ||
|
@@ -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 |
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 |
---|---|---|
@@ -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 |
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 |
---|---|---|
@@ -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 |
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/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 */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
@@ -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> > | ||
|
File renamed without changes.
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 |
---|---|---|
@@ -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 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.
Oops, something went wrong.