Skip to content

Commit dbc59be

Browse files
committed
[vector] Add specialization of fill_n
Addresses #625
1 parent 71fc833 commit dbc59be

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

stl/inc/xutility

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4596,7 +4596,11 @@ _CONSTEXPR20 _OutIt fill_n(_OutIt _Dest, const _Diff _Count_raw, const _Ty& _Val
45964596
_Algorithm_int_t<_Diff> _Count = _Count_raw;
45974597
if (0 < _Count) {
45984598
auto _UDest = _Get_unwrapped_n(_Dest, _Count);
4599-
if constexpr (_Fill_memset_is_safe<decltype(_UDest), _Ty>) {
4599+
if constexpr (_Is_vb_iterator<_OutIt>) {
4600+
const auto _Last = _Dest + static_cast<typename _OutIt::difference_type>(_Count);
4601+
_Fill_vbool(_Dest, _Last, _Val);
4602+
return _Last;
4603+
} else if constexpr (_Fill_memset_is_safe<decltype(_UDest), _Ty>) {
46004604
#ifdef __cpp_lib_is_constant_evaluated
46014605
if (!_STD is_constant_evaluated())
46024606
#endif // __cpp_lib_is_constant_evaluated

tests/std/tests/GH_000625_vector_bool_optimization/test.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ void test_fill_helper(const size_t length) {
2323

2424
fill(dest.begin(), prev(dest.end(), 3), false);
2525
assert(dest == result_false);
26+
27+
const auto res_fill_n = fill_n(dest.begin(), length, true);
28+
assert(dest == result_true);
29+
assert(res_fill_n == prev(dest.end(), 3));
30+
31+
fill_n(dest.begin(), length, false);
32+
assert(dest == result_false);
2633
}
2734

2835
// With offset
@@ -38,6 +45,13 @@ void test_fill_helper(const size_t length) {
3845

3946
fill(next(dest.begin()), prev(dest.end(), 3), false);
4047
assert(dest == result_false);
48+
49+
const auto res_fill_n = fill_n(next(dest.begin()), length - 1, true);
50+
assert(dest == result_true);
51+
assert(res_fill_n == prev(dest.end(), 3));
52+
53+
fill_n(next(dest.begin()), length - 1, false);
54+
assert(dest == result_false);
4155
}
4256
}
4357

0 commit comments

Comments
 (0)