9
3

iterator.hpp 4.3 KB

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