source: NonGTP/Boost/boost/ptr_container/detail/associative_ptr_container.hpp @ 857

Revision 857, 7.3 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
13#ifndef BOOST_PTR_CONTAINER_DETAIL_ASSOCIATIVE_PTR_CONTAINER_HPP
14#define BOOST_PTR_CONTAINER_DETAIL_ASSOCIATIVE_PTR_CONTAINER_HPP
15
16#if defined(_MSC_VER) && (_MSC_VER >= 1200)
17# pragma once
18#endif
19
20#include <boost/ptr_container/detail/reversible_ptr_container.hpp>
21
22namespace boost
23{
24
25namespace ptr_container_detail
26{
27    template
28    <
29        class Config,
30        class CloneAllocator
31    >
32    class associative_ptr_container :
33        public reversible_ptr_container<Config,CloneAllocator>
34    {
35        typedef reversible_ptr_container<Config,CloneAllocator>
36                                base_type;
37
38        typedef BOOST_DEDUCED_TYPENAME base_type::scoped_deleter
39                                scoped_deleter;
40
41    public: // typedefs
42        typedef BOOST_DEDUCED_TYPENAME Config::key_type
43                                key_type;
44        typedef BOOST_DEDUCED_TYPENAME Config::key_compare
45                                key_compare;
46        typedef BOOST_DEDUCED_TYPENAME Config::value_compare
47                                value_compare;
48        typedef BOOST_DEDUCED_TYPENAME Config::iterator
49                                iterator;
50        typedef BOOST_DEDUCED_TYPENAME Config::const_iterator
51                                const_iterator;
52        typedef BOOST_DEDUCED_TYPENAME base_type::size_type
53                                size_type;
54
55    public: // foundation
56       
57       template< class Compare, class Allocator >
58       associative_ptr_container( const Compare& comp,
59                                    const Allocator& a )
60         : base_type( comp, a )
61       { }
62
63       template< class InputIterator, class Compare, class Allocator >
64       associative_ptr_container( InputIterator first, InputIterator last,
65                                    const Compare& comp,
66                                    const Allocator& a )
67         : base_type( first, last, comp, a )
68       { }
69
70       template< class PtrContainer >
71       associative_ptr_container( std::auto_ptr<PtrContainer> r )
72         : base_type( r, key_compare() )
73       { }
74       
75       template< class PtrContainer >
76       void operator=( std::auto_ptr<PtrContainer> r )
77       {
78           base_type::operator=( r );
79       }
80
81    public: // associative container interface
82        key_compare key_comp() const
83        {
84            return this->c_private().key_comp();
85        }
86       
87        value_compare value_comp() const
88        {
89            return this->c_private().value_comp();
90        }
91       
92        iterator erase( iterator before ) // nothrow
93        {
94            BOOST_ASSERT( !this->empty() );
95            BOOST_ASSERT( before != this->end() );
96           
97            this->remove( before );                      // nothrow
98            iterator res( before );                      // nothrow   
99            ++res;                                       // nothrow
100            this->c_private().erase( before.base() );    // nothrow
101            return res;                                  // nothrow
102        }
103       
104        size_type erase( const key_type& x ) // nothrow
105        {
106            BOOST_ASSERT( !this->empty() );       
107            iterator i = find( x );                     // nothrow
108            if( i == this->end() )                      // nothrow
109                return 0;                               // nothrow
110            this->remove( i );                          // nothrow
111            return this->c_private().erase( i.base() ); // nothrow
112        }
113       
114        iterator erase( iterator first,
115                        iterator last ) // nothrow
116        {
117            BOOST_ASSERT( !this->empty() );
118
119            iterator res( last );                                // nothrow
120            if( res != this->end() )
121                ++res;                                           // nothrow
122
123            this->remove( first, last );                         // nothrow
124            this->c_private().erase( first.base(), last.base() );// nothrow
125            return res;                                          // nothrow
126        }
127
128    protected:
129
130        void multi_transfer( iterator object,
131                             associative_ptr_container& from ) // strong
132        {
133            BOOST_ASSERT( &from != this );
134            BOOST_ASSERT( !from.empty() && "Cannot transfer from empty container" );
135
136            this->c_private().insert( *object.base() );     // strong
137            from.c_private().erase( object.base() );        // nothrow
138        }
139
140        size_type multi_transfer( iterator first,
141                                  iterator last,
142                                  associative_ptr_container& from ) // basic
143        {
144            BOOST_ASSERT( &from != this );
145            BOOST_ASSERT( !from.empty() && "Cannot transfer from empty container" );
146
147            size_type res = 0;
148            for( ; first != last; )
149            {
150                BOOST_ASSERT( first != from.end() );
151                this->c_private().insert( *first.base() );     // strong
152                iterator to_delete( first );
153                ++first;
154                from.c_private().erase( to_delete.base() );    // nothrow
155                ++res;
156            }
157
158            return res;
159        }
160
161        bool single_transfer( iterator object,
162                              associative_ptr_container& from ) // strong
163        {
164            BOOST_ASSERT( &from != this );
165            BOOST_ASSERT( !from.empty() && "Cannot transfer from empty container" );
166
167            std::pair<BOOST_DEDUCED_TYPENAME base_type::ptr_iterator,bool> p =
168                this->c_private().insert( *object.base() );     // strong
169            if( p.second )
170                from.c_private().erase( object.base() );        // nothrow
171
172            return p.second;
173        }
174
175        size_type single_transfer( iterator first,
176                                   iterator last,
177                                   associative_ptr_container& from ) // basic
178        {
179            BOOST_ASSERT( &from != this );
180            BOOST_ASSERT( !from.empty() && "Cannot transfer from empty container" );
181
182            size_type res = 0;
183            for( ; first != last; )
184            {
185                BOOST_ASSERT( first != from.end() );
186                std::pair<BOOST_DEDUCED_TYPENAME base_type::ptr_iterator,bool> p =
187                    this->c_private().insert( *first.base() );     // strong
188                iterator to_delete( first );
189                ++first;
190                if( p.second )
191                {
192                    from.c_private().erase( to_delete.base() );   // nothrow
193                    ++res;
194                }
195            }
196            return res;
197        }
198       
199     }; // class 'associative_ptr_container'
200   
201} // namespace 'ptr_container_detail'
202   
203} // namespace 'boost'
204
205
206#endif
Note: See TracBrowser for help on using the repository browser.