Compare commits

..

No commits in common. "8392edbcedd490f6fb187919aaa42d144f92c514" and "bd3d7fd58ec4aa4c75147039f1c7c570ffd76caf" have entirely different histories.

2 changed files with 13 additions and 54 deletions

View File

@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.26)
project(const_container)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD 23)
add_library(const_container INTERFACE "${CMAKE_CURRENT_LIST_DIR}/include/const_vector.hpp"
include/helper.h)

View File

@ -41,9 +41,11 @@ namespace cc {
constexpr const_vector() noexcept = default;
constexpr explicit const_vector(const value_type &value) noexcept;
constexpr const_vector(size_type count, const value_type &value) noexcept;
constexpr explicit const_vector(const value_type &value);
constexpr const_vector(size_type size, const value_type &value);
/*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;
@ -58,14 +60,6 @@ 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]; }
@ -86,17 +80,17 @@ namespace cc {
template<typename T, std::size_t N>
constexpr const_vector<T, N>::const_vector(const value_type &value) noexcept
constexpr const_vector<T, N>::const_vector(const value_type &value)
: _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 count, const value_type &value) noexcept
constexpr const_vector<T, N>::const_vector(size_type size, const value_type &value)
{
if (count > N) count = N;
_size = count;
if (size > N) size = N;
_size = size;
std::fill(_arr, _arr + _size, value);
}
@ -140,8 +134,9 @@ namespace cc {
if (this == &other) return *this;
static_assert(N == other._len, "Cannot assign const_vector to other with different size");
assign(other.begin(), other.end());
if (N != other.N) throw std::exception();
std::copy(other.begin(), other.end(), _arr);
return *this;
}
@ -160,46 +155,10 @@ 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
{
assign(std::begin(array), std::end(array));
*this = 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_