map.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. #ifndef MAP_H
  2. #define MAP_H
  3. #include <stddef.h>
  4. #include <stdint.h>
  5. #ifndef MAP_MAX_SIZE
  6. #define MAP_MAX_SIZE 5000
  7. #endif /* MAP_MAX_SIZE */
  8. struct map_item {
  9. void *data;
  10. size_t size;
  11. const uint64_t hkey;
  12. struct map_item *hit;
  13. };
  14. struct map {
  15. /* uint64_t *hkeys; */
  16. struct map_item *items;
  17. size_t size;
  18. };
  19. struct map* map_create();
  20. __attribute__((access (read_write, 1), access (read_only, 2),
  21. access (read_only, 3), nonnull))
  22. void map_add(struct map *__restrict__ map, const char *__restrict__ key,
  23. void *__restrict__ data, size_t size);
  24. __attribute__((access (read_write, 1), access (read_only, 3), nonnull))
  25. void map_add_h(struct map *__restrict__ map, uint64_t hkey,
  26. void *__restrict__ data, size_t size);
  27. __attribute__((access (read_only, 1), access (read_only, 2), nonnull))
  28. void* map_get(struct map const *__restrict__ map, const char *__restrict__ key);
  29. __attribute__((access (read_only, 1), nonnull))
  30. void* map_get_h(struct map const *__restrict__ map, uint64_t hkey);
  31. __attribute__((access (read_only, 1), access (read_only, 2), nonnull))
  32. struct map_item* map_get_item(struct map const *__restrict__ map,
  33. const char *__restrict__ key);
  34. __attribute__((access (read_only, 1), nonnull))
  35. struct map_item* map_get_item_h(struct map const *__restrict__ map,
  36. uint64_t hkey);
  37. __attribute__((access (read_write, 1), nonnull))
  38. void map_destroy(struct map *map);
  39. #define MAP_IMP
  40. #if defined(MAP_IMP) || defined(IMPLEMENTATIONS)
  41. #define HASH_IMP
  42. #include "./hash.h"
  43. #undef HASH_IMP
  44. #include <stdio.h>
  45. #include <stdlib.h>
  46. #include <string.h>
  47. struct map* map_create() {
  48. /* NOTE: Should probraly do something more here */
  49. struct map *ret = calloc(1, sizeof(struct map));
  50. if ( ret == NULL ) {
  51. fprintf(stderr, "[ERROR] Failed to calloc: map_create\n");
  52. exit(EXIT_FAILURE);
  53. }
  54. ret->items = calloc(MAP_MAX_SIZE, sizeof(struct map_item));
  55. if ( ret->items == NULL ) {
  56. fprintf(stderr, "[ERROR] Failed to calloc: map_create\n");
  57. exit(EXIT_FAILURE);
  58. }
  59. return ret;
  60. }
  61. void map_add(struct map *__restrict__ map, const char *__restrict__ key,
  62. void *__restrict__ data, size_t size) {
  63. map_add_h(map, hash(key, strlen(key)), data, size);
  64. }
  65. void map_add_h(struct map *__restrict__ map, uint64_t hkey,
  66. void *__restrict__ data, size_t size) {
  67. struct map_item *pitem = &map->items[hkey % MAP_MAX_SIZE];
  68. while ( pitem->data != NULL ) {
  69. if ( pitem->hit == NULL ) {
  70. pitem->hit = calloc(1, sizeof(struct map_item));
  71. if ( pitem->hit == NULL ) {
  72. fprintf(stderr, "[ERROR] Failed to calloc: map_add\n");
  73. exit(EXIT_FAILURE);
  74. }
  75. }
  76. pitem = pitem->hit;
  77. }
  78. pitem->data = calloc(1, size);
  79. if ( pitem->data == NULL ) {
  80. fprintf(stderr, "[ERROR] Failed to calloc: map_add\n");
  81. exit(EXIT_FAILURE);
  82. }
  83. pitem->size = size;
  84. memcpy(pitem->data, data, size);
  85. /* const kkkkkk */
  86. *(uint64_t*) &pitem->hkey = hkey;
  87. }
  88. void* map_get(struct map const *__restrict__ map,
  89. const char *__restrict__ key) {
  90. return map_get_h(map, hash(key, strlen(key)));
  91. }
  92. void* map_get_h(struct map const *__restrict__ map, uint64_t hkey) {
  93. struct map_item *pitem = map_get_item_h(map, hkey);
  94. if ( pitem == NULL ) {
  95. return NULL;
  96. }
  97. return pitem->data;
  98. }
  99. struct map_item* map_get_item(struct map const *__restrict__ map,
  100. const char *__restrict__ key) {
  101. return map_get_item_h(map, hash(key, strlen(key)));
  102. }
  103. struct map_item* map_get_item_h(struct map const *__restrict__ map,
  104. uint64_t hkey) {
  105. struct map_item *pitem = &map->items[hkey % MAP_MAX_SIZE];
  106. while ( pitem->hkey != hkey) {
  107. if ( pitem->hit == NULL ) {
  108. return NULL;
  109. }
  110. pitem = pitem->hit;
  111. }
  112. return pitem;
  113. }
  114. void map_destroy(struct map *map) {
  115. (void) map;
  116. }
  117. #endif /* MAP_IMP || IMPLEMENTATIONS */
  118. #endif /* MAP_H */