Merge branch 'master' into scapegoat
This commit is contained in:
commit
3f633df74f
6 changed files with 265 additions and 4 deletions
|
@ -1,4 +1,4 @@
|
|||
/* Copyright 2016-2024 Michele Santullo
|
||||
/* Copyright 2016-2025 Michele Santullo
|
||||
* This file is part of "duckhandy".
|
||||
*
|
||||
* "duckhandy" is free software: you can redistribute it and/or modify
|
||||
|
|
|
@ -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
|
||||
|
|
194
include/duckhandy/packed_pointer.hpp
Normal file
194
include/duckhandy/packed_pointer.hpp
Normal file
|
@ -0,0 +1,194 @@
|
|||
/* Copyright 2016-2025 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/>.
|
||||
*/
|
||||
|
||||
#ifndef id83CE9FAB90684250B98EA584C548E13F
|
||||
#define id83CE9FAB90684250B98EA584C548E13F
|
||||
|
||||
#include "bitfield_pack.hpp"
|
||||
#include <cstdint>
|
||||
#include <utility>
|
||||
#include <climits>
|
||||
|
||||
namespace dhandy {
|
||||
namespace implem {
|
||||
|
||||
//This monster expression is just the one-liner version of this
|
||||
//implementation of log2:
|
||||
//unsigned int v; // 32-bit value to find the log2 of
|
||||
//register unsigned int r; // result of log2(v) will go here
|
||||
//register unsigned int shift;
|
||||
//
|
||||
//r = (v > 0xFFFF) << 4; v >>= r;
|
||||
//shift = (v > 0xFF ) << 3; v >>= shift; r |= shift;
|
||||
//shift = (v > 0xF ) << 2; v >>= shift; r |= shift;
|
||||
//shift = (v > 0x3 ) << 1; v >>= shift; r |= shift;
|
||||
// r |= (v >> 1);
|
||||
//see https://graphics.stanford.edu/~seander/bithacks.html#IntegerLog for
|
||||
//an explanation of that.
|
||||
//This is a shorter version of it but it only works if the input is a power
|
||||
//of 2:
|
||||
//(((((NUM&0xAAAAAAAA)!=0)|((NUM&0xFFFF0000)!=0)<<4)|((NUM&0xFF00FF00)!=0)<<
|
||||
//3)|((NUM&0xF0F0F0F0)!=0)<<2)|((NUM&0xCCCCCCCC)!=0)<<1
|
||||
//Source for that is also available at the page linked above.
|
||||
template <std::uint32_t NUM> const constexpr std::size_t
|
||||
pointer_unused_bit_count =
|
||||
((((((NUM > 0xFFFF) << 4) | (((NUM >> ((NUM > 0xFFFF) << 4)) > 0xFF) <<
|
||||
3)) | ((((NUM >> ((NUM > 0xFFFF) << 4)) >> (((NUM >> ((NUM > 0xFFFF) <<
|
||||
4)) > 0xFF) << 3)) > 0xF) << 2)) | (((((NUM >> ((NUM > 0xFFFF) << 4)) >>
|
||||
(((NUM >> ((NUM > 0xFFFF) << 4)) > 0xFF) << 3)) >> ((((NUM >> ((NUM >
|
||||
0xFFFF) << 4)) >> (((NUM >> ((NUM > 0xFFFF) << 4)) > 0xFF) << 3)) > 0xF)
|
||||
<< 2)) > 0x3) << 1)) | (((((NUM >> ((NUM > 0xFFFF) << 4)) >> (((NUM >> (
|
||||
(NUM > 0xFFFF) << 4)) > 0xFF) << 3)) >> ((((NUM >> ((NUM > 0xFFFF) << 4)
|
||||
) >> (((NUM >> ((NUM > 0xFFFF) << 4)) > 0xFF) << 3)) > 0xF) << 2)) >> ((
|
||||
(((NUM >> ((NUM > 0xFFFF) << 4)) >> (((NUM >> ((NUM > 0xFFFF) << 4)) >
|
||||
0xFF) << 3)) >> ((((NUM >> ((NUM > 0xFFFF) << 4)) >> (((NUM >> ((NUM >
|
||||
0xFFFF) << 4)) > 0xFF) << 3)) > 0xF) << 2)) > 0x3) << 1)) >> 1))
|
||||
;
|
||||
|
||||
template <std::size_t... S>
|
||||
BitfieldPack<
|
||||
std::uintptr_t,
|
||||
sizeof(std::uintptr_t) * CHAR_BIT - sizeof...(S), (S+1)/(S+1)...
|
||||
>
|
||||
guess_bitfield_type (std::index_sequence<S...>);
|
||||
|
||||
template <typename T>
|
||||
using BaseBitfieldPackForPackedPointer = decltype(
|
||||
implem::guess_bitfield_type(
|
||||
std::make_index_sequence<implem::pointer_unused_bit_count<alignof(T)>>()
|
||||
)
|
||||
);
|
||||
} //namespace implem
|
||||
|
||||
template <typename T>
|
||||
class PackedPointer : private implem::BaseBitfieldPackForPackedPointer<T> {
|
||||
static_assert(sizeof(T*) == sizeof(std::uintptr_t), "Mismatching int/pointer size");
|
||||
typedef implem::BaseBitfieldPackForPackedPointer<T> parent_type;
|
||||
public:
|
||||
static const constexpr std::size_t FlagsCount = parent_type::TotalEntries - 1;
|
||||
|
||||
PackedPointer();
|
||||
PackedPointer (T* ptr);
|
||||
~PackedPointer() = default;
|
||||
|
||||
operator T*() const;
|
||||
operator bool() const;
|
||||
template <std::size_t Idx> bool flag () const;
|
||||
template <std::size_t Idx> void set_flag (bool v);
|
||||
|
||||
PackedPointer& operator= (T* ptr);
|
||||
T* operator->();
|
||||
const T* operator->() const;
|
||||
T& operator*();
|
||||
const T& operator*() const;
|
||||
bool operator== (const PackedPointer& other) const;
|
||||
bool operator== (T* other) const;
|
||||
bool operator!= (const PackedPointer& other) const;
|
||||
bool operator!= (T* other) const;
|
||||
|
||||
private:
|
||||
//static const constexpr uintptr_t PtrMask = static_cast<uintptr_t>(-1) << FlagsCount;
|
||||
};
|
||||
|
||||
template <typename T> const constexpr std::size_t PackedPointer<T>::FlagsCount;
|
||||
//template <typename T> const constexpr std::uintptr_t PackedPointer<T>::PtrMask;
|
||||
|
||||
template <typename T>
|
||||
PackedPointer<T>::PackedPointer() :
|
||||
PackedPointer(nullptr)
|
||||
{
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
PackedPointer<T>::PackedPointer (T* ptr) :
|
||||
parent_type(0)
|
||||
{
|
||||
(*this) = ptr;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
PackedPointer<T>::operator T*() const {
|
||||
return reinterpret_cast<T*>(this->get(0) << FlagsCount);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
PackedPointer<T>::operator bool() const {
|
||||
return static_cast<T*>(*this) != nullptr;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
template <std::size_t Idx>
|
||||
bool PackedPointer<T>::flag() const {
|
||||
static_assert(Idx < FlagsCount, "Index out of range");
|
||||
return static_cast<bool>(this->get(Idx + 1));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
template <std::size_t Idx>
|
||||
void PackedPointer<T>::set_flag (bool v) {
|
||||
static_assert(Idx < FlagsCount, "Index out of range");
|
||||
this->set(Idx + 1, (v ? 1 : 0));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
PackedPointer<T>& PackedPointer<T>::operator= (T* ptr) {
|
||||
this->set(0, reinterpret_cast<std::uintptr_t>(ptr) >> FlagsCount);
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
T* PackedPointer<T>::operator->() {
|
||||
return static_cast<T*>(*this);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
const T* PackedPointer<T>::operator->() const {
|
||||
return static_cast<T*>(*this);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
T& PackedPointer<T>::operator*() {
|
||||
return *static_cast<T*>(*this);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
const T& PackedPointer<T>::operator*() const {
|
||||
return *static_cast<T*>(*this);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool PackedPointer<T>::operator== (const PackedPointer& other) const {
|
||||
return this == &other or static_cast<T*>(*this) == static_cast<T*>(other);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool PackedPointer<T>::operator== (T* other) const {
|
||||
return static_cast<T*>(*this) == other;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool PackedPointer<T>::operator!= (const PackedPointer& other) const {
|
||||
return not this->operator==(other);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool PackedPointer<T>::operator!= (T* other) const {
|
||||
return not this->operator==(other);
|
||||
}
|
||||
} //namepace dhandy
|
||||
|
||||
#endif
|
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
|
|
@ -3,4 +3,5 @@ revision = b5373dadca40b7edc8570cf9470b9b1cb1934d40
|
|||
url = https://github.com/catchorg/Catch2.git
|
||||
|
||||
[provide]
|
||||
Catch2 = catch2_with_main_dep
|
||||
catch2-with-main = catch2_with_main_dep
|
||||
Catch2 = catch2_dep
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
catch2_dep = dependency('Catch2', version: '>=3.5.2')
|
||||
catch2_dep = dependency('catch2-with-main', version: '>=3.5.2')
|
||||
|
||||
unit_test_prog = executable(meson.project_name(),
|
||||
'int_conv_test.cpp',
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue