gl_anyrbtree_list1.h 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /* Sequential list data type implemented by a binary tree.
  2. Copyright (C) 2006, 2009-2020 Free Software Foundation, Inc.
  3. Written by Bruno Haible <bruno@clisp.org>, 2006.
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <https://www.gnu.org/licenses/>. */
  14. /* Common code of gl_rbtree_list.c and gl_rbtreehash_list.c. */
  15. /* A red-black tree is a binary tree where every node is colored black or
  16. red such that
  17. 1. The root is black.
  18. 2. No red node has a red parent.
  19. Or equivalently: No red node has a red child.
  20. 3. All paths from the root down to any NULL endpoint contain the same
  21. number of black nodes.
  22. Let's call this the "black-height" bh of the tree. It follows that every
  23. such path contains exactly bh black and between 0 and bh red nodes. (The
  24. extreme cases are a path containing only black nodes, and a path colored
  25. alternately black-red-black-red-...-black-red.) The height of the tree
  26. therefore is >= bh, <= 2*bh.
  27. */
  28. /* -------------------------- gl_list_t Data Type -------------------------- */
  29. /* Color of a node. */
  30. typedef enum color { BLACK, RED } color_t;
  31. /* Concrete list node implementation, valid for this file only. */
  32. struct gl_list_node_impl
  33. {
  34. #if WITH_HASHTABLE
  35. struct gl_hash_entry h; /* hash table entry fields; must be first */
  36. #endif
  37. struct gl_list_node_impl *left; /* left branch, or NULL */
  38. struct gl_list_node_impl *right; /* right branch, or NULL */
  39. /* Parent pointer, or NULL. The parent pointer is not needed for most
  40. operations. It is needed so that a gl_list_node_t can be returned
  41. without memory allocation, on which the functions gl_list_remove_node,
  42. gl_list_add_before, gl_list_add_after can be implemented. */
  43. struct gl_list_node_impl *parent;
  44. color_t color; /* node's color */
  45. size_t branch_size; /* number of nodes in this branch,
  46. = branchsize(left)+branchsize(right)+1 */
  47. const void *value;
  48. };
  49. /* Concrete gl_list_impl type, valid for this file only. */
  50. struct gl_list_impl
  51. {
  52. struct gl_list_impl_base base;
  53. #if WITH_HASHTABLE
  54. /* A hash table: managed as an array of collision lists. */
  55. struct gl_hash_entry **table;
  56. size_t table_size;
  57. #endif
  58. struct gl_list_node_impl *root; /* root node or NULL */
  59. };
  60. /* A red-black tree of height h has a black-height bh >= ceil(h/2) and
  61. therefore at least 2^ceil(h/2) - 1 elements. So, h <= 116 (because a tree
  62. of height h >= 117 would have at least 2^59 - 1 elements, and because even
  63. on 64-bit machines,
  64. sizeof (gl_list_node_impl) * (2^59 - 1) > 2^64
  65. this would exceed the address space of the machine. */
  66. #define MAXHEIGHT 116