separate_interval_set.hpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 from a base interval_set.
  89. template<class SubType>
  90. void assign(const interval_base_set<SubType,DomainT,Compare,Interval,Alloc>& src)
  91. {
  92. this->clear();
  93. this->_set.insert(src.begin(), src.end());
  94. }
  95. /// Assignment operator for base type
  96. template<class SubType>
  97. separate_interval_set& operator =
  98. (const interval_base_set<SubType,DomainT,Compare,Interval,Alloc>& src)
  99. {
  100. this->assign(src);
  101. return *this;
  102. }
  103. # ifndef BOOST_ICL_NO_CXX11_RVALUE_REFERENCES
  104. //==========================================================================
  105. //= Move semantics
  106. //==========================================================================
  107. /// Move constructor
  108. separate_interval_set(separate_interval_set&& src)
  109. : base_type(boost::move(src))
  110. {}
  111. /// Move assignment operator
  112. separate_interval_set& operator = (separate_interval_set src)
  113. {
  114. base_type::operator=(boost::move(src));
  115. return *this;
  116. }
  117. //==========================================================================
  118. # else
  119. /// Assignment operator
  120. separate_interval_set& operator = (const separate_interval_set& src)
  121. {
  122. base_type::operator=(src);
  123. return *this;
  124. }
  125. # endif // BOOST_ICL_NO_CXX11_RVALUE_REFERENCES
  126. private:
  127. // Private functions that shall be accessible by the baseclass:
  128. friend class
  129. interval_base_set<separate_interval_set<DomainT,Compare,Interval,Alloc>,
  130. DomainT,Compare,Interval,Alloc>;
  131. iterator handle_inserted(iterator inserted_)
  132. {
  133. return inserted_;
  134. }
  135. iterator add_over(const interval_type& addend, iterator last_)
  136. {
  137. return segmental::join_under(*this, addend, last_);
  138. }
  139. iterator add_over(const interval_type& addend)
  140. {
  141. return segmental::join_under(*this, addend);
  142. }
  143. } ;
  144. //-----------------------------------------------------------------------------
  145. // type traits
  146. //-----------------------------------------------------------------------------
  147. template <class DomainT, ICL_COMPARE Compare, ICL_INTERVAL(ICL_COMPARE) Interval, ICL_ALLOC Alloc>
  148. struct is_set<icl::separate_interval_set<DomainT,Compare,Interval,Alloc> >
  149. {
  150. typedef is_set<icl::separate_interval_set<DomainT,Compare,Interval,Alloc> > type;
  151. BOOST_STATIC_CONSTANT(bool, value = true);
  152. };
  153. template <class DomainT, ICL_COMPARE Compare, ICL_INTERVAL(ICL_COMPARE) Interval, ICL_ALLOC Alloc>
  154. struct is_interval_container<icl::separate_interval_set<DomainT,Compare,Interval,Alloc> >
  155. {
  156. typedef is_interval_container<icl::separate_interval_set<DomainT,Compare,Interval,Alloc> > type;
  157. BOOST_STATIC_CONSTANT(bool, value = true);
  158. };
  159. template <class DomainT, ICL_COMPARE Compare, ICL_INTERVAL(ICL_COMPARE) Interval, ICL_ALLOC Alloc>
  160. struct is_interval_separator<icl::separate_interval_set<DomainT,Compare,Interval,Alloc> >
  161. {
  162. typedef is_interval_separator<icl::separate_interval_set<DomainT,Compare,Interval,Alloc> > type;
  163. BOOST_STATIC_CONSTANT(bool, value = true);
  164. };
  165. //-----------------------------------------------------------------------------
  166. // type representation
  167. //-----------------------------------------------------------------------------
  168. template <class DomainT, ICL_COMPARE Compare, ICL_INTERVAL(ICL_COMPARE) Interval, ICL_ALLOC Alloc>
  169. struct type_to_string<icl::separate_interval_set<DomainT,Compare,Interval,Alloc> >
  170. {
  171. static std::string apply()
  172. { return "se_itv_set<"+ type_to_string<DomainT>::apply() +">"; }
  173. };
  174. }} // namespace icl boost
  175. #endif