]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/ptr_container/test/view_example.cpp
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 // This example is intended to show you how to
14 // use the 'view_clone_manager'. The idea
15 // is that we have a container of non-polymorphic
16 // objects and want to keep then sorted by different
17 // criteria at the same time.
21 // We'll go for 'ptr_vector' here. Using a node-based
22 // container would be a waste of space here.
23 // All container headers will also include
24 // the Clone Managers.
26 #include <boost/ptr_container/ptr_vector.hpp>
27 #include <boost/ptr_container/indirect_fun.hpp>
29 #include <functional> // For 'binary_fnuction'
30 #include <cstdlib> // For 'rand()'
31 #include <algorithm> // For 'std::sort()'
32 #include <iostream> // For 'std::cout'
37 // This is our simple example data-structure. It can
38 // be ordered in three ways.
42 photon() : color( rand() ),
53 // Our big container is a standard vector
55 typedef std::vector
<photon
> vector_type
;
58 // Now we define our view type by adding a second template argument.
59 // The 'view_clone_manager' will implements Cloning by taking address
62 // Notice the first template argument is 'photon' and not
63 // 'const photon' to allow the view container write access.
65 typedef boost::ptr_vector
<photon
,boost::view_clone_allocator
> view_type
;
68 // Our first sort criterium
70 struct sort_by_color
: std::binary_function
<photon
,photon
,bool>
72 bool operator()( const photon
& l
, const photon
& r
) const
74 return l
.color
< r
.color
;
79 // Our second sort criterium
81 struct sort_by_direction
: std::binary_function
<photon
,photon
,bool>
83 bool operator()( const photon
& l
, const photon
& r
) const
85 return l
.direction
< r
.direction
;
91 // Our third sort criterium
93 struct sort_by_power
: std::binary_function
<photon
,photon
,bool>
95 bool operator()( const photon
& l
, const photon
& r
) const
97 return l
.power
< r
.power
;
102 // This function inserts "Clones" into the
105 // We need to pass the first argument
106 // as a non-const reference to be able to store
107 // 'T*' instead of 'const T*' objects. Alternatively,
108 // we might change the declaration of the 'view_type'
110 // typedef boost::ptr_vector<const photon,boost::view_clone_manager>
113 void insert( vector_type
& from
, view_type
& to
)
122 enum { sz
= 10, count
= 500 };
125 // First we create the main container and two views
127 std::vector
<vector_type
> photons
;
128 view_type color_view
;
129 view_type direction_view
;
132 // Then we fill the main container with some random data
134 for( int i
= 0; i
!= sz
; ++i
)
136 photons
.push_back( vector_type() );
138 for( int j
= 0; j
!= count
; ++j
)
139 photons
[i
].push_back( photon() );
143 // Then we create the two views.
145 for( int i
= 0; i
!= sz
; ++i
)
147 insert( photons
[i
], color_view
);
148 insert( photons
[i
], direction_view
);
152 // First we sort the original photons, using one of
153 // the view classes. This may sound trivial, but consider that
154 // the objects are scatered all around 'sz' different vectors;
155 // the view makes them act as one big vector.
157 std::sort( color_view
.begin(), color_view
.end(), sort_by_power() );
160 // And now we can sort the views themselves. Notice how
161 // we switch to different iterators and different predicates:
163 color_view
.sort( sort_by_color() );
165 direction_view
.sort( sort_by_direction() );