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_COPY_MAP_HPP
|
Chris@16
|
10 #define BOOST_MULTI_INDEX_DETAIL_COPY_MAP_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/no_exceptions_support.hpp>
|
Chris@16
|
19 #include <boost/multi_index/detail/auto_space.hpp>
|
Chris@16
|
20 #include <boost/noncopyable.hpp>
|
Chris@16
|
21 #include <cstddef>
|
Chris@16
|
22 #include <functional>
|
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 /* copy_map is used as an auxiliary structure during copy_() operations.
|
Chris@16
|
31 * When a container with n nodes is replicated, node_map holds the pairings
|
Chris@16
|
32 * between original and copied nodes, and provides a fast way to find a
|
Chris@16
|
33 * copied node from an original one.
|
Chris@16
|
34 * The semantics of the class are not simple, and no attempt has been made
|
Chris@16
|
35 * to enforce it: multi_index_container handles it right. On the other hand,
|
Chris@16
|
36 * the const interface, which is the one provided to index implementations,
|
Chris@16
|
37 * only allows for:
|
Chris@16
|
38 * - Enumeration of pairs of (original,copied) nodes (excluding the headers),
|
Chris@16
|
39 * - fast retrieval of copied nodes (including the headers.)
|
Chris@16
|
40 */
|
Chris@16
|
41
|
Chris@16
|
42 template <typename Node>
|
Chris@16
|
43 struct copy_map_entry
|
Chris@16
|
44 {
|
Chris@16
|
45 copy_map_entry(Node* f,Node* s):first(f),second(s){}
|
Chris@16
|
46
|
Chris@16
|
47 Node* first;
|
Chris@16
|
48 Node* second;
|
Chris@16
|
49
|
Chris@16
|
50 bool operator<(const copy_map_entry<Node>& x)const
|
Chris@16
|
51 {
|
Chris@16
|
52 return std::less<Node*>()(first,x.first);
|
Chris@16
|
53 }
|
Chris@16
|
54 };
|
Chris@16
|
55
|
Chris@16
|
56 template <typename Node,typename Allocator>
|
Chris@16
|
57 class copy_map:private noncopyable
|
Chris@16
|
58 {
|
Chris@16
|
59 public:
|
Chris@16
|
60 typedef const copy_map_entry<Node>* const_iterator;
|
Chris@16
|
61
|
Chris@16
|
62 copy_map(
|
Chris@16
|
63 const Allocator& al,std::size_t size,Node* header_org,Node* header_cpy):
|
Chris@16
|
64 al_(al),size_(size),spc(al_,size_),n(0),
|
Chris@16
|
65 header_org_(header_org),header_cpy_(header_cpy),released(false)
|
Chris@16
|
66 {}
|
Chris@16
|
67
|
Chris@16
|
68 ~copy_map()
|
Chris@16
|
69 {
|
Chris@16
|
70 if(!released){
|
Chris@16
|
71 for(std::size_t i=0;i<n;++i){
|
Chris@16
|
72 boost::detail::allocator::destroy(&(spc.data()+i)->second->value());
|
Chris@16
|
73 deallocate((spc.data()+i)->second);
|
Chris@16
|
74 }
|
Chris@16
|
75 }
|
Chris@16
|
76 }
|
Chris@16
|
77
|
Chris@16
|
78 const_iterator begin()const{return &*spc.data();}
|
Chris@16
|
79 const_iterator end()const{return &*(spc.data()+n);}
|
Chris@16
|
80
|
Chris@16
|
81 void clone(Node* node)
|
Chris@16
|
82 {
|
Chris@16
|
83 (spc.data()+n)->first=node;
|
Chris@16
|
84 (spc.data()+n)->second=&*al_.allocate(1);
|
Chris@16
|
85 BOOST_TRY{
|
Chris@16
|
86 boost::detail::allocator::construct(
|
Chris@16
|
87 &(spc.data()+n)->second->value(),node->value());
|
Chris@16
|
88 }
|
Chris@16
|
89 BOOST_CATCH(...){
|
Chris@16
|
90 deallocate((spc.data()+n)->second);
|
Chris@16
|
91 BOOST_RETHROW;
|
Chris@16
|
92 }
|
Chris@16
|
93 BOOST_CATCH_END
|
Chris@16
|
94 ++n;
|
Chris@16
|
95
|
Chris@16
|
96 if(n==size_)std::sort(&*spc.data(),&*spc.data()+size_);
|
Chris@16
|
97 }
|
Chris@16
|
98
|
Chris@16
|
99 Node* find(Node* node)const
|
Chris@16
|
100 {
|
Chris@16
|
101 if(node==header_org_)return header_cpy_;
|
Chris@16
|
102 return std::lower_bound(
|
Chris@16
|
103 begin(),end(),copy_map_entry<Node>(node,0))->second;
|
Chris@16
|
104 }
|
Chris@16
|
105
|
Chris@16
|
106 void release()
|
Chris@16
|
107 {
|
Chris@16
|
108 released=true;
|
Chris@16
|
109 }
|
Chris@16
|
110
|
Chris@16
|
111 private:
|
Chris@101
|
112 typedef typename boost::detail::allocator::rebind_to<
|
Chris@101
|
113 Allocator,Node
|
Chris@101
|
114 >::type allocator_type;
|
Chris@101
|
115 typedef typename allocator_type::pointer allocator_pointer;
|
Chris@16
|
116
|
Chris@101
|
117 allocator_type al_;
|
Chris@101
|
118 std::size_t size_;
|
Chris@101
|
119 auto_space<copy_map_entry<Node>,Allocator> spc;
|
Chris@101
|
120 std::size_t n;
|
Chris@101
|
121 Node* header_org_;
|
Chris@101
|
122 Node* header_cpy_;
|
Chris@101
|
123 bool released;
|
Chris@16
|
124
|
Chris@16
|
125 void deallocate(Node* node)
|
Chris@16
|
126 {
|
Chris@16
|
127 al_.deallocate(static_cast<allocator_pointer>(node),1);
|
Chris@16
|
128 }
|
Chris@16
|
129 };
|
Chris@16
|
130
|
Chris@16
|
131 } /* namespace multi_index::detail */
|
Chris@16
|
132
|
Chris@16
|
133 } /* namespace multi_index */
|
Chris@16
|
134
|
Chris@16
|
135 } /* namespace boost */
|
Chris@16
|
136
|
Chris@16
|
137 #endif
|