Leaked source code of windows server 2003
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.

179 lines
4.1 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1997 - 1997
  6. //
  7. // File: algos.h
  8. //
  9. //--------------------------------------------------------------------------
  10. //
  11. // algos.h: additions to <algorithms>
  12. //
  13. #ifndef _ALGOS_H_
  14. #define _ALGOS_H_
  15. #include "mscver.h"
  16. #include <vector>
  17. #include <valarray>
  18. #include <algorithm>
  19. #include <functional>
  20. #include <assert.h>
  21. using namespace std;
  22. ////////////////////////////////////////////////////////////////////////////////////
  23. //
  24. // Extensions to (plagarisms from) "algorithm" templates
  25. //
  26. ////////////////////////////////////////////////////////////////////////////////////
  27. // Template Function count_set_intersection()
  28. // Return the number of elements in common between two ORDERED sets.
  29. // Elements must support operator<.
  30. //
  31. // Usage: count_set_intersection ( iter_beg_1, iter_end_1, iter_beg_2, iter_end_2 );
  32. //
  33. template<class _II1, class _II2> inline
  34. int count_set_intersection(_II1 _F1, _II1 _L1, _II2 _F2, _II2 _L2)
  35. {
  36. for (int _C=0; _F1 != _L1 && _F2 != _L2; )
  37. {
  38. if (*_F1 < *_F2)
  39. ++_F1;
  40. else if (*_F2 < *_F1)
  41. ++_F2;
  42. else
  43. ++_F1, ++_F2, ++_C;
  44. }
  45. return _C;
  46. }
  47. // Template Function count_set_intersection() with predicate. Same as above;
  48. // a predicate function is used to determine ordering; must behave as
  49. // operator<.
  50. template<class _II1, class _II2, class _Pr> inline
  51. int count_set_intersection(_II1 _F1, _II1 _L1, _II2 _F2, _II2 _L2, _Pr _P)
  52. {
  53. for (int _C=0; _F1 != _L1 && _F2 != _L2; )
  54. {
  55. if (_P(*_F1, *_F2))
  56. ++_F1;
  57. else if (_P(*_F2, *_F1))
  58. ++_F2;
  59. else
  60. ++_F1, ++_F2, ++_C;
  61. }
  62. return _C;
  63. }
  64. // Template function ifind().
  65. // Return the index of an item in a vector or -1 if not found.
  66. template <class _VT, class _T>
  67. int ifind ( const _VT & vt, _T t )
  68. {
  69. _VT::const_iterator vtibeg = vt.begin();
  70. _VT::const_iterator vtiend = vt.end();
  71. _VT::const_iterator vtiter = find( vtibeg, vtiend, t );
  72. return vtiter == vtiend
  73. ? -1
  74. : vtiter - vtibeg;
  75. }
  76. // Template function pexchange().
  77. // Exchange contents of a pair of pointers
  78. template<class _T>
  79. void pexchange ( _T * & pta, _T * & ptb )
  80. {
  81. _T * ptt = pta;
  82. pta = ptb;
  83. ptb = ptt;
  84. }
  85. // Template function vswap().
  86. // Swap elements of a vector
  87. template<class _T>
  88. void vswap ( vector<_T> & vt, int ia, int ib )
  89. {
  90. assert( ia < vt.size() );
  91. assert( ib < vt.size() );
  92. if ( ia != ib )
  93. {
  94. _T tt = vt[ia];
  95. vt[ia] = vt[ib];
  96. vt[ib] = tt;
  97. }
  98. }
  99. // Template function appendset().
  100. // Append to vector-based set (add if not present)
  101. template <class _T>
  102. bool appendset ( vector<_T> & vt, _T t )
  103. {
  104. if ( ifind( vt, t ) >= 0 )
  105. return false;
  106. vt.push_back(t);
  107. return true;
  108. }
  109. // Template function vclear().
  110. // Clear a valarray or vector to a single value
  111. template <class _VT, class _T>
  112. _VT & vclear ( _VT & vt, const _T & t )
  113. {
  114. for ( int i = 0; i < vt.size(); )
  115. vt[i++] = t;
  116. return vt;
  117. }
  118. // Template function vdup().
  119. // Duplicate a valarray or vector from one or the other
  120. template <class _VTA, class _VTB>
  121. _VTA & vdup ( _VTA & vta, const _VTB & vtb )
  122. {
  123. vta.resize( vtb.size() );
  124. for ( int i = 0; i < vta.size(); i++ )
  125. vta[i] = vtb[i];
  126. return vta;
  127. }
  128. // Template function vequal()
  129. // Compare valarrays or vectors for equality
  130. template <class _VTA, class _VTB>
  131. bool vequal ( _VTA & vta, const _VTB & vtb )
  132. {
  133. if ( vta.size() != vtb.size() )
  134. return false;
  135. for ( int i = 0; i < vta.size(); i++ )
  136. {
  137. if ( vta[i] != vtb[i] )
  138. return false;
  139. }
  140. return true;
  141. }
  142. // Template function vdimchk()
  143. // Treating the first argument as a subscript vector
  144. // and the second as a vector of dimensions, return true
  145. // if the subscript vector is valid for the space.
  146. template <class _VTA, class _VTB>
  147. bool vdimchk ( const _VTA & vta, const _VTB & vtb )
  148. {
  149. if ( vta.size() != vtb.size() )
  150. return false;
  151. for ( int i = 0; i < vta.size(); i++ )
  152. {
  153. if ( vta[i] >= vtb[i] )
  154. return false;
  155. }
  156. return true;
  157. }
  158. #endif