fixed all insert() variants
This commit is contained in:
parent
a5f5e45fbd
commit
518fce5ef5
|
|
@ -396,41 +396,49 @@ namespace cc {
|
|||
template<typename T, std::size_t N>
|
||||
constexpr const_vector<T, N>::iterator const_vector<T, N>::insert(const_vector::const_iterator pos, const T &value)
|
||||
{
|
||||
if (_size == N) throw std::exception();
|
||||
if (_size == N) throw std::length_error("No space left in vector");
|
||||
|
||||
ptrdiff_t i = pos - _arr;
|
||||
std::move(_arr + i, _arr + _size, _arr + i + 1);
|
||||
_arr[i] = value;
|
||||
auto it = const_cast<iterator>(pos);
|
||||
|
||||
if (pos != end()) {
|
||||
std::move_backward(it, end(), end() + 1);
|
||||
}
|
||||
*it = value;
|
||||
++_size;
|
||||
return pos;
|
||||
|
||||
return it;
|
||||
}
|
||||
|
||||
template<typename T, std::size_t N>
|
||||
constexpr const_vector<T, N>::iterator const_vector<T, N>::insert(const_vector::const_iterator pos, T &&value)
|
||||
{
|
||||
if (_size == N) throw std::exception();
|
||||
if (_size == N) throw std::length_error("No space left in vector");
|
||||
|
||||
ptrdiff_t i = pos - _arr;
|
||||
std::move(_arr + i, _arr + _size, _arr + i + 1);
|
||||
_arr[i] = std::move(value);
|
||||
auto it = const_cast<iterator>(pos);
|
||||
|
||||
if (pos != end()) {
|
||||
std::move_backward(it, end(), end() + 1);
|
||||
}
|
||||
*it = std::forward<value_type>(value);
|
||||
++_size;
|
||||
return pos;
|
||||
|
||||
return it;
|
||||
}
|
||||
|
||||
template<typename T, std::size_t N>
|
||||
constexpr const_vector<T, N>::iterator
|
||||
const_vector<T, N>::insert(const_vector::const_iterator pos, const_vector::size_type count, const T &value)
|
||||
{
|
||||
if (count == 0) return pos;
|
||||
if (_size + count >= N) throw std::exception();
|
||||
auto it = const_cast<iterator>(pos);
|
||||
|
||||
if (count == 0) return it;
|
||||
if (_size + count > N) throw std::length_error("Not enough space left in vector for " + std::to_string(count) + " elements");
|
||||
|
||||
std::move(pos, _arr + _size, pos + count);
|
||||
std::fill(pos, pos + count, value);
|
||||
std::move_backward(it, end(), end() + count);
|
||||
std::fill(it, it + count, value);
|
||||
|
||||
_size += count;
|
||||
return pos;
|
||||
return it;
|
||||
}
|
||||
|
||||
template<typename T, std::size_t N>
|
||||
|
|
@ -439,16 +447,16 @@ namespace cc {
|
|||
const_vector<T, N>::insert(const_vector::const_iterator pos, InputIt first, InputIt last)
|
||||
{
|
||||
auto count = std::distance(first, last);
|
||||
auto it = const_cast<iterator>(pos);
|
||||
|
||||
if (first == last) return pos;
|
||||
if (_size + count >= N) throw std::exception();
|
||||
if (first == last) return it;
|
||||
if (_size + count > N) throw std::length_error("Not enough space left in vector for " + std::to_string(count) + " elements");
|
||||
|
||||
|
||||
std::move(pos, _arr + _size, pos + count);
|
||||
std::copy(first, last, pos);
|
||||
std::move_backward(it, end(), end() + count);
|
||||
std::copy(first, last, it);
|
||||
|
||||
_size += count;
|
||||
return pos;
|
||||
return it;
|
||||
}
|
||||
|
||||
template<typename T, std::size_t N>
|
||||
|
|
|
|||
Loading…
Reference in New Issue