Skip to content

Commit bf976e0

Browse files
committed
change several methods in container::vector
1 parent ec29628 commit bf976e0

File tree

2 files changed

+32
-26
lines changed

2 files changed

+32
-26
lines changed

include/internal/vector.hpp

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,7 @@ namespace wigcpp::internal::container{
143143

144144
template <typename ...Args>
145145
vector(size_type size, const Args &... args) noexcept {
146-
static_assert(sizeof...(Args) == 0 ?
147-
std::is_nothrow_default_constructible_v<value_type>:
148-
std::is_nothrow_constructible_v<value_type, Args...>, "value_type T must have nothrow constructor under given parameters");
146+
static_assert(std::is_nothrow_constructible_v<value_type, Args...>, "value_type T must have nothrow constructor under given parameters");
149147

150148
value_type *new_data = alloc(size);
151149

@@ -158,18 +156,26 @@ namespace wigcpp::internal::container{
158156
construct_at(it, args...);
159157
}
160158
}else{
161-
if constexpr(sizeof...(Args) == 0){
162-
std::uninitialized_value_construct_n(data, size);
163-
}else if constexpr(sizeof...(Args) == 1){
159+
if constexpr(sizeof...(Args) == 1){
164160
const value_type &val = templates::first_value(args...);
165161
for(value_type *it = data; it != first_free; ++it){
166162
*it = val;
167163
}
168164
}else{
169-
static_assert(sizeof...(Args) <= 1, "constructor for trival types must have less than 2 parameters");
165+
static_assert(sizeof...(Args) == 1, "constructor for trival types must have less than 2 parameters");
170166
}
171167
}
172168
}
169+
170+
vector(size_type size) noexcept {
171+
value_type *new_data = alloc(size);
172+
173+
data = new_data;
174+
first_free = new_data + size;
175+
cap = new_data + size;
176+
177+
std::uninitialized_value_construct_n(data, size);
178+
}
173179

174180
~vector()noexcept{
175181
free();
@@ -276,22 +282,17 @@ namespace wigcpp::internal::container{
276282
if(size > capof){
277283
reserve(size);
278284
}
279-
if constexpr(!std::is_trivially_default_constructible_v<value_type>){
280-
if(size > sz){
281-
for(value_type *it = first_free; it != data + size; ++it){
282-
construct_at(it, value_type{});
283-
}
284-
}else{
285-
for(value_type *it = first_free; it != data + size; ){
285+
if(size > sz){
286+
std::uninitialized_value_construct_n(first_free, size - sz);
287+
}else{
288+
if constexpr(!std::is_trivially_destructible_v<value_type>){
289+
for(value_type *it = first_free; it != data + size;){
286290
--it;
287291
destroy_at(it);
288292
}
289293
}
290-
}else{
291-
if(size > sz){
292-
std::uninitialized_value_construct_n(first_free, size - sz);
293-
}
294294
}
295+
295296
first_free = data + size;
296297
}
297298

@@ -309,21 +310,21 @@ namespace wigcpp::internal::container{
309310
if(size > capof){
310311
reserve(size);
311312
}
312-
if constexpr(!std::is_trivially_copy_constructible_v<value_type>){
313-
if(size > sz){
313+
if(size > sz){
314+
if constexpr(std::is_trivially_copy_constructible_v<value_type>){
315+
std::uninitialized_fill_n(first_free, size - sz, src);
316+
}else{
314317
for(value_type *it = first_free; it != data + size; ++it){
315-
construct_at(it,src);
318+
construct_at(it, src);
316319
}
317-
}else{
320+
}
321+
}else{
322+
if constexpr(!std::is_trivially_destructible_v<value_type>){
318323
for(value_type *it = first_free; it != data + size; ){
319324
--it;
320325
destroy_at(it);
321326
}
322327
}
323-
}else{
324-
if(size > sz){
325-
std::uninitialized_fill_n(first_free, size - sz, src);
326-
}
327328
}
328329
first_free = data + size;
329330
}

test/test_vector.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,11 @@ TEST(test_vector, test_pod){
3939
for(std::size_t i = 0; i < f.size(); ++i){
4040
EXPECT_EQ(f[i], 0.0);
4141
}
42+
vector<int> g(8);
43+
EXPECT_EQ(g.size(), 8);
44+
for(int i = 0; i < g.size(); ++i){
45+
EXPECT_EQ(g[i], 0);
46+
}
4247
}
4348

4449
TEST(test_vector, test_non_trivial){

0 commit comments

Comments
 (0)