1 /* Copyright 2003-2022 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)
6 * See http://www.boost.org/libs/multi_index for library home page.
9 #ifndef BOOST_MULTI_INDEX_DETAIL_COPY_MAP_HPP
10 #define BOOST_MULTI_INDEX_DETAIL_COPY_MAP_HPP
16 #include <boost/config.hpp> /* keep it first to prevent nasty warns in MSVC */
18 #include <boost/core/addressof.hpp>
19 #include <boost/core/no_exceptions_support.hpp>
20 #include <boost/core/noncopyable.hpp>
21 #include <boost/move/core.hpp>
22 #include <boost/move/utility_core.hpp>
23 #include <boost/multi_index/detail/allocator_traits.hpp>
24 #include <boost/multi_index/detail/auto_space.hpp>
25 #include <boost/multi_index/detail/raw_ptr.hpp>
30 namespace multi_index{
34 /* copy_map is used as an auxiliary structure during copy_() operations.
35 * When a container with n nodes is replicated, node_map holds the pairings
36 * between original and copied nodes, and provides a fast way to find a
37 * copied node from an original one.
38 * The semantics of the class are not simple, and no attempt has been made
39 * to enforce it: multi_index_container handles it right. On the other hand,
40 * the const interface, which is the one provided to index implementations,
42 * - Enumeration of pairs of (original,copied) nodes (excluding the headers),
43 * - fast retrieval of copied nodes (including the headers.)
46 template <typename Node>
49 copy_map_entry(Node* f,Node* s):first(f),second(s){}
54 bool operator<(const copy_map_entry<Node>& x)const
56 return std::less<Node*>()(first,x.first);
60 struct copy_map_value_copier
62 template<typename Value>
63 const Value& operator()(Value& x)const{return x;}
66 struct copy_map_value_mover
68 template<typename Value>
69 BOOST_RV_REF(Value) operator()(Value& x)const{return boost::move(x);}
72 template <typename Node,typename Allocator>
73 class copy_map:private noncopyable
75 typedef typename rebind_alloc_for<
77 >::type allocator_type;
78 typedef allocator_traits<allocator_type> alloc_traits;
79 typedef typename alloc_traits::pointer pointer;
82 typedef const copy_map_entry<Node>* const_iterator;
83 typedef typename alloc_traits::size_type size_type;
86 const Allocator& al,size_type size,Node* header_org,Node* header_cpy):
87 al_(al),size_(size),spc(al_,size_),n(0),
88 header_org_(header_org),header_cpy_(header_cpy),released(false)
94 for(size_type i=0;i<n;++i){
95 alloc_traits::destroy(
96 al_,boost::addressof((spc.data()+i)->second->value()));
97 deallocate((spc.data()+i)->second);
102 const_iterator begin()const{return raw_ptr<const_iterator>(spc.data());}
103 const_iterator end()const{return raw_ptr<const_iterator>(spc.data()+n);}
105 void copy_clone(Node* node){clone(node,copy_map_value_copier());}
106 void move_clone(Node* node){clone(node,copy_map_value_mover());}
108 Node* find(Node* node)const
110 if(node==header_org_)return header_cpy_;
111 return std::lower_bound(
112 begin(),end(),copy_map_entry<Node>(node,0))->second;
123 auto_space<copy_map_entry<Node>,Allocator> spc;
131 return alloc_traits::allocate(al_,1);
134 void deallocate(Node* node)
136 alloc_traits::deallocate(al_,static_cast<pointer>(node),1);
139 template<typename ValueAccess>
140 void clone(Node* node,ValueAccess access)
142 (spc.data()+n)->first=node;
143 (spc.data()+n)->second=raw_ptr<Node*>(allocate());
145 alloc_traits::construct(
146 al_,boost::addressof((spc.data()+n)->second->value()),
147 access(node->value()));
150 deallocate((spc.data()+n)->second);
158 raw_ptr<copy_map_entry<Node>*>(spc.data()),
159 raw_ptr<copy_map_entry<Node>*>(spc.data())+size_);
164 } /* namespace multi_index::detail */
166 } /* namespace multi_index */
168 } /* namespace boost */