]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/ptr_container/test/view_example.cpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / libs / ptr_container / test / view_example.cpp
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 // 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.
18 //
19
20 //
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.
25 //
26 #include <boost/ptr_container/ptr_vector.hpp>
27 #include <boost/ptr_container/indirect_fun.hpp>
28
29 #include <functional> // For 'binary_fnuction'
30 #include <cstdlib> // For 'rand()'
31 #include <algorithm> // For 'std::sort()'
32 #include <iostream> // For 'std::cout'
33
34 using namespace std;
35
36 //
37 // This is our simple example data-structure. It can
38 // be ordered in three ways.
39 //
40 struct photon
41 {
42 photon() : color( rand() ),
43 direction( rand() ),
44 power( rand() )
45 { }
46
47 int color;
48 int direction;
49 int power;
50 };
51
52 //
53 // Our big container is a standard vector
54 //
55 typedef std::vector<photon> vector_type;
56
57 //
58 // Now we define our view type by adding a second template argument.
59 // The 'view_clone_manager' will implements Cloning by taking address
60 // of objects.
61 //
62 // Notice the first template argument is 'photon' and not
63 // 'const photon' to allow the view container write access.
64 //
65 typedef boost::ptr_vector<photon,boost::view_clone_allocator> view_type;
66
67 //
68 // Our first sort criterium
69 //
70 struct sort_by_color : std::binary_function<photon,photon,bool>
71 {
72 bool operator()( const photon& l, const photon& r ) const
73 {
74 return l.color < r.color;
75 }
76 };
77
78 //
79 // Our second sort criterium
80 //
81 struct sort_by_direction : std::binary_function<photon,photon,bool>
82 {
83 bool operator()( const photon& l, const photon& r ) const
84 {
85 return l.direction < r.direction;
86 }
87 };
88
89
90 //
91 // Our third sort criterium
92 //
93 struct sort_by_power : std::binary_function<photon,photon,bool>
94 {
95 bool operator()( const photon& l, const photon& r ) const
96 {
97 return l.power < r.power;
98 }
99 };
100
101 //
102 // This function inserts "Clones" into the
103 // the view.
104 //
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'
109 // to
110 // typedef boost::ptr_vector<const photon,boost::view_clone_manager>
111 // view_type; ^^^^^^
112 //
113 void insert( vector_type& from, view_type& to )
114 {
115 to.insert( to.end(),
116 from.begin(),
117 from.end() );
118 }
119
120 int main()
121 {
122 enum { sz = 10, count = 500 };
123
124 //
125 // First we create the main container and two views
126 //
127 std::vector<vector_type> photons;
128 view_type color_view;
129 view_type direction_view;
130
131 //
132 // Then we fill the main container with some random data
133 //
134 for( int i = 0; i != sz; ++i )
135 {
136 photons.push_back( vector_type() );
137
138 for( int j = 0; j != count; ++j )
139 photons[i].push_back( photon() );
140 }
141
142 //
143 // Then we create the two views.
144 //
145 for( int i = 0; i != sz; ++i )
146 {
147 insert( photons[i], color_view );
148 insert( photons[i], direction_view );
149 }
150
151 //
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.
156 //
157 std::sort( color_view.begin(), color_view.end(), sort_by_power() );
158
159 //
160 // And now we can sort the views themselves. Notice how
161 // we switch to different iterators and different predicates:
162 //
163 color_view.sort( sort_by_color() );
164
165 direction_view.sort( sort_by_direction() );
166
167 return 0;
168 }