ldap_avl.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /* ldap_avl.h - avl tree definitions */
  2. /* $OpenLDAP$ */
  3. /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
  4. *
  5. * Copyright 1998-2022 The OpenLDAP Foundation.
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted only as authorized by the OpenLDAP
  10. * Public License.
  11. *
  12. * A copy of this license is available in file LICENSE in the
  13. * top-level directory of the distribution or, alternatively, at
  14. * <http://www.OpenLDAP.org/license.html>.
  15. */
  16. /* Portions Copyright (c) 1993 Regents of the University of Michigan.
  17. * All rights reserved.
  18. *
  19. * Redistribution and use in source and binary forms are permitted
  20. * provided that this notice is preserved and that due credit is given
  21. * to the University of Michigan at Ann Arbor. The name of the University
  22. * may not be used to endorse or promote products derived from this
  23. * software without specific prior written permission. This software
  24. * is provided ``as is'' without express or implied warranty.
  25. */
  26. #ifndef _AVL
  27. #define _AVL
  28. #include <ldap_cdefs.h>
  29. /*
  30. * this structure represents a generic avl tree node.
  31. */
  32. LDAP_BEGIN_DECL
  33. typedef struct avlnode Avlnode;
  34. struct avlnode {
  35. void* avl_data;
  36. struct avlnode *avl_link[2];
  37. char avl_bits[2];
  38. signed char avl_bf;
  39. };
  40. #define avl_left avl_link[0]
  41. #define avl_right avl_link[1]
  42. #define avl_lbit avl_bits[0]
  43. #define avl_rbit avl_bits[1]
  44. typedef struct tavlnode TAvlnode;
  45. struct tavlnode {
  46. void* avl_data;
  47. struct tavlnode *avl_link[2];
  48. char avl_bits[2];
  49. signed char avl_bf;
  50. };
  51. #ifdef AVL_INTERNAL
  52. /* balance factor values */
  53. #define LH (-1)
  54. #define EH 0
  55. #define RH 1
  56. #define avl_bf2str(bf) ((bf) == -1 ? "LH" : (bf) == 0 ? "EH" : (bf) == 1 ? "RH" : "(unknown)" )
  57. /* thread bits */
  58. #define AVL_CHILD 0
  59. #define AVL_THREAD 1
  60. /* avl routines */
  61. #define ldap_avl_getone(x) ((x) == 0 ? 0 : (x)->avl_data)
  62. #define ldap_avl_onenode(x) ((x) == 0 || ((x)->avl_left == 0 && (x)->avl_right == 0))
  63. #endif /* AVL_INTERNALS */
  64. #define ldap_avl_child(x,dir) ((x)->avl_bits[dir]) == AVL_CHILD ? \
  65. (x)->avl_link[dir] : NULL
  66. #define ldap_avl_lchild(x) ldap_avl_child(x,0)
  67. #define ldap_avl_rchild(x) ldap_avl_child(x,1)
  68. typedef int (*AVL_APPLY) LDAP_P((void *, void*));
  69. typedef int (*AVL_CMP) LDAP_P((const void*, const void*));
  70. typedef int (*AVL_DUP) LDAP_P((void*, void*));
  71. typedef void (*AVL_FREE) LDAP_P((void*));
  72. LDAP_AVL_F( int )
  73. ldap_avl_free LDAP_P(( Avlnode *root, AVL_FREE dfree ));
  74. LDAP_AVL_F( int )
  75. ldap_avl_insert LDAP_P((Avlnode **, void*, AVL_CMP, AVL_DUP));
  76. LDAP_AVL_F( void* )
  77. ldap_avl_delete LDAP_P((Avlnode **, void*, AVL_CMP));
  78. LDAP_AVL_F( void* )
  79. ldap_avl_find LDAP_P((Avlnode *, const void*, AVL_CMP));
  80. LDAP_AVL_F( Avlnode* )
  81. ldap_avl_find2 LDAP_P((Avlnode *, const void*, AVL_CMP));
  82. LDAP_AVL_F( void* )
  83. ldap_avl_find_lin LDAP_P((Avlnode *, const void*, AVL_CMP));
  84. #ifdef AVL_NONREENTRANT
  85. LDAP_AVL_F( void* )
  86. ldap_avl_getfirst LDAP_P((Avlnode *));
  87. LDAP_AVL_F( void* )
  88. ldap_avl_getnext LDAP_P((void));
  89. #endif
  90. LDAP_AVL_F( int )
  91. ldap_avl_dup_error LDAP_P((void*, void*));
  92. LDAP_AVL_F( int )
  93. ldap_avl_dup_ok LDAP_P((void*, void*));
  94. LDAP_AVL_F( int )
  95. ldap_avl_apply LDAP_P((Avlnode *, AVL_APPLY, void*, int, int));
  96. LDAP_AVL_F( int )
  97. ldap_avl_prefixapply LDAP_P((Avlnode *, void*, AVL_CMP, void*, AVL_CMP, void*, int));
  98. LDAP_AVL_F( int )
  99. ldap_tavl_free LDAP_P(( TAvlnode *root, AVL_FREE dfree ));
  100. LDAP_AVL_F( int )
  101. ldap_tavl_insert LDAP_P((TAvlnode **, void*, AVL_CMP, AVL_DUP));
  102. LDAP_AVL_F( void* )
  103. ldap_tavl_delete LDAP_P((TAvlnode **, void*, AVL_CMP));
  104. LDAP_AVL_F( void* )
  105. ldap_tavl_find LDAP_P((TAvlnode *, const void*, AVL_CMP));
  106. LDAP_AVL_F( TAvlnode* )
  107. ldap_tavl_find2 LDAP_P((TAvlnode *, const void*, AVL_CMP));
  108. LDAP_AVL_F( TAvlnode* )
  109. ldap_tavl_find3 LDAP_P((TAvlnode *, const void*, AVL_CMP, int *ret));
  110. #define TAVL_DIR_LEFT 0
  111. #define TAVL_DIR_RIGHT 1
  112. LDAP_AVL_F( TAvlnode* )
  113. ldap_tavl_end LDAP_P((TAvlnode *, int direction));
  114. LDAP_AVL_F( TAvlnode* )
  115. ldap_tavl_next LDAP_P((TAvlnode *, int direction));
  116. /* apply traversal types */
  117. #define AVL_PREORDER 1
  118. #define AVL_INORDER 2
  119. #define AVL_POSTORDER 3
  120. /* what apply returns if it ran out of nodes */
  121. #define AVL_NOMORE (-6)
  122. LDAP_END_DECL
  123. #endif /* _AVL */