From e971e68258c6207671a1609aecc9a05cfd3a6689 Mon Sep 17 00:00:00 2001 From: cyborg1811m Date: Sun, 24 Dec 2023 14:39:12 +0100 Subject: [PATCH] added constructors and access functionality --- CMakeLists.txt | 9 +++ include/const_vector.hpp | 164 +++++++++++++++++++++++++++++++++++++++ include/helper.h | 17 ++++ 3 files changed, 190 insertions(+) create mode 100644 CMakeLists.txt create mode 100644 include/const_vector.hpp create mode 100644 include/helper.h diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..274dc9a --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,9 @@ +cmake_minimum_required(VERSION 3.26) +project(const_container) + +set(CMAKE_CXX_STANDARD 23) + +#add_library(imm_container INTERFACE) +add_library(const_container INTERFACE "${CMAKE_CURRENT_LIST_DIR}/include/const_vector.hpp" + include/helper.h) +target_include_directories(const_container INTERFACE "${CMAKE_CURRENT_LIST_DIR}/include") diff --git a/include/const_vector.hpp b/include/const_vector.hpp new file mode 100644 index 0000000..0106e0a --- /dev/null +++ b/include/const_vector.hpp @@ -0,0 +1,164 @@ +// +// Created by Patrick Maschek on 19.12.2023. +// + +#ifndef UDIFF_IMMARRAY_H_ +#define UDIFF_IMMARRAY_H_ + +#include +#include +#include +#include +#include "helper.h" + +namespace cc { + + template + class const_vector { + + static_assert(N > 0, "Capacity of const_vector has to be greater than 0"); + + public: + + using value_type = T; + using size_type = std::size_t; + using difference_type = std::ptrdiff_t; + using reference = value_type&; + using const_reference = const value_type&; + using pointer = value_type *; + using const_pointer = const value_type *; + using iterator = pointer; + using const_iterator = const_pointer; + using reverse_iterator = std::reverse_iterator; + using const_reverse_iterator = std::reverse_iterator; + + protected: + T _arr[N] = {}; + static constexpr const size_type _len = N; + size_type _size = 0; + + public: + + constexpr const_vector() noexcept = default; + + constexpr explicit const_vector(const value_type &value); + constexpr const_vector(size_type size, const value_type &value); + + /*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; + + template + constexpr const_vector(InputIt first, InputIt last); + + constexpr const_vector(const const_vector& other) noexcept; + constexpr const_vector(const_vector&& other) noexcept; + + constexpr const_vector& operator=(const const_vector& other); + constexpr const_vector& operator=(const_vector&& other) noexcept; + constexpr const_vector& operator=(const value_type (&array)[N]) noexcept; + + [[nodiscard]] constexpr size_type size() const noexcept { return _size; } + + constexpr T& operator[](size_type pos) { return _arr[pos]; } + constexpr const T& operator[](size_type pos) const { return _arr[pos]; } + +#ifdef UNIT_TEST + friend test_const_vector; +#endif + + }; + + template + const_vector(A[]) -> const_vector>; + + template + const_vector(T...) -> const_vector, sizeof...(T)>; + + + + template + constexpr const_vector::const_vector(const value_type &value) + : _size(N) + { + std::fill(std::begin(_arr), std::end(_arr), value); + } + + template + constexpr const_vector::const_vector(size_type size, const value_type &value) + { + if (size > N) size = N; + _size = size; + std::fill(_arr, _arr + _size, value); + } + + template + constexpr const_vector::const_vector(const value_type (&array)[N]) noexcept + : _size(N) + { + std::move(std::begin(array), std::end(array), _arr); + } + + template + constexpr const_vector::const_vector(std::initializer_list values) noexcept + : _size(values.size()) + { + std::move(values.begin(), values.end(), _arr); + } + + template + template + constexpr const_vector::const_vector(InputIt first, InputIt last) { + //static_assert(std::distance(first, last) > N, "tried inserting more elements than const_vector is in size"); + _size = std::distance(first, last); + std::copy(first, last, _arr); + } + + template + constexpr const_vector::const_vector(const const_vector &other) noexcept { + static_assert(N >= other._size, "size of other has to be equal to or smaller than this"); + std::copy(other._arr, other._arr, _arr); + } + + template + constexpr const_vector::const_vector(const_vector &&other) noexcept { + static_assert(N == other.N, "size of const_vectors does not match"); + std::move(other.begin(), other.end(), _arr); + } + + template + constexpr const_vector &const_vector::operator=(const const_vector &other) + { + 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); + + return *this; + } + + template + constexpr const_vector &const_vector::operator=(const_vector &&other) noexcept + { + static_assert(N == other._len, "Cannot assign const_vector to other with different size"); + if (N != other.N) throw std::exception(); + + std::move(other.begin(), other.end(), _arr); + + return *this; + } + + template + constexpr const_vector &const_vector::operator=(const value_type (&array)[N]) noexcept + { + *this = array; + return *this; + } + +}; // cc + +#endif //UDIFF_IMMARRAY_H_ diff --git a/include/helper.h b/include/helper.h new file mode 100644 index 0000000..dd98101 --- /dev/null +++ b/include/helper.h @@ -0,0 +1,17 @@ +// +// Created by Patrick Maschek on 24.12.2023. +// + +#ifndef UDIFF_HELPER_H_ +#define UDIFF_HELPER_H_ + +#include + +namespace cc::helper { + + template + constexpr std::size_t array_size(const T(&)[N]) { return N; } + +}; // cc + +#endif //UDIFF_HELPER_H_