Skip to content

Commit 1168c12

Browse files
author
Mortalite
committed
feat: map in progress 2, refactored max_size
1 parent dd90f77 commit 1168c12

8 files changed

+225
-33
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Project exclude paths
2+
/cmake-build-debug/

CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@ project(ft_containers)
33

44
set(CMAKE_CXX_STANDARD 98)
55

6-
add_executable(ft_containers main.cpp list.hpp iteratorList.hpp utils.hpp vector.hpp iteratorVector.hpp stack.hpp queue.hpp map.hpp)
6+
add_executable(ft_containers main.cpp list.hpp iteratorList.hpp utils.hpp vector.hpp iteratorVector.hpp stack.hpp queue.hpp map.hpp iteratorMap.hpp)

iteratorList.hpp

+6-4
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@ namespace ft {
1010
*/
1111
template<typename T>
1212
class DLLNode {
13-
public:
14-
T* _data;
15-
DLLNode<T>* _next;
16-
DLLNode<T>* _prev;
13+
14+
public:
15+
T* _data;
16+
DLLNode<T>* _next;
17+
DLLNode<T>* _prev;
18+
1719
};
1820

1921
template<typename T, typename Category = std::bidirectional_iterator_tag>

iteratorMap.hpp

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
#ifndef ITERATORMAP_HPP
2+
#define ITERATORMAP_HPP
3+
4+
namespace ft {
5+
6+
template<typename Key, typename T>
7+
class TreeNode {
8+
9+
public:
10+
std::pair<Key, T>* _data;
11+
TreeNode<Key,T>* _left;
12+
TreeNode<Key,T>* _right;
13+
TreeNode<Key,T>* _parent;
14+
15+
};
16+
17+
template<typename Key, typename T, typename Category = std::bidirectional_iterator_tag>
18+
class IteratorMap {
19+
20+
private:
21+
TreeNode<const Key,T>* _ptr;
22+
23+
public:
24+
25+
typedef std::ptrdiff_t difference_type;
26+
typedef T value_type;
27+
typedef T* pointer;
28+
typedef T& reference;
29+
typedef Category iterator_category;
30+
31+
IteratorMap() {}
32+
IteratorMap(const IteratorMap &other):_ptr(other._ptr) {}
33+
IteratorMap(TreeNode<const Key,T> *node):_ptr(node) {}
34+
~IteratorMap() {}
35+
36+
TreeNode<Key,T>* getPtr() { return (_ptr); }
37+
bool operator==(const IteratorMap &other) const { return (_ptr == other._ptr); }
38+
bool operator!=(const IteratorMap &other) const { return (_ptr != other._ptr); }
39+
reference operator*() { return (*_ptr->_data); }
40+
pointer operator->() const { return (_ptr->_data); }
41+
42+
IteratorMap &operator=(const IteratorMap &other) {
43+
_ptr = other._ptr;
44+
return (*this);
45+
}
46+
47+
IteratorMap &operator++() {
48+
if (_ptr && _ptr->_next)
49+
_ptr = _ptr->_next;
50+
return (*this);
51+
}
52+
53+
IteratorMap operator++(int) {
54+
IteratorList < T, Category > tmp(*this);
55+
operator++();
56+
return (tmp);
57+
}
58+
59+
IteratorMap &operator--() {
60+
if (_ptr && _ptr->_prev)
61+
_ptr = _ptr->_prev;
62+
return (*this);
63+
}
64+
65+
IteratorMap operator--(int) {
66+
IteratorList < T, Category > tmp(*this);
67+
operator--();
68+
return (tmp);
69+
}
70+
71+
};
72+
73+
}
74+
75+
#endif

list.hpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ namespace ft {
193193
}
194194

195195
size_type max_size() const {
196-
return (std::numeric_limits<size_type>::max());
196+
return (_alloc.max_size());
197197
}
198198

199199
/*
@@ -202,12 +202,15 @@ namespace ft {
202202
reference front() {
203203
return (*_first->_data);
204204
}
205+
205206
const_reference front() const {
206207
return (*_first->_data);
207208
}
209+
208210
reference back() {
209211
return (*_last->_data);
210212
}
213+
211214
const_reference back() const {
212215
return (*_last->_data);
213216
}

main.cpp

+8-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "list.hpp"
77
#include "vector.hpp"
88
#include "stack.hpp"
9+
#include "map.hpp"
910
#include "iteratorList.hpp"
1011

1112
#define RED "\033[31m"
@@ -537,10 +538,15 @@ int main() {
537538
testSwap<ft::vector<cType>, ft::vector<cType> >();
538539
testOperators<ft::vector<cType>, std::vector<cType> >();
539540

540-
ft::stack<cType, ft::list<cType> > stack;
541-
stack.push((cType)(10));
541+
/* ft::stack<cType, ft::list<cType> > stack;
542+
stack.push((cType)(10));*/
542543
// ft::stack<cType, ft::list<cType> > stack2;
543544
// stack2.push((cType)(10));
544545
// std::cout << BLUE << "==" << (stack == stack2) << RESET << std::endl;
546+
ft::map<int, int> map;
547+
map.insert(std::make_pair(1, 10));
548+
map.insert(std::make_pair(2, 25));
549+
map.insert(std::make_pair(3, 35));
550+
map.runPostOrderTreeWalk();
545551
return (0);
546552
}

map.hpp

+128-24
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,14 @@
11
#ifndef MAP_HPP
22
#define MAP_HPP
33

4-
namespace ft {
4+
#include "iteratorMap.hpp"
55

6-
template<typename K, typename T>
7-
class RedBlackTreeNode {
8-
K* _key;
9-
T* _data;
10-
TreeNode<T>* _left;
11-
TreeNode<T>* _right;
12-
TreeNode<T>* _parent;
13-
short _color;
14-
};
6+
namespace ft {
157

168
template < class Key, // map::key_type
179
class T, // map::mapped_type
18-
class Compare = less<Key>, // map::key_compare
19-
class Alloc = allocator<pair<const Key,T> > // map::allocator_type
10+
class Compare = std::less<Key>, // map::key_compare
11+
class Alloc = std::allocator<std::pair<const Key,T> > // map::allocator_type
2012
>
2113
class map {
2214

@@ -25,16 +17,15 @@ namespace ft {
2517
*/
2618
public:
2719
typedef Key key_type;
28-
typedef T mapped_value;
29-
typedef pair<const key_type,mapped_type> value_type;
20+
typedef T mapped_type;
21+
typedef std::pair<const key_type, mapped_type> value_type;
3022
typedef Compare key_compare;
31-
//typedef
3223
typedef Alloc allocator_type;
33-
typedef reference allocator_type::reference;
34-
typedef const_reference allocator_type::const_reference;
35-
typedef pointer allocator_type::pointer;
36-
typedef const_pointer allocator_type::const_pointer;
37-
// typedef ft::IteratorList<T> iterator;
24+
typedef typename allocator_type::reference reference;
25+
typedef typename allocator_type::const_reference const_reference;
26+
typedef typename allocator_type::pointer pointer;
27+
typedef typename allocator_type::const_pointer const_pointer;
28+
typedef ft::IteratorMap<Key,T> iterator;
3829
// typedef ft::ReverseIteratorList<T> reverse_iterator;
3930
// typedef ft::ConstIteratorList<T> const_iterator;
4031
// typedef ft::ConstReverseIteratorList<T> const_reverse_iterator;
@@ -45,12 +36,125 @@ namespace ft {
4536
** Class members
4637
*/
4738
private:
48-
RedBlackTreeNode<Key,T>* _root;
49-
key_compare comp;
50-
allocator_type _alloc;
51-
size_type _size;
39+
TreeNode<const Key,T>* _root;
40+
key_compare _comp;
41+
allocator_type _alloc;
42+
size_type _size;
43+
44+
TreeNode<Key,T>* treeSearch(TreeNode<Key,T> root, value_type& val) {
45+
while (root && val != root._data->first) {
46+
if (val < root._data->first)
47+
root = root._left;
48+
else
49+
root = root._right;
50+
}
51+
return (root);
52+
}
5253

5354
public:
55+
/*
56+
** Constructors
57+
*/
58+
explicit map (const key_compare& comp = key_compare(),
59+
const allocator_type& alloc = allocator_type()) {
60+
_root = NULL;
61+
_comp = comp;
62+
_alloc = alloc;
63+
_size = 0;
64+
}
65+
66+
/*
67+
** Iterators
68+
*/
69+
70+
/*
71+
** Capacity
72+
*/
73+
bool empty() const {
74+
return (_size == 0);
75+
}
76+
77+
size_type size() const {
78+
return (_size);
79+
}
80+
81+
size_type max_size() const {
82+
return (_alloc.max_size());
83+
}
84+
85+
/*
86+
** Element access
87+
*/
88+
89+
90+
/*
91+
** Modifiers
92+
*/
93+
94+
std::pair<iterator,bool> insert (const value_type& val) {
95+
/* TreeNode<Key,T> *y = NULL;
96+
TreeNode<Key,T> *x = _root;
97+
TreeNode<Key,T> *z = new TreeNode<Key,T>;
98+
z->_data = _alloc.allocate(1);
99+
_alloc.construct(z->_data, val);
100+
while (x) {
101+
y = x;
102+
if (z->_data->first < x->_data->first)
103+
x = x->_left;
104+
else
105+
x = x->_right;
106+
}
107+
z->_parent = y;
108+
if (!y)
109+
_root = z;
110+
else if (z->_data->first < y->_data->first)
111+
y->_left = z;
112+
else
113+
y->_right = z;*/
114+
TreeNode<const Key,T> *y = NULL;
115+
TreeNode<const Key,T> *x = _root;
116+
while (x) {
117+
y = x;
118+
if (val.first == x->_data->first)
119+
return (std::make_pair(iterator(x), false));
120+
else if (val.first < x->_data->first)
121+
x = x->_left;
122+
else
123+
x = x->_right;
124+
}
125+
TreeNode<const Key,T> *z = new TreeNode<const Key,T>;
126+
z->_data = _alloc.allocate(1);
127+
_alloc.construct(z->_data, val);
128+
z->_parent = y;
129+
if (!y)
130+
_root = z;
131+
else if (z->_data->first < y->_data->first)
132+
y->_left = z;
133+
else
134+
y->_right = z;
135+
return (std::make_pair(iterator(z), true));
136+
}
137+
138+
void runPostOrderTreeWalk() {
139+
postOrderTreeWalk(_root);
140+
}
141+
142+
void postOrderTreeWalk(TreeNode<const Key,T>* treeNode) {
143+
if (!treeNode)
144+
return ;
145+
postOrderTreeWalk(treeNode->_left);
146+
postOrderTreeWalk(treeNode->_right);
147+
std::cout << "first = " << treeNode->_data->first << ", second = " << treeNode->_data->second << std::endl;
148+
}
149+
150+
/*
151+
** Observers
152+
*/
153+
154+
/*
155+
** Operations
156+
*/
157+
54158

55159
};
56160

vector.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ namespace ft {
132132
}
133133

134134
size_type max_size() const {
135-
return (std::numeric_limits<size_type>::max());
135+
return (_alloc.max_size());
136136
}
137137

138138
void resize (size_type n, value_type val = value_type()) {

0 commit comments

Comments
 (0)