Leaked source code of windows server 2003
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.

108 lines
2.9 KiB

  1. /*
  2. redblack.h
  3. Prototypes and node structure definition for red-black binary trees.
  4. See redblack.c for details and implementation.
  5. Author: Tom McGuire (tommcg) 1/98
  6. Copyright (C) Microsoft, 1998.
  7. 2/98, modified this version of redblack.h for debug symbol lookups.
  8. 8/98, modified this version of redblack.h for generic name table.
  9. */
  10. #ifndef _REDBLACK_H_
  11. #define _REDBLACK_H_
  12. #pragma warning( disable: 4200 ) // zero-sized array in struct/union
  13. typedef struct _NAME_NODE NAME_NODE, *PNAME_NODE;
  14. typedef struct _NAME_TREE NAME_TREE, *PNAME_TREE;
  15. struct _NAME_NODE {
  16. PNAME_NODE Left;
  17. PNAME_NODE Right;
  18. ULONG Hash;
  19. union {
  20. ULONG NameLengthAndColorBit;
  21. struct {
  22. ULONG NameLength:31;
  23. ULONG Red:1;
  24. };
  25. };
  26. PVOID Context;
  27. CHAR Name[ 0 ];
  28. };
  29. struct _NAME_TREE {
  30. PNAME_NODE Root;
  31. HANDLE SubAllocator;
  32. };
  33. #define RBNIL ((PNAME_NODE)&NameRbEmptyNode)
  34. extern const NAME_NODE NameRbEmptyNode;
  35. //
  36. // Although "Red" can be stored in its own 1-byte or 4-byte field, keeping the
  37. // nodes smaller by encoding "Red" as a one-bit field with another value
  38. // provides better performance (more nodes tend to stay in the cache). To
  39. // provide flexibility in storage of the RED property, all references to RED
  40. // and BLACK are made through the following macros which can be changed as
  41. // necessary:
  42. //
  43. #define IS_RED( Node ) ( (Node)->Red )
  44. #define IS_BLACK( Node ) ( ! (Node)->Red )
  45. #define MARK_RED( Node ) ( (Node)->Red = 1 )
  46. #define MARK_BLACK( Node ) ( (Node)->Red = 0 )
  47. //
  48. // The maximum tree depth is 2*Lg(N). Since we could never have more than
  49. // 2^X nodes with X-bit pointers, we can safely say the absolute maximum
  50. // depth will be 2*Lg(2^X) which is 2*X. The size of a pointer in bits is
  51. // its size in bytes times 8 bits, so 2*(sizeof(p)*8) is our maximum depth.
  52. // So for 32-bit pointers, our maximum depth is 64.
  53. //
  54. // If you know the maximum possible number of nodes in advance (like the size
  55. // of the address space divided by the size of a node), you can tweak this
  56. // value a bit smaller to 2*Lg(N). Note that it's important for this max
  57. // depth be evalutated to a constant value at compile time.
  58. //
  59. // For this implementation, we'll assume the maximum number of nodes is
  60. // 1 million, so the max depth is 40 (2*Lg(2^20)). Note that no runtime
  61. // checks are made to ensure we don't exceed this number.
  62. //
  63. #define MAX_DEPTH 40
  64. //
  65. // The following prototypes are the red-black tree interface.
  66. //
  67. VOID
  68. NameRbInitTree(
  69. IN OUT PNAME_TREE Tree,
  70. IN HANDLE SubAllocator
  71. );
  72. PNAME_NODE
  73. NameRbInsert(
  74. IN OUT PNAME_TREE Tree,
  75. IN LPCSTR Name
  76. );
  77. PNAME_NODE
  78. NameRbFind(
  79. IN PNAME_TREE Tree,
  80. IN LPCSTR Name
  81. );
  82. #endif // _REDBLACK_H_