#ifndef __REDBLACKTREE_CPP #define __REDBLACKTREE_CPP /* * Class: * * WmiAllocator * * Description: * * Provides abstraction above heap allocation functions * * Version: * * Initial * * Last Changed: * * See Source Depot for change history * */ #if 0 #include #include #include #include #endif /****************************************************************************** * * Name: * * * Description: * * *****************************************************************************/ template WmiRedBlackTree :: WmiRedBlackTree ( WmiAllocator &a_Allocator ) : m_Allocator ( a_Allocator ) , m_Size ( 0 ) , m_Root ( NULL ) { } /****************************************************************************** * * Name: * * * Description: * * *****************************************************************************/ template WmiRedBlackTree :: ~WmiRedBlackTree () { WmiStatusCode t_StatusCode = UnInitialize () ; } /****************************************************************************** * * Name: * * * Description: * * *****************************************************************************/ template WmiStatusCode WmiRedBlackTree :: Initialize () { WmiStatusCode t_StatusCode = e_StatusCode_Success ; return t_StatusCode ; } /****************************************************************************** * * Name: * * * Description: * * *****************************************************************************/ template WmiStatusCode WmiRedBlackTree :: UnInitialize () { WmiStatusCode t_StatusCode = e_StatusCode_Success ; if ( m_Root ) { t_StatusCode = RecursiveUnInitialize ( m_Root ) ; m_Root = NULL ; } return t_StatusCode ; } /****************************************************************************** * * Name: * * * Description: * * *****************************************************************************/ template WmiStatusCode WmiRedBlackTree :: RecursiveUnInitialize ( WmiRedBlackNode *a_Node ) { WmiStatusCode t_StatusCode = e_StatusCode_Success ; WmiRedBlackNode *t_Left = a_Node->m_Left ; if ( t_Left ) { t_StatusCode = RecursiveUnInitialize ( t_Left ) ; t_Left->~WmiRedBlackNode () ; WmiStatusCode t_StatusCode = m_Allocator.Delete ( ( void * ) t_Left ) ; t_Left = NULL ; } WmiRedBlackNode *t_Right = a_Node->m_Right ; if ( t_Right ) { t_StatusCode = RecursiveUnInitialize ( t_Right ) ; t_Right->~WmiRedBlackNode () ; WmiStatusCode t_StatusCode = m_Allocator.Delete ( ( void * ) t_Right ) ; t_Right = NULL ; } return t_StatusCode ; } /****************************************************************************** * * Name: * * * Description: * * *****************************************************************************/ template WmiStatusCode WmiRedBlackTree :: Insert ( const WmiKey &a_Key , const WmiElement &a_Element , Iterator &a_Iterator ) { WmiStatusCode t_StatusCode = e_StatusCode_Success ; WmiRedBlackNode *t_AllocNode = NULL ; t_StatusCode = m_Allocator.New ( ( void ** ) & t_AllocNode , sizeof ( WmiRedBlackNode ) ) ; if ( t_StatusCode == e_StatusCode_Success ) { :: new ( ( void* ) t_AllocNode ) WmiRedBlackNode () ; try { t_AllocNode->m_Element = a_Element ; t_AllocNode->m_Key = a_Key ; } catch ( Wmi_Heap_Exception &a_Exception ) { t_AllocNode->~WmiRedBlackNode () ; WmiStatusCode t_StatusCode = m_Allocator.Delete ( ( void * ) t_AllocNode ) ; return e_StatusCode_OutOfMemory ; } catch ( ... ) { t_AllocNode->~WmiRedBlackNode () ; WmiStatusCode t_StatusCode = m_Allocator.Delete ( ( void * ) t_AllocNode ) ; return e_StatusCode_Unknown ; } a_Iterator = Iterator ( t_AllocNode ) ; if ( m_Root ) { t_StatusCode = RecursiveInsert ( m_Root , t_AllocNode ) ; if ( t_StatusCode == e_StatusCode_Success ) { m_Size ++ ; } else { t_AllocNode->~WmiRedBlackNode () ; WmiStatusCode t_StatusCode = m_Allocator.Delete ( ( void * ) t_AllocNode ) ; } } else { m_Root = t_AllocNode ; m_Size ++ ; } } else { a_Iterator = Iterator () ; } return t_StatusCode ; } /****************************************************************************** * * Name: * * * Description: * * *****************************************************************************/ template WmiStatusCode WmiRedBlackTree :: Delete ( const WmiKey &a_Key ) { WmiStatusCode t_StatusCode = e_StatusCode_Success ; if ( m_Root ) { t_StatusCode = RecursiveDelete ( m_Root , a_Key ) ; if ( t_StatusCode == e_StatusCode_Success ) { m_Size -- ; } } else { t_StatusCode = e_StatusCode_NotFound ; } return t_StatusCode ; } /****************************************************************************** * * Name: * * * Description: * * *****************************************************************************/ template WmiStatusCode WmiRedBlackTree :: Find ( const WmiKey &a_Key , Iterator &a_Iterator ) { WmiStatusCode t_StatusCode = e_StatusCode_Success ; if ( m_Root ) { t_StatusCode = RecursiveFind ( m_Root , a_Key , a_Iterator ) ; } else { return e_StatusCode_NotFound ; } return t_StatusCode ; } /****************************************************************************** * * Name: * * * Description: * * *****************************************************************************/ template WmiStatusCode WmiRedBlackTree :: FindNext ( const WmiKey &a_Key , Iterator &a_Iterator ) { WmiStatusCode t_StatusCode = e_StatusCode_Success ; if ( m_Root ) { t_StatusCode = RecursiveFindNext ( m_Root , a_Key , a_Iterator ) ; } else { return e_StatusCode_NotFound ; } return t_StatusCode ; } /****************************************************************************** * * Name: * * * Description: * * *****************************************************************************/ template WmiStatusCode WmiRedBlackTree :: RecursiveDelete ( WmiRedBlackNode *a_Node , const WmiKey &a_Key ) { WmiStatusCode t_StatusCode = e_StatusCode_NotFound ; LONG t_Compare = CompareElement ( a_Key , a_Node->m_Key ) ; if ( t_Compare == 0 ) { t_StatusCode = DeleteFixup ( a_Node ) ; } else { if ( t_Compare < 0 ) { WmiRedBlackNode *t_Left = a_Node->m_Left ; if ( t_Left ) { t_StatusCode = RecursiveDelete ( t_Left , a_Key ) ; } } else { WmiRedBlackNode *t_Right = a_Node->m_Right ; if ( t_Right ) { t_StatusCode = RecursiveDelete ( t_Right , a_Key ) ; } } } return t_StatusCode ; } /****************************************************************************** * * Name: * * * Description: * * *****************************************************************************/ template WmiStatusCode WmiRedBlackTree :: RecursiveInsert ( WmiRedBlackNode *a_Node , WmiRedBlackNode *a_Element ) { WmiStatusCode t_StatusCode = e_StatusCode_Success ; LONG t_Compare = CompareElement ( a_Element->m_Key , a_Node->m_Key ) ; if ( t_Compare == 0 ) { t_StatusCode = e_StatusCode_AlreadyExists ; } else { if ( t_Compare < 0 ) { WmiRedBlackNode *t_Left = a_Node->m_Left ; if ( t_Left ) { t_StatusCode = RecursiveInsert ( t_Left , a_Element ) ; } else { a_Element->m_Parent = a_Node ; a_Node->m_Left = a_Element ; } } else { WmiRedBlackNode *t_Right = a_Node->m_Right ; if ( t_Right ) { t_StatusCode = RecursiveInsert ( t_Right , a_Element ) ; } else { a_Element->m_Parent = a_Node ; a_Node->m_Right = a_Element ; } } } return t_StatusCode ; } /****************************************************************************** * * Name: * * * Description: * * *****************************************************************************/ template WmiStatusCode WmiRedBlackTree :: RecursiveFind ( WmiRedBlackNode *a_Node , const WmiKey &a_Key , Iterator &a_Iterator ) { WmiStatusCode t_StatusCode = e_StatusCode_NotFound ; LONG t_Compare = CompareElement ( a_Key , a_Node->m_Key ) ; if ( t_Compare == 0 ) { a_Iterator = Iterator ( a_Node ) ; t_StatusCode = e_StatusCode_Success ; } else { if ( t_Compare < 0 ) { WmiRedBlackNode *t_Left = a_Node->m_Left ; if ( t_Left ) { t_StatusCode = RecursiveFind ( t_Left , a_Key , a_Iterator ) ; } } else { WmiRedBlackNode *t_Right = a_Node->m_Right ; if ( t_Right ) { t_StatusCode = RecursiveFind ( t_Right , a_Key , a_Iterator ) ; } } } return t_StatusCode ; } /****************************************************************************** * * Name: * * * Description: * * *****************************************************************************/ template WmiStatusCode WmiRedBlackTree :: RecursiveFindNext ( WmiRedBlackNode *a_Node , const WmiKey &a_Key , Iterator &a_Iterator ) { WmiStatusCode t_StatusCode = e_StatusCode_NotFound ; LONG t_Compare = CompareElement ( a_Key , a_Node->m_Key ) ; if ( t_Compare == 0 ) { a_Iterator = Iterator ( a_Node ).Increment () ; t_StatusCode = e_StatusCode_Success ; } else { if ( t_Compare < 0 ) { WmiRedBlackNode *t_Left = a_Node->m_Left ; if ( t_Left ) { t_StatusCode = RecursiveFindNext ( t_Left , a_Key , a_Iterator ) ; } else { a_Iterator = Iterator ( a_Node ).Increment () ; t_StatusCode = e_StatusCode_Success ; } } else { WmiRedBlackNode *t_Right = a_Node->m_Right ; if ( t_Right ) { t_StatusCode = RecursiveFindNext ( t_Right , a_Key , a_Iterator ) ; } else { a_Iterator = Iterator ( a_Node ).Increment () ; t_StatusCode = e_StatusCode_Success ; } } } return t_StatusCode ; } /****************************************************************************** * * Name: * * * Description: * * *****************************************************************************/ template WmiStatusCode WmiRedBlackTree :: LeftRotate ( WmiRedBlackNode *a_Node ) { WmiStatusCode t_StatusCode = e_StatusCode_Success ; return t_StatusCode ; } /****************************************************************************** * * Name: * * * Description: * * *****************************************************************************/ template WmiStatusCode WmiRedBlackTree :: RightRotate ( WmiRedBlackNode *a_Node ) { WmiStatusCode t_StatusCode = e_StatusCode_Success ; return t_StatusCode ; } /****************************************************************************** * * Name: * * * Description: * * *****************************************************************************/ template WmiStatusCode WmiRedBlackTree :: DeleteFixup ( WmiRedBlackNode *a_Node ) { WmiStatusCode t_StatusCode = e_StatusCode_Success ; WmiRedBlackNode *t_Left = a_Node->m_Left ; WmiRedBlackNode *t_Right = a_Node->m_Right ; WmiRedBlackNode *t_Parent = a_Node->m_Parent ; if ( t_Left && t_Right ) { Iterator t_Iterator ( a_Node ) ; t_Iterator.Increment () ; WmiRedBlackNode *t_Successor = t_Iterator.m_Node ; if ( t_Parent ) { if ( t_Parent->m_Left == a_Node ) { t_Parent->m_Left = t_Successor ; } else { t_Parent->m_Right = t_Successor; } } else { m_Root = t_Successor ; } if ( t_Successor->m_Parent != a_Node ) { if ( a_Node->m_Left ) { a_Node->m_Left->m_Parent = t_Successor ; } if ( a_Node->m_Right ) { a_Node->m_Right->m_Parent = t_Successor ; } WmiRedBlackNode *t_Node = t_Successor->m_Parent ; t_Successor->m_Parent->m_Left = t_Successor->m_Right ; if ( t_Successor->m_Left ) { t_Successor->m_Left->m_Parent = t_Successor->m_Parent ; } if ( t_Successor->m_Right ) { t_Successor->m_Right->m_Parent = t_Successor->m_Parent ; } t_Successor->m_Left = a_Node->m_Left ; t_Successor->m_Right = a_Node->m_Right ; t_Successor->m_Parent = a_Node->m_Parent ; } else { if ( a_Node->m_Left ) { a_Node->m_Left->m_Parent = t_Successor ; } if ( a_Node->m_Right ) { a_Node->m_Right->m_Parent = t_Successor ; } t_Successor->m_Left = a_Node->m_Left ; t_Successor->m_Parent = a_Node->m_Parent ; } } else { if ( t_Left ) { t_Left->m_Parent = a_Node->m_Parent ; } else if ( t_Right ) { t_Right->m_Parent = a_Node->m_Parent ; } if ( t_Parent ) { if ( t_Parent->m_Left == a_Node ) { t_Parent->m_Left = t_Left ? t_Left : t_Right ; } else { t_Parent->m_Right = t_Left ? t_Left : t_Right ; } } else { m_Root = a_Node->m_Left ? a_Node->m_Left : a_Node->m_Right ; } } a_Node->~WmiRedBlackNode () ; t_StatusCode = m_Allocator.Delete ( ( void * ) a_Node ) ; return t_StatusCode ; } /****************************************************************************** * * Name: * * * Description: * * *****************************************************************************/ template WmiStatusCode WmiRedBlackTree :: Merge ( WmiRedBlackTree &a_Tree ) { WmiStatusCode t_StatusCode = e_StatusCode_Success ; Iterator t_Iterator = a_Tree.Root (); while ( ! t_Iterator.Null () ) { Iterator t_InsertIterator ; WmiStatusCode t_StatusCode = Insert ( t_Iterator.GetKey () , t_Iterator.GetElement () , t_InsertIterator ) ; if ( t_StatusCode ) { t_StatusCode = a_Tree.Delete ( t_Iterator.GetKey () ) ; } t_Iterator = a_Tree.Root () ; } return t_StatusCode ; } #endif __REDBLACKTREE_CPP