/*-----------------------------------------------------------------------------+ Copyright (c) 2010-2010: Joachim Faulhaber +------------------------------------------------------------------------------+ Distributed under the Boost Software License, Version 1.0. (See accompanying file LICENCE.txt or copy at http://www.boost.org/LICENSE_1_0.txt) +-----------------------------------------------------------------------------*/ #ifndef BOOST_ICL_DISCRETE_INTERVAL_HPP_JOFA_100403 #define BOOST_ICL_DISCRETE_INTERVAL_HPP_JOFA_100403 #include #include #include #include #include #include #include #include #include #include #include namespace boost{namespace icl { template class discrete_interval { public: typedef discrete_interval type; typedef DomainT domain_type; typedef ICL_COMPARE_DOMAIN(Compare,DomainT) domain_compare; typedef typename bounded_value::type bounded_domain_type; public: //========================================================================== //= Construct, copy, destruct //========================================================================== /** Default constructor; yields an empty interval [0,0). */ discrete_interval() : _lwb(identity_element::value()), _upb(identity_element::value()) , _bounds(interval_bounds::right_open()) { BOOST_CONCEPT_ASSERT((DefaultConstructibleConcept)); BOOST_CONCEPT_ASSERT((LessThanComparableConcept)); BOOST_STATIC_ASSERT((icl::is_discrete::value)); } //NOTE: Compiler generated copy constructor is used /** Constructor for a closed singleton interval [val,val] */ explicit discrete_interval(const DomainT& val) : _lwb(val), _upb(val), _bounds(interval_bounds::closed()) { BOOST_CONCEPT_ASSERT((DefaultConstructibleConcept)); BOOST_CONCEPT_ASSERT((LessThanComparableConcept)); BOOST_STATIC_ASSERT((icl::is_discrete::value)); } /** Interval from low to up with bounds bounds */ discrete_interval(const DomainT& low, const DomainT& up, interval_bounds bounds = interval_bounds::right_open(), discrete_interval* = 0) : _lwb(low), _upb(up), _bounds(bounds) { BOOST_CONCEPT_ASSERT((DefaultConstructibleConcept)); BOOST_CONCEPT_ASSERT((LessThanComparableConcept)); BOOST_STATIC_ASSERT((icl::is_discrete::value)); } domain_type lower()const { return _lwb; } domain_type upper()const { return _upb; } interval_bounds bounds()const{ return _bounds; } static discrete_interval open (const DomainT& lo, const DomainT& up){ return discrete_interval(lo, up, interval_bounds::open()); } static discrete_interval right_open(const DomainT& lo, const DomainT& up){ return discrete_interval(lo, up, interval_bounds::right_open());} static discrete_interval left_open (const DomainT& lo, const DomainT& up){ return discrete_interval(lo, up, interval_bounds::left_open()); } static discrete_interval closed (const DomainT& lo, const DomainT& up){ return discrete_interval(lo, up, interval_bounds::closed()); } private: domain_type _lwb; domain_type _upb; interval_bounds _bounds; }; //============================================================================== //=T discrete_interval -> concept intervals //============================================================================== template struct interval_traits< icl::discrete_interval > { typedef interval_traits type; typedef DomainT domain_type; typedef ICL_COMPARE_DOMAIN(Compare,DomainT) domain_compare; typedef icl::discrete_interval interval_type; static interval_type construct(const domain_type& lo, const domain_type& up) { return interval_type(lo, up); } static domain_type lower(const interval_type& inter_val){ return inter_val.lower(); }; static domain_type upper(const interval_type& inter_val){ return inter_val.upper(); }; }; //============================================================================== //=T discrete_interval -> concept dynamic_interval_traits //============================================================================== template struct dynamic_interval_traits > { typedef dynamic_interval_traits type; typedef boost::icl::discrete_interval interval_type; typedef DomainT domain_type; typedef ICL_COMPARE_DOMAIN(Compare,DomainT) domain_compare; static interval_type construct(const domain_type& lo, const domain_type& up, interval_bounds bounds) { return interval_type(lo, up, bounds, static_cast(0) ); } static interval_type construct_bounded(const bounded_value& lo, const bounded_value& up) { return interval_type ( lo.value(), up.value(), lo.bound().left() | up.bound().right(), static_cast(0) ); } }; //============================================================================== //= Type traits //============================================================================== template struct interval_bound_type< discrete_interval > { typedef interval_bound_type type; BOOST_STATIC_CONSTANT(bound_type, value = interval_bounds::dynamic); }; template struct is_discrete_interval > { typedef is_discrete_interval > type; BOOST_STATIC_CONSTANT(bool, value = is_discrete::value); }; template struct type_to_string > { static std::string apply() { return "dI<"+ type_to_string::apply() +">"; } }; template struct value_size > { static std::size_t apply(const icl::discrete_interval&) { return 2; } }; }} // namespace icl boost #endif