profiler_algos.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // -*- C++ -*-
  2. //
  3. // Copyright (C) 2010-2018 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
  7. // terms of the GNU General Public License as published by the
  8. // Free Software Foundation; either version 3, or (at your option)
  9. // any later version.
  10. //
  11. // This library is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU General Public License for more details.
  15. // Under Section 7 of GPL version 3, you are granted additional
  16. // permissions described in the GCC Runtime Library Exception, version
  17. // 3.1, as published by the Free Software Foundation.
  18. // You should have received a copy of the GNU General Public License along
  19. // with this library; see the file COPYING3. If not see
  20. // <http://www.gnu.org/licenses/>.
  21. /** @file profile/impl/profiler_algos.h
  22. * @brief Algorithms used by the profile extension.
  23. *
  24. * This file is needed to avoid including \<algorithm\> or \<bits/stl_algo.h\>.
  25. * Including those files would result in recursive includes.
  26. * These implementations are oversimplified. In general, efficiency may be
  27. * sacrificed to minimize maintenance overhead.
  28. */
  29. #ifndef _GLIBCXX_PROFILE_PROFILER_ALGOS_H
  30. #define _GLIBCXX_PROFILE_PROFILER_ALGOS_H 1
  31. namespace __gnu_profile
  32. {
  33. /* Helper for __top_n. Insert in sorted vector, but not beyond Nth elem. */
  34. template<typename _Container>
  35. void
  36. __insert_top_n(_Container& __output,
  37. const typename _Container::value_type& __value,
  38. typename _Container::size_type __n)
  39. {
  40. typename _Container::iterator __it = __output.begin();
  41. typename _Container::size_type __count = 0;
  42. // Skip up to N - 1 elements larger than VALUE.
  43. // XXX: Could do binary search for random iterators.
  44. while (true)
  45. {
  46. if (__count >= __n)
  47. // VALUE is not in top N.
  48. return;
  49. if (__it == __output.end())
  50. break;
  51. if (*__it < __value)
  52. break;
  53. ++__it;
  54. ++__count;
  55. }
  56. __output.insert(__it, __value);
  57. }
  58. /* Copy the top N elements in INPUT, sorted in reverse order, to OUTPUT. */
  59. template<typename _Container>
  60. void
  61. __top_n(const _Container& __input, _Container& __output,
  62. typename _Container::size_type __n)
  63. {
  64. __output.clear();
  65. typename _Container::const_iterator __it;
  66. for (__it = __input.begin(); __it != __input.end(); ++__it)
  67. __insert_top_n(__output, *__it, __n);
  68. }
  69. /* Simplified clone of std::for_each. */
  70. template<typename _InputIterator, typename _Function>
  71. _Function
  72. __for_each(_InputIterator __first, _InputIterator __last, _Function __f)
  73. {
  74. for (; __first != __last; ++__first)
  75. __f(*__first);
  76. return __f;
  77. }
  78. /* Simplified clone of std::remove. */
  79. template<typename _ForwardIterator, typename _Tp>
  80. _ForwardIterator
  81. __remove(_ForwardIterator __first, _ForwardIterator __last,
  82. const _Tp& __value)
  83. {
  84. if(__first == __last)
  85. return __first;
  86. _ForwardIterator __result = __first;
  87. ++__first;
  88. for(; __first != __last; ++__first)
  89. if(!(*__first == __value))
  90. {
  91. *__result = *__first;
  92. ++__result;
  93. }
  94. return __result;
  95. }
  96. } // namespace __gnu_profile
  97. #endif /* _GLIBCXX_PROFILE_PROFILER_ALGOS_H */