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.

2058 lines
38 KiB

  1. #ifndef _ThreadedAvlTREE_CPP
  2. #define _ThreadedAvlTREE_CPP
  3. /*
  4. * Class:
  5. *
  6. * WmiAllocator
  7. *
  8. * Description:
  9. *
  10. * Provides abstraction above heap allocation functions
  11. *
  12. * Version:
  13. *
  14. * Initial
  15. *
  16. * Last Changed:
  17. *
  18. * See Source Depot for change history
  19. *
  20. */
  21. #if 0
  22. #include <precomp.h>
  23. #include <windows.h>
  24. #include <stdio.h>
  25. #include <ThreadedAvlTree.h>
  26. #endif
  27. #if 1
  28. #define INLINE_COMPARE
  29. #endif
  30. /******************************************************************************
  31. *
  32. * Name:
  33. *
  34. *
  35. * Description:
  36. *
  37. *
  38. *****************************************************************************/
  39. template <class WmiKey,class WmiElement>
  40. WmiThreadedAvlTree <WmiKey,WmiElement> :: WmiThreadedAvlTree <WmiKey,WmiElement> (
  41. WmiAllocator &a_Allocator
  42. ) : m_Allocator ( a_Allocator ) ,
  43. m_Size ( 0 ) ,
  44. m_Root ( NULL ) ,
  45. m_Head ( NULL ) ,
  46. m_Tail ( NULL )
  47. {
  48. }
  49. /******************************************************************************
  50. *
  51. * Name:
  52. *
  53. *
  54. * Description:
  55. *
  56. *
  57. *****************************************************************************/
  58. template <class WmiKey,class WmiElement>
  59. WmiThreadedAvlTree <WmiKey,WmiElement> :: ~WmiThreadedAvlTree <WmiKey,WmiElement> ()
  60. {
  61. WmiStatusCode t_StatusCode = UnInitialize () ;
  62. }
  63. /******************************************************************************
  64. *
  65. * Name:
  66. *
  67. *
  68. * Description:
  69. *
  70. *
  71. *****************************************************************************/
  72. template <class WmiKey,class WmiElement>
  73. WmiStatusCode WmiThreadedAvlTree <WmiKey,WmiElement> :: Initialize ()
  74. {
  75. WmiStatusCode t_StatusCode = e_StatusCode_Success ;
  76. return t_StatusCode ;
  77. }
  78. /******************************************************************************
  79. *
  80. * Name:
  81. *
  82. *
  83. * Description:
  84. *
  85. *
  86. *****************************************************************************/
  87. template <class WmiKey,class WmiElement>
  88. WmiStatusCode WmiThreadedAvlTree <WmiKey,WmiElement> :: UnInitialize ()
  89. {
  90. WmiStatusCode t_StatusCode = e_StatusCode_Success ;
  91. if ( m_Root )
  92. {
  93. t_StatusCode = RecursiveUnInitialize ( m_Root ) ;
  94. m_Root = m_Head = m_Tail = NULL;
  95. }
  96. return t_StatusCode ;
  97. }
  98. /******************************************************************************
  99. *
  100. * Name:
  101. *
  102. *
  103. * Description:
  104. *
  105. *
  106. *****************************************************************************/
  107. template <class WmiKey,class WmiElement>
  108. WmiStatusCode WmiThreadedAvlTree <WmiKey,WmiElement> :: RecursiveUnInitialize ( WmiThreadedAvlNode *a_Node )
  109. {
  110. WmiStatusCode t_StatusCode = e_StatusCode_Success ;
  111. WmiThreadedAvlNode *t_Left = a_Node->m_Left ;
  112. if ( t_Left )
  113. {
  114. t_StatusCode = RecursiveUnInitialize ( t_Left ) ;
  115. t_Left->~WmiThreadedAvlNode () ;
  116. WmiStatusCode t_TempStatusCode = m_Allocator.Delete (
  117. ( void * ) t_Left
  118. ) ;
  119. t_Left = NULL ;
  120. }
  121. WmiThreadedAvlNode *t_Right = a_Node->m_Right ;
  122. if ( t_Right )
  123. {
  124. t_StatusCode = RecursiveUnInitialize ( t_Right ) ;
  125. t_Right->~WmiThreadedAvlNode () ;
  126. WmiStatusCode t_TempStatusCode = m_Allocator.Delete (
  127. ( void * ) t_Right
  128. ) ;
  129. t_Right = NULL ;
  130. }
  131. return t_StatusCode ;
  132. }
  133. /******************************************************************************
  134. *
  135. * Name:
  136. *
  137. *
  138. * Description:
  139. *
  140. *
  141. *****************************************************************************/
  142. template <class WmiKey,class WmiElement>
  143. WmiStatusCode WmiThreadedAvlTree <WmiKey,WmiElement> :: Find (
  144. const WmiKey &a_Key ,
  145. Iterator &a_Iterator
  146. )
  147. {
  148. WmiThreadedAvlNode *t_Node = m_Root ;
  149. while ( t_Node )
  150. {
  151. #ifdef INLINE_COMPARE
  152. LONG t_Compare = CompareElement ( a_Key , t_Node->m_Key ) ;
  153. if ( t_Compare == 0 )
  154. #else
  155. if ( a_Key == t_Node->m_Key )
  156. #endif
  157. {
  158. a_Iterator = Iterator ( t_Node ) ;
  159. return e_StatusCode_Success ;
  160. }
  161. else
  162. {
  163. #ifdef INLINE_COMPARE
  164. if ( t_Compare < 0 )
  165. #else
  166. if ( a_Key < t_Node->m_Key )
  167. #endif
  168. {
  169. t_Node = t_Node->m_Left ;
  170. }
  171. else
  172. {
  173. t_Node = t_Node->m_Right ;
  174. }
  175. }
  176. }
  177. return e_StatusCode_NotFound ;
  178. }
  179. /******************************************************************************
  180. *
  181. * Name:
  182. *
  183. *
  184. * Description:
  185. *
  186. *
  187. *****************************************************************************/
  188. template <class WmiKey,class WmiElement>
  189. WmiStatusCode WmiThreadedAvlTree <WmiKey,WmiElement> :: FindNext (
  190. const WmiKey &a_Key ,
  191. Iterator &a_Iterator
  192. )
  193. {
  194. WmiThreadedAvlNode *t_Node = m_Root ;
  195. while ( t_Node )
  196. {
  197. #ifdef INLINE_COMPARE
  198. LONG t_Compare = CompareElement ( a_Key , t_Node->m_Key ) ;
  199. if ( t_Compare == 0 )
  200. #else
  201. if ( a_Key == t_Node->m_Key )
  202. #endif
  203. {
  204. a_Iterator = Iterator ( t_Node ).Increment () ;
  205. return a_Iterator.Null () ? e_StatusCode_NotFound : e_StatusCode_Success ; ;
  206. }
  207. else
  208. {
  209. #ifdef INLINE_COMPARE
  210. if ( t_Compare < 0 )
  211. #else
  212. if ( a_Key < t_Node->m_Key )
  213. #endif
  214. {
  215. if ( t_Node->m_Left )
  216. {
  217. t_Node = t_Node->m_Left ;
  218. }
  219. else
  220. {
  221. a_Iterator = Iterator ( t_Node ) ;
  222. return e_StatusCode_Success ;
  223. }
  224. }
  225. else
  226. {
  227. if ( t_Node->m_Right )
  228. {
  229. t_Node = t_Node->m_Right ;
  230. }
  231. else
  232. {
  233. a_Iterator = Iterator ( t_Node ).Increment () ;
  234. return a_Iterator.Null () ? e_StatusCode_NotFound : e_StatusCode_Success ;
  235. }
  236. }
  237. }
  238. }
  239. return e_StatusCode_NotFound ;
  240. }
  241. /******************************************************************************
  242. *
  243. * Name:
  244. *
  245. *
  246. * Description:
  247. *
  248. *
  249. *****************************************************************************/
  250. template <class WmiKey,class WmiElement>
  251. WmiStatusCode WmiThreadedAvlTree <WmiKey,WmiElement> :: Insert (
  252. const WmiKey &a_Key ,
  253. const WmiElement &a_Element ,
  254. Iterator &a_Iterator
  255. )
  256. {
  257. if ( m_Root )
  258. {
  259. bool t_Increased ;
  260. WmiThreadedAvlNode *t_Node = m_Root ;
  261. while ( t_Node )
  262. {
  263. #ifdef INLINE_COMPARE
  264. LONG t_Compare = CompareElement ( a_Key , t_Node->m_Key ) ;
  265. if ( t_Compare == 0 )
  266. #else
  267. if ( a_Key == t_Node->m_Key )
  268. #endif
  269. {
  270. return e_StatusCode_AlreadyExists ;
  271. }
  272. else
  273. {
  274. #ifdef INLINE_COMPARE
  275. if ( t_Compare < 0 )
  276. #else
  277. if ( a_Key < t_Node->m_Key )
  278. #endif
  279. {
  280. if ( t_Node->m_Left )
  281. {
  282. t_Node = t_Node->m_Left ;
  283. }
  284. else
  285. {
  286. WmiThreadedAvlNode *t_AllocNode ;
  287. WmiStatusCode t_StatusCode = m_Allocator.New (
  288. ( void ** ) & t_AllocNode ,
  289. sizeof ( WmiThreadedAvlNode )
  290. ) ;
  291. if ( t_StatusCode == e_StatusCode_Success )
  292. {
  293. :: new ( ( void* ) t_AllocNode ) WmiThreadedAvlNode () ;
  294. try
  295. {
  296. t_AllocNode->m_Element = a_Element ;
  297. t_AllocNode->m_Key = a_Key ;
  298. }
  299. catch ( Wmi_Heap_Exception &a_Exception )
  300. {
  301. t_AllocNode->~WmiThreadedAvlNode () ;
  302. WmiStatusCode t_StatusCode = m_Allocator.Delete (
  303. ( void * ) t_AllocNode
  304. ) ;
  305. return e_StatusCode_OutOfMemory ;
  306. }
  307. catch ( ... )
  308. {
  309. t_AllocNode->~WmiThreadedAvlNode () ;
  310. WmiStatusCode t_StatusCode = m_Allocator.Delete (
  311. ( void * ) t_AllocNode
  312. ) ;
  313. return e_StatusCode_Unknown ;
  314. }
  315. a_Iterator = Iterator ( t_AllocNode ) ;
  316. m_Size ++ ;
  317. t_Increased = true ;
  318. WmiThreadedAvlNode *t_PreviousNode = t_Node->m_Previous ;
  319. t_AllocNode->m_Previous = t_PreviousNode ;
  320. t_AllocNode->m_Next = t_Node ;
  321. t_Node->m_Previous = t_AllocNode ;
  322. if ( t_PreviousNode )
  323. {
  324. t_PreviousNode->m_Next = t_AllocNode ;
  325. }
  326. else
  327. {
  328. m_Head = t_AllocNode ;
  329. }
  330. t_AllocNode->m_Parent = t_Node ;
  331. t_Node->m_Left = t_AllocNode ;
  332. t_StatusCode = Insert_LeftBalance (
  333. t_Node ,
  334. t_Node->m_Left ,
  335. t_Increased
  336. ) ;
  337. while ( t_Increased )
  338. {
  339. WmiThreadedAvlNode *t_ParentNode = t_Node->m_Parent ;
  340. if ( t_ParentNode )
  341. {
  342. if ( t_ParentNode->m_Left == t_Node )
  343. {
  344. t_StatusCode = Insert_LeftBalance (
  345. t_ParentNode ,
  346. t_Node ,
  347. t_Increased
  348. ) ;
  349. }
  350. else
  351. {
  352. t_StatusCode = Insert_RightBalance (
  353. t_ParentNode ,
  354. t_Node ,
  355. t_Increased
  356. ) ;
  357. }
  358. t_Node = t_Node->m_Parent ;
  359. }
  360. else
  361. {
  362. return e_StatusCode_Success ;
  363. }
  364. }
  365. return e_StatusCode_Success ;
  366. }
  367. else
  368. {
  369. return t_StatusCode ;
  370. }
  371. }
  372. }
  373. else
  374. {
  375. if ( t_Node->m_Right )
  376. {
  377. t_Node = t_Node->m_Right ;
  378. }
  379. else
  380. {
  381. WmiThreadedAvlNode *t_AllocNode ;
  382. WmiStatusCode t_StatusCode = m_Allocator.New (
  383. ( void ** ) & t_AllocNode ,
  384. sizeof ( WmiThreadedAvlNode )
  385. ) ;
  386. if ( t_StatusCode == e_StatusCode_Success )
  387. {
  388. :: new ( ( void* ) t_AllocNode ) WmiThreadedAvlNode () ;
  389. try
  390. {
  391. t_AllocNode->m_Element = a_Element ;
  392. t_AllocNode->m_Key = a_Key ;
  393. }
  394. catch ( Wmi_Heap_Exception &a_Exception )
  395. {
  396. t_AllocNode->~WmiThreadedAvlNode () ;
  397. WmiStatusCode t_StatusCode = m_Allocator.Delete (
  398. ( void * ) t_AllocNode
  399. ) ;
  400. return e_StatusCode_OutOfMemory ;
  401. }
  402. catch ( ... )
  403. {
  404. t_AllocNode->~WmiThreadedAvlNode () ;
  405. WmiStatusCode t_StatusCode = m_Allocator.Delete (
  406. ( void * ) t_AllocNode
  407. ) ;
  408. return e_StatusCode_Unknown ;
  409. }
  410. a_Iterator = Iterator ( t_AllocNode ) ;
  411. m_Size ++ ;
  412. t_Increased = true ;
  413. WmiThreadedAvlNode *t_NextNode = t_Node->m_Next ;
  414. t_AllocNode->m_Previous = t_Node ;
  415. t_AllocNode->m_Next = t_NextNode ;
  416. t_Node->m_Next = t_AllocNode ;
  417. if ( t_NextNode )
  418. {
  419. t_NextNode->m_Previous = t_AllocNode ;
  420. }
  421. else
  422. {
  423. m_Tail = t_AllocNode ;
  424. }
  425. t_AllocNode->m_Parent = t_Node ;
  426. t_Node->m_Right = t_AllocNode ;
  427. t_StatusCode = Insert_RightBalance (
  428. t_Node ,
  429. t_Node->m_Right ,
  430. t_Increased
  431. ) ;
  432. while ( t_Increased )
  433. {
  434. WmiThreadedAvlNode *t_ParentNode = t_Node->m_Parent ;
  435. if ( t_ParentNode )
  436. {
  437. if ( t_ParentNode->m_Left == t_Node )
  438. {
  439. t_StatusCode = Insert_LeftBalance (
  440. t_ParentNode ,
  441. t_Node ,
  442. t_Increased
  443. ) ;
  444. }
  445. else
  446. {
  447. t_StatusCode = Insert_RightBalance (
  448. t_ParentNode ,
  449. t_Node ,
  450. t_Increased
  451. ) ;
  452. }
  453. t_Node = t_ParentNode ;
  454. }
  455. else
  456. {
  457. return e_StatusCode_Success ;
  458. }
  459. }
  460. return e_StatusCode_Success ;
  461. }
  462. else
  463. {
  464. return t_StatusCode ;
  465. }
  466. }
  467. }
  468. }
  469. }
  470. return e_StatusCode_Failed ;
  471. }
  472. else
  473. {
  474. WmiThreadedAvlNode *t_AllocNode ;
  475. WmiStatusCode t_StatusCode = m_Allocator.New (
  476. ( void ** ) & t_AllocNode ,
  477. sizeof ( WmiThreadedAvlNode )
  478. ) ;
  479. if ( t_StatusCode == e_StatusCode_Success )
  480. {
  481. :: new ( ( void* ) t_AllocNode ) WmiThreadedAvlNode () ;
  482. try
  483. {
  484. t_AllocNode->m_Element = a_Element ;
  485. t_AllocNode->m_Key = a_Key ;
  486. }
  487. catch ( Wmi_Heap_Exception &a_Exception )
  488. {
  489. t_AllocNode->~WmiThreadedAvlNode () ;
  490. WmiStatusCode t_StatusCode = m_Allocator.Delete (
  491. ( void * ) t_AllocNode
  492. ) ;
  493. return e_StatusCode_OutOfMemory ;
  494. }
  495. catch ( ... )
  496. {
  497. t_AllocNode->~WmiThreadedAvlNode () ;
  498. WmiStatusCode t_StatusCode = m_Allocator.Delete (
  499. ( void * ) t_AllocNode
  500. ) ;
  501. return e_StatusCode_Unknown ;
  502. }
  503. a_Iterator = Iterator ( t_AllocNode ) ;
  504. m_Root = m_Head = m_Tail = t_AllocNode ;
  505. m_Size ++ ;
  506. }
  507. return t_StatusCode ;
  508. }
  509. }
  510. /******************************************************************************
  511. *
  512. * Name:
  513. *
  514. *
  515. * Description:
  516. *
  517. *
  518. *****************************************************************************/
  519. template <class WmiKey,class WmiElement>
  520. WmiStatusCode WmiThreadedAvlTree <WmiKey,WmiElement> :: Delete (
  521. const WmiKey &a_Key
  522. )
  523. {
  524. if ( m_Root )
  525. {
  526. bool t_Decreased ;
  527. WmiThreadedAvlNode *t_Node = m_Root ;
  528. while ( t_Node )
  529. {
  530. #ifdef INLINE_COMPARE
  531. LONG t_Compare = CompareElement ( a_Key , t_Node->m_Key ) ;
  532. if ( t_Compare == 0 )
  533. #else
  534. if ( a_Key == t_Node->m_Key )
  535. #endif
  536. {
  537. WmiThreadedAvlNode *t_ParentNode = t_Node->m_Parent ;
  538. if ( t_ParentNode )
  539. {
  540. bool t_LeftSide = t_ParentNode->m_Left == t_Node ? true : false ;
  541. WmiThreadedAvlNode *t_PreviousNode = t_Node->m_Previous ;
  542. WmiThreadedAvlNode *t_NextNode = t_Node->m_Next ;
  543. if ( t_PreviousNode )
  544. {
  545. t_PreviousNode->m_Next = t_NextNode;
  546. }
  547. else
  548. {
  549. m_Head = t_NextNode ;
  550. }
  551. if ( t_NextNode )
  552. {
  553. t_NextNode->m_Previous = t_PreviousNode ;
  554. }
  555. else
  556. {
  557. m_Tail = t_PreviousNode ;
  558. }
  559. WmiStatusCode t_StatusCode = DeleteFixup ( t_Node , t_Decreased ) ;
  560. m_Size -- ;
  561. while ( t_Decreased )
  562. {
  563. if ( t_ParentNode )
  564. {
  565. if ( t_LeftSide )
  566. {
  567. t_StatusCode = Delete_LeftBalance (
  568. t_ParentNode ,
  569. t_ParentNode->m_Right ,
  570. t_Decreased
  571. ) ;
  572. }
  573. else
  574. {
  575. t_StatusCode = Delete_RightBalance (
  576. t_ParentNode ,
  577. t_ParentNode->m_Left ,
  578. t_Decreased
  579. ) ;
  580. }
  581. t_Node = t_ParentNode ;
  582. t_ParentNode = t_Node->m_Parent ;
  583. if ( t_ParentNode )
  584. {
  585. t_LeftSide = t_ParentNode->m_Left == t_Node ? true : false ;
  586. }
  587. }
  588. else
  589. {
  590. return e_StatusCode_Success ;
  591. }
  592. }
  593. return e_StatusCode_Success ;
  594. }
  595. else
  596. {
  597. WmiThreadedAvlNode *t_PreviousNode = t_Node->m_Previous ;
  598. WmiThreadedAvlNode *t_NextNode = t_Node->m_Next ;
  599. if ( t_PreviousNode )
  600. {
  601. t_PreviousNode->m_Next = t_NextNode;
  602. }
  603. else
  604. {
  605. m_Head = t_NextNode ;
  606. }
  607. if ( t_NextNode )
  608. {
  609. t_NextNode->m_Previous = t_PreviousNode ;
  610. }
  611. else
  612. {
  613. m_Tail = t_PreviousNode ;
  614. }
  615. m_Size -- ;
  616. return DeleteFixup ( t_Node , t_Decreased ) ;
  617. }
  618. }
  619. else
  620. {
  621. #ifdef INLINE_COMPARE
  622. if ( t_Compare < 0 )
  623. #else
  624. if ( a_Key < t_Node->m_Key )
  625. #endif
  626. {
  627. if ( t_Node->m_Left )
  628. {
  629. t_Node = t_Node->m_Left ;
  630. }
  631. else
  632. {
  633. return e_StatusCode_NotFound ;
  634. }
  635. }
  636. else
  637. {
  638. if ( t_Node->m_Right )
  639. {
  640. t_Node = t_Node->m_Right ;
  641. }
  642. else
  643. {
  644. return e_StatusCode_NotFound ;
  645. }
  646. }
  647. }
  648. }
  649. return e_StatusCode_Failed ;
  650. }
  651. else
  652. {
  653. return e_StatusCode_NotFound ;
  654. }
  655. }
  656. /******************************************************************************
  657. *
  658. * Name:
  659. *
  660. *
  661. * Description:
  662. *
  663. *
  664. * case 1:
  665. *
  666. * N A
  667. * / \
  668. * A ->
  669. *
  670. * Parent Decreased,on side based on recursion step
  671. *
  672. * case 2:
  673. *
  674. * N A
  675. * / \
  676. * A ->
  677. *
  678. * Parent Decreased,on side based on recursion step
  679. *
  680. * case 3:
  681. * N B
  682. * / \ / \
  683. * A B -> A Y
  684. * / \
  685. * Y
  686. *
  687. * B decreased on Right
  688. *
  689. * case 4:
  690. *
  691. * N B
  692. * / \ / \
  693. * A C -> A C
  694. * / \ / \
  695. * B Y X Y
  696. * \
  697. * X
  698. *
  699. * C decreased on Left, Apply LeftBalance on C
  700. * Apply RightBalance on B
  701. *
  702. * N D
  703. * / \ / \
  704. * A C -> A C
  705. * / \ / \
  706. * B Y B Y
  707. * / \ / \
  708. * D X E X
  709. * \
  710. * E
  711. *
  712. *****************************************************************************/
  713. template <class WmiKey,class WmiElement>
  714. WmiStatusCode WmiThreadedAvlTree <WmiKey,WmiElement> :: DeleteFixup ( WmiThreadedAvlNode *a_Node , bool &a_Decreased )
  715. {
  716. WmiStatusCode t_StatusCode = e_StatusCode_Success ;
  717. a_Decreased = true ;
  718. WmiThreadedAvlNode *t_Left = a_Node->m_Left ;
  719. WmiThreadedAvlNode *t_Right = a_Node->m_Right ;
  720. WmiThreadedAvlNode *t_Parent = a_Node->m_Parent ;
  721. if ( t_Left && t_Right )
  722. {
  723. Iterator t_Iterator ( a_Node ) ;
  724. t_Iterator.Increment () ;
  725. WmiThreadedAvlNode *t_Successor = t_Iterator.m_Node ;
  726. t_Successor->m_State = a_Node->m_State ;
  727. if ( t_Parent )
  728. {
  729. if ( t_Parent->m_Left == a_Node )
  730. {
  731. t_Parent->m_Left = t_Successor ;
  732. }
  733. else
  734. {
  735. t_Parent->m_Right = t_Successor;
  736. }
  737. }
  738. else
  739. {
  740. m_Root = t_Successor ;
  741. }
  742. if ( t_Successor->m_Parent != a_Node )
  743. {
  744. /* case 4 */
  745. if ( a_Node->m_Left )
  746. {
  747. a_Node->m_Left->m_Parent = t_Successor ;
  748. }
  749. if ( a_Node->m_Right )
  750. {
  751. a_Node->m_Right->m_Parent = t_Successor ;
  752. }
  753. WmiThreadedAvlNode *t_Node = t_Successor->m_Parent ;
  754. t_Successor->m_Parent->m_Left = t_Successor->m_Right ;
  755. if ( t_Successor->m_Left )
  756. {
  757. t_Successor->m_Left->m_Parent = t_Successor->m_Parent ;
  758. }
  759. if ( t_Successor->m_Right )
  760. {
  761. t_Successor->m_Right->m_Parent = t_Successor->m_Parent ;
  762. }
  763. t_Successor->m_Left = a_Node->m_Left ;
  764. t_Successor->m_Right = a_Node->m_Right ;
  765. t_Successor->m_Parent = a_Node->m_Parent ;
  766. do
  767. {
  768. t_StatusCode = Delete_LeftBalance ( t_Node , t_Node->m_Right , a_Decreased ) ;
  769. #if 0
  770. ULONG t_Count = 1 ;
  771. ULONG t_Height = 0 ;
  772. ULONG t_MaxHeight = 0 ;
  773. t_StatusCode = RecursiveCheck ( t_Node , t_Count , t_Height , t_MaxHeight ) ;
  774. if ( t_StatusCode == e_StatusCode_Success )
  775. {
  776. }
  777. #endif
  778. t_Node = t_Node->m_Parent ;
  779. }
  780. while ( ( t_StatusCode == e_StatusCode_Success ) && ( t_Node != t_Successor ) ) ;
  781. if ( t_StatusCode == e_StatusCode_Success )
  782. {
  783. t_StatusCode = Delete_RightBalance ( t_Node , t_Node->m_Left , a_Decreased ) ;
  784. #if 0
  785. ULONG t_Count = 1 ;
  786. ULONG t_Height = 0 ;
  787. ULONG t_MaxHeight = 0 ;
  788. t_StatusCode = RecursiveCheck ( t_Node , t_Count , t_Height , t_MaxHeight ) ;
  789. if ( t_StatusCode == e_StatusCode_Success )
  790. {
  791. }
  792. #endif
  793. }
  794. }
  795. else
  796. {
  797. /* case 3 */
  798. if ( a_Node->m_Left )
  799. {
  800. a_Node->m_Left->m_Parent = t_Successor ;
  801. }
  802. if ( a_Node->m_Right )
  803. {
  804. a_Node->m_Right->m_Parent = t_Successor ;
  805. }
  806. t_Successor->m_Left = a_Node->m_Left ;
  807. t_Successor->m_Parent = a_Node->m_Parent ;
  808. t_StatusCode = Delete_RightBalance ( t_Successor , t_Successor->m_Left , a_Decreased ) ;
  809. }
  810. }
  811. else
  812. {
  813. if ( t_Parent )
  814. {
  815. if ( t_Parent->m_Left == a_Node )
  816. {
  817. t_Parent->m_Left = t_Left ? t_Left : t_Right ;
  818. }
  819. else
  820. {
  821. t_Parent->m_Right = t_Left ? t_Left : t_Right ;
  822. }
  823. }
  824. else
  825. {
  826. m_Root = a_Node->m_Left ? a_Node->m_Left : a_Node->m_Right ;
  827. }
  828. if ( t_Left )
  829. {
  830. /* case 1 */
  831. t_Left->m_Parent = a_Node->m_Parent ;
  832. t_Left->m_State = a_Node->m_State ;
  833. t_StatusCode = Delete_LeftBalance ( t_Left , t_Left->m_Right , a_Decreased ) ;
  834. }
  835. else if ( t_Right )
  836. {
  837. /* case 2 */
  838. t_Right->m_Parent = a_Node->m_Parent ;
  839. t_Right->m_State = a_Node->m_State ;
  840. t_StatusCode = Delete_RightBalance ( t_Right , t_Right->m_Left , a_Decreased ) ;
  841. }
  842. }
  843. a_Node->~WmiThreadedAvlNode () ;
  844. t_StatusCode = m_Allocator.Delete (
  845. ( void * ) a_Node
  846. ) ;
  847. return t_StatusCode ;
  848. }
  849. /******************************************************************************
  850. *
  851. * Name:
  852. *
  853. *
  854. * Description:
  855. *
  856. *
  857. *****************************************************************************/
  858. template <class WmiKey,class WmiElement>
  859. WmiStatusCode WmiThreadedAvlTree <WmiKey,WmiElement> :: Insert_LeftBalance (
  860. WmiThreadedAvlNode *&A ,
  861. WmiThreadedAvlNode *B ,
  862. bool &a_Increased
  863. )
  864. {
  865. WmiStatusCode t_StatusCode = e_StatusCode_Success ;
  866. if ( a_Increased )
  867. {
  868. switch ( A->m_State )
  869. {
  870. case WmiThreadedAvlNode :: e_Equal:
  871. {
  872. A->m_State = WmiThreadedAvlNode :: e_LeftHigher ;
  873. }
  874. break ;
  875. case WmiThreadedAvlNode :: e_RightHigher:
  876. {
  877. A->m_State = WmiThreadedAvlNode :: e_Equal ;
  878. a_Increased = false ;
  879. }
  880. break ;
  881. case WmiThreadedAvlNode :: e_LeftHigher:
  882. {
  883. a_Increased = false ;
  884. switch ( B->m_State )
  885. {
  886. case WmiThreadedAvlNode :: e_Equal:
  887. {
  888. }
  889. break ;
  890. case WmiThreadedAvlNode :: e_LeftHigher:
  891. {
  892. WmiThreadedAvlNode *t_Parent = A->m_Parent ;
  893. if ( t_Parent )
  894. {
  895. if ( t_Parent->m_Left == A )
  896. {
  897. t_Parent->m_Left = B ;
  898. }
  899. else
  900. {
  901. t_Parent->m_Right = B ;
  902. }
  903. }
  904. else
  905. {
  906. m_Root = B ;
  907. }
  908. if ( B->m_Right )
  909. {
  910. B->m_Right->m_Parent = A ;
  911. }
  912. A->m_State = WmiThreadedAvlNode :: e_Equal ;
  913. A->m_Left = B->m_Right ;
  914. A->m_Parent = B ;
  915. B->m_State = WmiThreadedAvlNode :: e_Equal ;
  916. B->m_Right = A ;
  917. B->m_Parent = t_Parent ;
  918. A = B ;
  919. }
  920. break ;
  921. case WmiThreadedAvlNode :: e_RightHigher:
  922. {
  923. WmiThreadedAvlNode *C = A->m_Left->m_Right ;
  924. WmiThreadedAvlNode *t_Parent = A->m_Parent ;
  925. if ( t_Parent )
  926. {
  927. if ( t_Parent->m_Left == A )
  928. {
  929. t_Parent->m_Left = C ;
  930. }
  931. else
  932. {
  933. t_Parent->m_Right = C ;
  934. }
  935. }
  936. else
  937. {
  938. m_Root = C ;
  939. }
  940. A->m_Parent = C ;
  941. B->m_Parent = C ;
  942. if ( C->m_Left )
  943. {
  944. C->m_Left->m_Parent = B ;
  945. }
  946. if ( C->m_Right )
  947. {
  948. C->m_Right->m_Parent = A ;
  949. }
  950. A->m_Left = C->m_Right ;
  951. B->m_Right = C->m_Left ;
  952. C->m_Left = B ;
  953. C->m_Right = A ;
  954. C->m_Parent = t_Parent ;
  955. switch ( C->m_State )
  956. {
  957. case WmiThreadedAvlNode :: e_LeftHigher:
  958. {
  959. B->m_State = WmiThreadedAvlNode :: e_Equal ;
  960. A->m_State = WmiThreadedAvlNode :: e_RightHigher ;
  961. }
  962. break ;
  963. case WmiThreadedAvlNode :: e_RightHigher:
  964. {
  965. B->m_State = WmiThreadedAvlNode :: e_LeftHigher ;
  966. A->m_State = WmiThreadedAvlNode :: e_Equal ;
  967. }
  968. break ;
  969. case WmiThreadedAvlNode :: e_Equal:
  970. {
  971. B->m_State = WmiThreadedAvlNode :: e_Equal ;
  972. A->m_State = WmiThreadedAvlNode :: e_Equal ;
  973. }
  974. break ;
  975. default:
  976. {
  977. t_StatusCode = e_StatusCode_Unknown ;
  978. }
  979. break ;
  980. }
  981. C->m_State = WmiThreadedAvlNode :: e_Equal ;
  982. A = C ;
  983. }
  984. break ;
  985. default:
  986. {
  987. t_StatusCode = e_StatusCode_Unknown ;
  988. }
  989. break ;
  990. }
  991. }
  992. break ;
  993. default:
  994. {
  995. t_StatusCode = e_StatusCode_Unknown ;
  996. }
  997. break ;
  998. }
  999. }
  1000. return t_StatusCode ;
  1001. }
  1002. /******************************************************************************
  1003. *
  1004. * Name:
  1005. *
  1006. *
  1007. * Description:
  1008. *
  1009. *
  1010. *****************************************************************************/
  1011. template <class WmiKey,class WmiElement>
  1012. WmiStatusCode WmiThreadedAvlTree <WmiKey,WmiElement> :: Insert_RightBalance (
  1013. WmiThreadedAvlNode *&A ,
  1014. WmiThreadedAvlNode *B ,
  1015. bool &a_Increased
  1016. )
  1017. {
  1018. WmiStatusCode t_StatusCode = e_StatusCode_Success ;
  1019. if ( a_Increased )
  1020. {
  1021. switch ( A->m_State )
  1022. {
  1023. case WmiThreadedAvlNode :: e_Equal:
  1024. {
  1025. A->m_State = WmiThreadedAvlNode :: e_RightHigher ;
  1026. }
  1027. break ;
  1028. case WmiThreadedAvlNode :: e_LeftHigher:
  1029. {
  1030. A->m_State = WmiThreadedAvlNode :: e_Equal ;
  1031. a_Increased = false ;
  1032. }
  1033. break ;
  1034. case WmiThreadedAvlNode :: e_RightHigher:
  1035. {
  1036. a_Increased = false ;
  1037. switch ( B->m_State )
  1038. {
  1039. case WmiThreadedAvlNode :: e_Equal:
  1040. {
  1041. }
  1042. break ;
  1043. case WmiThreadedAvlNode :: e_RightHigher:
  1044. {
  1045. WmiThreadedAvlNode *t_Parent = A->m_Parent ;
  1046. if ( t_Parent )
  1047. {
  1048. if ( t_Parent->m_Left == A )
  1049. {
  1050. t_Parent->m_Left = B ;
  1051. }
  1052. else
  1053. {
  1054. t_Parent->m_Right = B ;
  1055. }
  1056. }
  1057. else
  1058. {
  1059. m_Root = B ;
  1060. }
  1061. if ( B->m_Left )
  1062. {
  1063. B->m_Left->m_Parent = A ;
  1064. }
  1065. A->m_State = WmiThreadedAvlNode :: e_Equal ;
  1066. A->m_Right = B->m_Left ;
  1067. A->m_Parent = B ;
  1068. B->m_State = WmiThreadedAvlNode :: e_Equal ;
  1069. B->m_Left = A ;
  1070. B->m_Parent = t_Parent ;
  1071. A = B ;
  1072. }
  1073. break ;
  1074. case WmiThreadedAvlNode :: e_LeftHigher:
  1075. {
  1076. WmiThreadedAvlNode *C = A->m_Right->m_Left ;
  1077. WmiThreadedAvlNode *t_Parent = A->m_Parent ;
  1078. if ( t_Parent )
  1079. {
  1080. if ( t_Parent->m_Left == A )
  1081. {
  1082. t_Parent->m_Left = C ;
  1083. }
  1084. else
  1085. {
  1086. t_Parent->m_Right = C ;
  1087. }
  1088. }
  1089. else
  1090. {
  1091. m_Root = C ;
  1092. }
  1093. A->m_Parent = C ;
  1094. B->m_Parent = C ;
  1095. if ( C->m_Right )
  1096. {
  1097. C->m_Right->m_Parent = B ;
  1098. }
  1099. if ( C->m_Left )
  1100. {
  1101. C->m_Left->m_Parent = A ;
  1102. }
  1103. B->m_Left = C->m_Right ;
  1104. A->m_Right = C->m_Left ;
  1105. C->m_Right = B ;
  1106. C->m_Left = A ;
  1107. C->m_Parent = t_Parent ;
  1108. switch ( C->m_State )
  1109. {
  1110. case WmiThreadedAvlNode :: e_LeftHigher:
  1111. {
  1112. B->m_State = WmiThreadedAvlNode :: e_RightHigher ;
  1113. A->m_State = WmiThreadedAvlNode :: e_Equal ;
  1114. }
  1115. break ;
  1116. case WmiThreadedAvlNode :: e_RightHigher:
  1117. {
  1118. B->m_State = WmiThreadedAvlNode :: e_Equal ;
  1119. A->m_State = WmiThreadedAvlNode :: e_LeftHigher ;
  1120. }
  1121. break ;
  1122. case WmiThreadedAvlNode :: e_Equal:
  1123. {
  1124. B->m_State = WmiThreadedAvlNode :: e_Equal ;
  1125. A->m_State = WmiThreadedAvlNode :: e_Equal ;
  1126. }
  1127. break ;
  1128. default:
  1129. {
  1130. t_StatusCode = e_StatusCode_Unknown ;
  1131. }
  1132. break ;
  1133. }
  1134. C->m_State = WmiThreadedAvlNode :: e_Equal ;
  1135. A = C ;
  1136. }
  1137. break ;
  1138. default:
  1139. {
  1140. t_StatusCode = e_StatusCode_Unknown ;
  1141. }
  1142. break ;
  1143. }
  1144. }
  1145. break ;
  1146. default:
  1147. {
  1148. t_StatusCode = e_StatusCode_Unknown ;
  1149. }
  1150. break ;
  1151. }
  1152. }
  1153. return t_StatusCode ;
  1154. }
  1155. /******************************************************************************
  1156. *
  1157. * Name:
  1158. *
  1159. *
  1160. * Description:
  1161. *
  1162. *
  1163. *****************************************************************************/
  1164. template <class WmiKey,class WmiElement>
  1165. WmiStatusCode WmiThreadedAvlTree <WmiKey,WmiElement> :: Delete_LeftBalance (
  1166. WmiThreadedAvlNode *&A ,
  1167. WmiThreadedAvlNode *B ,
  1168. bool &a_Decreased
  1169. )
  1170. {
  1171. WmiStatusCode t_StatusCode = e_StatusCode_Success ;
  1172. if ( a_Decreased )
  1173. {
  1174. switch ( A->m_State )
  1175. {
  1176. case WmiThreadedAvlNode :: e_Equal:
  1177. {
  1178. A->m_State = WmiThreadedAvlNode :: e_RightHigher ;
  1179. a_Decreased = false ;
  1180. }
  1181. break ;
  1182. case WmiThreadedAvlNode :: e_LeftHigher:
  1183. {
  1184. A->m_State = WmiThreadedAvlNode :: e_Equal ;
  1185. }
  1186. break ;
  1187. case WmiThreadedAvlNode :: e_RightHigher:
  1188. {
  1189. switch ( B->m_State )
  1190. {
  1191. case WmiThreadedAvlNode :: e_Equal:
  1192. {
  1193. a_Decreased = false ;
  1194. WmiThreadedAvlNode *t_Parent = A->m_Parent ;
  1195. if ( t_Parent )
  1196. {
  1197. if ( t_Parent->m_Left == A )
  1198. {
  1199. t_Parent->m_Left = B ;
  1200. }
  1201. else
  1202. {
  1203. t_Parent->m_Right = B ;
  1204. }
  1205. }
  1206. else
  1207. {
  1208. m_Root = B ;
  1209. }
  1210. if ( B->m_Left )
  1211. {
  1212. B->m_Left->m_Parent = A ;
  1213. }
  1214. A->m_State = WmiThreadedAvlNode :: e_RightHigher ;
  1215. A->m_Right = B->m_Left ;
  1216. A->m_Parent = B ;
  1217. B->m_State = WmiThreadedAvlNode :: e_LeftHigher ;
  1218. B->m_Left = A ;
  1219. B->m_Parent = t_Parent ;
  1220. A = B ;
  1221. }
  1222. break ;
  1223. case WmiThreadedAvlNode :: e_RightHigher:
  1224. {
  1225. WmiThreadedAvlNode *t_Parent = A->m_Parent ;
  1226. if ( t_Parent )
  1227. {
  1228. if ( t_Parent->m_Left == A )
  1229. {
  1230. t_Parent->m_Left = B ;
  1231. }
  1232. else
  1233. {
  1234. t_Parent->m_Right = B ;
  1235. }
  1236. }
  1237. else
  1238. {
  1239. m_Root = B ;
  1240. }
  1241. if ( B->m_Left )
  1242. {
  1243. B->m_Left->m_Parent = A ;
  1244. }
  1245. A->m_State = WmiThreadedAvlNode :: e_Equal ;
  1246. A->m_Right = B->m_Left ;
  1247. A->m_Parent = B ;
  1248. B->m_State = WmiThreadedAvlNode :: e_Equal ;
  1249. B->m_Left = A ;
  1250. B->m_Parent = t_Parent ;
  1251. A = B ;
  1252. }
  1253. break ;
  1254. case WmiThreadedAvlNode :: e_LeftHigher:
  1255. {
  1256. WmiThreadedAvlNode *C = A->m_Right->m_Left ;
  1257. WmiThreadedAvlNode *t_Parent = A->m_Parent ;
  1258. if ( t_Parent )
  1259. {
  1260. if ( t_Parent->m_Left == A )
  1261. {
  1262. t_Parent->m_Left = C ;
  1263. }
  1264. else
  1265. {
  1266. t_Parent->m_Right = C ;
  1267. }
  1268. }
  1269. else
  1270. {
  1271. m_Root = C ;
  1272. }
  1273. A->m_Parent = C ;
  1274. B->m_Parent = C ;
  1275. if ( C->m_Left )
  1276. {
  1277. C->m_Left->m_Parent = A ;
  1278. }
  1279. if ( C->m_Right )
  1280. {
  1281. C->m_Right->m_Parent = B ;
  1282. }
  1283. A->m_Right = C->m_Left ;
  1284. B->m_Left = C->m_Right ;
  1285. C->m_Left = A ;
  1286. C->m_Right = B ;
  1287. C->m_Parent = t_Parent ;
  1288. switch ( C->m_State )
  1289. {
  1290. case WmiThreadedAvlNode :: e_LeftHigher:
  1291. {
  1292. A->m_State = WmiThreadedAvlNode :: e_Equal ;
  1293. B->m_State = WmiThreadedAvlNode :: e_RightHigher ;
  1294. }
  1295. break ;
  1296. case WmiThreadedAvlNode :: e_RightHigher:
  1297. {
  1298. B->m_State = WmiThreadedAvlNode :: e_Equal ;
  1299. A->m_State = WmiThreadedAvlNode :: e_LeftHigher ;
  1300. }
  1301. break ;
  1302. case WmiThreadedAvlNode :: e_Equal:
  1303. {
  1304. B->m_State = WmiThreadedAvlNode :: e_Equal ;
  1305. A->m_State = WmiThreadedAvlNode :: e_Equal ;
  1306. }
  1307. break ;
  1308. default:
  1309. {
  1310. t_StatusCode = e_StatusCode_Unknown ;
  1311. }
  1312. break ;
  1313. }
  1314. C->m_State = WmiThreadedAvlNode :: e_Equal ;
  1315. A = C ;
  1316. }
  1317. break ;
  1318. default:
  1319. {
  1320. t_StatusCode = e_StatusCode_Unknown ;
  1321. }
  1322. break ;
  1323. }
  1324. }
  1325. break ;
  1326. default:
  1327. {
  1328. t_StatusCode = e_StatusCode_Unknown ;
  1329. }
  1330. break ;
  1331. }
  1332. }
  1333. return t_StatusCode ;
  1334. }
  1335. /******************************************************************************
  1336. *
  1337. * Name:
  1338. *
  1339. *
  1340. * Description:
  1341. *
  1342. *
  1343. *****************************************************************************/
  1344. template <class WmiKey,class WmiElement>
  1345. WmiStatusCode WmiThreadedAvlTree <WmiKey,WmiElement> :: Delete_RightBalance (
  1346. WmiThreadedAvlNode *&A ,
  1347. WmiThreadedAvlNode *B ,
  1348. bool &a_Decreased
  1349. )
  1350. {
  1351. WmiStatusCode t_StatusCode = e_StatusCode_Success ;
  1352. if ( a_Decreased )
  1353. {
  1354. switch ( A->m_State )
  1355. {
  1356. case WmiThreadedAvlNode :: e_Equal:
  1357. {
  1358. A->m_State = WmiThreadedAvlNode :: e_LeftHigher ;
  1359. a_Decreased = false ;
  1360. }
  1361. break ;
  1362. case WmiThreadedAvlNode :: e_RightHigher:
  1363. {
  1364. A->m_State = WmiThreadedAvlNode :: e_Equal ;
  1365. }
  1366. break ;
  1367. case WmiThreadedAvlNode :: e_LeftHigher:
  1368. {
  1369. switch ( B->m_State )
  1370. {
  1371. case WmiThreadedAvlNode :: e_Equal:
  1372. {
  1373. a_Decreased = false ;
  1374. WmiThreadedAvlNode *t_Parent = A->m_Parent ;
  1375. if ( t_Parent )
  1376. {
  1377. if ( t_Parent->m_Left == A )
  1378. {
  1379. t_Parent->m_Left = B ;
  1380. }
  1381. else
  1382. {
  1383. t_Parent->m_Right = B ;
  1384. }
  1385. }
  1386. else
  1387. {
  1388. m_Root = B ;
  1389. }
  1390. if ( B->m_Right )
  1391. {
  1392. B->m_Right->m_Parent = A ;
  1393. }
  1394. A->m_State = WmiThreadedAvlNode :: e_LeftHigher ;
  1395. A->m_Left = B->m_Right ;
  1396. A->m_Parent = B ;
  1397. B->m_State = WmiThreadedAvlNode :: e_RightHigher ;
  1398. B->m_Right = A ;
  1399. B->m_Parent = t_Parent ;
  1400. A = B ;
  1401. }
  1402. break ;
  1403. case WmiThreadedAvlNode :: e_LeftHigher:
  1404. {
  1405. WmiThreadedAvlNode *t_Parent = A->m_Parent ;
  1406. if ( t_Parent )
  1407. {
  1408. if ( t_Parent->m_Left == A )
  1409. {
  1410. t_Parent->m_Left = B ;
  1411. }
  1412. else
  1413. {
  1414. t_Parent->m_Right = B ;
  1415. }
  1416. }
  1417. else
  1418. {
  1419. m_Root = B ;
  1420. }
  1421. if ( B->m_Right )
  1422. {
  1423. B->m_Right->m_Parent = A ;
  1424. }
  1425. A->m_State = WmiThreadedAvlNode :: e_Equal ;
  1426. A->m_Left = B->m_Right ;
  1427. A->m_Parent = B ;
  1428. B->m_State = WmiThreadedAvlNode :: e_Equal ;
  1429. B->m_Right = A ;
  1430. B->m_Parent = t_Parent ;
  1431. A = B ;
  1432. }
  1433. break ;
  1434. case WmiThreadedAvlNode :: e_RightHigher:
  1435. {
  1436. WmiThreadedAvlNode *C = A->m_Left->m_Right ;
  1437. WmiThreadedAvlNode *t_Parent = A->m_Parent ;
  1438. if ( t_Parent )
  1439. {
  1440. if ( t_Parent->m_Left == A )
  1441. {
  1442. t_Parent->m_Left = C ;
  1443. }
  1444. else
  1445. {
  1446. t_Parent->m_Right = C ;
  1447. }
  1448. }
  1449. else
  1450. {
  1451. m_Root = C ;
  1452. }
  1453. A->m_Parent = C ;
  1454. B->m_Parent = C ;
  1455. if ( C->m_Left )
  1456. {
  1457. C->m_Left->m_Parent = B ;
  1458. }
  1459. if ( C->m_Right )
  1460. {
  1461. C->m_Right->m_Parent = A ;
  1462. }
  1463. A->m_Left = C->m_Right ;
  1464. B->m_Right = C->m_Left ;
  1465. C->m_Left = B ;
  1466. C->m_Right = A ;
  1467. C->m_Parent = t_Parent ;
  1468. switch ( C->m_State )
  1469. {
  1470. case WmiThreadedAvlNode :: e_LeftHigher:
  1471. {
  1472. B->m_State = WmiThreadedAvlNode :: e_Equal ;
  1473. A->m_State = WmiThreadedAvlNode :: e_RightHigher ;
  1474. }
  1475. break ;
  1476. case WmiThreadedAvlNode :: e_RightHigher:
  1477. {
  1478. A->m_State = WmiThreadedAvlNode :: e_Equal ;
  1479. B->m_State = WmiThreadedAvlNode :: e_LeftHigher ;
  1480. }
  1481. break ;
  1482. case WmiThreadedAvlNode :: e_Equal:
  1483. {
  1484. B->m_State = WmiThreadedAvlNode :: e_Equal ;
  1485. A->m_State = WmiThreadedAvlNode :: e_Equal ;
  1486. }
  1487. break ;
  1488. default:
  1489. {
  1490. t_StatusCode = e_StatusCode_Unknown ;
  1491. }
  1492. break ;
  1493. }
  1494. C->m_State = WmiThreadedAvlNode :: e_Equal ;
  1495. A = C ;
  1496. }
  1497. break ;
  1498. default:
  1499. {
  1500. t_StatusCode = e_StatusCode_Unknown ;
  1501. }
  1502. break ;
  1503. }
  1504. }
  1505. break ;
  1506. default:
  1507. {
  1508. t_StatusCode = e_StatusCode_Unknown ;
  1509. }
  1510. break ;
  1511. }
  1512. }
  1513. return t_StatusCode ;
  1514. }
  1515. /******************************************************************************
  1516. *
  1517. * Name:
  1518. *
  1519. *
  1520. * Description:
  1521. *
  1522. *
  1523. *****************************************************************************/
  1524. template <class WmiKey,class WmiElement>
  1525. WmiStatusCode WmiThreadedAvlTree <WmiKey,WmiElement> :: Merge (
  1526. WmiThreadedAvlTree <WmiKey,WmiElement> &a_Tree
  1527. )
  1528. {
  1529. WmiStatusCode t_StatusCode = e_StatusCode_Success ;
  1530. Iterator t_Iterator = a_Tree.Root ();
  1531. while ( ( ! t_Iterator.Null () ) && ( t_StatusCode == e_StatusCode_Success ) )
  1532. {
  1533. Iterator t_InsertIterator ;
  1534. t_StatusCode = Insert ( t_Iterator.GetKey () , t_Iterator.GetElement () , t_InsertIterator ) ;
  1535. if ( t_StatusCode == e_StatusCode_Success )
  1536. {
  1537. t_StatusCode = a_Tree.Delete ( t_Iterator.GetKey () ) ;
  1538. }
  1539. t_Iterator = a_Tree.Root () ;
  1540. }
  1541. return t_StatusCode ;
  1542. }
  1543. /******************************************************************************
  1544. *
  1545. * Name:
  1546. *
  1547. *
  1548. * Description:
  1549. *
  1550. *
  1551. *****************************************************************************/
  1552. template <class WmiKey,class WmiElement>
  1553. WmiStatusCode WmiThreadedAvlTree <WmiKey,WmiElement> :: Check ( ULONG & a_MaxHeight )
  1554. {
  1555. WmiStatusCode t_StatusCode = e_StatusCode_Success ;
  1556. #if 0
  1557. printf ( "\nStart Check ( %ld )" , m_Size ) ;
  1558. #endif
  1559. if ( m_Root )
  1560. {
  1561. if ( m_Root->m_Parent == NULL )
  1562. {
  1563. ULONG t_Count = 1 ;
  1564. ULONG t_Height = 0 ;
  1565. a_MaxHeight = 0 ;
  1566. t_StatusCode = RecursiveCheck ( m_Root , t_Count , t_Height , a_MaxHeight ) ;
  1567. if ( t_StatusCode == e_StatusCode_Success )
  1568. {
  1569. if ( t_Count != m_Size )
  1570. {
  1571. t_StatusCode = e_StatusCode_Failed ;
  1572. }
  1573. }
  1574. }
  1575. else
  1576. {
  1577. t_StatusCode = e_StatusCode_Failed ;
  1578. }
  1579. }
  1580. #if 0
  1581. printf ( "\nEnd Check" ) ;
  1582. #endif
  1583. return t_StatusCode ;
  1584. }
  1585. /******************************************************************************
  1586. *
  1587. * Name:
  1588. *
  1589. *
  1590. * Description:
  1591. *
  1592. *
  1593. *****************************************************************************/
  1594. template <class WmiKey,class WmiElement>
  1595. WmiStatusCode WmiThreadedAvlTree <WmiKey,WmiElement> :: RecursiveCheck (
  1596. WmiThreadedAvlNode *a_Node ,
  1597. ULONG &a_Count ,
  1598. ULONG a_Height ,
  1599. ULONG &a_MaxHeight
  1600. )
  1601. {
  1602. WmiStatusCode t_StatusCode = e_StatusCode_Success ;
  1603. a_Height ++ ;
  1604. #if 0
  1605. printf ( "\n" ) ;
  1606. for ( ULONG t_TabIndex = 0 ; t_TabIndex < a_Height ; t_TabIndex ++ )
  1607. {
  1608. printf ( "++++" ) ;
  1609. }
  1610. printf ( "%ld" , a_Node->m_Key ) ;
  1611. switch ( a_Node->m_State )
  1612. {
  1613. case WmiThreadedAvlNode :: e_LeftHigher:
  1614. {
  1615. printf ( "\t LH" ) ;
  1616. }
  1617. break ;
  1618. case WmiThreadedAvlNode :: e_RightHigher:
  1619. {
  1620. printf ( "\t RH" ) ;
  1621. }
  1622. break ;
  1623. case WmiThreadedAvlNode :: e_Equal:
  1624. {
  1625. printf ( "\t E" ) ;
  1626. }
  1627. break ;
  1628. }
  1629. #endif
  1630. if ( t_StatusCode == e_StatusCode_Success )
  1631. {
  1632. ULONG t_LeftHeight = a_Height ;
  1633. WmiThreadedAvlNode *t_Left = a_Node->m_Left ;
  1634. if ( t_Left )
  1635. {
  1636. if ( t_Left->m_Parent == a_Node )
  1637. {
  1638. a_Count ++ ;
  1639. t_StatusCode = RecursiveCheck ( t_Left , a_Count , a_Height , t_LeftHeight ) ;
  1640. }
  1641. else
  1642. {
  1643. t_StatusCode = e_StatusCode_Failed ;
  1644. }
  1645. }
  1646. else
  1647. {
  1648. #if 0
  1649. printf ( "\n" ) ;
  1650. for ( ULONG t_TabIndex = 0 ; t_TabIndex <= a_Height ; t_TabIndex ++ )
  1651. {
  1652. printf ( "++++" ) ;
  1653. }
  1654. printf ( "NULL" ) ;
  1655. printf ( "\t E" ) ;
  1656. #endif
  1657. }
  1658. ULONG t_RightHeight = a_Height ;
  1659. WmiThreadedAvlNode *t_Right = a_Node->m_Right ;
  1660. if ( t_Right )
  1661. {
  1662. if ( t_Right->m_Parent == a_Node )
  1663. {
  1664. a_Count ++ ;
  1665. t_StatusCode = RecursiveCheck ( t_Right , a_Count , a_Height , t_RightHeight ) ;
  1666. }
  1667. else
  1668. {
  1669. t_StatusCode = e_StatusCode_Failed ;
  1670. }
  1671. }
  1672. else
  1673. {
  1674. #if 0
  1675. printf ( "\n" ) ;
  1676. for ( ULONG t_TabIndex = 0 ; t_TabIndex <= a_Height ; t_TabIndex ++ )
  1677. {
  1678. printf ( "++++" ) ;
  1679. }
  1680. printf ( "NULL" ) ;
  1681. printf ( "\t E" ) ;
  1682. #endif
  1683. }
  1684. switch ( a_Node->m_State )
  1685. {
  1686. case WmiThreadedAvlNode :: e_LeftHigher:
  1687. {
  1688. if ( t_LeftHeight <= t_RightHeight )
  1689. {
  1690. t_StatusCode = e_StatusCode_Failed ;
  1691. }
  1692. }
  1693. break ;
  1694. case WmiThreadedAvlNode :: e_RightHigher:
  1695. {
  1696. if ( t_LeftHeight >= t_RightHeight )
  1697. {
  1698. t_StatusCode = e_StatusCode_Failed ;
  1699. }
  1700. }
  1701. break ;
  1702. case WmiThreadedAvlNode :: e_Equal:
  1703. {
  1704. if ( t_LeftHeight != t_RightHeight )
  1705. {
  1706. t_StatusCode = e_StatusCode_Failed ;
  1707. }
  1708. }
  1709. break ;
  1710. }
  1711. if ( t_LeftHeight < t_RightHeight )
  1712. {
  1713. a_MaxHeight = t_RightHeight ;
  1714. }
  1715. else
  1716. {
  1717. a_MaxHeight = t_LeftHeight ;
  1718. }
  1719. }
  1720. if ( t_StatusCode == e_StatusCode_Success )
  1721. {
  1722. if ( a_Node->m_State == WmiThreadedAvlNode :: e_Equal )
  1723. {
  1724. if ( ( ( a_Node->m_Left == 0 ) && ( a_Node->m_Right == 0 ) ) )
  1725. {
  1726. }
  1727. else if ( ! ( a_Node->m_Left && a_Node->m_Right ) )
  1728. {
  1729. t_StatusCode = e_StatusCode_Failed ;
  1730. }
  1731. }
  1732. if ( a_Node->m_State == WmiThreadedAvlNode :: e_LeftHigher )
  1733. {
  1734. if ( a_Node->m_Left == NULL )
  1735. {
  1736. t_StatusCode = e_StatusCode_Failed ;
  1737. }
  1738. }
  1739. if ( a_Node->m_State == WmiThreadedAvlNode :: e_RightHigher )
  1740. {
  1741. if ( a_Node->m_Right == NULL )
  1742. {
  1743. t_StatusCode = e_StatusCode_Failed ;
  1744. }
  1745. }
  1746. }
  1747. return t_StatusCode ;
  1748. }
  1749. #endif _ThreadedAvlTREE_CPP