From 8392edbcedd490f6fb187919aaa42d144f92c514 Mon Sep 17 00:00:00 2001 From: cyborg1811m Date: Sat, 30 Dec 2023 23:20:04 +0100 Subject: [PATCH] implemented assign and at --- include/const_vector.hpp | 65 ++++++++++++++++++++++++++++++++-------- 1 file changed, 53 insertions(+), 12 deletions(-) diff --git a/include/const_vector.hpp b/include/const_vector.hpp index 0106e0a..52d7c42 100644 --- a/include/const_vector.hpp +++ b/include/const_vector.hpp @@ -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 > - constexpr const_vector(const T (&array)[aN]) noexcept;*/ constexpr explicit const_vector(const value_type (&array)[N]) noexcept; constexpr const_vector(std::initializer_list values) noexcept; @@ -60,6 +58,14 @@ namespace cc { constexpr const_vector& operator=(const_vector&& other) noexcept; constexpr const_vector& operator=(const value_type (&array)[N]) noexcept; + constexpr void assign(size_type count, const value_type& value) noexcept; + template + constexpr void assign(InputIt first, InputIt last); + constexpr void assign(std::initializer_list 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 - constexpr const_vector::const_vector(const value_type &value) + constexpr const_vector::const_vector(const value_type &value) noexcept : _size(N) { std::fill(std::begin(_arr), std::end(_arr), value); } template - constexpr const_vector::const_vector(size_type size, const value_type &value) + constexpr const_vector::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 constexpr const_vector &const_vector::operator=(const value_type (&array)[N]) noexcept { - *this = array; + assign(std::begin(array), std::end(array)); return *this; } + template + constexpr void const_vector::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 + template + constexpr void const_vector::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 + constexpr void const_vector::assign(std::initializer_list 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 + constexpr const_vector::reference const_vector::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 + constexpr const_vector::const_reference const_vector::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_