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.

173 lines
3.9 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1997 - 1998
  6. //
  7. // File: mddist.h
  8. //
  9. //--------------------------------------------------------------------------
  10. //
  11. // mddist.h: model distributions
  12. //
  13. #ifndef _MDDIST_H_
  14. #define _MDDIST_H_
  15. #include <map>
  16. #include "mdvect.h"
  17. #include "leakchk.h"
  18. ////////////////////////////////////////////////////////////////////
  19. // Probability table declarations
  20. ////////////////////////////////////////////////////////////////////
  21. // Dense multidimensional array. Note that a null dimension set
  22. // produces a one entry array.
  23. typedef TMDVDENSE<REAL> MDVDENSE;
  24. class MDVCPD : public MDVDENSE
  25. {
  26. public:
  27. MDVCPD ( const VIMD & vimd )
  28. : MDVDENSE ( vimd )
  29. {}
  30. MDVCPD () {}
  31. ~ MDVCPD () {}
  32. void Init ( const VIMD & vimd, size_t start = 0 )
  33. {
  34. if ( vimd.size() > 0 )
  35. {
  36. MDVDENSE::Init( vimd, start );
  37. }
  38. else
  39. {
  40. assert( start == 0 );
  41. MDVDENSE::Init( 1, 1 );
  42. }
  43. }
  44. void Init ( int cdim, ... )
  45. {
  46. if ( cdim > 0 )
  47. {
  48. va_list vl;
  49. va_start( vl, cdim );
  50. MDVDENSE::Init( cdim, vl );
  51. }
  52. else
  53. {
  54. MDVDENSE::Init( 1, 1 );
  55. }
  56. }
  57. void Clear ( REAL r = 0.0 )
  58. {
  59. size_t celem = size();
  60. for ( int i = 0; i < celem; )
  61. self[i++] = r;
  62. }
  63. REAL RSum () const
  64. {
  65. return first.sum();
  66. }
  67. void Normalize ()
  68. {
  69. REAL rSum = RSum();
  70. if ( rSum != 0.0 && rSum != 1.0 )
  71. {
  72. size_t celem = size();
  73. for ( int i = 0; i < celem; )
  74. self[i++] /= rSum;
  75. }
  76. }
  77. MDVCPD & operator = ( const MDVCPD & mdv )
  78. {
  79. MDVDENSE::Init( mdv.Slice() );
  80. first = mdv.first;
  81. return self;
  82. }
  83. // Convert this MDVCPD to a single-dimension object
  84. MDVCPD & operator = ( const VLREAL & vlr )
  85. {
  86. Init( 1, vlr.size() );
  87. first = vlr;
  88. return self;
  89. }
  90. // Given a partial dimension (incomplete) subscript, update the appropriate
  91. // range of elements. Note that providing an incomplete (i.e., short)
  92. // subscript array to the "offset" functions is valid; the results
  93. // are the same as if the missing lower-order elements were zero.
  94. void UpdatePartial ( const VIMD & vimd, const VLREAL & vlr )
  95. {
  96. const VIMD vimdDim = VimdDim();
  97. // Compute the appropriate number of elements
  98. size_t cElem = 1;
  99. assert( vimd.size() <= vimdDim.size() );
  100. for ( int idim = vimd.size(); idim < vimdDim.size(); idim++ )
  101. {
  102. cElem *= vimdDim[idim];
  103. }
  104. ASSERT_THROW( vlr.size() == cElem,
  105. EC_MDVECT_MISUSE,
  106. "m-d vector partial projection count invalid" );
  107. // Index to the proper position and update from the source data
  108. assert( second._IOff(vimd) + cElem <= first.size() );
  109. REAL * prSelf = & self[vimd];
  110. for ( int iElem = 0; iElem < cElem; )
  111. *prSelf++ = vlr[iElem++];
  112. }
  113. void Clone ( const MDVCPD & mdv )
  114. {
  115. self = mdv;
  116. }
  117. };
  118. // Class MPCPDD: Distribution map by specific index
  119. // Hungarian: 'drmap'
  120. class MPCPDD : public map<VIMD, VLREAL, lessv<VIMD> >
  121. {
  122. public:
  123. MPCPDD () {}
  124. ~ MPCPDD () {}
  125. void Clone ( const MPCPDD & dmap )
  126. {
  127. self = dmap;
  128. }
  129. // Return a pointer to the dimensionless "default" vector or NULL
  130. const VLREAL * PVlrDefault () const
  131. {
  132. VIMD vimdDefault;
  133. const_iterator itdm = find( vimdDefault );
  134. return itdm == end()
  135. ? NULL
  136. : & (*itdm).second;
  137. }
  138. bool operator == ( const MPCPDD & mpcpdd ) const
  139. {
  140. if ( size() != mpcpdd.size() )
  141. return false;
  142. const_iterator itself = begin();
  143. const_iterator itmp = mpcpdd.begin();
  144. for ( ; itself != end(); itself++, itmp++ )
  145. {
  146. if ( (*itself).first != (*itmp).first )
  147. return false;
  148. if ( ! vequal( (*itself).second, (*itmp).second ) )
  149. return false;
  150. }
  151. return true;
  152. }
  153. bool operator != ( const MPCPDD & mpcpdd ) const
  154. { return !(self == mpcpdd); }
  155. };
  156. #endif