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/
12 #ifndef BOOST_PTR_CONTAINER_PTR_SET_ADAPTER_HPP
13 #define BOOST_PTR_CONTAINER_PTR_SET_ADAPTER_HPP
15 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
19 #include <boost/ptr_container/detail/associative_ptr_container.hpp>
20 #include <boost/ptr_container/detail/void_ptr_iterator.hpp>
21 #include <boost/range/iterator_range.hpp>
25 namespace ptr_container_detail
37 typedef BOOST_DEDUCED_TYPENAME VoidPtrSet::allocator_type
40 typedef Key value_type;
45 typedef BOOST_DEDUCED_TYPENAME VoidPtrSet::value_compare
51 typedef void_ptr_iterator<
52 BOOST_DEDUCED_TYPENAME VoidPtrSet::iterator, Key >
55 typedef void_ptr_iterator<
56 BOOST_DEDUCED_TYPENAME VoidPtrSet::const_iterator, const Key >
59 template< class Iter >
60 static Key* get_pointer( Iter i )
62 return static_cast<Key*>( *i.base() );
65 template< class Iter >
66 static const Key* get_const_pointer( Iter i )
68 return static_cast<const Key*>( *i.base() );
71 BOOST_STATIC_CONSTANT(bool, allow_null = false );
80 class CloneAllocator = heap_clone_allocator
82 class ptr_set_adapter_base
83 : public ptr_container_detail::associative_ptr_container< set_config<Key,VoidPtrSet>,
86 typedef ptr_container_detail::associative_ptr_container< set_config<Key,VoidPtrSet>,
90 typedef BOOST_DEDUCED_TYPENAME base_type::iterator
92 typedef BOOST_DEDUCED_TYPENAME base_type::const_iterator
95 typedef BOOST_DEDUCED_TYPENAME base_type::size_type
99 ptr_set_adapter_base()
100 : base_type( BOOST_DEDUCED_TYPENAME VoidPtrSet::key_compare(),
101 BOOST_DEDUCED_TYPENAME VoidPtrSet::allocator_type() )
106 template< class Compare, class Allocator >
107 ptr_set_adapter_base( const Compare& comp,
109 : base_type( comp, a )
112 template< class InputIterator, class Compare, class Allocator >
113 ptr_set_adapter_base( InputIterator first, InputIterator last,
116 : base_type( first, last, comp, a )
119 template< class PtrContainer >
120 ptr_set_adapter_base( std::auto_ptr<PtrContainer> clone )
124 template< typename PtrContainer >
125 void operator=( std::auto_ptr<PtrContainer> clone )
127 base_type::operator=( clone );
130 iterator find( const key_type& x )
132 return iterator( this->c_private().
133 find( const_cast<key_type*>(&x) ) );
136 const_iterator find( const key_type& x ) const
138 return const_iterator( this->c_private().
139 find( const_cast<key_type*>(&x) ) );
142 size_type count( const key_type& x ) const
144 return this->c_private().count( const_cast<key_type*>(&x) );
147 iterator lower_bound( const key_type& x )
149 return iterator( this->c_private().
150 lower_bound( const_cast<key_type*>(&x) ) );
153 const_iterator lower_bound( const key_type& x ) const
155 return const_iterator( this->c_private().
156 lower_bound( const_cast<key_type*>(&x) ) );
159 iterator upper_bound( const key_type& x )
161 return iterator( this->c_private().
162 upper_bound( const_cast<key_type*>(&x) ) );
165 const_iterator upper_bound( const key_type& x ) const
167 return const_iterator( this->c_private().
168 upper_bound( const_cast<key_type*>(&x) ) );
171 iterator_range<iterator> equal_range( const key_type& x )
173 std::pair<BOOST_DEDUCED_TYPENAME base_type::ptr_iterator,
174 BOOST_DEDUCED_TYPENAME base_type::ptr_iterator>
175 p = this->c_private().
176 equal_range( const_cast<key_type*>(&x) );
177 return make_iterator_range( iterator( p.first ),
178 iterator( p.second ) );
181 iterator_range<const_iterator> equal_range( const key_type& x ) const
183 std::pair<BOOST_DEDUCED_TYPENAME base_type::ptr_const_iterator,
184 BOOST_DEDUCED_TYPENAME base_type::ptr_const_iterator>
185 p = this->c_private().
186 equal_range( const_cast<key_type*>(&x) );
187 return make_iterator_range( const_iterator( p.first ),
188 const_iterator( p.second ) );
193 } // ptr_container_detail
195 /////////////////////////////////////////////////////////////////////////
197 /////////////////////////////////////////////////////////////////////////
203 class CloneAllocator = heap_clone_allocator
205 class ptr_set_adapter :
206 public ptr_container_detail::ptr_set_adapter_base<Key,VoidPtrSet,CloneAllocator>
208 typedef ptr_container_detail::ptr_set_adapter_base<Key,VoidPtrSet,CloneAllocator>
213 typedef BOOST_DEDUCED_TYPENAME base_type::iterator
215 typedef BOOST_DEDUCED_TYPENAME base_type::const_iterator
217 typedef BOOST_DEDUCED_TYPENAME base_type::size_type
219 typedef Key key_type;
220 typedef BOOST_DEDUCED_TYPENAME base_type::auto_type
222 typedef BOOST_DEDUCED_TYPENAME VoidPtrSet::key_compare
224 typedef BOOST_DEDUCED_TYPENAME VoidPtrSet::allocator_type
228 template< typename II >
229 void set_basic_clone_and_insert( II first, II last ) // basic
231 while( first != last )
233 if( this->find( *first ) == this->end() )
234 insert( CloneAllocator::allocate_clone( *first ) ); // strong, commit
241 explicit ptr_set_adapter( const key_compare& comp = key_compare(),
242 const allocator_type& a = allocator_type() )
243 : base_type( comp, a )
245 BOOST_ASSERT( this->empty() );
248 template< class InputIterator, class Compare, class Allocator >
249 ptr_set_adapter( InputIterator first, InputIterator last,
250 const Compare& comp = Compare(),
251 const Allocator a = Allocator() )
252 : base_type( comp, a )
254 BOOST_ASSERT( this->empty() );
255 set_basic_clone_and_insert( first, last );
259 ptr_set_adapter( std::auto_ptr<T> r ) : base_type( r )
263 void operator=( std::auto_ptr<T> r )
265 base_type::operator=( r );
268 std::pair<iterator,bool> insert( key_type* x ) // strong
270 this->enforce_null_policy( x, "Null pointer in 'ptr_set::insert()'" );
273 std::pair<BOOST_DEDUCED_TYPENAME base_type::ptr_iterator,bool>
274 res = this->c_private().insert( x );
277 return std::make_pair( iterator( res.first ), res.second );
281 std::pair<iterator,bool> insert( std::auto_ptr<U> x )
283 return insert( x.release() );
287 iterator insert( iterator where, key_type* x ) // strong
289 this->enforce_null_policy( x, "Null pointer in 'ptr_set::insert()'" );
292 BOOST_DEDUCED_TYPENAME base_type::ptr_iterator
293 res = this->c_private().insert( where.base(), x );
296 return iterator( res);
300 iterator insert( iterator where, std::auto_ptr<U> x )
302 return insert( where, x.release() );
305 template< typename InputIterator >
306 void insert( InputIterator first, InputIterator last ) // basic
308 set_basic_clone_and_insert( first, last );
311 #if defined(BOOST_NO_SFINAE) || BOOST_WORKAROUND(__SUNPRO_CC, <= 0x580)
314 template< class Range >
315 BOOST_DEDUCED_TYPENAME
316 boost::disable_if< ptr_container_detail::is_pointer_or_integral<Range> >::type
317 insert( const Range& r )
319 insert( boost::begin(r), boost::end(r) );
324 template< class PtrSetAdapter >
325 bool transfer( BOOST_DEDUCED_TYPENAME PtrSetAdapter::iterator object,
326 PtrSetAdapter& from ) // strong
328 return this->single_transfer( object, from );
331 template< class PtrSetAdapter >
333 transfer( BOOST_DEDUCED_TYPENAME PtrSetAdapter::iterator first,
334 BOOST_DEDUCED_TYPENAME PtrSetAdapter::iterator last,
335 PtrSetAdapter& from ) // basic
337 return this->single_transfer( first, last, from );
340 #if defined(BOOST_NO_SFINAE) || BOOST_WORKAROUND(__SUNPRO_CC, <= 0x580)
343 template< class PtrSetAdapter, class Range >
344 BOOST_DEDUCED_TYPENAME boost::disable_if< boost::is_same< Range,
345 BOOST_DEDUCED_TYPENAME PtrSetAdapter::iterator >,
347 transfer( const Range& r, PtrSetAdapter& from ) // basic
349 return transfer( boost::begin(r), boost::end(r), from );
354 template< class PtrSetAdapter >
355 size_type transfer( PtrSetAdapter& from ) // basic
357 return transfer( from.begin(), from.end(), from );
362 /////////////////////////////////////////////////////////////////////////
363 // ptr_multiset_adapter
364 /////////////////////////////////////////////////////////////////////////
369 class VoidPtrMultiSet,
370 class CloneAllocator = heap_clone_allocator
372 class ptr_multiset_adapter :
373 public ptr_container_detail::ptr_set_adapter_base<Key,VoidPtrMultiSet,CloneAllocator>
375 typedef ptr_container_detail::ptr_set_adapter_base<Key,VoidPtrMultiSet,CloneAllocator> base_type;
379 typedef BOOST_DEDUCED_TYPENAME base_type::iterator
381 typedef BOOST_DEDUCED_TYPENAME base_type::size_type
383 typedef Key key_type;
384 typedef BOOST_DEDUCED_TYPENAME base_type::auto_type
386 typedef BOOST_DEDUCED_TYPENAME VoidPtrMultiSet::key_compare
388 typedef BOOST_DEDUCED_TYPENAME VoidPtrMultiSet::allocator_type
391 template< typename II >
392 void set_basic_clone_and_insert( II first, II last ) // basic
394 while( first != last )
396 insert( CloneAllocator::allocate_clone( *first ) ); // strong, commit
403 explicit ptr_multiset_adapter( const key_compare& comp = key_compare(),
404 const allocator_type& a = allocator_type() )
405 : base_type( comp, a )
408 template< class InputIterator >
409 ptr_multiset_adapter( InputIterator first, InputIterator last,
410 const key_compare& comp = key_compare(),
411 const allocator_type& a = allocator_type() )
412 : base_type( comp, a )
414 set_basic_clone_and_insert( first, last );
418 ptr_multiset_adapter( std::auto_ptr<T> r ) : base_type( r )
422 void operator=( std::auto_ptr<T> r )
424 base_type::operator=( r );
427 iterator insert( iterator before, key_type* x ) // strong
429 return base_type::insert( before, x );
433 iterator insert( iterator before, std::auto_ptr<U> x )
435 return insert( before, x.release() );
438 iterator insert( key_type* x ) // strong
440 this->enforce_null_policy( x, "Null pointer in 'ptr_multiset::insert()'" );
443 BOOST_DEDUCED_TYPENAME base_type::ptr_iterator
444 res = this->c_private().insert( x );
446 return iterator( res );
450 iterator insert( std::auto_ptr<U> x )
452 return insert( x.release() );
455 template< typename InputIterator >
456 void insert( InputIterator first, InputIterator last ) // basic
458 set_basic_clone_and_insert( first, last );
461 #if defined(BOOST_NO_SFINAE) || BOOST_WORKAROUND(__SUNPRO_CC, <= 0x580)
464 template< class Range >
465 BOOST_DEDUCED_TYPENAME
466 boost::disable_if< ptr_container_detail::is_pointer_or_integral<Range> >::type
467 insert( const Range& r )
469 insert( boost::begin(r), boost::end(r) );
474 template< class PtrSetAdapter >
475 void transfer( BOOST_DEDUCED_TYPENAME PtrSetAdapter::iterator object,
476 PtrSetAdapter& from ) // strong
478 this->multi_transfer( object, from );
481 template< class PtrSetAdapter >
482 size_type transfer( BOOST_DEDUCED_TYPENAME PtrSetAdapter::iterator first,
483 BOOST_DEDUCED_TYPENAME PtrSetAdapter::iterator last,
484 PtrSetAdapter& from ) // basic
486 return this->multi_transfer( first, last, from );
489 #if defined(BOOST_NO_SFINAE) || BOOST_WORKAROUND(__SUNPRO_CC, <= 0x580)
492 template< class PtrSetAdapter, class Range >
493 BOOST_DEDUCED_TYPENAME boost::disable_if< boost::is_same< Range,
494 BOOST_DEDUCED_TYPENAME PtrSetAdapter::iterator >, size_type >::type
495 transfer( const Range& r, PtrSetAdapter& from ) // basic
497 return transfer( boost::begin(r), boost::end(r), from );
502 template< class PtrSetAdapter >
503 void transfer( PtrSetAdapter& from ) // basic
505 transfer( from.begin(), from.end(), from );
506 BOOST_ASSERT( from.empty() );
511 } // namespace 'boost'