implemented assign and at
This commit is contained in:
parent
6431b73b1a
commit
8392edbced
|
|
@ -41,11 +41,9 @@ namespace cc {
|
|||
|
||||
constexpr const_vector() noexcept = default;
|
||||
|
||||
constexpr explicit const_vector(const value_type &value);
|
||||
constexpr const_vector(size_type size, const value_type &value);
|
||||
constexpr explicit const_vector(const value_type &value) noexcept;
|
||||
constexpr const_vector(size_type count, const value_type &value) noexcept;
|
||||
|
||||
/*template <std::size_t aN, typename = std::enable_if<aN < N>>
|
||||
constexpr const_vector(const T (&array)[aN]) noexcept;*/
|
||||
constexpr explicit const_vector(const value_type (&array)[N]) noexcept;
|
||||
|
||||
constexpr const_vector(std::initializer_list<value_type> values) noexcept;
|
||||
|
|
@ -60,6 +58,14 @@ namespace cc {
|
|||
constexpr const_vector<T, N>& operator=(const_vector&& other) noexcept;
|
||||
constexpr const_vector<T, N>& operator=(const value_type (&array)[N]) noexcept;
|
||||
|
||||
constexpr void assign(size_type count, const value_type& value) noexcept;
|
||||
template<std::input_iterator InputIt>
|
||||
constexpr void assign(InputIt first, InputIt last);
|
||||
constexpr void assign(std::initializer_list<value_type> values);
|
||||
|
||||
constexpr reference at(size_type pos);
|
||||
constexpr const_reference at(size_type pos) const;
|
||||
|
||||
[[nodiscard]] constexpr size_type size() const noexcept { return _size; }
|
||||
|
||||
constexpr T& operator[](size_type pos) { return _arr[pos]; }
|
||||
|
|
@ -80,17 +86,17 @@ namespace cc {
|
|||
|
||||
|
||||
template<typename T, std::size_t N>
|
||||
constexpr const_vector<T, N>::const_vector(const value_type &value)
|
||||
constexpr const_vector<T, N>::const_vector(const value_type &value) noexcept
|
||||
: _size(N)
|
||||
{
|
||||
std::fill(std::begin(_arr), std::end(_arr), value);
|
||||
}
|
||||
|
||||
template<typename T, std::size_t N>
|
||||
constexpr const_vector<T, N>::const_vector(size_type size, const value_type &value)
|
||||
constexpr const_vector<T, N>::const_vector(size_type count, const value_type &value) noexcept
|
||||
{
|
||||
if (size > N) size = N;
|
||||
_size = size;
|
||||
if (count > N) count = N;
|
||||
_size = count;
|
||||
std::fill(_arr, _arr + _size, value);
|
||||
}
|
||||
|
||||
|
|
@ -134,9 +140,8 @@ namespace cc {
|
|||
if (this == &other) return *this;
|
||||
|
||||
static_assert(N == other._len, "Cannot assign const_vector to other with different size");
|
||||
if (N != other.N) throw std::exception();
|
||||
|
||||
std::copy(other.begin(), other.end(), _arr);
|
||||
|
||||
assign(other.begin(), other.end());
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
|
@ -155,10 +160,46 @@ namespace cc {
|
|||
template<typename T, std::size_t N>
|
||||
constexpr const_vector<T, N> &const_vector<T, N>::operator=(const value_type (&array)[N]) noexcept
|
||||
{
|
||||
*this = array;
|
||||
assign(std::begin(array), std::end(array));
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<typename T, std::size_t N>
|
||||
constexpr void const_vector<T, N>::assign(const_vector::size_type count, const value_type &value) noexcept {
|
||||
if (count > N) count = N;
|
||||
_size = count;
|
||||
std::fill(std::begin(_arr), std::end(_arr), value);
|
||||
}
|
||||
|
||||
template<typename T, std::size_t N>
|
||||
template<std::input_iterator InputIt>
|
||||
constexpr void const_vector<T, N>::assign(InputIt first, InputIt last) {
|
||||
auto distance = std::distance(first, last);
|
||||
if (distance > N) throw std::invalid_argument("Iterator distance in assign surpasses size" + std::to_string(distance) + ">=" + std::to_string(N));
|
||||
_size = distance;
|
||||
std::copy(first, last, _arr);
|
||||
}
|
||||
|
||||
template<typename T, std::size_t N>
|
||||
constexpr void const_vector<T, N>::assign(std::initializer_list<value_type> values) {
|
||||
auto values_size = std::distance(values.begin(), values.end());
|
||||
if (values_size > N) throw std::invalid_argument("Initializer list in assign has more elements than size" + std::to_string(values_size) + ">=" + std::to_string(N));
|
||||
_size = values_size;
|
||||
std::copy(values.begin(), values.end(), _arr);
|
||||
}
|
||||
|
||||
template<typename T, std::size_t N>
|
||||
constexpr const_vector<T, N>::reference const_vector<T, N>::at(const_vector::size_type pos) {
|
||||
if (pos >= _size) throw std::out_of_range("Pos " + std::to_string(pos) + " is out of range");
|
||||
return _arr[pos];
|
||||
}
|
||||
|
||||
template<typename T, std::size_t N>
|
||||
constexpr const_vector<T, N>::const_reference const_vector<T, N>::at(const_vector::size_type pos) const {
|
||||
if (pos >= _size) throw std::out_of_range("Pos " + std::to_string(pos) + " is out of range");
|
||||
return _arr[pos];
|
||||
}
|
||||
|
||||
}; // cc
|
||||
|
||||
#endif //UDIFF_IMMARRAY_H_
|
||||
|
|
|
|||
Loading…
Reference in New Issue