separate_interval_set.hpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*-----------------------------------------------------------------------------+
  2. Copyright (c) 2007-2009: Joachim Faulhaber
  3. +------------------------------------------------------------------------------+
  4. Distributed under the Boost Software License, Version 1.0.
  5. (See accompanying file LICENCE.txt or copy at
  6. http://www.boost.org/LICENSE_1_0.txt)
  7. +-----------------------------------------------------------------------------*/
  8. #ifndef BOOST_ICL_SEPARATE_INTERVAL_SET_HPP_JOFA_080608
  9. #define BOOST_ICL_SEPARATE_INTERVAL_SET_HPP_JOFA_080608
  10. #include <boost/assert.hpp>
  11. #include <boost/icl/type_traits/is_interval_separator.hpp>
  12. #include <boost/icl/interval_base_set.hpp>
  13. #include <boost/icl/interval_set.hpp>
  14. namespace boost{namespace icl
  15. {
  16. /** \brief Implements a set as a set of intervals - leaving adjoining intervals separate */
  17. template
  18. <
  19. typename DomainT,
  20. ICL_COMPARE Compare = ICL_COMPARE_INSTANCE(ICL_COMPARE_DEFAULT, DomainT),
  21. ICL_INTERVAL(ICL_COMPARE) Interval = ICL_INTERVAL_INSTANCE(ICL_INTERVAL_DEFAULT, DomainT, Compare),
  22. ICL_ALLOC Alloc = std::allocator
  23. >
  24. class separate_interval_set:
  25. public interval_base_set<separate_interval_set<DomainT,Compare,Interval,Alloc>,
  26. DomainT,Compare,Interval,Alloc>
  27. {
  28. public:
  29. typedef separate_interval_set<DomainT,Compare,Interval,Alloc> type;
  30. typedef interval_base_set<type,DomainT,Compare,Interval,Alloc> base_type;
  31. typedef type overloadable_type;
  32. typedef type key_object_type;
  33. typedef interval_set<DomainT,Compare,Interval,Alloc> joint_type;
  34. /// The domain type of the set
  35. typedef DomainT domain_type;
  36. /// The codomaintype is the same as domain_type
  37. typedef DomainT codomain_type;
  38. /// The element type of the set
  39. typedef DomainT element_type;
  40. /// The interval type of the set
  41. typedef ICL_INTERVAL_TYPE(Interval,DomainT,Compare) interval_type;
  42. /// The segment type of the set
  43. typedef interval_type segment_type;
  44. /// Comparison functor for domain values
  45. typedef ICL_COMPARE_DOMAIN(Compare,DomainT) domain_compare;
  46. /// Comparison functor for intervals
  47. typedef exclusive_less_than<interval_type> interval_compare;
  48. /// Comparison functor for keys
  49. typedef exclusive_less_than<interval_type> key_compare;
  50. /// The allocator type of the set
  51. typedef Alloc<interval_type> allocator_type;
  52. /// allocator type of the corresponding element set
  53. typedef Alloc<DomainT> domain_allocator_type;
  54. /// The corresponding atomized type representing this interval container of elements
  55. typedef typename base_type::atomized_type atomized_type;
  56. /// Container type for the implementation
  57. typedef typename base_type::ImplSetT ImplSetT;
  58. /// key type of the implementing container
  59. typedef typename ImplSetT::key_type key_type;
  60. /// data type of the implementing container
  61. typedef typename ImplSetT::value_type data_type;
  62. /// value type of the implementing container
  63. typedef typename ImplSetT::value_type value_type;
  64. /// iterator for iteration over intervals
  65. typedef typename ImplSetT::iterator iterator;
  66. /// const_iterator for iteration over intervals
  67. typedef typename ImplSetT::const_iterator const_iterator;
  68. enum { fineness = 2 };
  69. public:
  70. //==========================================================================
  71. //= Construct, copy, destruct
  72. //==========================================================================
  73. /// Default constructor for the empty object
  74. separate_interval_set(): base_type() {}
  75. /// Copy constructor
  76. separate_interval_set(const separate_interval_set& src): base_type(src) {}
  77. /// Copy constructor for base_type
  78. template<class SubType>
  79. separate_interval_set
  80. (const interval_base_set<SubType,DomainT,Compare,Interval,Alloc>& src)
  81. {
  82. this->assign(src);
  83. }
  84. /// Constructor for a single element
  85. explicit separate_interval_set(const domain_type& elem): base_type() { this->add(elem); }
  86. /// Constructor for a single interval
  87. explicit separate_interval_set(const interval_type& itv): base_type() { this->add(itv); }
  88. /// Assignment operator
  89. separate_interval_set& operator = (const separate_interval_set& src)
  90. {
  91. base_type::operator=(src);
  92. return *this;
  93. }
  94. /// Assignment operator for base type
  95. template<class SubType>
  96. separate_interval_set& operator =
  97. (const interval_base_set<SubType,DomainT,Compare,Interval,Alloc>& src)
  98. {
  99. this->assign(src);
  100. return *this;
  101. }
  102. /// Assignment from a base interval_set.
  103. template<class SubType>
  104. void assign(const interval_base_set<SubType,DomainT,Compare,Interval,Alloc>& src)
  105. {
  106. this->clear();
  107. this->_set.insert(src.begin(), src.end());
  108. }
  109. # ifndef BOOST_ICL_NO_CXX11_RVALUE_REFERENCES
  110. //==========================================================================
  111. //= Move semantics
  112. //==========================================================================
  113. /// Move constructor
  114. separate_interval_set(separate_interval_set&& src)
  115. : base_type(boost::move(src))
  116. {}
  117. /// Move assignment operator
  118. separate_interval_set& operator = (separate_interval_set&& src)
  119. {
  120. base_type::operator=(boost::move(src));
  121. return *this;
  122. }
  123. //==========================================================================
  124. # endif // BOOST_ICL_NO_CXX11_RVALUE_REFERENCES
  125. private:
  126. // Private functions that shall be accessible by the baseclass:
  127. friend class
  128. interval_base_set<separate_interval_set<DomainT,Compare,Interval,Alloc>,
  129. DomainT,Compare,Interval,Alloc>;
  130. iterator handle_inserted(iterator inserted_)
  131. {
  132. return inserted_;
  133. }
  134. iterator add_over(const interval_type& addend, iterator last_)
  135. {
  136. return segmental::join_under(*this, addend, last_);
  137. }
  138. iterator add_over(const interval_type& addend)
  139. {
  140. return segmental::join_under(*this, addend);
  141. }
  142. } ;
  143. //-----------------------------------------------------------------------------
  144. // type traits
  145. //-----------------------------------------------------------------------------
  146. template <class DomainT, ICL_COMPARE Compare, ICL_INTERVAL(ICL_COMPARE) Interval, ICL_ALLOC Alloc>
  147. struct is_set<icl::separate_interval_set<DomainT,Compare,Interval,Alloc> >
  148. {
  149. typedef is_set<icl::separate_interval_set<DomainT,Compare,Interval,Alloc> > type;
  150. BOOST_STATIC_CONSTANT(bool, value = true);
  151. };
  152. template <class DomainT, ICL_COMPARE Compare, ICL_INTERVAL(ICL_COMPARE) Interval, ICL_ALLOC Alloc>
  153. struct is_interval_container<icl::separate_interval_set<DomainT,Compare,Interval,Alloc> >
  154. {
  155. typedef is_interval_container<icl::separate_interval_set<DomainT,Compare,Interval,Alloc> > type;
  156. BOOST_STATIC_CONSTANT(bool, value = true);
  157. };
  158. template <class DomainT, ICL_COMPARE Compare, ICL_INTERVAL(ICL_COMPARE) Interval, ICL_ALLOC Alloc>
  159. struct is_interval_separator<icl::separate_interval_set<DomainT,Compare,Interval,Alloc> >
  160. {
  161. typedef is_interval_separator<icl::separate_interval_set<DomainT,Compare,Interval,Alloc> > type;
  162. BOOST_STATIC_CONSTANT(bool, value = true);
  163. };
  164. //-----------------------------------------------------------------------------
  165. // type representation
  166. //-----------------------------------------------------------------------------
  167. template <class DomainT, ICL_COMPARE Compare, ICL_INTERVAL(ICL_COMPARE) Interval, ICL_ALLOC Alloc>
  168. struct type_to_string<icl::separate_interval_set<DomainT,Compare,Interval,Alloc> >
  169. {
  170. static std::string apply()
  171. { return "se_itv_set<"+ type_to_string<DomainT>::apply() +">"; }
  172. };
  173. }} // namespace icl boost
  174. #endif