pfor/src/pfor/comparators.h

99 lines
2.7 KiB
C++

#ifndef PFOR_PFOR_COMPARATORS_H
#define PFOR_PFOR_COMPARATORS_H
#include "mp/meta.h"
#include "mp/pack.h"
namespace pfor {
/**
* @brief compares two unsigned integers boxed in a type
*
* @param Lhs a boxed unsigned integer
* @param Rhs a boxed unsigned integer
*
* @return Lhs < Rhs
*/
template<typename, typename> struct ComparatorUIntToType;
template<std::size_t lhs, std::size_t rhs>
struct ComparatorUIntToType<UIntToType<lhs>, UIntToType<rhs>> {
static constexpr bool value = lhs < rhs;
};
template<typename Lhs, typename Rhs>
constexpr bool comparatorUIntToType = ComparatorUIntToType<Lhs, Rhs>::value;
/**
* @brief compares two packs of boxed unsigned integers
*
* @param Lhs a pack of boxed unsigned integer
* @param Rhs a pack of boxed unsigned integer
*
* @return Lhs < Rhs
*/
template<typename, typename> struct ComparatorUIntPack;
template<std::size_t a, typename... As, std::size_t b, typename... Bs>
struct ComparatorUIntPack<Pack<UIntToType<a>, As...>, Pack<UIntToType<b>, Bs...>> {
static constexpr bool value = a == b? ComparatorUIntPack<Pack<As...>, Pack<Bs...>>::value:(a < b);
};
template<std::size_t... bs>
struct ComparatorUIntPack<Pack<>, Pack<UIntToType<bs>...>> {
static constexpr bool value = true;
};
template<std::size_t... as>
struct ComparatorUIntPack<Pack<UIntToType<as>...>, Pack<>> {
static constexpr bool value = false;
};
template<>
struct ComparatorUIntPack<Pack<>, Pack<>> {
static constexpr bool value = false;
};
template<typename Lhs, typename Rhs>
constexpr bool comparatorUIntPack = ComparatorUIntPack<Lhs, Rhs>::value;
/**
* @brief compares two expression's information (boxed integer, W, R)
*
* @param Lhs an expression's information
* @param Rhs an expression's information
*
* @return Lhs < Rhs (using the boxed integer)
*/
template<typename, typename> struct ComparatorExpressionInfo;
template<std::size_t i1, typename W1, typename R1, std::size_t i2, typename W2, typename R2>
struct ComparatorExpressionInfo<Pack<UIntToType<i1>, W1, R1>, Pack<UIntToType<i2>, W2, R2>> {
static constexpr bool value = i1 < i2;
};
template<typename Lhs, typename Rhs>
constexpr bool comparatorExpressionInfo = ComparatorExpressionInfo<Lhs, Rhs>::value;
/**
* @brief compares two written variables sets
*
* @param Lhs an expression's information
* @param Rhs an expression's information
*
* @return Lhs < Rhs (using the W pack)
*/
template<typename, typename> struct ComparatorWritePack;
template<typename I1, typename WT1, typename RT1, typename I2, typename WT2, typename RT2>
struct ComparatorWritePack<Pack<I1, WT1, RT1>, Pack<I2, WT2, RT2>> {
static constexpr bool value = comparatorUIntPack<WT1, WT2>;
};
template<typename Lhs, typename Rhs>
constexpr bool comparatorWritePack = ComparatorWritePack<Lhs, Rhs>::value;
}
#endif