Skip to content

Commit

Permalink
Optimise adjacentFind function acting on values.
Browse files Browse the repository at this point in the history
  • Loading branch information
HaxyM authored Aug 26, 2024
1 parent 54a833b commit f45810f
Showing 1 changed file with 85 additions and 10 deletions.
95 changes: 85 additions & 10 deletions include/crap/algorithm.d/adjacentfindvalue.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#include "../utility.d/valuelist.h"
#include "../version.d/libintegralconstantcallable.h"

#include <cstddef>

namespace crap
{
template <class Type, template <Type, Type> class, Type...> struct adjacentFindValue;
Expand All @@ -19,7 +21,8 @@ namespace crap
#endif
};

template <class Type, template <Type, Type> class Operator, Type Value> struct adjacentFindValue<Type, Operator, Value>
template <class Type, template <Type, Type> class Operator, Type Value>
struct adjacentFindValue<Type, Operator, Value>
{
constexpr const static std :: size_t value = 1u;
constexpr const static std :: size_t npos = 1u;
Expand All @@ -30,27 +33,84 @@ namespace crap
#endif
};

template <class Type, template <Type, Type> class Operator, Type Value1, Type Value2>
struct adjacentFindValue<Type, Operator, Value1, Value2>
{
constexpr const static std :: size_t value = ((Operator <Value1, Value2> :: value) ? 0u : 2u);
constexpr const static std :: size_t npos = 2u;
using value_type = decltype(value);
constexpr operator value_type () const noexcept;
#if (crap_lib_integral_constant_callable >= 201304L)
constexpr value_type operator () () const noexcept;
#endif
};

template <class Type, template <Type, Type> class Operator, Type ... Values> struct adjacentFindValue
{
private:
using values = valueList<Type, Values...>;
using values = valueList<Values...>;
constexpr const static std :: size_t half = values :: size / 2u;
template <Type ... SubValues> using This = adjacentFindValue<Type, Operator, SubValues...>;
using lower = typename values :: template till<half, This>;
using upper = typename values :: template since<half, This>;
constexpr const static Type lowerLast = values :: template At <half - 1u> :: value;
constexpr const static Type upperFirst = values :: template At <half> :: value;
constexpr const static bool foundInLower = ((lower :: value) != (lower :: npos));
constexpr const static bool foundOnEdge = Operator <lowerLast, upperFirst> :: value;
template <std :: size_t LowerValue, std :: size_t LowerNpos> struct checkMiddle;
template <std :: size_t LowerNpos> struct checkMiddle<LowerNpos, LowerNpos>;
public:
constexpr const static std :: size_t value = (foundInLower ? (lower :: value) : (foundOnEdge ? (half - 1u) : ((lower :: npos) + (upper :: value))));
constexpr const static std :: size_t npos = (lower :: npos) + (upper :: npos);
constexpr const static std :: size_t value = checkMiddle <lower :: value, lower :: npos> :: value;
constexpr const static std :: size_t npos = sizeof...(Values);
using value_type = decltype(value);
constexpr operator value_type () const noexcept;
#if (crap_lib_integral_constant_callable >= 201304L)
constexpr value_type operator () () const noexcept;
#endif
};

template <class Type, template <Type, Type> class Operator, Type ... Values>
template <std :: size_t LowerValue, std :: size_t LowerNpos>
struct adjacentFindValue <Type, Operator, Values...> :: checkMiddle
{
constexpr const static std :: size_t value = LowerValue;
};

template <class Type, template <Type, Type> class Operator, Type ... Values>
template <std :: size_t LowerNpos>
struct adjacentFindValue <Type, Operator, Values...> :: checkMiddle<LowerNpos, LowerNpos>
{
private:
using values = valueList<Values...>;
constexpr const static std :: size_t half = values :: size / 2u;
constexpr const static Type value1 = values :: template At <half - 1u> :: value;
constexpr const static Type value2 = values :: template At <half> :: value;
template <bool, class...> struct checkUpper;
template <class ... Empty> struct checkUpper<true, Empty...>;
template <class ... Empty> struct checkUpper<false, Empty...>;
public:
constexpr const static std :: size_t value = checkUpper <Operator <value1, value2> :: value> :: value;
};

template <class Type, template <Type, Type> class Operator, Type ... Values>
template <std :: size_t LowerNpos>
template <class ... Empty>
struct adjacentFindValue <Type, Operator, Values...> ::
checkMiddle <LowerNpos, LowerNpos> :: checkUpper<true, Empty...>
{
constexpr const static std :: size_t value = LowerNpos - 1u;
};

template <class Type, template <Type, Type> class Operator, Type ... Values>
template <std :: size_t LowerNpos>
template <class ... Empty>
struct adjacentFindValue <Type, Operator, Values...> ::
checkMiddle <LowerNpos, LowerNpos> ::
checkUpper<false, Empty...>
{
private:
using values = valueList<Values...>;
constexpr const static std :: size_t half = values :: size / 2u;
template <Type ... SubValues> using This = adjacentFindValue<Type, Operator, SubValues...>;
using upper = typename values :: template since<half, This>;
public:
constexpr const static std :: size_t value = LowerNpos + (upper :: value);
};
}

template <class Type, template <Type, Type> class Operator>
Expand Down Expand Up @@ -85,6 +145,22 @@ template <class Type, template <Type, Type> class Operator, Type Value>
}
#endif

template <class Type, template <Type, Type> class Operator, Type Value1, Type Value2>
inline constexpr crap :: adjacentFindValue <Type, Operator, Value1, Value2> :: operator
typename crap :: adjacentFindValue <Type, Operator, Value1, Value2> :: value_type () const noexcept
{
return crap :: adjacentFindValue <Type, Operator, Value1, Value2> :: value;
}
#if (crap_lib_integral_constant_callable >= 201304L)

template <class Type, template <Type, Type> class Operator, Type Value1, Type Value2>
inline constexpr typename crap :: adjacentFindValue <Type, Operator, Value1, Value2> :: value_type
crap :: adjacentFindValue <Type, Operator, Value1, Value2> :: operator () () const noexcept
{
return crap :: adjacentFindValue <Type, Operator, Value1, Value2> :: value;
}
#endif

template <class Type, template <Type, Type> class Operator, Type ... Values>
inline constexpr crap :: adjacentFindValue <Type, Operator, Values...> :: operator
typename crap :: adjacentFindValue <Type, Operator, Values...> :: value_type () const noexcept
Expand All @@ -101,4 +177,3 @@ template <class Type, template <Type, Type> class Operator, Type ... Values>
}
#endif
#endif

0 comments on commit f45810f

Please sign in to comment.