test_value_maker.hpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /*-----------------------------------------------------------------------------+
  2. Copyright (c) 2008-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_TEST_VALUE_MAKER_HPP_JOFA_080916
  9. #define BOOST_ICL_TEST_VALUE_MAKER_HPP_JOFA_080916
  10. #include <boost/icl/type_traits/identity_element.hpp>
  11. #include <boost/icl/interval_bounds.hpp>
  12. namespace boost{ namespace icl
  13. {
  14. struct mono
  15. {
  16. mono(){};
  17. mono& operator ++ (){ return *this; }
  18. mono& operator -- (){ return *this; }
  19. mono& operator += (const mono&){ return *this; }
  20. };
  21. bool operator == (const mono&, const mono&){ return true; }
  22. bool operator < (const mono&, const mono&){ return false; }
  23. template<class CharType, class CharTraits>
  24. std::basic_ostream<CharType, CharTraits>&
  25. operator << (std::basic_ostream<CharType, CharTraits>& stream, const mono& object)
  26. {
  27. return stream << "*";
  28. }
  29. template <class BicrementableT>
  30. BicrementableT make(int n)
  31. {
  32. BicrementableT value = identity_element<BicrementableT>::value();
  33. if(n>=0)
  34. for(int i=0; i<n; i++)
  35. ++value;
  36. else
  37. for(int i=0; i>n; i--)
  38. --value;
  39. return value;
  40. }
  41. template <class Type>
  42. struct test_value;
  43. template<>
  44. struct test_value<std::string>
  45. {
  46. static std::string make(int n)
  47. {
  48. std::string value = identity_element<std::string>::value();
  49. int abs_n = n<0 ? -n : n;
  50. for(int i=1; i<abs_n; i++)
  51. value += (i%2==1 ? "hello " : "world ");
  52. return value;
  53. }
  54. };
  55. template <class Type>
  56. struct test_value<Type*>
  57. {
  58. static bool map_integers(Type values[], int size)
  59. {
  60. static const int offset = size/2;
  61. for(int idx = 0; idx < size; idx++)
  62. values[idx] = test_value<Type>::make(idx - offset);
  63. return true;
  64. }
  65. static Type* make(int n)
  66. {
  67. static bool initialized;
  68. static const int size = 100;
  69. static const int offset = size/2;
  70. static Type values[size];
  71. if(!initialized)
  72. initialized = map_integers(values, size);
  73. Type* value = values + offset;
  74. if(n>=0)
  75. for(int i=0; i<n; i++)
  76. ++value;
  77. else
  78. for(int i=0; i>n; i--)
  79. --value;
  80. return value;
  81. }
  82. };
  83. template <class Type>
  84. struct test_value
  85. {
  86. static Type make(int n)
  87. {
  88. Type value = identity_element<Type>::value();
  89. if(n>=0)
  90. for(int i=0; i<n; i++)
  91. ++value;
  92. else
  93. for(int i=0; i>n; i--)
  94. --value;
  95. return value;
  96. }
  97. };
  98. template <class ItvMapT>
  99. struct map_val
  100. {
  101. typedef typename ItvMapT::domain_type domain_type;
  102. typedef typename ItvMapT::codomain_type codomain_type;
  103. typedef typename ItvMapT::interval_type interval_type;
  104. typedef typename ItvMapT::value_type value_type;
  105. typedef typename ItvMapT::segment_type segment_type;
  106. typedef typename ItvMapT::domain_mapping_type domain_mapping_type;
  107. typedef std::pair<domain_type, codomain_type> std_pair_type;
  108. static segment_type mk_segment(const interval_type& inter_val, int val)
  109. {
  110. return segment_type(inter_val, test_value<codomain_type>::make(val));
  111. }
  112. /*CL?
  113. static interval_type interval_(int lower, int upper, int bounds = 2)
  114. {
  115. return segment_type(inter_val, test_value<codomain_type>::make(val));
  116. }
  117. static segment_type val_pair(int lower, int upper, int val, int bounds = 2)
  118. {
  119. return segment_type( interval_(lower, upper, static_cast<bound_type>(bounds)),
  120. test_value<codomain_type>::make(val) );
  121. }
  122. */
  123. static domain_mapping_type map_pair(int key, int val)
  124. {
  125. return domain_mapping_type(test_value< domain_type>::make(key),
  126. test_value<codomain_type>::make(val));
  127. }
  128. static std_pair_type std_pair(int key, int val)
  129. {
  130. return std_pair_type(test_value< domain_type>::make(key),
  131. test_value<codomain_type>::make(val));
  132. }
  133. };
  134. // Very short value denotation for intervals
  135. // Assumption typename T and IntervalT exists in scope
  136. //I_I : [a,b]
  137. #define I_I(low,up) icl::interval<T>::closed (test_value<T>::make(low), test_value<T>::make(up))
  138. //I_D : [a,b)
  139. #define I_D(low,up) icl::interval<T>::right_open(test_value<T>::make(low), test_value<T>::make(up))
  140. //C_I : (a,b]
  141. #define C_I(low,up) icl::interval<T>::left_open (test_value<T>::make(low), test_value<T>::make(up))
  142. //C_D : (a,b)
  143. #define C_D(low,up) icl::interval<T>::open (test_value<T>::make(low), test_value<T>::make(up))
  144. #define MK_I(ItvT,low,up) ItvT(test_value<T>::make(low), test_value<T>::make(up))
  145. #define MK_v(key) test_value<T>::make(key)
  146. #define MK_u(key) test_value<U>::make(key)
  147. // Very short value denotation for interval value pairs
  148. // Assumption typename IntervalMapT existes in scope
  149. #define IIv(low,up,val) map_val<IntervalMapT>::mk_segment(I_I(low,up), val)
  150. #define IDv(low,up,val) map_val<IntervalMapT>::mk_segment(I_D(low,up), val)
  151. #define CIv(low,up,val) map_val<IntervalMapT>::mk_segment(C_I(low,up), val)
  152. #define CDv(low,up,val) map_val<IntervalMapT>::mk_segment(C_D(low,up), val)
  153. #define K_v(key,val) map_val<IntervalMapT>::map_pair(key,val)
  154. #define sK_v(key,val) map_val<IntervalMapT>::std_pair(key,val)
  155. #define MK_seg(itv,val) map_val<IntervalMapT>::mk_segment(itv, val)
  156. }} // namespace boost icl
  157. #endif