hash_index_iterator.hpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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_HASH_INDEX_ITERATOR_HPP
  9. #define BOOST_MULTI_INDEX_DETAIL_HASH_INDEX_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 hashed indices.
  22. */
  23. struct hashed_index_global_iterator_tag{};
  24. struct hashed_index_local_iterator_tag{};
  25. template<
  26. typename Node,typename BucketArray,
  27. typename IndexCategory,typename IteratorCategory
  28. >
  29. class hashed_index_iterator:
  30. public forward_iterator_helper<
  31. hashed_index_iterator<Node,BucketArray,IndexCategory,IteratorCategory>,
  32. typename Node::value_type,
  33. typename Node::difference_type,
  34. const typename Node::value_type*,
  35. const typename Node::value_type&>
  36. {
  37. public:
  38. /* coverity[uninit_ctor]: suppress warning */
  39. hashed_index_iterator(){}
  40. hashed_index_iterator(Node* node_):node(node_){}
  41. const typename Node::value_type& operator*()const
  42. {
  43. return node->value();
  44. }
  45. hashed_index_iterator& operator++()
  46. {
  47. this->increment(IteratorCategory());
  48. return *this;
  49. }
  50. #if !defined(BOOST_MULTI_INDEX_DISABLE_SERIALIZATION)
  51. /* Serialization. As for why the following is public,
  52. * see explanation in safe_mode_iterator notes in safe_mode.hpp.
  53. */
  54. template<class Archive>
  55. void serialize(Archive& ar,const unsigned int version)
  56. {
  57. core::split_member(ar,*this,version);
  58. }
  59. typedef typename Node::base_type node_base_type;
  60. template<class Archive>
  61. void save(Archive& ar,const unsigned int)const
  62. {
  63. node_base_type* bnode=node;
  64. ar<<core::make_nvp("pointer",bnode);
  65. }
  66. template<class Archive>
  67. void load(Archive& ar,const unsigned int version)
  68. {
  69. load(ar,version,IteratorCategory());
  70. }
  71. template<class Archive>
  72. void load(
  73. Archive& ar,const unsigned int version,hashed_index_global_iterator_tag)
  74. {
  75. node_base_type* bnode;
  76. ar>>core::make_nvp("pointer",bnode);
  77. node=static_cast<Node*>(bnode);
  78. if(version<1){
  79. BucketArray* throw_away; /* consume unused ptr */
  80. ar>>core::make_nvp("pointer",throw_away);
  81. }
  82. }
  83. template<class Archive>
  84. void load(
  85. Archive& ar,const unsigned int version,hashed_index_local_iterator_tag)
  86. {
  87. node_base_type* bnode;
  88. ar>>core::make_nvp("pointer",bnode);
  89. node=static_cast<Node*>(bnode);
  90. if(version<1){
  91. BucketArray* buckets;
  92. ar>>core::make_nvp("pointer",buckets);
  93. if(buckets&&node&&node->impl()==buckets->end()->prior()){
  94. /* end local_iterators used to point to end node, now they are null */
  95. node=0;
  96. }
  97. }
  98. }
  99. #endif
  100. /* get_node is not to be used by the user */
  101. typedef Node node_type;
  102. Node* get_node()const{return node;}
  103. private:
  104. void increment(hashed_index_global_iterator_tag)
  105. {
  106. Node::template increment<IndexCategory>(node);
  107. }
  108. void increment(hashed_index_local_iterator_tag)
  109. {
  110. Node::template increment_local<IndexCategory>(node);
  111. }
  112. Node* node;
  113. };
  114. template<
  115. typename Node,typename BucketArray,
  116. typename IndexCategory,typename IteratorCategory
  117. >
  118. bool operator==(
  119. const hashed_index_iterator<
  120. Node,BucketArray,IndexCategory,IteratorCategory>& x,
  121. const hashed_index_iterator<
  122. Node,BucketArray,IndexCategory,IteratorCategory>& y)
  123. {
  124. return x.get_node()==y.get_node();
  125. }
  126. } /* namespace multi_index::detail */
  127. } /* namespace multi_index */
  128. #if !defined(BOOST_MULTI_INDEX_DISABLE_SERIALIZATION)
  129. /* class version = 1 : hashed_index_iterator does no longer serialize a bucket
  130. * array pointer.
  131. */
  132. namespace serialization {
  133. template<
  134. typename Node,typename BucketArray,
  135. typename IndexCategory,typename IteratorCategory
  136. >
  137. struct version<
  138. boost::multi_index::detail::hashed_index_iterator<
  139. Node,BucketArray,IndexCategory,IteratorCategory
  140. >
  141. >
  142. {
  143. BOOST_STATIC_CONSTANT(int,value=1);
  144. };
  145. } /* namespace serialization */
  146. #endif
  147. } /* namespace boost */
  148. #endif