Skip to content

Commit

Permalink
Prefer fill_n over fill where applicable
Browse files Browse the repository at this point in the history
We can avoid doing the addition ourselves when we do this.
  • Loading branch information
AreaZR committed Mar 16, 2023
1 parent 8eb3a06 commit 558d068
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 6 deletions.
7 changes: 3 additions & 4 deletions stl/inc/deque
Original file line number Diff line number Diff line change
Expand Up @@ -1488,7 +1488,7 @@ private:
}

_Mid = begin() + static_cast<difference_type>(_Count);
_STD fill(_Mid, _Mid + static_cast<difference_type>(_Off), _Val); // fill in rest of values
_STD fill(_Mid, _Off, _Val); // fill in rest of values
} else { // insert not longer than prefix
for (_Num = _Count; _Num > 0; --_Num) {
push_front(begin()[static_cast<difference_type>(_Count - 1)]); // push part of prefix
Expand All @@ -1514,7 +1514,7 @@ private:
}

_Mid = begin() + static_cast<difference_type>(_Off);
_STD fill(_Mid, _Mid + static_cast<difference_type>(_Rem), _Val); // fill in rest of values
_STD fill_n(_Mid, _Rem, _Val); // fill in rest of values
} else { // insert not longer than prefix
for (_Num = 0; _Num < _Count; ++_Num) {
_Emplace_back_internal(
Expand All @@ -1525,8 +1525,7 @@ private:
_Alloc_temporary2<_Alty> _Tmp(_Getal(), _Val); // in case _Val is in sequence
_STD move_backward(_Mid, _Mid + static_cast<difference_type>(_Rem - _Count),
_Mid + static_cast<difference_type>(_Rem)); // copy rest of prefix
_STD fill(_Mid, _Mid + static_cast<difference_type>(_Count),
_Tmp._Get_value()); // fill in values
_STD fill_n(_Mid, _Count, _Tmp._Get_value()); // fill in values
}
_Guard._Container = nullptr;
}
Expand Down
4 changes: 2 additions & 2 deletions stl/inc/vector
Original file line number Diff line number Diff line change
Expand Up @@ -1076,7 +1076,7 @@ public:
} else { // new stuff can all be assigned
_Mylast = _Uninitialized_move(_Oldlast - _Count, _Oldlast, _Oldlast, _Al);
_Move_backward_unchecked(_Whereptr, _Oldlast - _Count, _Oldlast);
_STD fill(_Whereptr, _Whereptr + _Count, _Tmp);
_STD fill_n(_Whereptr, _Count, _Tmp);
}
_ASAN_VECTOR_RELEASE_GUARD;
}
Expand Down Expand Up @@ -3458,7 +3458,7 @@ public:
_CONSTEXPR20 iterator _Insert_n(const_iterator _Where, size_type _Count, const bool& _Val) {
size_type _Off = _Insert_x(_Where, _Count);
const auto _Result = begin() + static_cast<difference_type>(_Off);
_STD fill(_Result, _Result + static_cast<difference_type>(_Count), _Val);
_STD fill_n(_Result, _Count, _Val);
return _Result;
}

Expand Down

0 comments on commit 558d068

Please sign in to comment.