rtree_move_pack.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. // Boost.Geometry Index
  2. // Unit Test
  3. // Copyright (c) 2015 Adam Wulkiewicz, Lodz, Poland.
  4. // Use, modification and distribution is subject to the Boost Software License,
  5. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. #include <rtree/test_rtree.hpp>
  8. #include <algorithm>
  9. #include <boost/container/vector.hpp>
  10. #include <boost/move/move.hpp>
  11. #include <boost/move/iterator.hpp>
  12. #include <boost/geometry/geometries/register/point.hpp>
  13. class point_cm
  14. {
  15. BOOST_COPYABLE_AND_MOVABLE(point_cm)
  16. public:
  17. point_cm(double xx = 0, double yy = 0)
  18. : x(xx)
  19. , y(yy)
  20. , moved(false)
  21. {}
  22. point_cm(point_cm const& other)
  23. : x(other.x)
  24. , y(other.y)
  25. , moved(false)
  26. {
  27. BOOST_CHECK_MESSAGE(false, "copy not allowed");
  28. }
  29. point_cm & operator=(BOOST_COPY_ASSIGN_REF(point_cm) other)
  30. {
  31. BOOST_CHECK_MESSAGE(false, "copy not allowed");
  32. x = other.x;
  33. y = other.y;
  34. moved = false;
  35. return *this;
  36. }
  37. point_cm(BOOST_RV_REF(point_cm) other)
  38. : x(other.x)
  39. , y(other.y)
  40. , moved(false)
  41. {
  42. BOOST_CHECK_MESSAGE(!other.moved, "only one move allowed");
  43. other.moved = true;
  44. }
  45. point_cm & operator=(BOOST_RV_REF(point_cm) other)
  46. {
  47. BOOST_CHECK_MESSAGE(!other.moved, "only one move allowed");
  48. x = other.x;
  49. y = other.y;
  50. moved = false;
  51. other.moved = true;
  52. return *this;
  53. }
  54. double x, y;
  55. bool moved;
  56. };
  57. template <typename Point>
  58. struct indexable
  59. {
  60. typedef Point const& result_type;
  61. result_type operator()(Point const& p) const
  62. {
  63. BOOST_CHECK_MESSAGE(!p.moved, "can't access indexable of moved Value");
  64. return p;
  65. }
  66. };
  67. BOOST_GEOMETRY_REGISTER_POINT_2D(point_cm, double, bg::cs::cartesian, x, y)
  68. template <typename Vector>
  69. void append(Vector & vec, double x, double y)
  70. {
  71. point_cm pt(x, y);
  72. BOOST_CHECK(!pt.moved);
  73. vec.push_back(boost::move(pt));
  74. BOOST_CHECK(pt.moved);
  75. }
  76. struct test_moved
  77. {
  78. test_moved(bool ex)
  79. : expected(ex)
  80. {}
  81. template <typename Point>
  82. void operator()(Point const& p) const
  83. {
  84. BOOST_CHECK_EQUAL(p.moved, expected);
  85. }
  86. bool expected;
  87. };
  88. template <typename Point, typename Params>
  89. void test_rtree(Params const& params = Params())
  90. {
  91. // sanity check
  92. boost::container::vector<Point> vec;
  93. append(vec, 0, 0); append(vec, 0, 1); append(vec, 0, 2);
  94. append(vec, 1, 0); append(vec, 1, 1); append(vec, 1, 2);
  95. append(vec, 2, 0); append(vec, 2, 1); append(vec, 2, 2);
  96. std::for_each(vec.begin(), vec.end(), test_moved(false));
  97. bgi::rtree<Point, Params, indexable<Point> > rt(
  98. boost::make_move_iterator(vec.begin()),
  99. boost::make_move_iterator(vec.end()),
  100. params);
  101. std::for_each(vec.begin(), vec.end(), test_moved(true));
  102. BOOST_CHECK_EQUAL(rt.size(), vec.size());
  103. }
  104. int test_main(int, char* [])
  105. {
  106. test_rtree< point_cm, bgi::linear<4> >();
  107. test_rtree< point_cm, bgi::quadratic<4> >();
  108. test_rtree< point_cm, bgi::rstar<4> >();
  109. test_rtree<point_cm>(bgi::dynamic_linear(4));
  110. test_rtree<point_cm>(bgi::dynamic_quadratic(4));
  111. test_rtree<point_cm>(bgi::dynamic_rstar(4));
  112. return 0;
  113. }