iterator.hpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // Boost.Geometry Index
  2. //
  3. // R-tree iterator visitor implementation
  4. //
  5. // Copyright (c) 2011-2015 Adam Wulkiewicz, Lodz, Poland.
  6. //
  7. // This file was modified by Oracle on 2021-2023.
  8. // Modifications copyright (c) 2021-2023 Oracle and/or its affiliates.
  9. // Contributed and/or modified by Vissarion Fysikopoulos, on behalf of Oracle
  10. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle
  11. //
  12. // Use, modification and distribution is subject to the Boost Software License,
  13. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  14. // http://www.boost.org/LICENSE_1_0.txt)
  15. #ifndef BOOST_GEOMETRY_INDEX_DETAIL_RTREE_VISITORS_ITERATOR_HPP
  16. #define BOOST_GEOMETRY_INDEX_DETAIL_RTREE_VISITORS_ITERATOR_HPP
  17. #include <boost/geometry/index/detail/rtree/node/concept.hpp>
  18. #include <boost/geometry/index/detail/rtree/node/node_elements.hpp>
  19. #include <boost/geometry/index/detail/rtree/node/variant_visitor.hpp>
  20. namespace boost { namespace geometry { namespace index {
  21. namespace detail { namespace rtree { namespace visitors {
  22. template <typename Value, typename Options, typename Translator, typename Box, typename Allocators>
  23. class iterator
  24. : public rtree::visitor<Value, typename Options::parameters_type, Box, Allocators, typename Options::node_tag, true>::type
  25. {
  26. public:
  27. typedef typename rtree::node<Value, typename Options::parameters_type, Box, Allocators, typename Options::node_tag>::type node;
  28. typedef typename rtree::internal_node<Value, typename Options::parameters_type, Box, Allocators, typename Options::node_tag>::type internal_node;
  29. typedef typename rtree::leaf<Value, typename Options::parameters_type, Box, Allocators, typename Options::node_tag>::type leaf;
  30. typedef typename Allocators::size_type size_type;
  31. typedef typename Allocators::const_reference const_reference;
  32. typedef typename Allocators::node_pointer node_pointer;
  33. typedef typename rtree::elements_type<internal_node>::type::const_iterator internal_iterator;
  34. typedef typename rtree::elements_type<leaf>::type leaf_elements;
  35. typedef typename rtree::elements_type<leaf>::type::const_iterator leaf_iterator;
  36. inline iterator()
  37. : m_values(NULL)
  38. , m_current()
  39. {}
  40. inline void operator()(internal_node const& n)
  41. {
  42. typedef typename rtree::elements_type<internal_node>::type elements_type;
  43. elements_type const& elements = rtree::elements(n);
  44. m_internal_stack.push_back(std::make_pair(elements.begin(), elements.end()));
  45. }
  46. inline void operator()(leaf const& n)
  47. {
  48. m_values = ::boost::addressof(rtree::elements(n));
  49. m_current = rtree::elements(n).begin();
  50. }
  51. const_reference dereference() const
  52. {
  53. BOOST_GEOMETRY_INDEX_ASSERT(m_values, "not dereferencable");
  54. return *m_current;
  55. }
  56. void initialize(node_pointer root)
  57. {
  58. rtree::apply_visitor(*this, *root);
  59. search_value();
  60. }
  61. void increment()
  62. {
  63. ++m_current;
  64. search_value();
  65. }
  66. void search_value()
  67. {
  68. for (;;)
  69. {
  70. // if leaf is choosen, move to the next value in leaf
  71. if ( m_values )
  72. {
  73. // there are more values in the current leaf
  74. if ( m_current != m_values->end() )
  75. {
  76. return;
  77. }
  78. // no more values, clear current leaf
  79. else
  80. {
  81. m_values = 0;
  82. }
  83. }
  84. // if leaf isn't choosen, move to the next leaf
  85. else
  86. {
  87. // return if there is no more nodes to traverse
  88. if ( m_internal_stack.empty() )
  89. return;
  90. // no more children in current node, remove it from stack
  91. if ( m_internal_stack.back().first == m_internal_stack.back().second )
  92. {
  93. m_internal_stack.pop_back();
  94. continue;
  95. }
  96. internal_iterator it = m_internal_stack.back().first;
  97. ++m_internal_stack.back().first;
  98. // push the next node to the stack
  99. rtree::apply_visitor(*this, *(it->second));
  100. }
  101. }
  102. }
  103. bool is_end() const
  104. {
  105. return 0 == m_values;
  106. }
  107. friend bool operator==(iterator const& l, iterator const& r)
  108. {
  109. return (l.m_values == r.m_values) && (0 == l.m_values || l.m_current == r.m_current );
  110. }
  111. private:
  112. std::vector< std::pair<internal_iterator, internal_iterator> > m_internal_stack;
  113. const leaf_elements * m_values;
  114. leaf_iterator m_current;
  115. };
  116. }}} // namespace detail::rtree::visitors
  117. }}} // namespace boost::geometry::index
  118. #endif // BOOST_GEOMETRY_INDEX_DETAIL_RTREE_VISITORS_ITERATOR_HPP