/Src/Dependencies/Boost/boost/multi_index/detail/rnd_index_loader.hpp

http://hadesmem.googlecode.com/ · C++ Header · 176 lines · 125 code · 29 blank · 22 comment · 5 complexity · c2a7c1edf3e0cb02a056c3cadd1ec2bf MD5 · raw file

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