3 // Copyright (C) 2005-2013 Free Software Foundation, Inc.
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 3, or (at your option) any later
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.
16 // You should have received a copy of the GNU General Public License
17 // along with this library; see the file COPYING3. If not see
18 // <http://www.gnu.org/licenses/>.
21 // Copyright (C) 2004 Ami Tavory and Vladimir Dreizin, IBM-HRL.
23 // Permission to use, copy, modify, sell, and distribute this software
24 // is hereby granted without fee, provided that the above copyright
25 // notice appears in all copies, and that both that copyright notice
26 // and this permission notice appear in supporting documentation. None
27 // of the above authors, nor IBM Haifa Research Laboratories, make any
28 // representation about the suitability of this software for any
29 // purpose. It is provided "as is" without express or implied
33 * @file modify_test.hpp
34 * Contains a modify performance test.
37 #ifndef PB_DS_JOIN_TEST_HPP
38 #define PB_DS_JOIN_TEST_HPP
40 #include <performance/time/timing_test_base.hpp>
41 #include <ext/pb_ds/detail/type_utils.hpp>
42 #include <performance/io/xml_formatter.hpp>
43 #include <common_type/priority_queue/string_form.hpp>
53 template<typename It
, class Cntnr
, class Tag
>
57 push_functor(It ins_it_b
, It ins_it_e
)
58 : m_ins_it_b(ins_it_b
), m_ins_it_e(ins_it_e
)
62 operator()(std::size_t resolution
)
64 typedef typename
Cntnr::point_iterator point_iterator
;
65 typedef typename
Cntnr::const_reference const_reference
;
66 for (std::size_t i
= 0; i
< resolution
; ++i
)
69 typedef std::vector
<point_iterator
> it_vec_t
;
71 for (It ins_it
= m_ins_it_b
; ins_it
!= m_ins_it_e
; ++ins_it
)
72 m_a_its
.push_back(c
.push(const_reference(ins_it
->first
)));
81 template<typename It
, class Cntnr
, class Tag
>
82 class push_modify_functor
85 typedef typename
Cntnr::point_iterator point_iterator
;
86 typedef typename
Cntnr::const_reference const_reference
;
87 typedef typename
Cntnr::value_type value_type
;
90 push_modify_functor(It ins_it_b
, It ins_it_e
, value_type mod_val
)
91 : m_ins_it_b(ins_it_b
), m_ins_it_e(ins_it_e
), m_mod_val(mod_val
)
95 operator()(std::size_t resolution
)
97 for (std::size_t i
= 0; i
< resolution
; ++i
)
100 typedef std::vector
<typename
Cntnr::point_iterator
> it_vec_t
;
102 for (It ins_it
= m_ins_it_b
; ins_it
!= m_ins_it_e
; ++ins_it
)
103 m_a_its
.push_back(c
.push(const_reference(ins_it
->first
)));
105 typename
it_vec_t::iterator mod_it
= m_a_its
.begin();
106 while (mod_it
!= m_a_its
.end())
107 c
.modify(*mod_it
++, m_mod_val
);
114 const value_type m_mod_val
;
118 template<typename It
, class Cntnr
>
119 class push_functor
<It
, Cntnr
, __gnu_pbds::binary_heap_tag
>
122 push_functor(It ins_it_b
, It ins_it_e
)
123 : m_ins_it_b(ins_it_b
), m_ins_it_e(ins_it_e
)
127 operator()(std::size_t resolution
)
129 typedef typename
Cntnr::const_reference const_reference
;
130 for (std::size_t i
= 0; i
< resolution
; ++i
)
133 for (It ins_it
= m_ins_it_b
; ins_it
!= m_ins_it_e
; ++ins_it
)
134 c
.push(const_reference(ins_it
->first
));
143 template<typename It
, class Cntnr
>
144 class push_functor
<It
, Cntnr
, __gnu_pbds::test::native_pq_tag
>
147 push_functor(It ins_it_b
, It ins_it_e
)
148 : m_ins_it_b(ins_it_b
), m_ins_it_e(ins_it_e
)
152 operator()(std::size_t resolution
)
154 typedef typename
Cntnr::const_reference const_reference
;
155 for (std::size_t i
= 0; i
< resolution
; ++i
)
159 for (It ins_it
= m_ins_it_b
; ins_it
!= m_ins_it_e
; ++ins_it
)
160 c
.push(const_reference(ins_it
->first
));
170 template<typename It
, class Cntnr
>
171 class push_modify_functor
<It
, Cntnr
, __gnu_pbds::binary_heap_tag
>
174 typedef typename
Cntnr::iterator iterator
;
175 typedef typename
Cntnr::const_reference const_reference
;
176 typedef typename
Cntnr::value_type value_type
;
179 push_modify_functor(It ins_it_b
, It ins_it_e
, value_type mod_val
)
180 : m_ins_it_b(ins_it_b
), m_ins_it_e(ins_it_e
), m_mod_val(mod_val
)
184 operator()(std::size_t resolution
)
186 for (std::size_t i
= 0; i
< resolution
; ++i
)
190 for (ins_it
= m_ins_it_b
; ins_it
!= m_ins_it_e
; ++ins_it
)
191 c
.push(const_reference(ins_it
->first
));
193 for (ins_it
= m_ins_it_b
; ins_it
!= m_ins_it_e
; ++ins_it
)
195 bool modified
= false;
196 for (iterator it
= c
.begin(); !modified
&& it
!= c
.end(); ++it
)
197 if (*it
== ins_it
->first
)
199 c
.modify(it
, m_mod_val
);
209 const value_type m_mod_val
;
212 template<typename It
, class Cntnr
>
213 class push_modify_functor
<It
, Cntnr
, __gnu_pbds::test::native_pq_tag
>
216 typedef typename
Cntnr::value_type value_type
;
217 typedef typename
Cntnr::const_reference const_reference
;
220 push_modify_functor(It ins_it_b
, It ins_it_e
, value_type mod_val
)
221 : m_ins_it_b(ins_it_b
), m_ins_it_e(ins_it_e
), m_mod_val(mod_val
)
225 operator()(std::size_t resolution
)
227 for (std::size_t i
= 0; i
< resolution
; ++i
)
231 for (ins_it
= m_ins_it_b
; ins_it
!= m_ins_it_e
; ++ins_it
)
232 c
.push(const_reference(ins_it
->first
));
233 for (ins_it
= m_ins_it_b
; ins_it
!= m_ins_it_e
; ++ins_it
)
234 c
.modify(ins_it
->first
, m_mod_val
);
241 const value_type m_mod_val
;
243 } // namespace detail
245 template<typename It
>
246 class modify_test
: private __gnu_pbds::test::detail::timing_test_base
249 modify_test(It b
, size_t vn
, size_t vs
, size_t vm
, bool modify_up
)
250 : m_ins_b(b
), m_ins_vn(vn
), m_ins_vs(vs
), m_ins_vm(vm
),
251 m_modify_up(modify_up
)
254 template<typename Cntnr
>
259 modify_test(const modify_test
&);
261 template<typename Cntnr
>
263 modify(Cntnr
, It ins_it_b
, It ins_it_e
)
265 typedef typename
Cntnr::const_reference const_reference
;
267 for (It ins_it
= ins_it_b
; ins_it
!= ins_it_e
; ++ins_it
)
268 cntnr
.modify(const_reference(*ins_it
));
272 const size_t m_ins_vn
;
273 const size_t m_ins_vs
;
274 const size_t m_ins_vm
;
275 const bool m_modify_up
;
278 template<typename It
>
279 template<typename Cntnr
>
284 typedef typename
Cntnr::value_type value_type
;
285 typedef typename
Cntnr::container_category container_category
;
286 typedef typename
Cntnr::const_reference const_reference
;
287 typedef detail::timing_test_base timing_test_base
;
288 typedef detail::push_functor
<It
, Cntnr
, container_category
> psh_fnct
;
289 typedef detail::push_modify_functor
<It
, Cntnr
, container_category
> psh_mod_fnct
;
290 typedef xml_result_set_performance_formatter formatter_type
;
291 formatter_type
res_set_fmt(string_form
<Cntnr
>::name(),
292 string_form
<Cntnr
>::desc());
294 for (size_t i
= 0; m_ins_vn
+ i
* m_ins_vs
< m_ins_vm
; ++i
)
296 const size_t v
= m_ins_vn
+ i
* m_ins_vs
;
301 psh_fnct
psh_fn(b
, e
);
302 const double psh_res
= timing_test_base::operator()(psh_fn
);
304 value_type val
= b
->first
;
306 Cntnr mod_val_container
;
307 for (It mod_val_it
= b
; mod_val_it
!= e
; ++mod_val_it
)
309 value_type pot
= mod_val_it
->first
;
310 if (m_modify_up
== mod_val_container
.get_cmp_fn()(val
, pot
))
315 psh_mod_fnct
psh_mod_fn(b
, e
, val
);
316 const double psh_mod_res
= timing_test_base::operator()(psh_mod_fn
);
318 const double min_res
= double(timing_test_base::min_time_res());
319 const double effective_delta
= std::max(psh_mod_res
- psh_res
,
322 res_set_fmt
.add_res(v
, effective_delta
/ v
);
326 } // namespace __gnu_pbds