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.

182 lines
3.3 KiB

  1. #ifdef _DEBUG
  2. #define new DEBUG_NEW
  3. #endif
  4. //////////////////////////////////////////////////////////////////////
  5. // CTreeNode implementation
  6. //////////////////////////////////////////////////////////////////////
  7. // Construction/Destruction
  8. //////////////////////////////////////////////////////////////////////
  9. template <class T>
  10. CTreeNode<T>::CTreeNode()
  11. {
  12. }
  13. template <class T>
  14. CTreeNode<T>::~CTreeNode()
  15. {
  16. Destroy();
  17. if( GetObject() )
  18. {
  19. delete GetObject();
  20. }
  21. }
  22. //////////////////////////////////////////////////////////////////////
  23. // Destroy
  24. //////////////////////////////////////////////////////////////////////
  25. template <class T>
  26. inline void CTreeNode<T>::Destroy()
  27. {
  28. for( int i = GetChildCount()-1; i >= 0; i-- )
  29. {
  30. RemoveChild(i);
  31. }
  32. }
  33. //////////////////////////////////////////////////////////////////////
  34. // Children Members
  35. //////////////////////////////////////////////////////////////////////
  36. template <class T>
  37. inline int CTreeNode<T>::GetChildCount()
  38. {
  39. return (int)m_Children.GetSize();
  40. }
  41. template <class T>
  42. inline CTreeNode<T>* CTreeNode<T>::GetChild(int iIndex)
  43. {
  44. if( iIndex > m_Children.GetUpperBound() )
  45. {
  46. return NULL;
  47. }
  48. if( iIndex < 0 )
  49. {
  50. return NULL;
  51. }
  52. return m_Children[iIndex];
  53. }
  54. template <class T>
  55. inline int CTreeNode<T>::AddChild(CTreeNode<T>* pNode)
  56. {
  57. return (int)m_Children.Add(pNode);
  58. }
  59. template <class T>
  60. inline void CTreeNode<T>::RemoveChild(CTreeNode<T>* pNode)
  61. {
  62. for( int i = 0; i < GetChildCount(); i++ )
  63. {
  64. CTreeNode<T>* pChildNode = GetChild(i);
  65. if( pNode == pChildNode )
  66. {
  67. RemoveChild(i);
  68. return;
  69. }
  70. }
  71. }
  72. template <class T>
  73. inline void CTreeNode<T>::RemoveChild(int iIndex)
  74. {
  75. CTreeNode<T>* pChildNode = GetChild(iIndex);
  76. if( pChildNode )
  77. {
  78. delete pChildNode;
  79. m_Children.RemoveAt(iIndex);
  80. }
  81. }
  82. //////////////////////////////////////////////////////////////////////
  83. // Association Members
  84. //////////////////////////////////////////////////////////////////////
  85. template <class T>
  86. inline int CTreeNode<T>::GetAssocCount()
  87. {
  88. return (int)m_Associations.GetSize();
  89. }
  90. template <class T>
  91. inline CTreeNode<T>* CTreeNode<T>::GetAssoc(int iIndex)
  92. {
  93. if( iIndex > m_Associations.GetUpperBound() )
  94. {
  95. return NULL;
  96. }
  97. if( iIndex < 0 )
  98. {
  99. return NULL;
  100. }
  101. return m_Associations[iIndex];
  102. }
  103. template <class T>
  104. inline int CTreeNode<T>::AddAssoc(CTreeNode<T>* pNode)
  105. {
  106. // disallow multiple associations for the same node
  107. for( int i = 0; i < GetAssocCount(); i++ )
  108. {
  109. if( pNode == GetAssoc(i) )
  110. {
  111. return i;
  112. }
  113. }
  114. return (int)m_Associations.Add(pNode);
  115. }
  116. template <class T>
  117. inline void CTreeNode<T>::RemoveAssoc(CTreeNode<T>* pNode)
  118. {
  119. for( int i = 0; i < GetAssocCount(); i++ )
  120. {
  121. CTreeNode<T>* pAssocNode = GetAssoc(i);
  122. if( pNode == pAssocNode )
  123. {
  124. RemoveAssoc(i);
  125. return;
  126. }
  127. }
  128. }
  129. template <class T>
  130. inline void CTreeNode<T>::RemoveAssoc(int iIndex)
  131. {
  132. CTreeNode<T>* pAssocNode = GetAssoc(iIndex);
  133. if( pAssocNode )
  134. {
  135. m_Associations.RemoveAt(iIndex);
  136. }
  137. }
  138. //////////////////////////////////////////////////////////////////////
  139. // CTree implementation
  140. //////////////////////////////////////////////////////////////////////
  141. // Construction/Destruction
  142. //////////////////////////////////////////////////////////////////////
  143. template <class T>
  144. CTree<T>::CTree()
  145. {
  146. m_pRootNode = NULL;
  147. }
  148. template <class T>
  149. CTree<T>::~CTree()
  150. {
  151. if( m_pRootNode )
  152. {
  153. delete m_pRootNode;
  154. m_pRootNode = NULL;
  155. }
  156. }