OSDN Git Service

2007-03-06 Paolo Carlini <pcarlini@suse.de>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / testsuite / util / performance / assoc / timing / tree_order_statistics_test.hpp
1 // -*- C++ -*-
2
3 // Copyright (C) 2005, 2006, 2007 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library.  This library is free
6 // software; you can redistribute it and/or modify it under the terms
7 // of the GNU General Public License as published by the Free Software
8 // Foundation; either version 2, or (at your option) any later
9 // version.
10
11 // This library is distributed in the hope that it will be useful, but
12 // WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // General Public License for more details.
15
16 // You should have received a copy of the GNU General Public License
17 // along with this library; see the file COPYING.  If not, write to
18 // the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
19 // MA 02111-1307, USA.
20
21 // As a special exception, you may use this file as part of a free
22 // software library without restriction.  Specifically, if other files
23 // instantiate templates or use macros or inline functions from this
24 // file, or you compile this file and link it with other files to
25 // produce an executable, this file does not by itself cause the
26 // resulting executable to be covered by the GNU General Public
27 // License.  This exception does not however invalidate any other
28 // reasons why the executable file might be covered by the GNU General
29 // Public License.
30
31 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
32
33 // Permission to use, copy, modify, sell, and distribute this software
34 // is hereby granted without fee, provided that the above copyright
35 // notice appears in all copies, and that both that copyright notice
36 // and this permission notice appear in supporting documentation. None
37 // of the above authors, nor IBM Haifa Research Laboratories, make any
38 // representation about the suitability of this software for any
39 // purpose. It is provided "as is" without express or implied
40 // warranty.
41
42 /**
43  * @file tree_order_statistics_test.hpp
44  * Contains a test for order_statisticsing trees
45  */
46
47 #ifndef PB_DS_TREE_ORDER_STATISTICS_TEST_HPP
48 #define PB_DS_TREE_ORDER_STATISTICS_TEST_HPP
49
50 #include <performance/time/timing_test_base.hpp>
51 #include <performance/io/xml_formatter.hpp>
52 #include <common_type/assoc/string_form.hpp>
53 #include <ext/pb_ds/detail/type_utils.hpp>
54 #include <iterator>
55 #include <cstdlib>
56
57 namespace pb_ds
58 {
59   namespace test
60   {
61     namespace detail
62     {
63       template<typename Cntnr, bool Native>
64       class order_statistics_functor
65       {
66       public:
67         order_statistics_functor(Cntnr& container) : m_r_container(container)
68         { }
69
70         void
71         operator()(std::size_t resolution)
72         {
73           enum
74             {
75               support_detected =
76               pb_ds::test::detail::tree_supports_order_statistics<Cntnr>::value
77             };
78
79           PB_DS_STATIC_ASSERT(correct_type, support_detected);
80
81           for (std::size_t i = 0; i < resolution; ++i)
82             {
83               typename Cntnr::const_iterator it = m_r_container.begin();
84               typename Cntnr::const_iterator e = m_r_container.end();
85               const size_t max_size = m_r_container.size();
86               while (it != e)
87                 if (m_r_container.order_of_key(*(it++)) > max_size)
88                   std::abort();
89             }
90         }
91
92       private:
93         Cntnr& m_r_container;
94       };
95
96       template<typename Cntnr>
97       class order_statistics_functor<Cntnr, false>
98       {
99       public:
100         order_statistics_functor(Cntnr& container) : m_r_container(container)
101         { }
102
103         void
104         operator()(std::size_t resolution)
105         {
106           for (std::size_t i = 0; i < resolution; ++i)
107             {
108               typedef typename Cntnr::const_iterator const_iterator;
109               const_iterator b = m_r_container.begin();
110               const_iterator e = m_r_container.end();
111               const_iterator it = b;
112               const size_t max_size = m_r_container.size();
113               while (it != e)
114                 {
115                   const_iterator f_it = m_r_container.find(*(it++));
116                   if (static_cast<size_t>(std::distance(b, f_it)) > max_size)
117                     std::abort();
118                 }
119             }
120         }
121
122       private:
123         Cntnr& m_r_container;
124       };
125     } // namespace detail
126
127     template<bool Support_Order_Statistics>
128     class tree_order_statistics_test 
129     : private pb_ds::test::detail::timing_test_base
130     {
131     public:
132       tree_order_statistics_test(size_t vn, size_t vs, size_t vm)
133       : m_vn(vn), m_vs(vs), m_vm(vm)
134       { }
135
136       template<typename Cntnr>
137       void
138       operator()(Cntnr);
139
140     private:
141       tree_order_statistics_test(const tree_order_statistics_test& );
142
143       template<typename Cntnr>
144       void
145       order_statistics(Cntnr& r_container, pb_ds::detail::true_type);
146
147       template<typename Cntnr>
148       void
149       order_statistics(Cntnr& r_container, pb_ds::detail::false_type);
150
151     private:
152       const size_t m_vn;
153       const size_t m_vs;
154       const size_t m_vm;
155     };
156
157     template<bool Support_Order_Statistics>
158     template<typename Cntnr>
159     void
160     tree_order_statistics_test<Support_Order_Statistics>::
161     operator()(Cntnr)
162     {
163       typedef xml_result_set_performance_formatter formatter_type;
164       formatter_type res_set_fmt(string_form<Cntnr>::name(), 
165                                  string_form<Cntnr>::desc());
166
167       for (size_t v = m_vn; v < m_vm; v += m_vs)
168         {
169           Cntnr cntnr;
170           for (size_t ins = 0; ins < v; ++ ins)
171             cntnr.insert((typename Cntnr::value_type)ins);
172
173           pb_ds::test::detail::order_statistics_functor<Cntnr, Support_Order_Statistics>
174             fn(cntnr);
175
176           const double res =
177             pb_ds::test::detail::timing_test_base::operator()(fn);
178
179           res_set_fmt.add_res(v, res / v);
180         }
181     }
182   } // namespace test
183 } // namespace pb_ds
184
185 #endif 
186