source: NonGTP/Boost/boost/ptr_container/ptr_set_adapter.hpp @ 857

Revision 857, 19.8 KB checked in by igarcia, 18 years ago (diff)
Line 
1//
2// Boost.Pointer Container
3//
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)
8//
9// For more information, see http://www.boost.org/libs/ptr_container/
10//
11
12#ifndef BOOST_PTR_CONTAINER_PTR_SET_ADAPTER_HPP
13#define BOOST_PTR_CONTAINER_PTR_SET_ADAPTER_HPP
14
15#if defined(_MSC_VER) && (_MSC_VER >= 1200)
16# pragma once
17#endif
18
19#include <boost/ptr_container/detail/associative_ptr_container.hpp>
20#include <boost/ptr_container/detail/void_ptr_iterator.hpp>
21#include <boost/range/reverse_iterator.hpp>
22#include <boost/range/const_reverse_iterator.hpp>
23#include <boost/range/iterator_range.hpp>
24
25namespace boost
26{
27namespace ptr_container_detail
28{
29    template
30    <
31        class Key,
32        class VoidPtrSet
33    >
34    struct set_config
35    {
36       typedef VoidPtrSet
37                    void_container_type;
38
39       typedef BOOST_DEDUCED_TYPENAME VoidPtrSet::allocator_type
40                    allocator_type;
41
42       typedef Key  value_type;
43
44       typedef value_type
45                    key_type;
46
47       typedef BOOST_DEDUCED_TYPENAME VoidPtrSet::value_compare
48                    value_compare;
49
50       typedef value_compare
51                    key_compare;
52
53       typedef void_ptr_iterator<
54                       BOOST_DEDUCED_TYPENAME VoidPtrSet::iterator, Key >
55                    iterator;
56
57       typedef void_ptr_iterator<
58                        BOOST_DEDUCED_TYPENAME VoidPtrSet::const_iterator, const Key >
59                    const_iterator;
60
61       typedef void_ptr_iterator<
62                       BOOST_DEDUCED_TYPENAME VoidPtrSet::reverse_iterator, Key >
63                   reverse_iterator;
64       
65       typedef void_ptr_iterator<
66                       BOOST_DEDUCED_TYPENAME VoidPtrSet::const_reverse_iterator, const Key >
67                   const_reverse_iterator;
68
69       typedef Key object_type;
70
71       template< class Iter >
72       static Key* get_pointer( Iter i )
73       {
74           return static_cast<Key*>( *i.base() );
75       }
76
77       template< class Iter >
78       static const Key* get_const_pointer( Iter i )
79       {
80           return static_cast<const Key*>( *i.base() );
81       }
82
83       BOOST_STATIC_CONSTANT(bool, allow_null = false );
84    };
85
86 
87   
88    template
89    <
90        class Key,
91        class VoidPtrSet,
92        class CloneAllocator = heap_clone_allocator
93    >
94    class ptr_set_adapter_base
95        : public ptr_container_detail::associative_ptr_container< set_config<Key,VoidPtrSet>,
96                                                      CloneAllocator >
97    {
98        typedef ptr_container_detail::associative_ptr_container< set_config<Key,VoidPtrSet>,
99                                                     CloneAllocator >
100              base_type;
101    public: 
102        typedef BOOST_DEDUCED_TYPENAME base_type::iterator
103                      iterator;
104        typedef BOOST_DEDUCED_TYPENAME base_type::const_iterator
105                      const_iterator;
106        typedef BOOST_DEDUCED_TYPENAME base_type::object_type
107                      key_type;
108        typedef BOOST_DEDUCED_TYPENAME base_type::size_type
109                      size_type;
110       
111    public:
112       template< class Compare, class Allocator >
113       ptr_set_adapter_base( const Compare& comp,
114                             const Allocator& a )
115         : base_type( comp, a )
116       { }
117
118       template< class InputIterator, class Compare, class Allocator >
119       ptr_set_adapter_base( InputIterator first, InputIterator last,
120                             const Compare& comp,
121                             const Allocator& a )
122         : base_type( first, last, comp, a )
123       { }
124
125        template< class PtrContainer >
126        ptr_set_adapter_base( std::auto_ptr<PtrContainer> clone )
127         : base_type( clone )
128        { }
129       
130        template< typename PtrContainer >
131        void operator=( std::auto_ptr<PtrContainer> clone )   
132        {
133            base_type::operator=( clone );
134        }
135                                                                                                             
136        iterator find( const key_type& x )                                               
137        {                                                                           
138            return iterator( this->c_private().
139                             find( const_cast<key_type*>(&x) ) );                               
140        }                                                                           
141
142        const_iterator find( const key_type& x ) const                                   
143        {                                                                           
144            return const_iterator( this->c_private().
145                                   find( const_cast<key_type*>(&x) ) );                         
146        }                                                                           
147
148        size_type count( const key_type& x ) const                                       
149        {                                                                           
150            return this->c_private().count( const_cast<key_type*>(&x) );                                           
151        }                                                                           
152                                                                                     
153        iterator lower_bound( const key_type& x )                                         
154        {                                                                           
155            return iterator( this->c_private().
156                             lower_bound( const_cast<key_type*>(&x) ) );                         
157        }                                                                           
158                                                                                     
159        const_iterator lower_bound( const key_type& x ) const                             
160        {                                                                           
161            return const_iterator( this->c_private().
162                                   lower_bound( const_cast<key_type*>(&x) ) );                   
163        }                                                                           
164                                                                                     
165        iterator upper_bound( const key_type& x )                                         
166        {                                                                           
167            return iterator( this->c_private().
168                             upper_bound( const_cast<key_type*>(&x) ) );                         
169        }                                                                           
170                                                                                     
171        const_iterator upper_bound( const key_type& x ) const                             
172        {                                                                           
173            return const_iterator( this->c_private().
174                                   upper_bound( const_cast<key_type*>(&x) ) );                   
175        }                                                                           
176                                                                                     
177        iterator_range<iterator> equal_range( const key_type& x )                   
178        {                                                                           
179            std::pair<BOOST_DEDUCED_TYPENAME base_type::ptr_iterator,
180                      BOOST_DEDUCED_TYPENAME base_type::ptr_iterator>
181                p = this->c_private().
182                equal_range( const_cast<key_type*>(&x) );   
183            return make_iterator_range( iterator( p.first ),
184                                        iterator( p.second ) );     
185        }                                                                           
186                                                                                     
187        iterator_range<const_iterator> equal_range( const key_type& x ) const 
188        {                                                                           
189            std::pair<BOOST_DEDUCED_TYPENAME base_type::ptr_const_iterator,
190                      BOOST_DEDUCED_TYPENAME base_type::ptr_const_iterator>
191                p = this->c_private().
192                equal_range( const_cast<key_type*>(&x) );
193            return make_iterator_range( const_iterator( p.first ),
194                                        const_iterator( p.second ) );   
195        }                                                                           
196                                                                                     
197    };
198
199} // ptr_container_detail
200
201    /////////////////////////////////////////////////////////////////////////
202    // ptr_set_adapter
203    /////////////////////////////////////////////////////////////////////////
204 
205    template
206    <
207        class Key,
208        class VoidPtrSet,
209        class CloneAllocator = heap_clone_allocator
210    >
211    class ptr_set_adapter :
212        public ptr_container_detail::ptr_set_adapter_base<Key,VoidPtrSet,CloneAllocator>
213    {
214        typedef ptr_container_detail::ptr_set_adapter_base<Key,VoidPtrSet,CloneAllocator>
215            base_type;
216   
217    public: // typedefs
218       
219        typedef BOOST_DEDUCED_TYPENAME base_type::iterator
220                     iterator;                 
221        typedef BOOST_DEDUCED_TYPENAME base_type::const_iterator
222                     const_iterator;                 
223        typedef BOOST_DEDUCED_TYPENAME base_type::size_type
224                     size_type;   
225        typedef BOOST_DEDUCED_TYPENAME base_type::object_type
226                     key_type;
227        typedef BOOST_DEDUCED_TYPENAME base_type::auto_type
228                     auto_type;
229        typedef BOOST_DEDUCED_TYPENAME VoidPtrSet::key_compare
230                      key_compare;
231        typedef BOOST_DEDUCED_TYPENAME VoidPtrSet::allocator_type
232                      allocator_type;       
233    private:
234       
235        template< typename II >                                               
236        void set_basic_clone_and_insert( II first, II last ) // basic                 
237        {                                                                     
238            while( first != last )                                           
239            {           
240                if( this->find( *first ) == this->end() )
241                    insert( CloneAllocator::allocate_clone( *first ) ); // strong, commit
242                ++first;                                                     
243            }                                                                 
244        }                         
245
246    public:
247       
248        explicit ptr_set_adapter( const key_compare& comp = key_compare(),
249                                  const allocator_type& a = allocator_type() )
250          : base_type( comp, a )
251        {
252            BOOST_ASSERT( this->empty() );
253        }
254       
255        template< class InputIterator, class Compare, class Allocator >
256        ptr_set_adapter( InputIterator first, InputIterator last,
257                         const Compare& comp = Compare(),
258                         const Allocator a = Allocator() )
259          : base_type( comp, a )
260        {
261            BOOST_ASSERT( this->empty() );
262            set_basic_clone_and_insert( first, last );
263        }
264
265        template< class T >
266        ptr_set_adapter( std::auto_ptr<T> r ) : base_type( r )
267        { }
268
269        template< class T >
270        void operator=( std::auto_ptr<T> r )
271        {
272            base_type::operator=( r );
273        }
274
275        std::pair<iterator,bool> insert( key_type* x ) // strong                     
276        {       
277            this->enforce_null_policy( x, "Null pointer in 'ptr_set::insert()'" );
278           
279            auto_type ptr( x );                               
280            std::pair<BOOST_DEDUCED_TYPENAME base_type::ptr_iterator,bool>
281                 res = this->c_private().insert( x );       
282            if( res.second )                                                 
283                ptr.release();                                                 
284            return std::make_pair( iterator( res.first ), res.second );     
285        }
286
287       
288        iterator insert( iterator where, key_type* x ) // strong
289        {
290            this->enforce_null_policy( x, "Null pointer in 'ptr_set::insert()'" );
291
292            auto_type ptr( x );                               
293            BOOST_DEDUCED_TYPENAME base_type::ptr_iterator
294                res = this->c_private().insert( where.base(), x );
295            if( *res == x )                                                 
296                ptr.release();                                                 
297            return iterator( res);
298        }
299       
300        template< typename InputIterator >
301        void insert( InputIterator first, InputIterator last ) // basic
302        {
303            set_basic_clone_and_insert( first, last );
304        }
305
306#ifdef BOOST_NO_SFINAE
307#else   
308       
309        template< class Range >
310        BOOST_DEDUCED_TYPENAME
311        boost::disable_if< ptr_container_detail::is_pointer_or_integral<Range> >::type
312        insert( const Range& r )
313        {
314            insert( this->adl_begin(r), this->adl_end(r) );
315        }
316
317#endif       
318       
319        bool transfer( iterator object,
320                       ptr_set_adapter& from ) // strong
321        {
322            return this->single_transfer( object, from );
323        }
324
325        size_type transfer( iterator first,
326                            iterator last,
327                            ptr_set_adapter& from ) // basic
328        {
329            return this->single_transfer( first, last, from );
330        }
331
332#ifdef BOOST_NO_SFINAE
333#else   
334
335        template< class Range >
336        BOOST_DEDUCED_TYPENAME boost::disable_if< boost::is_same< Range,
337                                                                  iterator >,
338                                                            size_type >::type
339        transfer( const Range& r, ptr_set_adapter& from ) // basic
340        {
341            return transfer( this->adl_begin(r), this->adl_end(r), from );
342        }
343
344#endif
345
346        size_type transfer( ptr_set_adapter& from ) // basic
347        {
348            return transfer( from.begin(), from.end(), from );
349        }
350
351    };
352   
353    /////////////////////////////////////////////////////////////////////////
354    // ptr_multiset_adapter
355    /////////////////////////////////////////////////////////////////////////
356
357    template
358    <
359        class Key,
360        class VoidPtrMultiSet,
361        class CloneAllocator = heap_clone_allocator
362    >
363    class ptr_multiset_adapter :
364        public ptr_container_detail::ptr_set_adapter_base<Key,VoidPtrMultiSet,CloneAllocator>
365    {
366         typedef ptr_container_detail::ptr_set_adapter_base<Key,VoidPtrMultiSet,CloneAllocator> base_type;
367   
368    public: // typedefs
369   
370        typedef BOOST_DEDUCED_TYPENAME base_type::iterator   
371                       iterator;         
372        typedef BOOST_DEDUCED_TYPENAME base_type::size_type
373                       size_type;
374        typedef BOOST_DEDUCED_TYPENAME base_type::object_type
375                      key_type;
376        typedef BOOST_DEDUCED_TYPENAME base_type::auto_type
377                      auto_type;
378        typedef BOOST_DEDUCED_TYPENAME VoidPtrMultiSet::key_compare
379                      key_compare;
380        typedef BOOST_DEDUCED_TYPENAME VoidPtrMultiSet::allocator_type
381                      allocator_type;       
382    private:
383        template< typename II >                                               
384        void set_basic_clone_and_insert( II first, II last ) // basic                 
385        {               
386            //BOOST_ASSERT( first != this->begin() );
387            //BOOST_ASSERT( last != this->end() );
388                                                                 
389            while( first != last )                                           
390            {           
391                insert( CloneAllocator::allocate_clone( *first ) ); // strong, commit                             
392                ++first;                                                     
393            }                                                                 
394        }                         
395   
396    public:
397
398        explicit ptr_multiset_adapter( const key_compare& comp = key_compare(),
399                                       const allocator_type& a = allocator_type() )
400        : base_type( comp, a )
401        { }
402   
403        template< class InputIterator >
404        ptr_multiset_adapter( InputIterator first, InputIterator last,
405                              const key_compare& comp = key_compare(),
406                              const allocator_type& a = allocator_type() )
407        : base_type( comp, a )
408        {
409            set_basic_clone_and_insert( first, last );
410        }
411
412        template< class T >
413        ptr_multiset_adapter( std::auto_ptr<T> r ) : base_type( r )
414        { }
415
416        template< class T >
417        void operator=( std::auto_ptr<T> r )
418        {
419            base_type::operator=( r );
420        }
421
422        iterator insert( iterator before, key_type* x ) // strong 
423        {
424            return base_type::insert( before, x );
425        }
426   
427        iterator insert( key_type* x ) // strong                                     
428        {   
429            this->enforce_null_policy( x, "Null pointer in 'ptr_multiset::insert()'" );
430   
431            auto_type ptr( x );                               
432            BOOST_DEDUCED_TYPENAME base_type::ptr_iterator
433                 res = this->c_private().insert( x );                         
434            ptr.release();                                                     
435            return iterator( res );                                             
436        }
437   
438        template< typename InputIterator >
439        void insert( InputIterator first, InputIterator last ) // basic
440        {
441            set_basic_clone_and_insert( first, last );
442        }
443
444#ifdef BOOST_NO_SFINAE
445#else   
446       
447        template< class Range >
448        BOOST_DEDUCED_TYPENAME
449        boost::disable_if< ptr_container_detail::is_pointer_or_integral<Range> >::type
450        insert( const Range& r )
451        {
452            insert( this->adl_begin(r), this->adl_end(r) );
453        }
454
455#endif
456
457        void transfer( iterator object,
458                       ptr_multiset_adapter& from ) // strong
459        {
460            this->multi_transfer( object, from );
461        }
462
463        size_type transfer( iterator first,
464                            iterator last,
465                            ptr_multiset_adapter& from ) // basic
466        {
467            return this->multi_transfer( first, last, from );
468        }
469
470#ifdef BOOST_NO_SFINAE
471#else   
472       
473        template< class Range >
474        BOOST_DEDUCED_TYPENAME boost::disable_if< boost::is_same< Range,
475                                           iterator >, size_type >::type
476        transfer(  const Range& r, ptr_multiset_adapter& from ) // basic
477        {
478            return transfer( this->adl_begin(r), this->adl_end(r), from );
479        }
480
481#endif       
482
483        void transfer( ptr_multiset_adapter& from ) // basic
484        {
485            transfer( from.begin(), from.end(), from );
486            BOOST_ASSERT( from.empty() );
487        }
488    };
489
490} // namespace 'boost' 
491
492#endif
Note: See TracBrowser for help on using the repository browser.