Skip to content

Commit

Permalink
Various fixes, refactoring of add_cons_close_oper
Browse files Browse the repository at this point in the history
  • Loading branch information
flisboac committed Jan 22, 2018
1 parent edc4353 commit 66919ba
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 102 deletions.
8 changes: 4 additions & 4 deletions include/adl/oct/cons/basic_oct_cons.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ namespace dsl {
typename VarType,
typename = std::enable_if_t<
adl::oct::common_var<VarType>::is_oct_space
&& std::is_arithmetic<ConstantType>::value>>
&& adl::oct::constant_limits<ConstantType>::valid>>
constexpr adl::oct::basic_oct_cons<ConstantType, VarType> operator>=(VarType var, ConstantType rhs) noexcept {
return adl::oct::make_lower_unit(var, rhs);
};
Expand All @@ -215,7 +215,7 @@ namespace dsl {
typename VarType,
typename = std::enable_if_t<
adl::oct::common_var<VarType>::is_oct_space
&& std::is_arithmetic<ConstantType>::value>>
&& adl::oct::constant_limits<ConstantType>::valid>>
constexpr adl::oct::basic_oct_cons<ConstantType, VarType> operator<=(VarType var, ConstantType rhs) noexcept {
return adl::oct::make_upper_unit(var, rhs);
};
Expand All @@ -225,7 +225,7 @@ namespace dsl {
typename VarType,
typename = std::enable_if_t<
adl::oct::common_var<VarType>::is_oct_space
&& std::is_arithmetic<ConstantType>::value>>
&& adl::oct::constant_limits<ConstantType>::valid>>
constexpr adl::oct::basic_oct_cons<ConstantType, VarType> operator>=(
adl::oct::basic_oct_vexpr<VarType> vexpr,
ConstantType rhs
Expand All @@ -238,7 +238,7 @@ namespace dsl {
typename VarType,
typename = std::enable_if_t<
adl::oct::common_var<VarType>::is_oct_space
&& std::is_arithmetic<ConstantType>::value>>
&& adl::oct::constant_limits<ConstantType>::valid>>
constexpr adl::oct::basic_oct_cons<ConstantType, VarType> operator<=(
adl::oct::basic_oct_vexpr<VarType> vexpr,
ConstantType rhs
Expand Down
2 changes: 2 additions & 0 deletions include/adl/oct/cons/cons_base_.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ class cons_base_ {
constexpr std::size_t operator()(cons_type const& lhs) const noexcept;
};

static_assert(constant_limits::valid, "Constant type is not valid for octagonal system construction."
" If you're using a primitive integer type, please use basic_float_int instead.");

private:
using subclass_ = cons_type;
Expand Down
34 changes: 19 additions & 15 deletions include/adl/oct/constant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ namespace oct {

namespace {
template <typename T, bool is_floating_point = std::is_floating_point<T>::value>
struct is_pair_ {
constexpr static bool is_pair(T const& n) { return n % 2 == 0; }
struct is_even_ {
constexpr static bool is_even(T const& n) { return n % 2 == 0; }
};

template <typename T>
struct is_pair_<T, true> {
constexpr static bool is_pair(T const& n) { return std::fmod(n, 2) == 0; }
struct is_even_<T, true> {
constexpr static bool is_even(T const& n) { return std::fmod(n, 2) == 0; }
};
}

Expand All @@ -45,15 +45,15 @@ struct constant_limits {
public:
using constant_type = ConstantType;

static_assert(numeric_limits_::is_specialized,
"The constant_limits class is not valid. Please, specialize it correctly for the provided value type.");
//static_assert(numeric_limits_::is_specialized,
// "The constant_limits class is not valid. Please, specialize it correctly for the provided value type.");

static_assert(!numeric_limits_::is_integer,
"This current implementation of the octagon domain algorithms does not support raw integer types. Please use basic_float_int instead.");
//static_assert(!numeric_limits_::is_integer,
// "This current implementation of the octagon domain algorithms does not support raw integer types. Please use basic_float_int instead.");

constexpr static const bool valid = numeric_limits_::is_specialized;
constexpr static const bool integer = numeric_limits_::is_integer;
constexpr static const bool native_integer = numeric_limits_::is_integer;
constexpr static const bool valid = numeric_limits_::is_specialized && !numeric_limits_::is_integer;
constexpr static const bool is_integer = numeric_limits_::is_integer;
constexpr static const bool is_native_integer = numeric_limits_::is_integer;

constexpr static constant_type top() noexcept { return numeric_limits_::has_infinity ? numeric_limits_::infinity() : numeric_limits_::max(); }
constexpr static bool is_top(constant_type c) noexcept { return numeric_limits_::has_infinity ? numeric_limits_::infinity() <= c : numeric_limits_::max() == c; }
Expand All @@ -66,7 +66,9 @@ struct constant_limits {
template <typename ConstantType_, typename = std::enable_if_t<!std::numeric_limits<ConstantType_>::is_integer>>
constexpr static constant_type floor(ConstantType_ b) { return std::floor(b); }
static std::string to_string(constant_type value) { return std::to_string(value); }
constexpr static bool is_pair(constant_type const& c) { return is_pair_<constant_type>::is_pair(c); }
// TODO Verify if I can really consider infinity as an even number in the context of octdomain algorithms
constexpr static bool is_even(constant_type const& c) { return is_top(c) || is_even_<constant_type>::is_even(c); }
constexpr static constant_type raw_value(constant_type c) { return c; }
};


Expand Down Expand Up @@ -172,8 +174,8 @@ struct constant_limits<basic_float_int<RawType>> {
using raw_type = RawType;

constexpr static const bool valid = numeric_limits_::is_specialized;
constexpr static const bool integer = true; // This is the sole reason for basic_float_int. What a ride! :D
constexpr static const bool native_integer = false;
constexpr static const bool is_integer = true; // This is the sole reason for basic_float_int. What a ride! :D
constexpr static const bool is_native_integer = false;

constexpr static constant_type top() noexcept { return constant_type(raw_constant_limits_::top()); }
constexpr static bool is_top(constant_type c) noexcept { return raw_constant_limits_::is_top(c.value()); }
Expand All @@ -183,7 +185,9 @@ struct constant_limits<basic_float_int<RawType>> {
constexpr static constant_type min(std::initializer_list<constant_type> list) { return std::min(list); }
constexpr static constant_type floor(constant_type const& b) { return b.to_floor(); }
static std::string to_string(constant_type value) { return std::to_string(value.value()); }
constexpr static bool is_pair(constant_type const& c) { return c % 2 == 0; }
// TODO Verify if I can really consider infinity as an even number in the context of octdomain algorithms
constexpr static bool is_even(constant_type const& c) { return is_top(c) || c % 2 == 0; }
constexpr static raw_type raw_value(constant_type const& c) { return c.value(); }
};

template <typename NumberType, typename RawType> using number_float_int_result_t_ = std::enable_if_t<
Expand Down
95 changes: 27 additions & 68 deletions include/adl/oct/cpu/add_cons_close_oper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,8 @@
adl_BEGIN_MAIN_MODULE(oct)
namespace cpu {

template <typename DbmType, typename ContextType, bool UsingIntegerAlgorithm = DbmType::constant_limits::integer>
class add_cons_close_oper_impl_ {};

/*
* REAL ALGORITHM IMPLEMENTATION
*/
template <typename DbmType, typename ContextType>
class add_cons_close_oper_impl_<DbmType, ContextType, false> :
class add_cons_close_oper :
public detail_::oper_base_<cpu::add_cons_close_oper<DbmType, ContextType>, DbmType, ContextType, bool>
{
using superclass_ = detail_::oper_base_<cpu::add_cons_close_oper<DbmType, ContextType>, DbmType, ContextType, bool>;
Expand All @@ -41,26 +35,32 @@ class add_cons_close_oper_impl_<DbmType, ContextType, false> :
using constant_limits = typename dbm_type::constant_limits;
using counterpart_cons_type = typename dbm_type::counterpart_identity_cons_type;
using cons_type = typename dbm_type::identity_cons_type;
using octdiff_conjunction_type = typename dbm_type::octdiff_conjunction_type;
using octdiff_conjunction_type = typename dbm_type::identity_octdiff_conjunction_type;

constexpr static const bool is_integer = constant_limits::is_integer;

add_cons_close_oper_impl_() = delete;
add_cons_close_oper_impl_(add_cons_close_oper_impl_ const&) = delete;
add_cons_close_oper_impl_(add_cons_close_oper_impl_ &&) noexcept = default;
add_cons_close_oper_impl_& operator=(add_cons_close_oper_impl_ const&) = delete;
add_cons_close_oper_impl_& operator=(add_cons_close_oper_impl_ &&) noexcept = default;
add_cons_close_oper() = delete;
add_cons_close_oper(add_cons_close_oper const&) = delete;
add_cons_close_oper(add_cons_close_oper &&) noexcept = default;
add_cons_close_oper& operator=(add_cons_close_oper const&) = delete;
add_cons_close_oper& operator=(add_cons_close_oper &&) noexcept = default;

add_cons_close_oper_impl_(queue_type&, dbm_type& dbm, cons_type di, cons_type dj) : superclass_(), dbm_(&dbm), di_(di), dj_(dj) {}
add_cons_close_oper_impl_(queue_type&, dbm_type& dbm, cons_type cons) : add_cons_close_oper_impl_(dbm, cons, cons_type::invalid()) {}
add_cons_close_oper_impl_(queue_type&, dbm_type& dbm, counterpart_cons_type oct_cons) : add_cons_close_oper_impl_(dbm, oct_cons.split()) {}
add_cons_close_oper_impl_(queue_type&, dbm_type& dbm, octdiff_conjunction_type conj) : add_cons_close_oper_impl_(dbm, conj.di(), conj.dj()) {}
add_cons_close_oper(queue_type& queue, dbm_type& dbm, cons_type di, cons_type dj) : superclass_(), dbm_(&dbm), di_(di), dj_(dj) {}
add_cons_close_oper(queue_type& queue, dbm_type& dbm, cons_type cons) : add_cons_close_oper(queue, dbm, cons, cons_type::invalid()) {}
add_cons_close_oper(queue_type& queue, dbm_type& dbm, counterpart_cons_type oct_cons) : add_cons_close_oper(queue, dbm, oct_cons.split()) {}
add_cons_close_oper(queue_type& queue, dbm_type& dbm, octdiff_conjunction_type conj) : add_cons_close_oper(queue, dbm, conj.di(), conj.dj()) {}

bool on_execute_() {
return add_cons_(di_) && (dj_.valid() && add_cons_(dj_));
return is_integer
? add_cons_int_(di_) && (!dj_.valid() || add_cons_int_(dj_))
: add_cons_float_(di_) && (!dj_.valid() || add_cons_float_(dj_));
}

private:
bool add_cons_(cons_type cons) {

bool add_cons_float_(cons_type cons) {
using namespace adl::operators;

auto &dbm = *dbm_;
auto const d = cons.constant();
auto const a = cons.xi();
Expand Down Expand Up @@ -118,52 +118,15 @@ class add_cons_close_oper_impl_<DbmType, ContextType, false> :
}
}

if (dbm.at(i, i) < 0) return false;
if (dbm.at(i, i) < 0) {
return false;
}
}

return true;
}

private:
dbm_type * dbm_;
cons_type di_;
cons_type dj_;
};

/*
* INTEGER ALGORITHM IMPLEMENTATION
*/
template <typename DbmType, typename ContextType>
class add_cons_close_oper_impl_<DbmType, ContextType, true> :
public detail_::oper_base_<cpu::add_cons_close_oper<DbmType, ContextType>, DbmType, ContextType, bool>
{
using superclass_ = detail_::oper_base_<add_cons_close_oper<DbmType, ContextType>, DbmType, ContextType, bool>;

public:
using dbm_type = DbmType; //typename superclass_::dbm_type;
using context_type = ContextType; //typename superclass_::context_type;
using constant_limits = typename dbm_type::constant_limits;
using counterpart_cons_type = typename dbm_type::counterpart_identity_cons_type;
using cons_type = typename dbm_type::identity_cons_type;
using octdiff_conjunction_type = typename dbm_type::octdiff_conjunction_type;

add_cons_close_oper_impl_() = delete;
add_cons_close_oper_impl_(add_cons_close_oper_impl_ const&) = delete;
add_cons_close_oper_impl_(add_cons_close_oper_impl_ &&) noexcept = default;
add_cons_close_oper_impl_& operator=(add_cons_close_oper_impl_ const&) = delete;
add_cons_close_oper_impl_& operator=(add_cons_close_oper_impl_ &&) noexcept = default;

add_cons_close_oper_impl_(dbm_type& dbm, cons_type di, cons_type dj) : superclass_(), dbm_(&dbm), di_(di), dj_(dj) {}
add_cons_close_oper_impl_(dbm_type& dbm, cons_type cons) : add_cons_close_oper_impl_(dbm, cons, cons_type::invalid()) {}
add_cons_close_oper_impl_(dbm_type& dbm, counterpart_cons_type oct_cons) : add_cons_close_oper_impl_(dbm, oct_cons.split()) {}
add_cons_close_oper_impl_(dbm_type& dbm, octdiff_conjunction_type conj) : add_cons_close_oper_impl_(dbm, conj.di(), conj.dj()) {}

bool on_execute_() {
return add_cons_(di_) && (dj_.valid() && add_cons_(dj_));
}

private:
bool add_cons_(cons_type cons) {
bool add_cons_int_(cons_type cons) {
using namespace adl::operators;
auto &dbm = *dbm_;
auto const d = cons.constant();
Expand Down Expand Up @@ -223,24 +186,20 @@ class add_cons_close_oper_impl_<DbmType, ContextType, true> :
}
}

if (dbm.at(i, i) < 0) return false;
if (dbm.at(i, i) < 0) {
return false;
}
}

return true;
}

private:
dbm_type * dbm_;
dbm_type * dbm_ = nullptr;
cons_type di_;
cons_type dj_;
};

template <typename DbmType, typename ContextType>
class add_cons_close_oper : public add_cons_close_oper_impl_<DbmType, ContextType> {
public:
using add_cons_close_oper_impl_<DbmType, ContextType>::add_cons_close_oper_impl_;
};

}
adl_END_MAIN_MODULE

Expand Down
2 changes: 1 addition & 1 deletion include/adl/oct/cpu/close_oper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class close_oper : public detail_::oper_base_<cpu::close_oper<DbmType, ContextTy
close_oper& operator=(close_oper const&) = delete;
close_oper& operator=(close_oper &&) noexcept = default;

explicit close_oper(queue_type&, dbm_type& dbm) : superclass_(), dbm_(&dbm) {}
close_oper(queue_type&, dbm_type& dbm) : superclass_(), dbm_(&dbm) {}

void on_execute_() {
using namespace adl::operators;
Expand Down
2 changes: 1 addition & 1 deletion include/adl/oct/cpu/closure_oper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
adl_BEGIN_MAIN_MODULE(oct)
namespace cpu {

template <typename DbmType, typename ContextType, bool UsingIntegerAlgorithm = DbmType::constant_limits::integer>
template <typename DbmType, typename ContextType, bool UsingIntegerAlgorithm = DbmType::constant_limits::is_integer>
class closure_oper_impl_ {};

/*
Expand Down
22 changes: 11 additions & 11 deletions include/adl/oct/cpu/oper_base_.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,19 @@ namespace cpu {
static std::enable_if_t<!has_ ## FunctionName ## _function_v<U>, ReturnType> \
do_ ## FunctionName(U& oper, Args...) { return (ReturnType) 0; }


namespace detail_ {

template <typename DbmType, typename ContextType, typename ResultType>
class oper_base_traits_ {
public:
adl_MAKE_FUNCTION_CHECK_CLASS(void, on_prepare_);
adl_MAKE_FUNCTION_CHECK_CLASS(ResultType, on_execute_);
adl_MAKE_FUNCTION_CHECK_CLASS(void, on_finished_);
};

template <typename DbmType, typename ContextType>
class oper_base_traits_<DbmType, ContextType, void> {
public:
adl_MAKE_FUNCTION_CHECK_CLASS(void, on_prepare_);
adl_MAKE_FUNCTION_CHECK_CLASS(void, on_execute_);
adl_MAKE_FUNCTION_CHECK_CLASS(void, on_finished_);
};

Expand All @@ -75,6 +72,9 @@ class oper_base_seq_ : public crtp_base<SubType> {
using traits_ = oper_base_traits_<DbmType, ContextType, ResultType>;
using timer_type_ = static_mark_timer<5>;

//static_assert(traits_::template has_on_execute__function_v<SubType>,
// "Please provide an implementation for a function named 'on_execute_' for this operator.");

public:
using context_type = ContextType;
using dbm_type = DbmType;
Expand Down Expand Up @@ -142,16 +142,16 @@ class oper_base_<SubType, DbmType, seq_context, ResultType> : public detail_::op
this->state_ = oper_state::queued;
this->timer_.mark(); // 1
case oper_state::queued:
traits_::do_on_prepare_(static_cast<SubType&>(*this));
traits_::do_on_prepare_(this->as_subclass_());
this->state_ = oper_state::prepared;
this->timer_.mark(); // 2
// non-stop
case oper_state::prepared: {
this->state_ = oper_state::started;
this->timer_.mark(); // 3
auto ret = traits_::do_on_execute_(static_cast<SubType&>(*this));
auto ret = this->as_subclass_().on_execute_();
this->state_ = oper_state::finished;
traits_::do_on_finished_(static_cast<SubType&>(*this));
traits_::do_on_finished_(this->as_subclass_());
this->timer_.mark(); // 4
return ret;
}
Expand All @@ -162,7 +162,7 @@ class oper_base_<SubType, DbmType, seq_context, ResultType> : public detail_::op

subtype_& discard() {
get();
return static_cast<SubType&>(*this);
return this->as_subclass_();
}
};

Expand Down Expand Up @@ -194,16 +194,16 @@ class oper_base_<SubType, DbmType, seq_context, void> : public detail_::oper_bas
this->state_ = oper_state::queued;
this->timer_.mark(); // 1
case oper_state::queued:
traits_::do_on_prepare_(static_cast<SubType&>(*this));
traits_::do_on_prepare_(this->as_subclass_());
this->state_ = oper_state::prepared;
this->timer_.mark(); // 2
// non-stop
case oper_state::prepared:
this->state_ = oper_state::started;
this->timer_.mark(); // 3
traits_::do_on_execute_(static_cast<SubType&>(*this));
this->as_subclass_().on_execute_();
this->state_ = oper_state::finished;
traits_::do_on_finished_(static_cast<SubType&>(*this));
traits_::do_on_finished_(this->as_subclass_());
this->timer_.mark(); // 4
break;
default:
Expand All @@ -213,7 +213,7 @@ class oper_base_<SubType, DbmType, seq_context, void> : public detail_::oper_bas

subtype_& discard() {
get();
return static_cast<SubType&>(*this);
return this->as_subclass_();
};
};

Expand Down
Loading

0 comments on commit 66919ba

Please sign in to comment.