rnd_node_iterator.hpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /* Copyright 2003-2023 Joaquin M Lopez Munoz.
  2. * Distributed under the Boost Software License, Version 1.0.
  3. * (See accompanying file LICENSE_1_0.txt or copy at
  4. * http://www.boost.org/LICENSE_1_0.txt)
  5. *
  6. * See http://www.boost.org/libs/multi_index for library home page.
  7. */
  8. #ifndef BOOST_MULTI_INDEX_DETAIL_RND_NODE_ITERATOR_HPP
  9. #define BOOST_MULTI_INDEX_DETAIL_RND_NODE_ITERATOR_HPP
  10. #if defined(_MSC_VER)
  11. #pragma once
  12. #endif
  13. #include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */
  14. #include <boost/operators.hpp>
  15. #if !defined(BOOST_MULTI_INDEX_DISABLE_SERIALIZATION)
  16. #include <boost/core/serialization.hpp>
  17. #endif
  18. namespace boost{
  19. namespace multi_index{
  20. namespace detail{
  21. /* Iterator class for node-based indices with random access iterators. */
  22. template<typename Node>
  23. class rnd_node_iterator:
  24. public random_access_iterator_helper<
  25. rnd_node_iterator<Node>,
  26. typename Node::value_type,
  27. typename Node::difference_type,
  28. const typename Node::value_type*,
  29. const typename Node::value_type&>
  30. {
  31. public:
  32. /* coverity[uninit_ctor]: suppress warning */
  33. rnd_node_iterator(){}
  34. explicit rnd_node_iterator(Node* node_):node(node_){}
  35. const typename Node::value_type& operator*()const
  36. {
  37. return node->value();
  38. }
  39. rnd_node_iterator& operator++()
  40. {
  41. Node::increment(node);
  42. return *this;
  43. }
  44. rnd_node_iterator& operator--()
  45. {
  46. Node::decrement(node);
  47. return *this;
  48. }
  49. rnd_node_iterator& operator+=(typename Node::difference_type n)
  50. {
  51. Node::advance(node,n);
  52. return *this;
  53. }
  54. rnd_node_iterator& operator-=(typename Node::difference_type n)
  55. {
  56. Node::advance(node,-n);
  57. return *this;
  58. }
  59. #if !defined(BOOST_MULTI_INDEX_DISABLE_SERIALIZATION)
  60. /* Serialization. As for why the following is public,
  61. * see explanation in safe_mode_iterator notes in safe_mode.hpp.
  62. */
  63. template<class Archive>
  64. void serialize(Archive& ar,const unsigned int version)
  65. {
  66. core::split_member(ar,*this,version);
  67. }
  68. typedef typename Node::base_type node_base_type;
  69. template<class Archive>
  70. void save(Archive& ar,const unsigned int)const
  71. {
  72. node_base_type* bnode=node;
  73. ar<<core::make_nvp("pointer",bnode);
  74. }
  75. template<class Archive>
  76. void load(Archive& ar,const unsigned int)
  77. {
  78. node_base_type* bnode;
  79. ar>>core::make_nvp("pointer",bnode);
  80. node=static_cast<Node*>(bnode);
  81. }
  82. #endif
  83. /* get_node is not to be used by the user */
  84. typedef Node node_type;
  85. Node* get_node()const{return node;}
  86. private:
  87. Node* node;
  88. };
  89. template<typename Node>
  90. bool operator==(
  91. const rnd_node_iterator<Node>& x,
  92. const rnd_node_iterator<Node>& y)
  93. {
  94. return x.get_node()==y.get_node();
  95. }
  96. template<typename Node>
  97. bool operator<(
  98. const rnd_node_iterator<Node>& x,
  99. const rnd_node_iterator<Node>& y)
  100. {
  101. return Node::distance(x.get_node(),y.get_node())>0;
  102. }
  103. template<typename Node>
  104. typename Node::difference_type operator-(
  105. const rnd_node_iterator<Node>& x,
  106. const rnd_node_iterator<Node>& y)
  107. {
  108. return Node::distance(y.get_node(),x.get_node());
  109. }
  110. } /* namespace multi_index::detail */
  111. } /* namespace multi_index */
  112. } /* namespace boost */
  113. #endif