order_type.hpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. //////////////////////////////////////////////////////////////////////////////
  2. //
  3. // (C) Copyright Ion Gaztanaga 2015-2016.
  4. // Distributed under the Boost Software License, Version 1.0.
  5. // (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. // See http://www.boost.org/libs/move for documentation.
  9. //
  10. //////////////////////////////////////////////////////////////////////////////
  11. #ifndef BOOST_MOVE_TEST_ORDER_TYPE_HPP
  12. #define BOOST_MOVE_TEST_ORDER_TYPE_HPP
  13. #include <boost/config.hpp>
  14. #include <boost/move/core.hpp>
  15. #include <boost/move/detail/iterator_traits.hpp>
  16. #include <cstddef>
  17. #include <cstdio>
  18. struct order_perf_type
  19. {
  20. public:
  21. std::size_t key;
  22. std::size_t val;
  23. order_perf_type()
  24. {
  25. ++num_elements;
  26. }
  27. order_perf_type(const order_perf_type& other)
  28. : key(other.key), val(other.val)
  29. {
  30. ++num_elements;
  31. ++num_copy;
  32. }
  33. order_perf_type & operator=(const order_perf_type& other)
  34. {
  35. ++num_copy;
  36. key = other.key;
  37. val = other.val;
  38. return *this;
  39. }
  40. ~order_perf_type ()
  41. {
  42. --num_elements;
  43. }
  44. static void reset_stats()
  45. {
  46. num_compare=0;
  47. num_copy=0;
  48. }
  49. friend bool operator< (const order_perf_type& left, const order_perf_type& right)
  50. { ++num_compare; return left.key < right.key; }
  51. static boost::ulong_long_type num_compare;
  52. static boost::ulong_long_type num_copy;
  53. static boost::ulong_long_type num_elements;
  54. };
  55. boost::ulong_long_type order_perf_type::num_compare = 0;
  56. boost::ulong_long_type order_perf_type::num_copy = 0;
  57. boost::ulong_long_type order_perf_type::num_elements = 0;
  58. struct order_move_type
  59. {
  60. BOOST_MOVABLE_BUT_NOT_COPYABLE(order_move_type)
  61. public:
  62. std::size_t key;
  63. std::size_t val;
  64. static const std::size_t moved_constr_mark = std::size_t(-1);
  65. static const std::size_t moved_assign_mark = std::size_t(-2);
  66. order_move_type()
  67. : key(0u), val(0u)
  68. {}
  69. order_move_type(BOOST_RV_REF(order_move_type) other)
  70. : key(other.key), val(other.val)
  71. {
  72. other.key = other.val = std::size_t(-1);
  73. }
  74. order_move_type & operator=(BOOST_RV_REF(order_move_type) other)
  75. {
  76. key = other.key;
  77. val = other.val;
  78. other.key = other.val = std::size_t(-2);
  79. return *this;
  80. }
  81. friend bool operator< (const order_move_type& left, const order_move_type& right)
  82. { return left.key < right.key; }
  83. ~order_move_type ()
  84. {
  85. key = val = std::size_t(-3);
  86. }
  87. };
  88. struct order_type_less
  89. {
  90. template<class T, class U>
  91. bool operator()(const T &a, U const &b) const
  92. { return a < b; }
  93. };
  94. template<class T>
  95. inline bool is_order_type_ordered(T *elements, std::size_t element_count, bool stable = true)
  96. {
  97. for(std::size_t i = 1; i < element_count; ++i){
  98. if(order_type_less()(elements[i], elements[i-1])){
  99. std::printf("\n Ord KO !!!!");
  100. return false;
  101. }
  102. if( stable && !(order_type_less()(elements[i-1], elements[i])) && (elements[i-1].val > elements[i].val) ){
  103. std::printf("\n Stb KO !!!! ");
  104. return false;
  105. }
  106. }
  107. return true;
  108. }
  109. namespace boost {
  110. namespace movelib {
  111. namespace detail_adaptive {
  112. }}}
  113. template<class T>
  114. inline bool is_key(T *elements, std::size_t element_count)
  115. {
  116. for(std::size_t i = 1; i < element_count; ++i){
  117. if(elements[i].key >= element_count){
  118. std::printf("\n Key.key KO !!!!");
  119. return false;
  120. }
  121. if(elements[i].val != std::size_t(-1)){
  122. std::printf("\n Key.val KO !!!!");
  123. return false;
  124. }
  125. }
  126. return true;
  127. }
  128. template<class T>
  129. inline bool is_buffer(T *elements, std::size_t element_count)
  130. {
  131. for(std::size_t i = 1; i < element_count; ++i){
  132. if(elements[i].key != std::size_t(-1)){
  133. std::printf("\n Buf.key KO !!!!");
  134. return false;
  135. }
  136. if(elements[i].val >= element_count){
  137. std::printf("\n Buf.val KO !!!!");
  138. return false;
  139. }
  140. }
  141. return true;
  142. }
  143. //size_type iterator
  144. template <class T, class D>
  145. class randit
  146. {
  147. public:
  148. typedef std::random_access_iterator_tag iterator_category;
  149. typedef T value_type;
  150. typedef D difference_type;
  151. typedef T* pointer;
  152. typedef T& reference;
  153. private:
  154. T* m_ptr;
  155. public:
  156. explicit randit(T* ptr)
  157. : m_ptr(ptr)
  158. {}
  159. public:
  160. //Constructors
  161. randit()
  162. : m_ptr() //Value initialization to achieve "null iterators" (N3644)
  163. {}
  164. randit(const randit& other)
  165. : m_ptr(other.m_ptr)
  166. {}
  167. randit & operator=(const randit& other)
  168. { m_ptr = other.m_ptr; return *this; }
  169. //T* like operators
  170. reference operator*() const
  171. { return *m_ptr; }
  172. pointer operator->() const
  173. { return m_ptr; }
  174. reference operator[](difference_type off) const
  175. { return m_ptr[off]; }
  176. //Increment / Decrement
  177. randit& operator++()
  178. { ++m_ptr; return *this; }
  179. randit operator++(int)
  180. { return randit(m_ptr++); }
  181. randit& operator--()
  182. { --m_ptr; return *this; }
  183. randit operator--(int)
  184. { return randit(m_ptr--); }
  185. //Arithmetic
  186. randit& operator+=(difference_type off)
  187. { m_ptr += off; return *this; }
  188. randit& operator-=(difference_type off)
  189. { m_ptr -= off; return *this; }
  190. friend randit operator+(const randit &x, difference_type off)
  191. { return randit(x.m_ptr+off); }
  192. friend randit operator+(difference_type off, randit right)
  193. { right.m_ptr += off; return right; }
  194. friend randit operator-(randit left, difference_type off)
  195. { left.m_ptr -= off; return left; }
  196. friend difference_type operator-(const randit &left, const randit& right)
  197. { return difference_type(left.m_ptr - right.m_ptr); }
  198. //Comparison operators
  199. friend bool operator== (const randit& l, const randit& r)
  200. { return l.m_ptr == r.m_ptr; }
  201. friend bool operator!= (const randit& l, const randit& r)
  202. { return l.m_ptr != r.m_ptr; }
  203. friend bool operator< (const randit& l, const randit& r)
  204. { return l.m_ptr < r.m_ptr; }
  205. friend bool operator<= (const randit& l, const randit& r)
  206. { return l.m_ptr <= r.m_ptr; }
  207. friend bool operator> (const randit& l, const randit& r)
  208. { return l.m_ptr > r.m_ptr; }
  209. friend bool operator>= (const randit& l, const randit& r)
  210. { return l.m_ptr >= r.m_ptr; }
  211. };
  212. struct less_int
  213. {
  214. bool operator()(int l, int r)
  215. { return l < r; }
  216. };
  217. #endif //BOOST_MOVE_TEST_ORDER_TYPE_HPP