Skip to content

Commit 9b66b93

Browse files
author
Mortalite
committed
feat: add corrections
1 parent ff6371f commit 9b66b93

File tree

5 files changed

+91
-28
lines changed

5 files changed

+91
-28
lines changed

includes/containers/list.hpp

+42-10
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,42 @@ namespace ft {
9393
push_back(*first++);
9494
}
9595

96+
void fill_assign(size_type n, const value_type& val) {
97+
clear();
98+
while (n--)
99+
push_back(val);
100+
}
101+
102+
template<typename Integer>
103+
void assign_dispatch(Integer n, Integer val, true_type) {
104+
fill_assign(static_cast<size_type>(n), val);
105+
}
106+
107+
template<typename InputIterator>
108+
void assign_dispatch(InputIterator first, InputIterator last, false_type) {
109+
clear();
110+
while (first != last)
111+
push_back(*first++);
112+
}
113+
114+
void fill_insert(iterator position, size_type n, const value_type& val) {
115+
while (n--)
116+
insert(position, val);
117+
}
118+
119+
template<typename Integer>
120+
void insert_dispatch(iterator position, Integer n, Integer val, true_type) {
121+
fill_insert(position, static_cast<size_type>(n), val);
122+
}
123+
124+
template<typename InputIterator>
125+
void insert_dispatch(iterator position, InputIterator first, InputIterator last, false_type) {
126+
while (first != last) {
127+
position = insert(position, *first++);
128+
position++;
129+
}
130+
}
131+
96132
public:
97133

98134
/*
@@ -232,15 +268,12 @@ namespace ft {
232268
*/
233269
template <class InputIterator>
234270
void assign(InputIterator first, InputIterator last) {
235-
clear();
236-
while (first != last)
237-
push_back(*first++);
271+
typedef typename ft::is_integer<InputIterator>::type Integral;
272+
assign_dispatch(first, last, Integral());
238273
}
239274

240275
void assign(size_type n, const value_type& val) {
241-
clear();
242-
while (n--)
243-
push_back(val);
276+
fill_assign(n, val);
244277
}
245278

246279
void push_front(const value_type& val) {
@@ -311,14 +344,13 @@ namespace ft {
311344
}
312345

313346
void insert(iterator position, size_type n, const value_type& val) {
314-
while (n--)
315-
insert(position, val);
347+
fill_insert(position, n, val);
316348
}
317349

318350
template<class InputIterator>
319351
void insert(iterator position, InputIterator first, InputIterator last) {
320-
while (first != last)
321-
insert(position, *first++);
352+
typedef typename ft::is_integer<InputIterator>::type Integral;
353+
insert_dispatch(position, first, last, Integral());
322354
}
323355

324356
iterator erase(iterator position) {

includes/containers/vector.hpp

+43-12
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,43 @@ namespace ft {
6363
_array = NULL;
6464
}
6565

66+
67+
void fill_assign(size_type n, const value_type& val) {
68+
clear();
69+
while (n--)
70+
push_back(val);
71+
}
72+
73+
template<typename Integer>
74+
void assign_dispatch(Integer n, Integer val, true_type) {
75+
fill_assign(static_cast<size_type>(n), val);
76+
}
77+
78+
template<typename InputIterator>
79+
void assign_dispatch(InputIterator first, InputIterator last, false_type) {
80+
clear();
81+
while (first != last)
82+
push_back(*first++);
83+
}
84+
85+
void fill_insert(iterator position, size_type n, const value_type& val) {
86+
while (n--)
87+
insert(position, val);
88+
}
89+
90+
template<typename Integer>
91+
void insert_dispatch(iterator position, Integer n, Integer val, true_type) {
92+
fill_insert(position, static_cast<size_type>(n), val);
93+
}
94+
95+
template<typename InputIterator>
96+
void insert_dispatch(iterator position, InputIterator first, InputIterator last, false_type) {
97+
while (first != last) {
98+
position = insert(position, *first++);
99+
position++;
100+
}
101+
}
102+
66103
public:
67104

68105
/*
@@ -242,15 +279,12 @@ namespace ft {
242279

243280
template <class InputIterator>
244281
void assign(InputIterator first, InputIterator last) {
245-
clear();
246-
while (first != last)
247-
push_back(*first++);
282+
typedef typename ft::is_integer<InputIterator>::type Integral;
283+
assign_dispatch(first, last, Integral());
248284
}
249285

250286
void assign(size_type n, const value_type& val) {
251-
clear();
252-
while (n--)
253-
push_back(val);
287+
fill_assign(n, val);
254288
}
255289

256290
void push_back(const value_type& val) {
@@ -278,16 +312,13 @@ namespace ft {
278312
}
279313

280314
void insert(iterator position, size_type n, const value_type& val) {
281-
while (n--)
282-
position = insert(position, val);
315+
fill_insert(position, n, val);
283316
}
284317

285318
template <class InputIterator>
286319
void insert(iterator position, InputIterator first, InputIterator last) {
287-
while (first != last) {
288-
position = insert(position, *first++);
289-
position++;
290-
}
320+
typedef typename ft::is_integer<InputIterator>::type Integral;
321+
insert_dispatch(position, first, last, Integral());
291322
}
292323

293324
iterator erase(iterator position) {

includes/defines.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ extern size_t g_mapMarginCoeff;
1515
extern int g_intMin, g_intMax;
1616
extern float g_floatMin, g_floatMax;
1717

18-
typedef float cType;
18+
typedef int cType;
1919

2020
#endif

includes/mainTest.hpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ namespace ft {
106106
T mainContainer2;
107107
C alterContainer2;
108108

109-
mainContainer2.assign(11, (typename T::size_type)10);
110-
alterContainer2.assign(11,(typename C::size_type)10);
109+
mainContainer2.assign(11, 10);
110+
alterContainer2.assign(11, 10);
111111
printCmpIterator(mainContainer2, alterContainer2);
112112
}
113113

@@ -158,7 +158,7 @@ namespace ft {
158158
typename C::iterator alterIt;
159159

160160
typename T::value_type randomValue = getRandomValue<T>();
161-
typename T::size_type randomTimes = getRandomValueByType<int>()%4 + 1;
161+
typename T::size_type randomTimes = getRandomValueByType<typename T::size_type>()%4 + 1;
162162
std::cout << BLUE << "Insert " << randomValue << " in begin()" << RESET << std::endl;
163163
mainIt = mainContainer.insert(mainContainer.begin(), randomValue);
164164
alterIt = alterContainer.insert(alterContainer.begin(), randomValue);

srcs/main.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,8 @@ int main() {
143143
std::cout.setf(std::ios::left);
144144

145145
std::cout << RED << "Mandatory part" << RESET << std::endl;
146-
// testList();
147-
testVector();
146+
testList();
147+
// testVector();
148148
// testMap();
149149
// testStack();
150150
// testQueue();

0 commit comments

Comments
 (0)