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.

271 lines
6.7 KiB

  1. /******************************************************************************
  2. * queue.cpp *
  3. *-----------*
  4. *
  5. *------------------------------------------------------------------------------
  6. * Copyright (C) 2000 Microsoft Corporation Date: 02/29/00
  7. * All Rights Reserved
  8. *
  9. ********************************************************************* PACOG ***/
  10. #include "stdafx.h"
  11. #include "queue.h"
  12. #include <stdio.h>
  13. #include <assert.h>
  14. /*****************************************************************************
  15. * CPhStrQueue::CPhStrQueue *
  16. *--------------------------*
  17. * Description:
  18. * Allocate memory to implement a queueue of the desired
  19. * length, and with the desired structure size
  20. * int length - length (or depth) of the queue.
  21. * int size - size of each of the queue elements in number of bytes
  22. ******************************************************************* PACOG ***/
  23. CPhStrQueue::CPhStrQueue (int iLength)
  24. {
  25. assert(iLength>0);
  26. m_pArrayBegin = 0;
  27. m_pArrayEnd = 0;
  28. m_pTop = 0;
  29. m_pBottom = 0;
  30. m_iLength = iLength;
  31. m_iNumEntries = 0;
  32. }
  33. /*****************************************************************************
  34. * CPhStrQueue::~CPhStrQueue *
  35. *---------------------------*
  36. * Description:
  37. *
  38. ******************************************************************* PACOG ***/
  39. CPhStrQueue::~CPhStrQueue()
  40. {
  41. if (m_pArrayBegin)
  42. {
  43. delete[] m_pArrayBegin;
  44. }
  45. }
  46. /*****************************************************************************
  47. * CPhStrQueue::Init *
  48. *-------------------*
  49. * Description:
  50. * Allocates memory for the queue.
  51. *
  52. ******************************************************************* PACOG ***/
  53. bool CPhStrQueue::Init ()
  54. {
  55. PhoneString* pData;
  56. assert(m_iLength>0);
  57. pData = new PhoneString[m_iLength]; // memory for the data elements
  58. if (pData)
  59. {
  60. m_pArrayBegin = pData; // Top of memory
  61. m_pArrayEnd = pData + m_iLength - 1; // last element of memory
  62. m_pTop = pData; // first entry in the queue
  63. m_pBottom = pData; // last entry in the queue
  64. return true;
  65. }
  66. return false;
  67. }
  68. /*****************************************************************************
  69. * CPhStrQueue::Reset *
  70. *--------------------*
  71. * Description:
  72. *
  73. *
  74. ******************************************************************* PACOG ***/
  75. void CPhStrQueue::Reset ()
  76. {
  77. while (m_iNumEntries>0)
  78. {
  79. if (m_pBottom == m_pArrayBegin)
  80. {
  81. m_pBottom = m_pArrayEnd - 1;
  82. }
  83. else
  84. {
  85. m_pBottom--;
  86. }
  87. m_iNumEntries--;
  88. free (m_pBottom->pPhones); //BUGBUG-- Do not free this way!!!
  89. free (m_pBottom->pfF0);
  90. }
  91. }
  92. /*****************************************************************************
  93. * CPhStrQueue::Push *
  94. *-------------------*
  95. * Description:
  96. * Push a new element on the queue
  97. *
  98. ******************************************************************* PACOG ***/
  99. bool CPhStrQueue::Push (Phone* pPhones, int iNumPhones, float* pfF0, int iNumF0)
  100. {
  101. assert(pPhones && iNumPhones>0);
  102. assert(pfF0 && iNumF0>0);
  103. if (m_pArrayBegin == 0 && ! Init() )
  104. {
  105. return false; // Couldn't allocate memory
  106. }
  107. if (m_iNumEntries >= m_iLength)
  108. {
  109. return false; // Out of space in queue
  110. }
  111. m_pBottom->pPhones = pPhones;
  112. m_pBottom->iNumPhones = iNumPhones;
  113. m_pBottom->pfF0 = pfF0;
  114. m_pBottom->iNumF0 = iNumF0;
  115. m_iNumEntries++;
  116. if (++m_pBottom >= m_pArrayEnd)
  117. {
  118. m_pBottom = m_pArrayBegin; // wrap arount linear memory to simulate circular buffer
  119. }
  120. return true;
  121. }
  122. /*****************************************************************************
  123. * CPhStrQueue::FirstElement *
  124. *---------------------------*
  125. * Description:
  126. * Examine an element on the top of the que (FIFO)
  127. * Returns true- if somethin was retrieved. false- if queue was empty
  128. *
  129. ******************************************************************* PACOG ***/
  130. bool CPhStrQueue::FirstElement (Phone** ppPhones, int* piNumPhones, float** ppfF0, int* piNumF0)
  131. {
  132. assert(ppPhones && piNumPhones);
  133. assert(ppfF0 && piNumF0);
  134. if (m_iNumEntries <= 0)
  135. {
  136. return false;
  137. }
  138. *ppPhones = m_pTop->pPhones;
  139. *piNumPhones = m_pTop->iNumPhones;
  140. *ppfF0 = m_pTop->pfF0;
  141. *piNumF0 = m_pTop->iNumF0;
  142. return true;
  143. }
  144. /*****************************************************************************
  145. * CPhStrQueue::Forward *
  146. *----------------------*
  147. * Description:
  148. * Skip to the next element on the top of the queue
  149. *
  150. ******************************************************************* PACOG ***/
  151. bool CPhStrQueue::Forward ()
  152. {
  153. if (m_iNumEntries <= 0)
  154. {
  155. return false;
  156. }
  157. if (++m_pTop >= m_pArrayEnd)
  158. {
  159. m_pTop = m_pArrayBegin;
  160. }
  161. m_iNumEntries--;
  162. return true;
  163. }
  164. /*****************************************************************************
  165. * CPhStrQueue::Pop *
  166. *------------------*
  167. * Description:
  168. * Remove the last added element from the bottom of the queue
  169. *
  170. * void *element - (O) pointer to the output elemnt
  171. *
  172. ******************************************************************* PACOG ***/
  173. bool CPhStrQueue::Pop (Phone** ppPhones, int* piNumPhones, float** ppfF0, int* piNumF0)
  174. {
  175. if (m_iNumEntries <= 0)
  176. {
  177. return false;
  178. }
  179. if (m_pBottom == m_pArrayBegin)
  180. {
  181. m_pBottom = m_pArrayEnd - 1;
  182. }
  183. else
  184. {
  185. m_pBottom--;
  186. }
  187. m_iNumEntries--;
  188. *ppPhones = m_pBottom->pPhones;
  189. *piNumPhones = m_pBottom->iNumPhones;
  190. *ppfF0 = m_pBottom->pfF0;
  191. *piNumF0 = m_pBottom->iNumF0;
  192. return true;
  193. }
  194. /*****************************************************************************
  195. * CPhStrQueue::Size *
  196. *-------------------*
  197. * Description:
  198. * Number of elements currently in the queue
  199. *
  200. ******************************************************************* PACOG ***/
  201. int CPhStrQueue::Size ()
  202. {
  203. return m_iNumEntries;
  204. }
  205. /*****************************************************************************
  206. * CPhStrQueue::Debug *
  207. *--------------------*
  208. * Description:
  209. * Prints all the elements of the queue struct
  210. *
  211. ******************************************************************* PACOG ***/
  212. void CPhStrQueue::Debug ()
  213. {
  214. fprintf (stderr, "arrayBegin = %x\n", m_pArrayBegin);
  215. fprintf (stderr, "arrayEnd = %x\n", m_pArrayEnd);
  216. fprintf (stderr, "top = %x\n", m_pTop);
  217. fprintf (stderr, "bottom = %x\n", m_pBottom);
  218. fprintf (stderr, "length = %d\n", m_iLength);
  219. fprintf (stderr, "nentries = %d\n", m_iNumEntries);
  220. };