rnd_index_loader.hpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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_RND_INDEX_LOADER_HPP
  9. #define BOOST_MULTI_INDEX_DETAIL_RND_INDEX_LOADER_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 <algorithm>
  15. #include <boost/multi_index/detail/allocator_traits.hpp>
  16. #include <boost/multi_index/detail/auto_space.hpp>
  17. #include <boost/multi_index/detail/rnd_index_ptr_array.hpp>
  18. #include <boost/noncopyable.hpp>
  19. namespace boost{
  20. namespace multi_index{
  21. namespace detail{
  22. /* This class implements a serialization rearranger for random access
  23. * indices. In order to achieve O(n) performance, the following strategy
  24. * is followed: the nodes of the index are handled as if in a bidirectional
  25. * list, where the next pointers are stored in the original
  26. * random_access_index_ptr_array and the prev pointers are stored in
  27. * an auxiliary array. Rearranging of nodes in such a bidirectional list
  28. * is constant time. Once all the arrangements are performed (on destruction
  29. * time) the list is traversed in reverse order and
  30. * pointers are swapped and set accordingly so that they recover its
  31. * original semantics ( *(node->up())==node ) while retaining the
  32. * new order.
  33. */
  34. template<typename Allocator>
  35. class random_access_index_loader_base:private noncopyable
  36. {
  37. protected:
  38. typedef random_access_index_node_impl<
  39. typename rebind_alloc_for<
  40. Allocator,
  41. char
  42. >::type
  43. > node_impl_type;
  44. typedef typename node_impl_type::pointer node_impl_pointer;
  45. typedef random_access_index_ptr_array<Allocator> ptr_array;
  46. random_access_index_loader_base(const Allocator& al_,ptr_array& ptrs_):
  47. al(al_),
  48. ptrs(ptrs_),
  49. header(*ptrs.end()),
  50. prev_spc(al,0),
  51. preprocessed(false)
  52. {}
  53. ~random_access_index_loader_base()
  54. {
  55. if(preprocessed)
  56. {
  57. node_impl_pointer n=header;
  58. next(n)=n;
  59. for(size_type i=ptrs.size();i--;){
  60. n=prev(n);
  61. size_type d=position(n);
  62. if(d!=i){
  63. node_impl_pointer m=prev(next_at(i));
  64. std::swap(m->up(),n->up());
  65. next_at(d)=next_at(i);
  66. std::swap(prev_at(d),prev_at(i));
  67. }
  68. next(n)=n;
  69. }
  70. }
  71. }
  72. void rearrange(node_impl_pointer position_,node_impl_pointer x)
  73. {
  74. preprocess(); /* only incur this penalty if rearrange() is ever called */
  75. if(position_==node_impl_pointer(0))position_=header;
  76. next(prev(x))=next(x);
  77. prev(next(x))=prev(x);
  78. prev(x)=position_;
  79. next(x)=next(position_);
  80. next(prev(x))=prev(next(x))=x;
  81. }
  82. private:
  83. typedef allocator_traits<Allocator> alloc_traits;
  84. typedef typename alloc_traits::size_type size_type;
  85. void preprocess()
  86. {
  87. if(!preprocessed){
  88. /* get space for the auxiliary prev array */
  89. auto_space<node_impl_pointer,Allocator> tmp(al,ptrs.size()+1);
  90. prev_spc.swap(tmp);
  91. /* prev_spc elements point to the prev nodes */
  92. std::rotate_copy(
  93. &*ptrs.begin(),&*ptrs.end(),&*ptrs.end()+1,&*prev_spc.data());
  94. /* ptrs elements point to the next nodes */
  95. std::rotate(&*ptrs.begin(),&*ptrs.begin()+1,&*ptrs.end()+1);
  96. preprocessed=true;
  97. }
  98. }
  99. size_type position(node_impl_pointer x)const
  100. {
  101. return (size_type)(x->up()-ptrs.begin());
  102. }
  103. node_impl_pointer& next_at(size_type n)const
  104. {
  105. return *ptrs.at(n);
  106. }
  107. node_impl_pointer& prev_at(size_type n)const
  108. {
  109. return *(prev_spc.data()+n);
  110. }
  111. node_impl_pointer& next(node_impl_pointer x)const
  112. {
  113. return *(x->up());
  114. }
  115. node_impl_pointer& prev(node_impl_pointer x)const
  116. {
  117. return prev_at(position(x));
  118. }
  119. Allocator al;
  120. ptr_array& ptrs;
  121. node_impl_pointer header;
  122. auto_space<node_impl_pointer,Allocator> prev_spc;
  123. bool preprocessed;
  124. };
  125. template<typename Node,typename Allocator>
  126. class random_access_index_loader:
  127. private random_access_index_loader_base<Allocator>
  128. {
  129. typedef random_access_index_loader_base<Allocator> super;
  130. typedef typename super::node_impl_pointer node_impl_pointer;
  131. typedef typename super::ptr_array ptr_array;
  132. public:
  133. random_access_index_loader(const Allocator& al_,ptr_array& ptrs_):
  134. super(al_,ptrs_)
  135. {}
  136. void rearrange(Node* position_,Node *x)
  137. {
  138. super::rearrange(
  139. position_?position_->impl():node_impl_pointer(0),x->impl());
  140. }
  141. };
  142. } /* namespace multi_index::detail */
  143. } /* namespace multi_index */
  144. } /* namespace boost */
  145. #endif