skip-list.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*
  2. * Copyright 2021
  3. *
  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. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. *
  17. * SPDX-License-Identifier: GPL-3.0+
  18. * License-Filename: LICENSE
  19. *
  20. */
  21. /**
  22. * @file: skip-list.h
  23. */
  24. #ifndef SKIP_LIST_H
  25. #define SKIP_LIST_H 1
  26. #undef __BEGIN_DECLS
  27. #undef __END_DECLS
  28. #ifdef __cplusplus
  29. #define __BEGIN_DECLS extern "C" {
  30. #define __END_DECLS }
  31. #else
  32. #define __BEGIN_DECLS /* empty */
  33. #define __END_DECLS /* empty */
  34. #endif
  35. /* */
  36. #define SKIP_LIST_MAX_LEVEL 32
  37. /* needed for type unintptr_t or use long long int */
  38. #include <stdint.h>
  39. /* how many bytes can a splay key to index on have max. */
  40. /* typedef unsigned long long int skip_list_key; */
  41. typedef uintptr_t skip_list_key;
  42. /* how many bytes can a splay value have max. */
  43. /* typedef unsigned long long int skip_list_value; */
  44. typedef uintptr_t skip_list_value;
  45. /* Forward declaration for a tree. */
  46. typedef struct skip_list_t *skip_list;
  47. /* The nodes in the splay tree. */
  48. struct skip_list_node_n {
  49. /* The key. */
  50. skip_list_key key;
  51. /* The value. */
  52. skip_list_value value;
  53. /* linkage */
  54. struct skip_list_node_n *next[1];
  55. };
  56. /* Forward declaration for a node in the tree. */
  57. typedef struct skip_list_node_n *skip_list_node;
  58. /* The type of a function which compares two skip-list keys. The
  59. function should return values as for qsort. */
  60. typedef int (*skip_list_compare_fn)(skip_list_key, skip_list_key);
  61. /* The type of a function used to deallocate any resources associated
  62. with the key. */
  63. typedef void (*skip_list_delete_key_fn)(skip_list_key);
  64. /* The type of a function used to deallocate any resources associated
  65. with the value. */
  66. typedef void (*skip_list_delete_value_fn)(skip_list_value);
  67. struct skip_list_t {
  68. /* level */
  69. int level;
  70. /* The root of the tree. */
  71. struct skip_list_node_n *head;
  72. /* The comparision function. */
  73. skip_list_compare_fn comp;
  74. /* The deallocate-key function. NULL if no cleanup is necessary. */
  75. skip_list_delete_key_fn delete_key;
  76. /* The deallocate-value function. NULL if no cleanup is necessary. */
  77. skip_list_delete_value_fn delete_value;
  78. };
  79. extern skip_list skip_list_new(skip_list_compare_fn fnc, skip_list_delete_key_fn fndk, skip_list_delete_value_fn fndv);
  80. extern skip_list skip_list_delete(skip_list sp);
  81. extern void skip_list_insert(skip_list sp, skip_list_key k, skip_list_value v);
  82. extern void skip_list_remove(skip_list sp, skip_list_key key);
  83. extern skip_list_node skip_list_lookup(skip_list sp, skip_list_key key);
  84. extern int skip_list_has_data(skip_list sp);
  85. extern void skip_list_free_value(skip_list_value value);
  86. extern void skip_list_free_key(skip_list_key key);
  87. extern int skip_list_compare_ints(skip_list_key k1, skip_list_key k2);
  88. extern int skip_list_compare_pointers(skip_list_key k1, skip_list_key k2);
  89. extern int skip_list_compare_strings(skip_list_key k1, skip_list_key k2);
  90. #endif
  91. /* end. */