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.

204 lines
5.1 KiB

  1. /*****************************************************************************/
  2. /** Microsoft LAN Manager **/
  3. /** Copyright(c) Microsoft Corp., 1987-1999 **/
  4. /*****************************************************************************/
  5. /*****************************************************************************
  6. File : idict.hxx
  7. Title : index based dictionary simulation
  8. History :
  9. 04-Aug-1991 VibhasC Created
  10. *****************************************************************************/
  11. #pragma warning ( disable : 4514 4710 )
  12. /****************************************************************************
  13. local defines and includes
  14. ****************************************************************************/
  15. #include "nulldefs.h"
  16. extern "C" {
  17. #include <stdio.h>
  18. }
  19. #include "idict.hxx"
  20. /****************************************************************************/
  21. /****************************************************************************
  22. idict class implementation
  23. ****************************************************************************/
  24. IDICT::IDICT(
  25. short InitialSize,
  26. short Increment )
  27. {
  28. IDICTELEMENT * pNewArray = new
  29. IDICTELEMENT [ InitialSize ];
  30. iNextIndex = 0;
  31. CurrentSize = InitialSize;
  32. IDictIncrement = Increment;
  33. SetArray( pNewArray );
  34. }
  35. /****************************************************************************
  36. SetElement:
  37. Set the element in the array to the value that is passed in
  38. ****************************************************************************/
  39. void
  40. IDICT::SetElement(
  41. IDICTKEY Key,
  42. IDICTELEMENT Element )
  43. {
  44. if( Key < iNextIndex )
  45. {
  46. pArray[ Key ] = Element;
  47. }
  48. }
  49. /****************************************************************************
  50. SetNewElement:
  51. Set the new element in the array to the value that is passed in
  52. ****************************************************************************/
  53. IDICTKEY
  54. IDICT::SetNewElement(
  55. IDICTELEMENT Element )
  56. {
  57. pArray[ iNextIndex ] = Element;
  58. return iNextIndex++;
  59. }
  60. /****************************************************************************
  61. ExpandArray:
  62. Expand the array to accomodate more elements coming in.
  63. ****************************************************************************/
  64. void
  65. IDICT::ExpandArray()
  66. {
  67. IDICTELEMENT * pOldArray = GetArray();
  68. IDICTELEMENT * pNewArray = new
  69. IDICTELEMENT [(CurrentSize+IDictIncrement)];
  70. int iIndex = 0;
  71. //.. pOldArray moves, so we need pTemp to mark the beginning
  72. IDICTELEMENT * pTemp = pOldArray;
  73. SetArray( pNewArray );
  74. CurrentSize = short( CurrentSize + IDictIncrement );
  75. if( pOldArray )
  76. {
  77. while( iIndex++ < iNextIndex )
  78. {
  79. *pNewArray++ = *pOldArray++;
  80. }
  81. }
  82. delete pTemp;
  83. }
  84. /***************************************************************************
  85. GetElement:
  86. return the element pointed to by the KEY (index)
  87. ***************************************************************************/
  88. IDICTELEMENT
  89. IDICT::GetElement(
  90. IDICTKEY Key )
  91. {
  92. IDICTELEMENT * pArray = GetArray();
  93. if( pArray && ( Key < iNextIndex ) )
  94. return pArray[ Key ];
  95. return (IDICTELEMENT) 0;
  96. }
  97. /***************************************************************************
  98. IsElementPresent:
  99. return the element pointed to by the KEY (index)
  100. ***************************************************************************/
  101. BOOL
  102. IDICT::IsElementPresent(
  103. IDICTELEMENT Element )
  104. {
  105. IDICTELEMENT * pArray = GetArray();
  106. short iIndex;
  107. if( pArray )
  108. {
  109. for( iIndex = 0; iIndex < iNextIndex; ++iIndex )
  110. {
  111. if( pArray[ iIndex ] == Element )
  112. return TRUE;
  113. }
  114. }
  115. return FALSE;
  116. }
  117. /***************************************************************************
  118. AddElement:
  119. Basically SetNewElement, with checks for needed expansion. Notice how the
  120. array pointer is never accessed in this routine directly. This is becuase
  121. ExpandArray can potentially change the array underneath. So after evry
  122. expand call, must make sure that any local pointers to the array are
  123. updated.
  124. ***************************************************************************/
  125. IDICTKEY
  126. IDICT::AddElement(
  127. IDICTELEMENT pNew )
  128. {
  129. if( iNextIndex >= CurrentSize )
  130. {
  131. ExpandArray();
  132. }
  133. return SetNewElement( pNew );
  134. }
  135. /*****************************************************************************/
  136. ISTACK::ISTACK(
  137. short MaxDepth )
  138. {
  139. InitNew( MaxDepth );
  140. CurrentElement = 0;
  141. }
  142. void
  143. ISTACK::InitNew(
  144. short MaxDepth )
  145. {
  146. MaxElements = MaxDepth;
  147. pStack = new IDICTELEMENT[ MaxElements ];
  148. }
  149. IDICTELEMENT
  150. ISTACK::Push(
  151. IDICTELEMENT Element )
  152. {
  153. if( CurrentElement == MaxElements )
  154. {
  155. int i;
  156. IDICTELEMENT (*pStackOld);
  157. pStackOld = pStack;
  158. InitNew( short( MaxElements + 10 ) );
  159. for( i = 0;
  160. i < CurrentElement;
  161. ++i )
  162. {
  163. pStack[ i ] = pStackOld[ i ];
  164. }
  165. delete pStackOld;
  166. }
  167. pStack[ CurrentElement++ ] = Element;
  168. return Element;
  169. }
  170. IDICTELEMENT
  171. ISTACK::Pop()
  172. {
  173. MIDL_ASSERT( CurrentElement != 0 );
  174. return pStack[ --CurrentElement ];
  175. }