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.

210 lines
5.4 KiB

  1. //------------------------------------------------------------------------------------------
  2. // ListPoint.cpp
  3. //
  4. //
  5. //
  6. //------------------------------------------------------------------------------------------
  7. #include "listWordBreak.hpp"
  8. //------------------------------------------------------------------------------------------
  9. // WordBreakElement::WordBreakElement
  10. //
  11. // Initialize a WordBreak element.
  12. //
  13. //------------------------------------------------------------------------------------------
  14. WordBreakElement::WordBreakElement(CTrie* pTrie, CTrie* pTrieTrigram)
  15. {
  16. breakTree = NULL;
  17. fFree = false;
  18. breakTree = new CThaiBreakTree();
  19. if (breakTree)
  20. {
  21. breakTree->Init(pTrie, pTrieTrigram);
  22. fFree = true;
  23. }
  24. else
  25. {
  26. breakTree = NULL;
  27. assert(false);
  28. }
  29. }
  30. //------------------------------------------------------------------------------------------
  31. // WordBreakElement::~WordBreakElement
  32. //
  33. // Destructor
  34. //
  35. //------------------------------------------------------------------------------------------
  36. WordBreakElement::~WordBreakElement()
  37. {
  38. if (breakTree != NULL)
  39. {
  40. delete breakTree;
  41. breakTree = NULL;
  42. }
  43. // The ThaiWordBreak should be free if all goes well.
  44. assert(fFree);
  45. }
  46. //------------------------------------------------------------------------------------------
  47. // ListWordBreak::ListWordBreak
  48. //------------------------------------------------------------------------------------------
  49. ListWordBreak::ListWordBreak()
  50. {
  51. m_pTrie = NULL;
  52. m_pTrieTrigram = NULL;
  53. #if defined (_DEBUG)
  54. fInit = false;
  55. #endif
  56. }
  57. //------------------------------------------------------------------------------------------
  58. // ListWordBreak::Init
  59. //------------------------------------------------------------------------------------------
  60. bool ListWordBreak::Init(CTrie* pTrie,CTrie* pTrieTrigram)
  61. {
  62. assert(pTrie != NULL);
  63. assert(pTrieTrigram != NULL);
  64. m_pTrie = pTrie;
  65. m_pTrieTrigram = pTrieTrigram;
  66. #if defined (_DEBUG)
  67. fInit = true;
  68. #endif
  69. return true;
  70. }
  71. //------------------------------------------------------------------------------------------
  72. // ListWordBreak::CreateWordBreak
  73. //
  74. // Create a WordBeakElement and place it at the end of the list of the list
  75. //------------------------------------------------------------------------------------------
  76. bool ListWordBreak::CreateWordBreak()
  77. {
  78. #if defined (_DEBUG)
  79. assert(fInit);
  80. #endif
  81. WordBreakElement* pWordBreakElement = NULL;
  82. pWordBreakElement = new WordBreakElement(m_pTrie,m_pTrieTrigram);
  83. if (pWordBreakElement)
  84. {
  85. Append((void*)pWordBreakElement);
  86. return true;
  87. }
  88. return false;
  89. }
  90. //------------------------------------------------------------------------------------------
  91. // ListWordBreak::GetNode
  92. //
  93. // get data of the current node.
  94. //------------------------------------------------------------------------------------------
  95. bool ListWordBreak::GetNode(CThaiBreakTree* pThaiBreakTree, bool* pfFree)
  96. {
  97. #if defined (_DEBUG)
  98. assert(fInit);
  99. #endif
  100. WordBreakElement* element = (WordBreakElement*)List::GetData();
  101. if (element == NULL)
  102. return false;
  103. pThaiBreakTree = element->breakTree;
  104. *pfFree = element->fFree;
  105. return true;
  106. }
  107. //------------------------------------------------------------------------------------------
  108. // ListWordBreak::Flush
  109. //
  110. // delete everything in the list.
  111. //------------------------------------------------------------------------------------------
  112. void ListWordBreak::Flush()
  113. {
  114. #if defined (_DEBUG)
  115. assert(fInit);
  116. #endif
  117. WordBreakElement* element = NULL;
  118. // delete all the list elements
  119. while (true)
  120. {
  121. element = (WordBreakElement*)List::Remove();
  122. if (element)
  123. {
  124. // assert(element->fFree);
  125. delete element;
  126. }
  127. else
  128. {
  129. break;
  130. }
  131. }
  132. }
  133. //------------------------------------------------------------------------------------------
  134. // ListWordBreak::GetFreeWB
  135. //
  136. // fCreateNode - If list is full create new WordBreak.
  137. //------------------------------------------------------------------------------------------
  138. WordBreakElement* ListWordBreak::GetFreeWB(bool fCreateNode)
  139. {
  140. #if defined (_DEBUG)
  141. assert(fInit);
  142. #endif
  143. // Declare local variables.
  144. WordBreakElement* element = NULL;
  145. // Move to beginning of the list.
  146. MoveFirst();
  147. while(true)
  148. {
  149. // Get current WordBreakElement.
  150. element = (WordBreakElement*)GetData();
  151. // Determine if the current WordBreakElement is free to be use.
  152. if (element && element->fFree)
  153. {
  154. return element;
  155. }
  156. // Move to Next Node.
  157. if (!MoveNext())
  158. {
  159. // Can we create a new WordBreak?
  160. if (fCreateNode && CreateWordBreak())
  161. {
  162. // Move to beggining of the list.
  163. MoveFirst();
  164. }
  165. else
  166. {
  167. // If unable to create new WordBreak drop out.
  168. break;
  169. }
  170. }
  171. }
  172. // Return NULL
  173. return NULL;
  174. }
  175. //------------------------------------------------------------------------------------------
  176. // ListWordBreak::MarkWordBreak
  177. //
  178. // Input:
  179. // pWordBreakElement - pointer to a WordBreakElement.
  180. // fFree - true = WordBreak is free to be used.
  181. // - false = WordBreak is in used.
  182. //------------------------------------------------------------------------------------------
  183. void ListWordBreak::MarkWordBreak(WordBreakElement* pWordBreakElement, bool fFree)
  184. {
  185. #if defined (_DEBUG)
  186. assert(fInit);
  187. #endif
  188. pWordBreakElement->fFree = fFree;
  189. }