2 // Boost.Pointer Container
4 // Copyright Thorsten Ottosen 2003-2005. Use, modification and
5 // distribution is subject to the Boost Software License, Version
6 // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
9 // For more information, see http://www.boost.org/libs/ptr_container/
13 #ifndef BOOST_PTR_CONTAINER_DETAIL_ASSOCIATIVE_PTR_CONTAINER_HPP
14 #define BOOST_PTR_CONTAINER_DETAIL_ASSOCIATIVE_PTR_CONTAINER_HPP
16 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
20 #include <boost/ptr_container/detail/reversible_ptr_container.hpp>
25 namespace ptr_container_detail
32 class associative_ptr_container :
33 public reversible_ptr_container<Config,CloneAllocator>
35 typedef reversible_ptr_container<Config,CloneAllocator>
38 typedef BOOST_DEDUCED_TYPENAME base_type::scoped_deleter
42 typedef BOOST_DEDUCED_TYPENAME Config::key_type
44 typedef BOOST_DEDUCED_TYPENAME Config::key_compare
46 typedef BOOST_DEDUCED_TYPENAME Config::value_compare
48 typedef BOOST_DEDUCED_TYPENAME Config::iterator
50 typedef BOOST_DEDUCED_TYPENAME Config::const_iterator
52 typedef BOOST_DEDUCED_TYPENAME base_type::size_type
57 template< class Compare, class Allocator >
58 associative_ptr_container( const Compare& comp,
60 : base_type( comp, a )
63 template< class InputIterator, class Compare, class Allocator >
64 associative_ptr_container( InputIterator first, InputIterator last,
67 : base_type( first, last, comp, a )
70 template< class PtrContainer >
71 associative_ptr_container( std::auto_ptr<PtrContainer> r )
72 : base_type( r, key_compare() )
75 template< class PtrContainer >
76 void operator=( std::auto_ptr<PtrContainer> r )
78 base_type::operator=( r );
81 public: // associative container interface
82 key_compare key_comp() const
84 return this->c_private().key_comp();
87 value_compare value_comp() const
89 return this->c_private().value_comp();
92 iterator erase( iterator before ) // nothrow
94 BOOST_ASSERT( !this->empty() );
95 BOOST_ASSERT( before != this->end() );
97 this->remove( before ); // nothrow
98 iterator res( before ); // nothrow
100 this->c_private().erase( before.base() ); // nothrow
101 return res; // nothrow
104 size_type erase( const key_type& x ) // nothrow
106 iterator i( this->c_private().find( x ) ); // nothrow
107 if( i == this->end() ) // nothrow
108 return 0u; // nothrow
109 this->remove( i ); // nothrow
110 return this->c_private().erase( x ); // nothrow
113 iterator erase( iterator first,
114 iterator last ) // nothrow
116 iterator res( last ); // nothrow
117 if( res != this->end() )
120 this->remove( first, last ); // nothrow
121 this->c_private().erase( first.base(), last.base() );// nothrow
122 return res; // nothrow
127 template< class AssociatePtrCont >
128 void multi_transfer( BOOST_DEDUCED_TYPENAME AssociatePtrCont::iterator object,
129 AssociatePtrCont& from ) // strong
131 BOOST_ASSERT( (void*)&from != (void*)this );
132 BOOST_ASSERT( !from.empty() && "Cannot transfer from empty container" );
134 this->c_private().insert( *object.base() ); // strong
135 from.c_private().erase( object.base() ); // nothrow
138 template< class AssociatePtrCont >
139 size_type multi_transfer( BOOST_DEDUCED_TYPENAME AssociatePtrCont::iterator first,
140 BOOST_DEDUCED_TYPENAME AssociatePtrCont::iterator last,
141 AssociatePtrCont& from ) // basic
143 BOOST_ASSERT( (void*)&from != (void*)this );
144 BOOST_ASSERT( !from.empty() && "Cannot transfer from empty container" );
147 for( ; first != last; )
149 BOOST_ASSERT( first != from.end() );
150 this->c_private().insert( *first.base() ); // strong
151 BOOST_DEDUCED_TYPENAME AssociatePtrCont::iterator
154 from.c_private().erase( to_delete.base() ); // nothrow
161 template< class AssociatePtrCont >
162 bool single_transfer( BOOST_DEDUCED_TYPENAME AssociatePtrCont::iterator object,
163 AssociatePtrCont& from ) // strong
165 BOOST_ASSERT( (void*)&from != (void*)this );
166 BOOST_ASSERT( !from.empty() && "Cannot transfer from empty container" );
168 std::pair<BOOST_DEDUCED_TYPENAME base_type::ptr_iterator,bool> p =
169 this->c_private().insert( *object.base() ); // strong
171 from.c_private().erase( object.base() ); // nothrow
176 template< class AssociatePtrCont >
177 size_type single_transfer( BOOST_DEDUCED_TYPENAME AssociatePtrCont::iterator first,
178 BOOST_DEDUCED_TYPENAME AssociatePtrCont::iterator last,
179 AssociatePtrCont& from ) // basic
181 BOOST_ASSERT( (void*)&from != (void*)this );
182 BOOST_ASSERT( !from.empty() && "Cannot transfer from empty container" );
185 for( ; first != last; )
187 BOOST_ASSERT( first != from.end() );
188 std::pair<BOOST_DEDUCED_TYPENAME base_type::ptr_iterator,bool> p =
189 this->c_private().insert( *first.base() ); // strong
190 BOOST_DEDUCED_TYPENAME AssociatePtrCont::iterator
195 from.c_private().erase( to_delete.base() ); // nothrow
202 }; // class 'associative_ptr_container'
204 } // namespace 'ptr_container_detail'
206 } // namespace 'boost'