1 /* Copyright 2003-2018 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/detail/no_exceptions_support.hpp>
20 #include <boost/multi_index/detail/auto_space.hpp>
21 #include <boost/multi_index/detail/raw_ptr.hpp>
22 #include <boost/noncopyable.hpp>
29 namespace multi_index{
33 /* copy_map is used as an auxiliary structure during copy_() operations.
34 * When a container with n nodes is replicated, node_map holds the pairings
35 * between original and copied nodes, and provides a fast way to find a
36 * copied node from an original one.
37 * The semantics of the class are not simple, and no attempt has been made
38 * to enforce it: multi_index_container handles it right. On the other hand,
39 * the const interface, which is the one provided to index implementations,
41 * - Enumeration of pairs of (original,copied) nodes (excluding the headers),
42 * - fast retrieval of copied nodes (including the headers.)
45 template <typename Node>
48 copy_map_entry(Node* f,Node* s):first(f),second(s){}
53 bool operator<(const copy_map_entry<Node>& x)const
55 return std::less<Node*>()(first,x.first);
59 template <typename Node,typename Allocator>
60 class copy_map:private noncopyable
63 typedef const copy_map_entry<Node>* const_iterator;
66 const Allocator& al,std::size_t size,Node* header_org,Node* header_cpy):
67 al_(al),size_(size),spc(al_,size_),n(0),
68 header_org_(header_org),header_cpy_(header_cpy),released(false)
74 for(std::size_t i=0;i<n;++i){
75 boost::detail::allocator::destroy(
76 boost::addressof((spc.data()+i)->second->value()));
77 deallocate((spc.data()+i)->second);
82 const_iterator begin()const{return raw_ptr<const_iterator>(spc.data());}
83 const_iterator end()const{return raw_ptr<const_iterator>(spc.data()+n);}
85 void clone(Node* node)
87 (spc.data()+n)->first=node;
88 (spc.data()+n)->second=raw_ptr<Node*>(allocate());
90 boost::detail::allocator::construct(
91 boost::addressof((spc.data()+n)->second->value()),node->value());
94 deallocate((spc.data()+n)->second);
102 raw_ptr<copy_map_entry<Node>*>(spc.data()),
103 raw_ptr<copy_map_entry<Node>*>(spc.data())+size_);
107 Node* find(Node* node)const
109 if(node==header_org_)return header_cpy_;
110 return std::lower_bound(
111 begin(),end(),copy_map_entry<Node>(node,0))->second;
120 typedef typename boost::detail::allocator::rebind_to<
122 >::type allocator_type;
123 #ifdef BOOST_NO_CXX11_ALLOCATOR
124 typedef typename allocator_type::pointer allocator_pointer;
126 typedef std::allocator_traits<allocator_type> allocator_traits;
127 typedef typename allocator_traits::pointer allocator_pointer;
132 auto_space<copy_map_entry<Node>,Allocator> spc;
138 allocator_pointer allocate()
140 #ifdef BOOST_NO_CXX11_ALLOCATOR
141 return al_.allocate(1);
143 return allocator_traits::allocate(al_,1);
147 void deallocate(Node* node)
149 #ifdef BOOST_NO_CXX11_ALLOCATOR
150 al_.deallocate(static_cast<allocator_pointer>(node),1);
152 allocator_traits::deallocate(al_,static_cast<allocator_pointer>(node),1);
157 } /* namespace multi_index::detail */
159 } /* namespace multi_index */
161 } /* namespace boost */