set.h 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /* This file is part of nit.
  2. *
  3. * Nit is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU Lesser General Public License as published by
  5. * the Free Software Foundation, either version 3 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * Nit 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 Lesser General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Lesser General Public License
  14. * along with nit. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #ifndef SET_H
  17. #define SET_H
  18. #include <stdint.h>
  19. #include "dispose.h"
  20. #include "list.h"
  21. #define set_foreach(set, bin, entry) \
  22. for (bin = 0; bin < bin_num[(set)->bin_pos]; ++bin) \
  23. for ((entry) = (set)->bins[bin]; (entry); \
  24. (entry) = (entry)->next)
  25. typedef struct Set_key {
  26. void *dat;
  27. uint32_t key_len;
  28. } Set_key;
  29. typedef struct Set_entry {
  30. void *dat;
  31. uint32_t key_len;
  32. uint32_t hash;
  33. struct Set_entry *next;
  34. } Set_entry;
  35. typedef void (*Set_dispose)(Disposer *disposer, uint32_t key_len, void *dat);
  36. typedef struct {
  37. Disposer *disposer;
  38. uint32_t size;
  39. int bin_pos;
  40. Set_entry **bins;
  41. } Set;
  42. const int bin_num[31];
  43. Nit_error
  44. set_init(Set *set, Disposer *disposer, int bin_pos);
  45. void
  46. set_dispose(Set *set, Set_dispose set_dispose);
  47. Nit_error
  48. set_add(Set *set, uint32_t key_len, const void *dat);
  49. Nit_error
  50. set_remove(Set *set, uint32_t key_len, void **dat);
  51. int
  52. set_get(const Set *set, uint32_t key_len, void **dat);
  53. int
  54. set_contains(const Set *set, uint32_t key_len, const void *dat);
  55. int
  56. set_subset(const Set *super, const Set *sub);
  57. int
  58. set_equal(const Set *set1, const Set *set2);
  59. Nit_error
  60. set_fill(Set *src, const Set *des);
  61. Nit_error
  62. set_compl(Set *des, const Set *src, const Set *dif);
  63. Nit_error
  64. set_inter(Set *des, const Set *src1, const Set *src2);
  65. Nit_error
  66. set_list(const Set *set, List *list, int init, int copy);
  67. #endif