3 // Copyright (C) 2010 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 2, 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 COPYING. If not, write to
18 // the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
19 // MA 02111-1307, USA.
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
31 /** @file profile/impl/profiler_algos.h
32 * @brief Algorithms used by the profile extension.
34 * This file is needed to avoid including <algorithm> or <bits/stl_algo.h>.
35 * Including those files would result in recursive includes.
36 * These implementations are oversimplified. In general, efficiency may be
37 * sacrificed to minimize maintenance overhead.
40 #ifndef _GLIBCXX_PROFILE_PROFILER_ALGOS_H
41 #define _GLIBCXX_PROFILE_PROFILER_ALGOS_H 1
43 namespace __gnu_profile
45 /* Helper for __top_n. Insert in sorted vector, but not beyond Nth elem. */
46 template<typename _Container>
48 __insert_top_n(_Container& __output,
49 const typename _Container::value_type& __value,
50 typename _Container::size_type __n)
52 typename _Container::iterator __it = __output.begin();
53 typename _Container::size_type __count = 0;
55 // Skip up to N - 1 elements larger than VALUE.
56 // XXX: Could do binary search for random iterators.
60 // VALUE is not in top N.
63 if (__it == __output.end())
73 __output.insert(__it, __value);
76 /* Copy the top N elements in INPUT, sorted in reverse order, to OUTPUT. */
77 template<typename _Container>
79 __top_n(const _Container& __input, _Container& __output,
80 typename _Container::size_type __n)
83 typename _Container::const_iterator __it;
84 for (__it = __input.begin(); __it != __input.end(); ++__it)
85 __insert_top_n(__output, *__it, __n);
88 /* Simplified clone of std::for_each. */
89 template<typename _InputIterator, typename _Function>
91 __for_each(_InputIterator __first, _InputIterator __last, _Function __f)
93 for (; __first != __last; ++__first)
98 /* Simplified clone of std::remove. */
99 template<typename _ForwardIterator, typename _Tp>
101 __remove(_ForwardIterator __first, _ForwardIterator __last,
104 if(__first == __last)
106 _ForwardIterator __result = __first;
108 for(; __first != __last; ++__first)
109 if(!(*__first == __value))
111 *__result = *__first;
116 } // namespace __gnu_profile
118 #endif /* _GLIBCXX_PROFILE_PROFILER_ALGOS_H */