annotate DEPENDENCIES/generic/include/boost/multi_index/detail/rnd_index_loader.hpp @ 60:01e6213c3f91

Merge
author Chris Cannam
date Fri, 12 Sep 2014 08:17:00 +0100
parents 2665513ce2d3
children c530137014c0
rev   line source
Chris@16 1 /* Copyright 2003-2008 Joaquin M Lopez Munoz.
Chris@16 2 * Distributed under the Boost Software License, Version 1.0.
Chris@16 3 * (See accompanying file LICENSE_1_0.txt or copy at
Chris@16 4 * http://www.boost.org/LICENSE_1_0.txt)
Chris@16 5 *
Chris@16 6 * See http://www.boost.org/libs/multi_index for library home page.
Chris@16 7 */
Chris@16 8
Chris@16 9 #ifndef BOOST_MULTI_INDEX_DETAIL_RND_INDEX_LOADER_HPP
Chris@16 10 #define BOOST_MULTI_INDEX_DETAIL_RND_INDEX_LOADER_HPP
Chris@16 11
Chris@16 12 #if defined(_MSC_VER)&&(_MSC_VER>=1200)
Chris@16 13 #pragma once
Chris@16 14 #endif
Chris@16 15
Chris@16 16 #include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */
Chris@16 17 #include <algorithm>
Chris@16 18 #include <boost/detail/allocator_utilities.hpp>
Chris@16 19 #include <boost/multi_index/detail/auto_space.hpp>
Chris@16 20 #include <boost/multi_index/detail/prevent_eti.hpp>
Chris@16 21 #include <boost/multi_index/detail/rnd_index_ptr_array.hpp>
Chris@16 22 #include <boost/noncopyable.hpp>
Chris@16 23 #include <cstddef>
Chris@16 24
Chris@16 25 namespace boost{
Chris@16 26
Chris@16 27 namespace multi_index{
Chris@16 28
Chris@16 29 namespace detail{
Chris@16 30
Chris@16 31 /* This class implements a serialization rearranger for random access
Chris@16 32 * indices. In order to achieve O(n) performance, the following strategy
Chris@16 33 * is followed: the nodes of the index are handled as if in a bidirectional
Chris@16 34 * list, where the next pointers are stored in the original
Chris@16 35 * random_access_index_ptr_array and the prev pointers are stored in
Chris@16 36 * an auxiliary array. Rearranging of nodes in such a bidirectional list
Chris@16 37 * is constant time. Once all the arrangements are performed (on destruction
Chris@16 38 * time) the list is traversed in reverse order and
Chris@16 39 * pointers are swapped and set accordingly so that they recover its
Chris@16 40 * original semantics ( *(node->up())==node ) while retaining the
Chris@16 41 * new order.
Chris@16 42 */
Chris@16 43
Chris@16 44 template<typename Allocator>
Chris@16 45 class random_access_index_loader_base:private noncopyable
Chris@16 46 {
Chris@16 47 protected:
Chris@16 48 typedef typename prevent_eti<
Chris@16 49 Allocator,
Chris@16 50 random_access_index_node_impl<
Chris@16 51 typename boost::detail::allocator::rebind_to<
Chris@16 52 Allocator,
Chris@16 53 char
Chris@16 54 >::type
Chris@16 55 >
Chris@16 56 >::type node_impl_type;
Chris@16 57 typedef typename node_impl_type::pointer node_impl_pointer;
Chris@16 58 typedef random_access_index_ptr_array<Allocator> ptr_array;
Chris@16 59
Chris@16 60 random_access_index_loader_base(const Allocator& al_,ptr_array& ptrs_):
Chris@16 61 al(al_),
Chris@16 62 ptrs(ptrs_),
Chris@16 63 header(*ptrs.end()),
Chris@16 64 prev_spc(al,0),
Chris@16 65 preprocessed(false)
Chris@16 66 {}
Chris@16 67
Chris@16 68 ~random_access_index_loader_base()
Chris@16 69 {
Chris@16 70 if(preprocessed)
Chris@16 71 {
Chris@16 72 node_impl_pointer n=header;
Chris@16 73 next(n)=n;
Chris@16 74
Chris@16 75 for(std::size_t i=ptrs.size();i--;){
Chris@16 76 n=prev(n);
Chris@16 77 std::size_t d=position(n);
Chris@16 78 if(d!=i){
Chris@16 79 node_impl_pointer m=prev(next_at(i));
Chris@16 80 std::swap(m->up(),n->up());
Chris@16 81 next_at(d)=next_at(i);
Chris@16 82 std::swap(prev_at(d),prev_at(i));
Chris@16 83 }
Chris@16 84 next(n)=n;
Chris@16 85 }
Chris@16 86 }
Chris@16 87 }
Chris@16 88
Chris@16 89 void rearrange(node_impl_pointer position,node_impl_pointer x)
Chris@16 90 {
Chris@16 91 preprocess(); /* only incur this penalty if rearrange() is ever called */
Chris@16 92 if(position==node_impl_pointer(0))position=header;
Chris@16 93 next(prev(x))=next(x);
Chris@16 94 prev(next(x))=prev(x);
Chris@16 95 prev(x)=position;
Chris@16 96 next(x)=next(position);
Chris@16 97 next(prev(x))=prev(next(x))=x;
Chris@16 98 }
Chris@16 99
Chris@16 100 private:
Chris@16 101 void preprocess()
Chris@16 102 {
Chris@16 103 if(!preprocessed){
Chris@16 104 /* get space for the auxiliary prev array */
Chris@16 105 auto_space<node_impl_pointer,Allocator> tmp(al,ptrs.size()+1);
Chris@16 106 prev_spc.swap(tmp);
Chris@16 107
Chris@16 108 /* prev_spc elements point to the prev nodes */
Chris@16 109 std::rotate_copy(
Chris@16 110 &*ptrs.begin(),&*ptrs.end(),&*ptrs.end()+1,&*prev_spc.data());
Chris@16 111
Chris@16 112 /* ptrs elements point to the next nodes */
Chris@16 113 std::rotate(&*ptrs.begin(),&*ptrs.begin()+1,&*ptrs.end()+1);
Chris@16 114
Chris@16 115 preprocessed=true;
Chris@16 116 }
Chris@16 117 }
Chris@16 118
Chris@16 119 std::size_t position(node_impl_pointer x)const
Chris@16 120 {
Chris@16 121 return (std::size_t)(x->up()-ptrs.begin());
Chris@16 122 }
Chris@16 123
Chris@16 124 node_impl_pointer& next_at(std::size_t n)const
Chris@16 125 {
Chris@16 126 return *ptrs.at(n);
Chris@16 127 }
Chris@16 128
Chris@16 129 node_impl_pointer& prev_at(std::size_t n)const
Chris@16 130 {
Chris@16 131 return *(prev_spc.data()+n);
Chris@16 132 }
Chris@16 133
Chris@16 134 node_impl_pointer& next(node_impl_pointer x)const
Chris@16 135 {
Chris@16 136 return *(x->up());
Chris@16 137 }
Chris@16 138
Chris@16 139 node_impl_pointer& prev(node_impl_pointer x)const
Chris@16 140 {
Chris@16 141 return prev_at(position(x));
Chris@16 142 }
Chris@16 143
Chris@16 144 Allocator al;
Chris@16 145 ptr_array& ptrs;
Chris@16 146 node_impl_pointer header;
Chris@16 147 auto_space<node_impl_pointer,Allocator> prev_spc;
Chris@16 148 bool preprocessed;
Chris@16 149 };
Chris@16 150
Chris@16 151 template<typename Node,typename Allocator>
Chris@16 152 class random_access_index_loader:
Chris@16 153 private random_access_index_loader_base<Allocator>
Chris@16 154 {
Chris@16 155 typedef random_access_index_loader_base<Allocator> super;
Chris@16 156 typedef typename super::node_impl_pointer node_impl_pointer;
Chris@16 157 typedef typename super::ptr_array ptr_array;
Chris@16 158
Chris@16 159 public:
Chris@16 160 random_access_index_loader(const Allocator& al_,ptr_array& ptrs_):
Chris@16 161 super(al_,ptrs_)
Chris@16 162 {}
Chris@16 163
Chris@16 164 void rearrange(Node* position,Node *x)
Chris@16 165 {
Chris@16 166 super::rearrange(position?position->impl():node_impl_pointer(0),x->impl());
Chris@16 167 }
Chris@16 168 };
Chris@16 169
Chris@16 170 } /* namespace multi_index::detail */
Chris@16 171
Chris@16 172 } /* namespace multi_index */
Chris@16 173
Chris@16 174 } /* namespace boost */
Chris@16 175
Chris@16 176 #endif