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>
|
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)
|
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;
|
auto it = const_cast<iterator>(pos);
|
||||||
std::move(_arr + i, _arr + _size, _arr + i + 1);
|
|
||||||
_arr[i] = value;
|
|
||||||
|
|
||||||
|
if (pos != end()) {
|
||||||
|
std::move_backward(it, end(), end() + 1);
|
||||||
|
}
|
||||||
|
*it = value;
|
||||||
++_size;
|
++_size;
|
||||||
return pos;
|
|
||||||
|
return it;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T, std::size_t N>
|
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)
|
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;
|
auto it = const_cast<iterator>(pos);
|
||||||
std::move(_arr + i, _arr + _size, _arr + i + 1);
|
|
||||||
_arr[i] = std::move(value);
|
|
||||||
|
|
||||||
|
if (pos != end()) {
|
||||||
|
std::move_backward(it, end(), end() + 1);
|
||||||
|
}
|
||||||
|
*it = std::forward<value_type>(value);
|
||||||
++_size;
|
++_size;
|
||||||
return pos;
|
|
||||||
|
return it;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T, std::size_t N>
|
template<typename T, std::size_t N>
|
||||||
constexpr const_vector<T, N>::iterator
|
constexpr const_vector<T, N>::iterator
|
||||||
const_vector<T, N>::insert(const_vector::const_iterator pos, const_vector::size_type count, const T &value)
|
const_vector<T, N>::insert(const_vector::const_iterator pos, const_vector::size_type count, const T &value)
|
||||||
{
|
{
|
||||||
if (count == 0) return pos;
|
auto it = const_cast<iterator>(pos);
|
||||||
if (_size + count >= N) throw std::exception();
|
|
||||||
|
|
||||||
std::move(pos, _arr + _size, pos + count);
|
if (count == 0) return it;
|
||||||
std::fill(pos, pos + count, value);
|
if (_size + count > N) throw std::length_error("Not enough space left in vector for " + std::to_string(count) + " elements");
|
||||||
|
|
||||||
|
std::move_backward(it, end(), end() + count);
|
||||||
|
std::fill(it, it + count, value);
|
||||||
|
|
||||||
_size += count;
|
_size += count;
|
||||||
return pos;
|
return it;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T, std::size_t N>
|
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)
|
const_vector<T, N>::insert(const_vector::const_iterator pos, InputIt first, InputIt last)
|
||||||
{
|
{
|
||||||
auto count = std::distance(first, last);
|
auto count = std::distance(first, last);
|
||||||
|
auto it = const_cast<iterator>(pos);
|
||||||
|
|
||||||
if (first == last) return pos;
|
if (first == last) return it;
|
||||||
if (_size + count >= N) throw std::exception();
|
if (_size + count > N) throw std::length_error("Not enough space left in vector for " + std::to_string(count) + " elements");
|
||||||
|
|
||||||
|
std::move_backward(it, end(), end() + count);
|
||||||
std::move(pos, _arr + _size, pos + count);
|
std::copy(first, last, it);
|
||||||
std::copy(first, last, pos);
|
|
||||||
|
|
||||||
_size += count;
|
_size += count;
|
||||||
return pos;
|
return it;
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T, std::size_t N>
|
template<typename T, std::size_t N>
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue