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.

163 lines
3.6 KiB

  1. /*++
  2. Copyright (c) 1999 Microsoft Corporation
  3. Module Name:
  4. btree.h
  5. Abstract:
  6. Prototypes and node structure definition for red-black binary trees.
  7. See btree.c for details and implementation.
  8. Author:
  9. Tom McGuire (tommcg) 1-Jan-1998
  10. Wesley Witt (wesw) 18-Dec-1998
  11. Revision History:
  12. --*/
  13. #ifndef _BTREE_H_
  14. #define _BTREE_H_
  15. #pragma warning( disable: 4200 ) // zero-sized array in struct/union
  16. typedef struct _NAME_NODE NAME_NODE, *PNAME_NODE;
  17. typedef struct _NAME_TREE NAME_TREE, *PNAME_TREE;
  18. struct _NAME_NODE {
  19. PNAME_NODE Left;
  20. PNAME_NODE Right;
  21. ULONG Hash;
  22. union {
  23. ULONG NameLengthAndColorBit;
  24. struct {
  25. ULONG NameLength:31;
  26. ULONG Red:1;
  27. };
  28. };
  29. PVOID Context;
  30. CHAR Name[ 0 ];
  31. };
  32. struct _NAME_TREE {
  33. PNAME_NODE Root;
  34. };
  35. #define RBNIL ((PNAME_NODE)&NameRbEmptyNode)
  36. extern const NAME_NODE NameRbEmptyNode;
  37. typedef struct _DWORD_NODE DWORD_NODE, *PDWORD_NODE;
  38. typedef struct _DWORD_TREE DWORD_TREE, *PDWORD_TREE;
  39. typedef LPCVOID DWORD_CONTEXT;
  40. struct _DWORD_NODE {
  41. PDWORD_NODE Left;
  42. PDWORD_NODE Right;
  43. struct {
  44. ULONG Key:31;
  45. ULONG Red:1;
  46. };
  47. INT_PTR Context[0]; // everything that goes into Context is guaranteed to be aligned on a machine-word boundary
  48. };
  49. struct _DWORD_TREE {
  50. PDWORD_NODE Root;
  51. };
  52. #define NODE_NIL ((PDWORD_NODE) &EmptyNode)
  53. extern const DWORD_NODE EmptyNode;
  54. //
  55. // Although "Red" can be stored in its own 1-byte or 4-byte field, keeping the
  56. // nodes smaller by encoding "Red" as a one-bit field with another value
  57. // provides better performance (more nodes tend to stay in the cache). To
  58. // provide flexibility in storage of the RED property, all references to RED
  59. // and BLACK are made through the following macros which can be changed as
  60. // necessary:
  61. //
  62. #define IS_RED( Node ) ( (Node)->Red )
  63. #define IS_BLACK( Node ) ( ! (Node)->Red )
  64. #define MARK_RED( Node ) ( (Node)->Red = 1 )
  65. #define MARK_BLACK( Node ) ( (Node)->Red = 0 )
  66. //
  67. // The maximum tree depth is 2*Lg(N). Since we could never have more than
  68. // 2^X nodes with X-bit pointers, we can safely say the absolute maximum
  69. // depth will be 2*Lg(2^X) which is 2*X. The size of a pointer in bits is
  70. // its size in bytes times 8 bits, so 2*(sizeof(p)*8) is our maximum depth.
  71. // So for 32-bit pointers, our maximum depth is 64.
  72. //
  73. // If you know the maximum possible number of nodes in advance (like the size
  74. // of the address space divided by the size of a node), you can tweak this
  75. // value a bit smaller to 2*Lg(N). Note that it's important for this max
  76. // depth be evalutated to a constant value at compile time.
  77. //
  78. // For this implementation, we'll assume the maximum number of nodes is
  79. // 1 million, so the max depth is 40 (2*Lg(2^20)). Note that no runtime
  80. // checks are made to ensure we don't exceed this number.
  81. //
  82. #define MAX_DEPTH 40
  83. //
  84. // The following prototypes are the red-black tree interface.
  85. //
  86. VOID
  87. BtreeInit(
  88. IN OUT PNAME_TREE Tree
  89. );
  90. PNAME_NODE
  91. BtreeInsert(
  92. IN OUT PNAME_TREE Tree,
  93. IN LPCWSTR Name,
  94. IN DWORD NameLength // in bytes, NOT characters
  95. );
  96. PNAME_NODE
  97. BtreeFind(
  98. IN PNAME_TREE Tree,
  99. IN LPCWSTR Name,
  100. IN DWORD NameLength // in bytes, NOT characters
  101. );
  102. VOID
  103. TreeInit(
  104. OUT PDWORD_TREE Tree
  105. );
  106. DWORD_CONTEXT
  107. TreeFind(
  108. IN PDWORD_TREE Tree,
  109. IN ULONG Key
  110. );
  111. DWORD_CONTEXT
  112. TreeInsert(
  113. IN OUT PDWORD_TREE Tree,
  114. IN ULONG Key,
  115. IN DWORD_CONTEXT Context,
  116. IN ULONG ContextSize
  117. );
  118. VOID
  119. TreeDestroy(
  120. IN OUT PDWORD_TREE Tree
  121. );
  122. #endif // _BTREE_H_