Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

105 lines
2.5 KiB

  1. /*++
  2. Copyright (c) 2000 Microsoft Corporation
  3. Module Name:
  4. FusionAlgorithm.h
  5. Abstract:
  6. Stuff inspired by and copied from <algorithm>.
  7. See also NVseeLibAlgorithm.
  8. StdFind
  9. ReverseFind
  10. StdSwap
  11. Author:
  12. Jay M. Krell (a-JayK) May 2000
  13. Revision History:
  14. --*/
  15. #pragma once
  16. /*-----------------------------------------------------------------------------
  17. code based on <algorithm>
  18. -----------------------------------------------------------------------------*/
  19. template<typename InputIterator, typename T>
  20. inline InputIterator
  21. StdFind(
  22. InputIterator begin,
  23. InputIterator end,
  24. const T& valueToFind
  25. )
  26. {
  27. for (; begin != end ; ++begin)
  28. {
  29. if (*begin == valueToFind)
  30. {
  31. break;
  32. }
  33. }
  34. return begin;
  35. }
  36. /*-----------------------------------------------------------------------------
  37. This is not in the STL in this form; it is there like so:
  38. std::vector<T> v;
  39. T valueToFind;
  40. i = std::find(v.rbegin(), v.rend(), valueToFind);
  41. if (i != v.rend())
  42. ..
  43. where rbegin and rend are implemented via the
  44. supplied "iterator adaptor" std::reverse_iterator<>:
  45. typedef std::reverse_iterator<const_iterator, value_type,
  46. const_reference, const_reference *, difference_type>
  47. const_reverse_iterator;
  48. const_reverse_iterator rbegin() const
  49. {return const_reverse_iterator(end()); }
  50. const_reverse_iterator rend() const
  51. {return const_reverse_iterator(begin()); }
  52. It is actually very elegant I believe, but I haven't used it, and we don't
  53. have an equivalent to std::reverse_iterator.
  54. -----------------------------------------------------------------------------*/
  55. /* InputIterator isn't quite the right name here, since we use -- instead of ++. */
  56. template<typename InputIterator, typename T>
  57. inline InputIterator
  58. ReverseFind(
  59. InputIterator begin,
  60. InputIterator end,
  61. const T& valueToFind
  62. )
  63. {
  64. for ( InputIterator scan = end ; scan != begin ; )
  65. {
  66. if (*--scan == valueToFind)
  67. {
  68. return scan;
  69. }
  70. }
  71. return end;
  72. }
  73. /*-----------------------------------------------------------------------------
  74. you should specialize this to memberwise swap, doing so
  75. usually makes it impossible for Swap to fail; two CFusionStringBuffers
  76. with the same heap can be swapped with no chance of failure
  77. -----------------------------------------------------------------------------*/
  78. template<typename T>
  79. inline VOID
  80. StdSwap(
  81. T& x,
  82. T& y
  83. )
  84. {
  85. T temp = x;
  86. x = y;
  87. y = temp;
  88. }