diff options
-rw-r--r-- | changelog.md | 9 | ||||
-rw-r--r-- | u8c-check/src/test.cc | 2 | ||||
-rw-r--r-- | u8c/include/u8c/arr | 55 | ||||
-rw-r--r-- | u8c/include/u8c/arr.d/arr | 58 | ||||
-rw-r--r-- | u8c/include/u8c/math | 2 | ||||
-rw-r--r-- | u8c/include/u8c/math.d/quota | 2 | ||||
-rw-r--r-- | u8c/include/u8c/math.d/trunc | 36 |
7 files changed, 80 insertions, 84 deletions
diff --git a/changelog.md b/changelog.md index 795b6bd..71f7f64 100644 --- a/changelog.md +++ b/changelog.md @@ -1,3 +1,12 @@ +# 24 + +* Remove constructor taking a single value for `u8c::arr`. +* Add new overload taking a single value for `u8c::arr::app`. +* Add function `u8c::arr::log` to enable logging of array operations *(doesn't currently log a lot)*. +* Remove `u8c::trunc`. +* Initialise memory allocated by `u8c::arr`. +* Add overload taking value used for memory-initialisation for `u8c::arr::alloc`. + # 23 * Rewrite for C++ *(read readme for list of current features)*. diff --git a/u8c-check/src/test.cc b/u8c-check/src/test.cc index 3f72a1e..1b600aa 100644 --- a/u8c-check/src/test.cc +++ b/u8c-check/src/test.cc @@ -50,8 +50,6 @@ static_assert(u8c::quota(0x1,0x3) == u8c::quota(0x2,0x6)); static_assert(u8c::quota<>::inf() == u8c::quota<>::inf()); static_assert(u8c::quota<>::nan() != u8c::quota<>::nan()); -static_assert(u8c::trunc(static_cast<u8c::ubyte>(std::numeric_limits<u8c::byte>::max()) + u8c_uint16c(0x1),u8c_bytec(0x0)) == std::numeric_limits<u8c::byte>::min()); - static_assert(u8c::cstrlen("This is a string!") == 0x11uz); static_assert(u8c::cstrlen("Das war ein Befehl!") == 0x13uz); diff --git a/u8c/include/u8c/arr b/u8c/include/u8c/arr index 81372f6..5d8fc4d 100644 --- a/u8c/include/u8c/arr +++ b/u8c/include/u8c/arr @@ -25,35 +25,38 @@ namespace u8c { template<typename T> class arr { public: - constexpr auto alloc( u8c::size num) -> void; - constexpr auto app( u8c::arr<T> const & oth) -> u8c::arr<T> const &; - [[nodiscard]] constexpr arr() noexcept = default; - [[nodiscard]] constexpr arr( T const * begin, T const * end); - [[nodiscard]] constexpr arr( u8c::size num); - [[nodiscard]] constexpr arr( u8c::arr<T> const & oth); - template<u8c::size N> [[nodiscard]] constexpr arr( T const (& arr)[N]) noexcept; - template<typename T0> requires std::convertible_to<T0,T> [[nodiscard]] constexpr arr( T0 val); - template<typename T0> requires std::convertible_to<T0,T> [[nodiscard]] constexpr arr( u8c::size num, T0 val); - [[nodiscard]] constexpr auto begin() const noexcept -> T *; - [[nodiscard]] constexpr auto end() const noexcept -> T *; - template<typename T0> requires std::convertible_to<T0,T> constexpr auto fill( T0 val) -> void; - template<typename T0> requires std::convertible_to<T0,T> constexpr auto fill( T * begin, T * end,T0 val) -> void; - [[nodiscard]] constexpr auto isstatic() const noexcept -> bool; - constexpr auto operator = ( u8c::arr<T> const & oth) -> u8c::arr<T> const &; - [[nodiscard]] constexpr auto operator [] (u8c::size pos) const noexcept -> T &; - constexpr auto realloc( u8c::size num) -> void; - constexpr auto set( T const * begin, T const * end) -> void; - constexpr auto set( u8c::arr<T> const & oth) -> void; - template<u8c::size N> constexpr auto set( T const (& arr)[N]) noexcept -> void; - template<typename T0> requires std::convertible_to<T0,T> constexpr auto set( T0 val) -> void; - [[nodiscard]] constexpr auto sub( T const * begin, T const * end) const -> u8c::arr<T>; - [[nodiscard]] constexpr auto sz() const noexcept -> u8c::size; - constexpr ~arr() noexcept; - constexpr static auto npos = -0x1uz; + constexpr auto alloc( u8c::size num) -> void; + template<std::convertible_to<T> T0> constexpr auto alloc( u8c::size num, T0 val) -> void; + constexpr auto app( u8c::arr<T> const & oth) -> u8c::arr<T> const &; + template<std::convertible_to<T> T0> constexpr auto app( T0 val) -> u8c::arr<T> const &; + [[nodiscard]] constexpr arr() noexcept = default; + [[nodiscard]] constexpr arr( T const * begin, T const * end); + [[nodiscard]] constexpr arr( u8c::size num); + [[nodiscard]] constexpr arr( u8c::arr<T> const & oth); + template<u8c::size N> [[nodiscard]] constexpr arr( T const (& arr)[N]) noexcept; + template<std::convertible_to<T> T0> [[nodiscard]] constexpr arr( u8c::size num, T0 val); + [[nodiscard]] constexpr auto begin() const noexcept -> T *; + [[nodiscard]] constexpr auto end() const noexcept -> T *; + template<std::convertible_to<T> T0> constexpr auto fill( T0 val) -> void; + template<std::convertible_to<T> T0> constexpr auto fill( T * begin, T * end,T0 val) -> void; + [[nodiscard]] constexpr auto isstatic() const noexcept -> bool; + constexpr auto log( bool val) noexcept -> void; + constexpr auto operator = ( u8c::arr<T> const & oth) -> u8c::arr<T> const &; + [[nodiscard]] constexpr auto operator [] (u8c::size pos) const noexcept -> T &; + constexpr auto realloc( u8c::size num) -> void; + constexpr auto set( T const * begin, T const * end) -> void; + constexpr auto set( u8c::arr<T> const & oth) -> void; + template<u8c::size N> constexpr auto set( T const (& arr)[N]) noexcept -> void; + template<std::convertible_to<T> T0> constexpr auto set( T0 val) -> void; + [[nodiscard]] constexpr auto sub( T const * begin, T const * end) const -> u8c::arr<T>; + [[nodiscard]] constexpr auto sz() const noexcept -> u8c::size; + constexpr ~arr() noexcept; + constexpr static auto npos = -0x1uz; private: bool _isstatic = false; + bool mutable _log = false; T * _ptr = nullptr; - u8c::size _sz = 0x0uz; + u8c::size _sz = 0x0uz; }; } diff --git a/u8c/include/u8c/arr.d/arr b/u8c/include/u8c/arr.d/arr index a0af8b7..63f35ad 100644 --- a/u8c/include/u8c/arr.d/arr +++ b/u8c/include/u8c/arr.d/arr @@ -33,14 +33,23 @@ template<typename T> constexpr auto u8c::arr<T>::alloc(u8c::size const _num) -> else { ::delete[] this->_ptr; } - this->_ptr = ::new T[_num]; + this->_ptr = ::new T[_num](); this->_sz = _num; } +template<typename T> template<std::convertible_to<T> T0> constexpr auto u8c::arr<T>::alloc(u8c::size const _num,T0 const _val) -> void { + this->alloc(_num); + this->fill(this->begin(),this->end(),_val); +} template<typename T> constexpr auto u8c::arr<T>::app(u8c::arr<T> const & _oth) -> u8c::arr<T> const & { this->realloc(this->sz() + _oth.sz()); std::copy(_oth.begin(),_oth.end(),this->begin() + this->sz() - _oth.sz()); return *this; } +template<typename T> template<std::convertible_to<T> T0> constexpr auto u8c::arr<T>::app(T0 const _val) -> u8c::arr<T> const & { + this->realloc(this->sz() + 0x1uz); + (*this)[this->sz() - 0x1uz] = static_cast<T>(_val); + return *this; +} template<typename T> constexpr u8c::arr<T>::arr(T const * const _begin,T const * const _end) { this->set(_begin,_end); } @@ -53,12 +62,8 @@ template<typename T> constexpr u8c::arr<T>::arr(u8c::arr<T> const & _oth) { template<typename T> template<u8c::size N> constexpr u8c::arr<T>::arr(T const (&_arr)[N]) noexcept { this->set(_arr); } -template<typename T> template<typename T0> requires std::convertible_to<T0,T> constexpr u8c::arr<T>::arr(T0 const _val) { - this->set(_val); -} -template<typename T> template<typename T0> requires std::convertible_to<T0,T> constexpr u8c::arr<T>::arr(u8c::size const _num,T0 const _val) { - this->alloc(_num); - this->fill(this->begin(),this->end(),_val); +template<typename T> template<std::convertible_to<T> T0> constexpr u8c::arr<T>::arr(u8c::size const _num,T0 const _val) { + this->alloc(_num,_val); } template<typename T> constexpr auto u8c::arr<T>::begin() const noexcept -> T * { return this->_ptr; @@ -66,32 +71,36 @@ template<typename T> constexpr auto u8c::arr<T>::begin() const noexcept -> T * { template<typename T> constexpr auto u8c::arr<T>::end() const noexcept -> T * { return this->begin() + this->_sz; } -template<typename T> template<typename T0> requires std::convertible_to<T0,T> constexpr auto u8c::arr<T>::fill(T0 const _val) -> void { +template<typename T> template<std::convertible_to<T> T0> constexpr auto u8c::arr<T>::fill(T0 const _val) -> void { this->fill(this->begin(),this->end(),_val); } -template<typename T> template<typename T0> requires std::convertible_to<T0,T> constexpr auto u8c::arr<T>::fill(T * const u8c_restr _begin,T * const u8c_restr _end,T0 const _val) -> void { +template<typename T> template<std::convertible_to<T> T0> constexpr auto u8c::arr<T>::fill(T * const u8c_restr _begin,T * const u8c_restr _end,T0 const _val) -> void { if (this->sz() == 0x0uz) [[unlikely]] { return; /* slime incident */ } - if (_begin < this->begin() || _end > this->end()) [[unlikely]] { - throw std::out_of_range("Beginning or end are out of this array's range."); + if constexpr (u8c::dbg) { + if (_begin < this->begin() || _end > this->end()) [[unlikely]] { + throw std::out_of_range("Beginning or end are out of this array's range."); + } } if (this->isstatic()) [[unlikely]] { - this->alloc(static_cast<u8c::size>(_end - _begin + 0x1uz)); + this->alloc(static_cast<u8c::size>(_end - _begin)); } std::fill(this->begin(),this->end(),static_cast<T>(_val)); } template<typename T> constexpr auto u8c::arr<T>::isstatic() const noexcept -> bool { return this->_isstatic; } +template<typename T> constexpr auto u8c::arr<T>::log(bool const _val) noexcept -> void { + this->_log = _val; +} template<typename T> constexpr auto u8c::arr<T>::operator = (u8c::arr<T> const & _oth) -> u8c::arr<T> const & { this->set(_oth); return *this; } template<typename T> constexpr auto u8c::arr<T>::operator [] (u8c::size const _pos) const noexcept -> T & { if constexpr (u8c::dbg) { - if (_pos > this->sz()) [[unlikely]] { - //std::cerr << "u8c :: " << std::source_location::current().function_name() << " :: Input parameter is out of range." << std::endl; + if (_pos >= this->sz()) [[unlikely]] { std::cerr << "u8c :: " << __func__ << " :: Input parameter is out of range." << std::endl; std::abort(); } @@ -126,13 +135,21 @@ template<typename T> template<u8c::size N> constexpr auto u8c::arr<T>::set(T con this->_ptr = _arr; this->_sz = N; } -template<typename T> template<typename T0> requires std::convertible_to<T0,T> constexpr auto u8c::arr<T>::set(T0 const _val) -> void { +template<typename T> template<std::convertible_to<T> T0> constexpr auto u8c::arr<T>::set(T0 const _val) -> void { this->alloc(0x1uz); *this->begin() = _val; } template<typename T> constexpr auto u8c::arr<T>::sub(T const * const u8c_restr _begin,T const * const u8c_restr _end) const -> u8c::arr<T> { - if (_begin < this->begin() || _end > this->end()) [[unlikely]] { - throw std::out_of_range("Beginning or end are out of this array's range."); + if (this->_log) [[unlikely]] { + std::cerr << "u8c :: Generating subarray" << std::endl; + } + if constexpr (u8c::dbg) { + if (_begin < this->begin() || _end > this->end()) [[unlikely]] { + if (this->_log) [[unlikely]] { + std::cerr << "u8c :: Input out of range" << std::endl; + } + throw std::out_of_range("Beginning or end are out of this array's range."); + } } u8c::size const sz = static_cast<u8c::size>(_end - _begin) + 0x1uz; u8c::arr<T> arr; @@ -151,8 +168,15 @@ template<typename T> constexpr auto u8c::arr<T>::sz() const noexcept -> u8c::siz } template<typename T> constexpr u8c::arr<T>::~arr<T>() noexcept { if (this->isstatic()) { + if (this->_log) [[unlikely]] { + std::cerr << "u8c :: Destroying static array" << std::endl; + } return; } + if (this->_log) [[unlikely]] { + std::cerr << "u8c :: Destroying dynamic array" << std::endl; + std::cerr << "u8c :: Deallocating" << std::endl; + } ::delete[] this->_ptr; } diff --git a/u8c/include/u8c/math b/u8c/include/u8c/math index 2b12906..d02a827 100644 --- a/u8c/include/u8c/math +++ b/u8c/include/u8c/math @@ -64,7 +64,6 @@ namespace u8c { template<u8c::arith T> [[u8c_attr_const]] constexpr auto pow( T base,T exp) noexcept -> T; template<typename T> [[u8c_attr_const]] constexpr auto sqrt( u8c::quota<T> val) noexcept -> u8c::quota<T>; template<u8c::arith T> [[u8c_attr_const]] constexpr auto sqrt( T val) noexcept -> T; - template<std::integral T,std::integral T0> [[u8c_attr_const]] constexpr auto trunc( T val, T0) noexcept -> T0; } #include <u8c/math.d/abs> @@ -75,6 +74,5 @@ namespace u8c { #include <u8c/math.d/pow> #include <u8c/math.d/quota> #include <u8c/math.d/sqrt> -#include <u8c/math.d/trunc> #endif diff --git a/u8c/include/u8c/math.d/quota b/u8c/include/u8c/math.d/quota index 3ea9350..4e3ecf8 100644 --- a/u8c/include/u8c/math.d/quota +++ b/u8c/include/u8c/math.d/quota @@ -72,7 +72,7 @@ template<std::signed_integral T> template<std::convertible_to<T> T0> constexpr a return this->upper() * _oth.lower() == _oth.upper() * this->lower(); } template<std::signed_integral T> template<std::integral T0> constexpr u8c::quota<T>::operator T0 () const noexcept { - return u8c::trunc<T0>(this->_upper / this->_lower); + return static_cast<T0>(this->_upper / this->_lower); } template<std::signed_integral T> template<std::convertible_to<T> T0> constexpr u8c::quota<T>::quota(T0 const _val) noexcept { if (u8c::isnan(_val)) [[unlikely]] { diff --git a/u8c/include/u8c/math.d/trunc b/u8c/include/u8c/math.d/trunc deleted file mode 100644 index d4ff731..0000000 --- a/u8c/include/u8c/math.d/trunc +++ /dev/null @@ -1,36 +0,0 @@ -/* - Copyright 2021 Gabriel Jensen - - This file is part of u8c. - - u8c is free software: you can redistribute it and/or modify it under the - terms of the GNU Affero General Public License as published by the Free - Software Foundation, either version 3 of the License, or (at your - option) any later version. - - u8c is distributed in the hope that it will be useful, but WITHOUT ANY - WARRANTY; without even the implied warranty of MERCHANTABILITY or - FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public - License for more details. - - You should have received a copy of the GNU Affero General Public License - along with u8c. If not, see <https://www.gnu.org/licenses/>. -*/ - -#if !defined(u8c_key_cQ6WEZj1q8fhHahe) -#define u8c_key_cQ6WEZj1q8fhHahe - -#include <concepts> /* std::integral */ -#include <limits> /* std::numeric_limits */ -#include <type_traits> /* std::make_unsigned_t */ - -template<std::integral T,std::integral T0> constexpr auto u8c::trunc(T const _val,T0) noexcept -> T0 { - if constexpr (std::numeric_limits<T0>::is_modulo) { - return static_cast<T0>(_val); - } - else { - return static_cast<T0>(static_cast<std::make_unsigned_t<T0>>(_val)); - } -} - -#endif |