No idea what this is
This stuff's been laying around for months and years, just committing and crossing fingers - pro coding here!
This commit is contained in:
parent
a2d6944249
commit
96c6ac1df3
2 changed files with 67 additions and 1 deletions
|
@ -18,6 +18,7 @@
|
|||
#ifndef idFC25566D624140559C54B39FFFE52F04
|
||||
#define idFC25566D624140559C54B39FFFE52F04
|
||||
|
||||
#include "../variadic_repeat_bt.hpp"
|
||||
#include <type_traits>
|
||||
#include <stdexcept>
|
||||
#if !defined(INT_CONV_WITHOUT_HELPERS)
|
||||
|
@ -37,13 +38,16 @@ namespace dhandy {
|
|||
|
||||
using iterator = T*;
|
||||
constexpr ReversedSizedArray() = default;
|
||||
constexpr ReversedSizedArray (const ReversedSizedArray&) = default;
|
||||
constexpr ReversedSizedArray (ReversedSizedArray&&) = default;
|
||||
template <typename... Items> constexpr explicit ReversedSizedArray (Items&&... items);
|
||||
~ReversedSizedArray() = default;
|
||||
|
||||
constexpr std::size_t size() const { return S - (m_curr + 1); }
|
||||
constexpr bool empty() const { return m_curr + 1 == S; }
|
||||
constexpr const T operator[] (std::size_t idx) const { if (idx >= size()) throw std::out_of_range("Out of bound array access"); return m_data[idx + m_curr + 1]; }
|
||||
constexpr T& operator[] (std::size_t idx) { if (idx >= size()) throw std::out_of_range("Out of bound array access"); return m_data[idx + m_curr + 1]; }
|
||||
constexpr void push_front (const T& itm) { if (size() == S) throw std::length_error("ReversedSizedArray is full"); m_data[m_curr--] = itm; }
|
||||
constexpr void push_front (const T& itm) { /*if (size() == S) throw std::length_error("ReversedSizedArray is full");*/ m_data[m_curr--] = itm; }
|
||||
constexpr const T* data() const { return m_data + m_curr + 1; }
|
||||
constexpr const T* base_ptr() const { return m_data; }
|
||||
constexpr iterator begin() { return m_data + m_curr + 1; }
|
||||
|
@ -67,6 +71,9 @@ namespace dhandy {
|
|||
#endif
|
||||
|
||||
private:
|
||||
template <T... ItemsFill, typename... Items>
|
||||
constexpr explicit ReversedSizedArray (bt::variadic_repeat<T, ItemsFill...>, Items&&... items);
|
||||
|
||||
T m_data[S];
|
||||
std::size_t m_curr {S - 1};
|
||||
};
|
||||
|
@ -78,6 +85,24 @@ namespace dhandy {
|
|||
}
|
||||
} //namespace implem
|
||||
|
||||
template <typename T, std::size_t S>
|
||||
template <typename... Items>
|
||||
inline constexpr ReversedSizedArray<T, S>::ReversedSizedArray (Items&&... items) :
|
||||
ReversedSizedArray<T, S>(
|
||||
bt::make_variadic_repeat<S - sizeof...(Items), T>{},
|
||||
std::forward<Items>(items)...
|
||||
)
|
||||
{ }
|
||||
|
||||
template <typename T, std::size_t S>
|
||||
template <T... ItemsFill, typename... Items>
|
||||
constexpr ReversedSizedArray<T, S>::ReversedSizedArray (bt::variadic_repeat<T, ItemsFill...>, Items&&... items) :
|
||||
m_data{ItemsFill..., std::forward<Items>(items)...},
|
||||
m_curr(S - 1 - sizeof...(Items))
|
||||
{
|
||||
static_assert(sizeof...(ItemsFill) + sizeof...(Items) == S, "Wrong number of input elements");
|
||||
}
|
||||
|
||||
#if !defined(INT_CONV_WITHOUT_HELPERS)
|
||||
template <typename T, std::size_t S>
|
||||
inline
|
||||
|
|
41
include/duckhandy/variadic_repeat_bt.hpp
Normal file
41
include/duckhandy/variadic_repeat_bt.hpp
Normal file
|
@ -0,0 +1,41 @@
|
|||
/* Copyright 2016-2024 Michele Santullo
|
||||
* This file is part of "duckhandy".
|
||||
*
|
||||
* "duckhandy" is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* "duckhandy" 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with "duckhandy". If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <cstddef>
|
||||
|
||||
namespace dhandy::bt {
|
||||
template <typename T, T... V>
|
||||
struct variadic_repeat {
|
||||
};
|
||||
|
||||
namespace detail {
|
||||
template <std::size_t N, typename T, T V0, T... V>
|
||||
struct VariadicRepeatBuilder;
|
||||
|
||||
template <typename T, T V0, T... V>
|
||||
struct VariadicRepeatBuilder<0, T, V0, V...> {
|
||||
typedef variadic_repeat<T, V...> type;
|
||||
};
|
||||
|
||||
template <std::size_t N, typename T, T V0, T... V>
|
||||
struct VariadicRepeatBuilder : public VariadicRepeatBuilder<N-1, T, V0, V..., V0> {
|
||||
};
|
||||
} //namespace detail
|
||||
|
||||
template <std::size_t Count, typename T, T Val=T{}>
|
||||
using make_variadic_repeat = typename detail::VariadicRepeatBuilder<Count, T, Val>::type;
|
||||
} //namespace dhandy::bt
|
Loading…
Add table
Add a link
Reference in a new issue