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