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
41 typedef BOOST_DEDUCED_TYPENAME Config::container_type
44 typedef BOOST_DEDUCED_TYPENAME Config::key_type
46 typedef BOOST_DEDUCED_TYPENAME Config::key_compare
48 typedef BOOST_DEDUCED_TYPENAME Config::value_compare
50 typedef BOOST_DEDUCED_TYPENAME Config::hasher
52 typedef BOOST_DEDUCED_TYPENAME Config::key_equal
54 typedef BOOST_DEDUCED_TYPENAME Config::iterator
56 typedef BOOST_DEDUCED_TYPENAME Config::const_iterator
58 typedef BOOST_DEDUCED_TYPENAME Config::local_iterator
60 typedef BOOST_DEDUCED_TYPENAME Config::const_local_iterator
62 typedef BOOST_DEDUCED_TYPENAME base_type::size_type
64 typedef BOOST_DEDUCED_TYPENAME base_type::reference
66 typedef BOOST_DEDUCED_TYPENAME base_type::const_reference
70 associative_ptr_container()
73 template< class SizeType >
74 associative_ptr_container( SizeType n, unordered_associative_container_tag tag )
78 template< class Compare, class Allocator >
79 associative_ptr_container( const Compare& comp,
81 : base_type( comp, a, container_type() )
84 template< class Hash, class Pred, class Allocator >
85 associative_ptr_container( const Hash& hash,
88 : base_type( hash, pred, a )
91 template< class InputIterator, class Compare, class Allocator >
92 associative_ptr_container( InputIterator first, InputIterator last,
95 : base_type( first, last, comp, a, container_type() )
98 template< class InputIterator, class Hash, class Pred, class Allocator >
99 associative_ptr_container( InputIterator first, InputIterator last,
103 : base_type( first, last, hash, pred, a )
106 template< class PtrContainer >
107 explicit associative_ptr_container( std::auto_ptr<PtrContainer> r )
111 associative_ptr_container( const associative_ptr_container& r )
112 : base_type( r.begin(), r.end(), container_type() )
115 template< class C, class V >
116 associative_ptr_container( const associative_ptr_container<C,V>& r )
117 : base_type( r.begin(), r.end(), container_type() )
120 template< class PtrContainer >
121 associative_ptr_container& operator=( std::auto_ptr<PtrContainer> r ) // nothrow
123 base_type::operator=( r );
127 associative_ptr_container& operator=( associative_ptr_container r ) // strong
133 public: // associative container interface
134 key_compare key_comp() const
136 return this->base().key_comp();
139 value_compare value_comp() const
141 return this->base().value_comp();
144 iterator erase( iterator before ) // nothrow
146 BOOST_ASSERT( !this->empty() );
147 BOOST_ASSERT( before != this->end() );
149 this->remove( before ); // nothrow
150 iterator res( before ); // nothrow
152 this->base().erase( before.base() ); // nothrow
153 return res; // nothrow
156 size_type erase( const key_type& x ) // nothrow
158 iterator i( this->base().find( x ) );
160 if( i == this->end() ) // nothrow
161 return 0u; // nothrow
162 this->remove( i ); // nothrow
163 return this->base().erase( x ); // nothrow
166 iterator erase( iterator first,
167 iterator last ) // nothrow
169 iterator res( last ); // nothrow
170 if( res != this->end() )
173 this->remove( first, last ); // nothrow
174 this->base().erase( first.base(), last.base() ); // nothrow
175 return res; // nothrow
178 #if defined(BOOST_NO_SFINAE) || defined(BOOST_NO_FUNCTION_TEMPLATE_ORDERING)
180 template< class Range >
181 BOOST_DEDUCED_TYPENAME boost::disable_if< boost::is_convertible<Range&,key_type&>,
183 erase( const Range& r )
185 return erase( boost::begin(r), boost::end(r) );
192 template< class AssociatePtrCont >
193 void multi_transfer( BOOST_DEDUCED_TYPENAME AssociatePtrCont::iterator object,
194 AssociatePtrCont& from ) // strong
196 BOOST_ASSERT( (void*)&from != (void*)this );
197 BOOST_ASSERT( !from.empty() && "Cannot transfer from empty container" );
199 this->base().insert( *object.base() ); // strong
200 from.base().erase( object.base() ); // nothrow
203 template< class AssociatePtrCont >
204 size_type multi_transfer( BOOST_DEDUCED_TYPENAME AssociatePtrCont::iterator first,
205 BOOST_DEDUCED_TYPENAME AssociatePtrCont::iterator last,
206 AssociatePtrCont& from ) // basic
208 BOOST_ASSERT( (void*)&from != (void*)this );
211 for( ; first != last; )
213 BOOST_ASSERT( first != from.end() );
214 this->base().insert( *first.base() ); // strong
215 BOOST_DEDUCED_TYPENAME AssociatePtrCont::iterator
218 from.base().erase( to_delete.base() ); // nothrow
225 template< class AssociatePtrCont >
226 bool single_transfer( BOOST_DEDUCED_TYPENAME AssociatePtrCont::iterator object,
227 AssociatePtrCont& from ) // strong
229 BOOST_ASSERT( (void*)&from != (void*)this );
230 BOOST_ASSERT( !from.empty() && "Cannot transfer from empty container" );
232 std::pair<BOOST_DEDUCED_TYPENAME base_type::ptr_iterator,bool> p =
233 this->base().insert( *object.base() ); // strong
235 from.base().erase( object.base() ); // nothrow
240 template< class AssociatePtrCont >
241 size_type single_transfer( BOOST_DEDUCED_TYPENAME AssociatePtrCont::iterator first,
242 BOOST_DEDUCED_TYPENAME AssociatePtrCont::iterator last,
243 AssociatePtrCont& from ) // basic
245 BOOST_ASSERT( (void*)&from != (void*)this );
248 for( ; first != last; )
250 BOOST_ASSERT( first != from.end() );
251 std::pair<BOOST_DEDUCED_TYPENAME base_type::ptr_iterator,bool> p =
252 this->base().insert( *first.base() ); // strong
253 BOOST_DEDUCED_TYPENAME AssociatePtrCont::iterator
258 from.base().erase( to_delete.base() ); // nothrow
267 BOOST_ASSERT( !this->empty() );
268 BOOST_ASSERT( *this->begin().base() != 0 );
269 return *this->begin();
272 const_reference front() const
274 return const_cast<associative_ptr_container*>(this)->front();
279 BOOST_ASSERT( !this->empty() );
280 BOOST_ASSERT( *(--this->end()).base() != 0 );
281 return *--this->end();
284 const_reference back() const
286 return const_cast<associative_ptr_container*>(this)->back();
289 protected: // unordered interface
290 hasher hash_function() const
292 return this->base().hash_function();
295 key_equal key_eq() const
297 return this->base().key_eq();
300 size_type bucket_count() const
302 return this->base().bucket_count();
305 size_type max_bucket_count() const
307 return this->base().max_bucket_count();
310 size_type bucket_size( size_type n ) const
312 return this->base().bucket_size( n );
315 float load_factor() const
317 return this->base().load_factor();
320 float max_load_factor() const
322 return this->base().max_load_factor();
325 void max_load_factor( float factor )
327 return this->base().max_load_factor( factor );
330 void rehash( size_type n )
332 this->base().rehash( n );
336 #if BOOST_WORKAROUND(__DECCXX_VER, BOOST_TESTED_AT(70190006))
339 return base_type::begin();
342 const_iterator begin() const
344 return base_type::begin();
349 return base_type::end();
352 const_iterator end() const
354 return base_type::end();
357 const_iterator cbegin() const
359 return base_type::cbegin();
362 const_iterator cend() const
364 return base_type::cend();
367 using base_type::begin;
368 using base_type::end;
369 using base_type::cbegin;
370 using base_type::cend;
374 local_iterator begin( size_type n )
376 return local_iterator( this->base().begin( n ) );
379 const_local_iterator begin( size_type n ) const
381 return const_local_iterator( this->base().begin( n ) );
384 local_iterator end( size_type n )
386 return local_iterator( this->base().end( n ) );
389 const_local_iterator end( size_type n ) const
391 return const_local_iterator( this->base().end( n ) );
394 const_local_iterator cbegin( size_type n ) const
396 return const_local_iterator( this->base().cbegin( n ) );
399 const_local_iterator cend( size_type n )
401 return const_local_iterator( this->base().cend( n ) );
404 }; // class 'associative_ptr_container'
406 } // namespace 'ptr_container_detail'
408 } // namespace 'boost'