Skip to content

Commit 80ae86b

Browse files
committed
Merge branch 'deque'
2 parents 7b591b8 + d625a9d commit 80ae86b

19 files changed

Lines changed: 882 additions & 330 deletions

Makefile

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ OBJ_BAD = $(patsubst %.cpp,$(BUILD_DIR)/%.bad.o,$(SRC_BAD))
2020
SRC = $(TESTER_DIR)/main.cpp \
2121
$(TESTER_DIR)/vector/constructor.cpp \
2222
$(TESTER_DIR)/vector/cplusplus_examples.cpp \
23-
$(TESTER_DIR)/deque/cplusplus_examples.cpp
23+
$(TESTER_DIR)/deque/cplusplus_examples.cpp \
24+
$(TESTER_DIR)/deque/main.cpp
2425
SRC_BAD = $(TESTER_DIR)/vector/constructor.cpp
2526
GCDA_FILES = $(patsubst %.cpp,$(BUILD_DIR)/%.gcda,$(SRC))
2627
GCNO_FILES = $(patsubst %.cpp,$(BUILD_DIR)/%.gcno,$(SRC))
@@ -50,7 +51,9 @@ PHC_GCH = $(BUILD_DIR)/$(INCLUDE_DIR)/_phc.hpp.gch
5051
fclean \
5152
re
5253

53-
all: $(NAME)
54+
# all: test
55+
all: build
56+
build: $(NAME)
5457

5558
$(NAME): $(OBJ) | $(BUILD_DIR)
5659
@echo "\033[1;34m[Linking] $@\033[0m"
@@ -76,18 +79,18 @@ $(BUILD_DIR)/%.bad.o: %.cpp | $(BUILD_DIR)
7679
exit 1; \
7780
fi
7881

79-
test: test-bad all std-test
82+
test: test-bad build std-test
8083
@$(NAME) > $(TEST_LOG_FT)
8184
@echo "\033[1;32m[Testing] Test logs can be found $(TEST_LOG_FT)\033[0m"
8285
@diff -c $(TEST_LOG_FT) $(TEST_LOG_STD)
8386
@echo "\033[1;32m[Testing] Passed All Tests.\033[0m"
8487

8588
test-leaks: CFLAGS+=-g
86-
test-leaks: all
89+
test-leaks: build
8790
valgrind $(VALGRIND_FLAGS) $(NAME)
8891

8992
cov: CFLAGS+=$(COVERAGE_FLAGS)
90-
cov: all
93+
cov: build
9194
@echo "\033[1;34m[Coverage] Generating coverage report...\033[0m"
9295
@$(NAME)
9396
@echo "\033[1;32m[Coverage] Coverage report generated.\033[0m"
@@ -157,6 +160,6 @@ fclean: clean
157160
@rm -f $(NAME) $(NAME_STD)
158161
@rm -rf $(BUILD_DIR)
159162

160-
re: fclean all
163+
re: fclean build
161164

162165
-include $(DEP)

include/deque.hpp

Lines changed: 72 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,35 @@
11
#ifndef DEQUE_HPP
22
#define DEQUE_HPP
33

4-
#include "deque/__impl_data_.hpp"
4+
#include "__types__.hpp"
55
#include "iterator.hpp"
66
#include "type_traits.hpp"
77

8+
#define CHUNK_SIZE 8
9+
810
namespace ft
911
{
1012

11-
template <class T> struct deque_iterator;
12-
13-
template <class T, class Alloc = std::allocator<T> >
14-
class deque : protected _deque_impl_data<Alloc>
13+
template <class T> class deque_iterator;
14+
template <class T, class Alloc = std::allocator<T> > class deque
1515
{
16+
typedef T* chunk_t;
17+
typedef chunk_t* map_t;
18+
19+
typedef typename Alloc::template rebind<chunk_t>::other amap_t;
20+
21+
map_t _map_ptr;
22+
size_t _begin_offset;
23+
size_t _past_end_offset;
24+
size_t _chunks_sz;
25+
Alloc _a;
26+
amap_t _a_map;
27+
28+
friend class deque_iterator<T>;
29+
friend class deque_iterator<const T>;
30+
friend class deque_iterator<volatile T>;
31+
friend class deque_iterator<const volatile T>;
32+
1633
public:
1734
typedef Alloc allocator_type;
1835
typedef ptrdiff_t difference_type;
@@ -60,7 +77,7 @@ class deque : protected _deque_impl_data<Alloc>
6077
bool empty() const;
6178
size_type max_size() const;
6279
size_type size() const;
63-
void resize(size_type size, value_type val = value_type());
80+
void resize(size_type sz, value_type val = value_type());
6481

6582
const_reference at(size_type pos) const;
6683
const_reference back() const;
@@ -95,6 +112,15 @@ class deque : protected _deque_impl_data<Alloc>
95112
void swap(deque& other);
96113
};
97114

115+
/**
116+
* forbidden class specialization - begin
117+
* missing definition prevents the user from instantiation
118+
*/
119+
template <class T> class deque<const T>;
120+
template <class T> class deque<volatile T>;
121+
template <class T> class deque<const volatile T>;
122+
/** forbidden class specialization - end */
123+
98124
template <class T, class Alloc>
99125
bool operator==(const deque<T, Alloc>& lhs, const deque<T, Alloc>& rhs);
100126
template <class T, class Alloc>
@@ -112,54 +138,59 @@ template <class T, class Alloc>
112138
void swap(deque<T, Alloc>& first, deque<T, Alloc>& second);
113139

114140
template <class T>
115-
struct deque_iterator : public ft::iterator<random_access_iterator_tag, T>
141+
class deque_iterator : public ft::iterator<random_access_iterator_tag, T>
116142
{
143+
typedef deque<typename remove_cv<T>::type> data_t;
144+
typedef typename conditional<is_const<T>::value, const data_t, data_t>::type deque_type;
145+
146+
deque_type* _deq;
147+
size_t _cur;
148+
149+
explicit deque_iterator(deque_type* _deq, size_t _cur = 0);
150+
151+
public:
152+
template <class U> friend class deque_iterator;
153+
friend class deque<typename remove_cv<T>::type>;
154+
117155
typedef ft::iterator<random_access_iterator_tag, T> _iterator;
118156

119157
using typename _iterator::difference_type;
120158
using typename _iterator::iterator_category;
121159
using typename _iterator::pointer;
122160
using typename _iterator::reference;
123161
using typename _iterator::value_type;
124-
125-
typedef chunk<value_type>** data_t;
126-
127162
deque_iterator();
128163
deque_iterator(const deque_iterator& cpy);
129164
deque_iterator& operator=(const deque_iterator& cpy);
130165
template <class U> explicit deque_iterator(const deque_iterator<U>& cpy);
131166
template <class U> deque_iterator& operator=(const deque_iterator<U>& cpy);
132167
~deque_iterator();
133168

134-
template <class U> operator deque_iterator<U>(); // NOLINT
135-
136-
explicit deque_iterator(data_t _data, size_t _current = 0);
137-
138-
bool operator!=(const deque_iterator& rhs);
139-
bool operator<(const deque_iterator& rhs);
140-
bool operator<=(const deque_iterator& rhs);
141-
bool operator==(const deque_iterator& rhs);
142-
bool operator>(const deque_iterator& rhs);
143-
bool operator>=(const deque_iterator& rhs);
144-
const pointer operator->() const;
145-
const reference operator[](int post) const;
146-
const reference operator*() const;
147-
pointer base() const;
148-
pointer operator->();
149-
reference operator[](int pos);
150-
reference operator*();
151-
template <class U> difference_type operator-(const deque_iterator<U>& rhs);
152-
deque_iterator operator--(int); // NOLINT
153-
deque_iterator operator-(const difference_type n);
154-
deque_iterator operator+(const difference_type n);
155-
deque_iterator operator++(int); // NOLINT
156-
deque_iterator& operator--();
157-
deque_iterator& operator-=(const difference_type n);
158-
deque_iterator& operator++();
159-
deque_iterator& operator+=(const difference_type n);
160-
161-
size_t current;
162-
data_t data;
169+
template <class U> operator deque_iterator<U>() const;
170+
171+
bool operator!=(const deque_iterator& rhs) const;
172+
bool operator<(const deque_iterator& rhs) const;
173+
bool operator<=(const deque_iterator& rhs) const;
174+
bool operator==(const deque_iterator& rhs) const;
175+
bool operator>(const deque_iterator& rhs) const;
176+
bool operator>=(const deque_iterator& rhs) const;
177+
const pointer operator->() const;
178+
const reference operator[](int post) const;
179+
const reference operator*() const;
180+
pointer base() const;
181+
pointer operator->();
182+
reference operator[](int pos);
183+
reference operator*();
184+
template <class U>
185+
difference_type operator-(const deque_iterator<U>& rhs) const;
186+
deque_iterator operator--(int);
187+
deque_iterator operator-(const difference_type n) const;
188+
deque_iterator operator+(const difference_type n) const;
189+
deque_iterator operator++(int);
190+
deque_iterator& operator--();
191+
deque_iterator& operator-=(const difference_type n);
192+
deque_iterator& operator++();
193+
deque_iterator& operator+=(const difference_type n);
163194
};
164195

165196
template <class T>
@@ -171,7 +202,7 @@ deque_iterator<T>& operator+(const typename deque_iterator<T>::difference_type l
171202
#include "deque/__deque__.hpp" // IWYU pragma: export
172203

173204
#ifdef TEST
174-
// template class ft::deque< int >;
205+
// template class ft::deque<int>;
175206
#endif
176207

177-
#endif
208+
#endif

include/deque/__constructor__.tpp

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#ifndef __CONSTRUCTOR__DEQUE_TPP
22
#define __CONSTRUCTOR__DEQUE_TPP
3-
// IWYU pragma: private, include "vector.hpp"
43

54
#pragma once
65
#include "deque.hpp"
@@ -9,22 +8,28 @@ namespace ft
98
{
109

1110
template <class T, class Alloc>
12-
deque<T, Alloc>::deque(const allocator_type& alloc)
13-
: _deque_impl_data<Alloc>(alloc)
11+
deque<T, Alloc>::deque(const allocator_type& alloc) : _a(alloc)
1412
{
13+
const size_t size = 1;
14+
this->_map_ptr = this->_a_map.allocate(size);
15+
this->_chunks_sz = size;
16+
this->_map_ptr[0] = this->_a.allocate(CHUNK_SIZE);
17+
this->_begin_offset = CHUNK_SIZE / 2;
18+
this->_past_end_offset = this->_begin_offset;
1519
}
1620

1721
template <class T, class Alloc>
1822
deque<T, Alloc>::deque(const deque& cpy)
19-
: _deque_impl_data<Alloc>(cpy.get_allocator())
23+
: _map_ptr(0), _begin_offset(0), _past_end_offset(0), _chunks_sz(0)
2024
{
2125
assign(cpy.begin(), cpy.end());
2226
}
2327

2428
template <class T, class Alloc>
2529
deque<T, Alloc>::deque(
2630
size_type n, const value_type& val, const allocator_type& alloc)
27-
: _deque_impl_data<Alloc>(alloc)
31+
: _map_ptr(0), _begin_offset(0), _past_end_offset(0), _chunks_sz(0),
32+
_a(alloc)
2833
{
2934
assign(n, val);
3035
}
@@ -35,7 +40,8 @@ deque<T, Alloc>::deque(InputIterator first,
3540
InputIterator last,
3641
const allocator_type& alloc,
3742
typename enable_if<!numeric_limits<InputIterator>::is_integer>::type* /*unused*/)
38-
: _deque_impl_data<Alloc>(alloc)
43+
: _map_ptr(0), _begin_offset(0), _past_end_offset(0), _chunks_sz(0),
44+
_a(alloc)
3945
{
4046
assign(first, last);
4147
}
@@ -56,4 +62,4 @@ deque<T, Alloc>& deque<T, Alloc>::operator=(const deque& cpy)
5662

5763
} // namespace ft
5864

59-
#endif
65+
#endif

include/deque/__deque__.hpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@
1313
#include "allocators.tpp"
1414
#include "capacity.tpp"
1515
#include "deletions.tpp"
16+
#include "element_access.tpp"
1617
#include "insertions.tpp"
1718
#include "iterators.tpp"
1819

1920
// IWYU pragma: end_exports
2021

21-
#endif
22+
#endif

include/deque/__destructor__.tpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@ namespace ft
1010
template <class T, class Alloc> deque<T, Alloc>::~deque<T, Alloc>()
1111
{
1212
clear();
13+
for (size_type i = 0; i < this->_chunks_sz; i++)
14+
{
15+
this->_a.deallocate(this->_map_ptr[i], CHUNK_SIZE);
16+
}
17+
if (this->_map_ptr != 0)
18+
{
19+
this->_a_map.deallocate(this->_map_ptr, this->_chunks_sz);
20+
}
1321
}
1422

1523
#ifdef TEST
@@ -18,4 +26,4 @@ template <class T, class Alloc> deque<T, Alloc>::~deque<T, Alloc>()
1826

1927
} // namespace ft
2028

21-
#endif
29+
#endif

0 commit comments

Comments
 (0)