hash_index_iterator.hpp 4.0 KB

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