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.

111 lines
3.3 KiB

  1. #ifndef __VARMAPINDEX_H__
  2. #define __VARMAPINDEX_H__
  3. /*---------------------------------------------------------------------------
  4. File: VarMapIndex.h
  5. Comments: Helper class for CMapStringToVar.
  6. CIndexTree implements a sorted, balanced binary tree. This is used by CMapStringToVar
  7. to provide enumeration in sorted order by key.
  8. (c) Copyright 1995-1998, Mission Critical Software, Inc., All Rights Reserved
  9. Proprietary and confidential to Mission Critical Software, Inc.
  10. REVISION LOG ENTRY
  11. Revision By: Christy Boles
  12. Revised on 11/19/98 18:01:34
  13. ---------------------------------------------------------------------------
  14. */
  15. #include "VarData.h"
  16. class CHashItem;
  17. class CIndexTree;
  18. class CIndexItem
  19. {
  20. friend class CIndexTree;
  21. CHashItem * pData;
  22. CIndexItem * pLeft;
  23. CIndexItem * pRight;
  24. CIndexItem * pParent;
  25. BOOL red;
  26. public:
  27. CVarData* GetValue();
  28. CString GetKey();
  29. protected:
  30. CIndexItem(CHashItem*pd) { pData = pd; pLeft = NULL; pRight = NULL; pParent = NULL; red = FALSE;}
  31. CHashItem* Data() { return pData; }
  32. CIndexItem* Left() { return pLeft; }
  33. CIndexItem* Right() { return pRight; }
  34. CIndexItem* Parent() { return pParent; }
  35. BOOL IsRed() { return red; }
  36. void Left(CIndexItem* l) { pLeft = l; }
  37. void Right(CIndexItem* r) { pRight = r; }
  38. void Parent(CIndexItem* par) { pParent = par; }
  39. void Data(CHashItem* p) { pData=p; }
  40. void Red(BOOL rd = TRUE) { red = rd; }
  41. void Black(BOOL blk = TRUE) { red = !blk; }
  42. void McLogInternalDiagnostics(CString keyName,int depth);
  43. };
  44. typedef int CompareFunction(CIndexItem* i1,CIndexItem* i2);
  45. typedef int CompareKeyFunction(CString s, CIndexItem* i);
  46. extern CompareFunction CompareItems;
  47. extern CompareKeyFunction CompareStringToItem;
  48. extern CompareFunction CompareItemsNoCase;
  49. extern CompareKeyFunction CompareStringToItemNoCase;
  50. class CIndexTree
  51. {
  52. friend class CMapStringToVar;
  53. CIndexItem* m_root;
  54. CompareFunction* m_Compare;
  55. CompareKeyFunction* m_CompareKey;
  56. BOOL m_CaseSensitive;
  57. protected:
  58. CIndexTree(CompareFunction * comp = &CompareItems, CompareKeyFunction * kc = &CompareStringToItem)
  59. {
  60. m_root = NULL;
  61. m_Compare = comp;
  62. m_CompareKey = kc;
  63. m_CaseSensitive = TRUE;
  64. }
  65. ~CIndexTree() { RemoveAll(); }
  66. public:
  67. CIndexItem* GetFirstItem() const { CIndexItem * curr = m_root; while ( curr && curr->Left() ) curr = curr->Left(); return curr; }
  68. CIndexItem* GetPrevItem(CIndexItem*curr) const;
  69. CIndexItem* GetNextItem(CIndexItem*curr) const;
  70. CIndexItem* GetFirstAfter(CString value) const;
  71. void McLogInternalDiagnostics(CString keyName);
  72. #ifdef _DEBUG
  73. BOOL AssertValid(int nItems) const;
  74. #endif
  75. protected:
  76. // functions called by CMapStringToVar to add/remove items from the tree
  77. void RemoveAll();
  78. CIndexItem * Insert(CHashItem* data);
  79. void SetCompareFunctions(CompareFunction * f, CompareKeyFunction * kc) { m_Compare = f; m_CompareKey = kc; }
  80. // Helper functions to maintain tree structure
  81. void LeftRotate(CIndexItem* item);
  82. void RightRotate(CIndexItem* item);
  83. void RemoveHelper(CIndexItem* item);
  84. };
  85. #endif //__VARMAPINDEX_H__