hashmap.h 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * hashmap.h
  3. *
  4. * Created on: 2019�~4��27��
  5. * Author: foluswen
  6. */
  7. #ifndef HASHMAP_H_
  8. #define HASHMAP_H_
  9. #define KEY_COUNT (1024*1024)
  10. #define KEY_MAX_LENGTH (37)
  11. #define VALUE_MAX_LENGTH (65)
  12. #define MAP_MISSING -3 /* No such element */
  13. #define MAP_FULL -2 /* Hashmap is full */
  14. #define MAP_OMEM -1 /* Out of Memory */
  15. #define MAP_OK 0 /* OK */
  16. typedef struct data_struct_s
  17. {
  18. char key_string[KEY_MAX_LENGTH];
  19. char key_value[VALUE_MAX_LENGTH];
  20. } data_struct_t;
  21. /*
  22. * any_t is a pointer. This allows you to put arbitrary structures in
  23. * the hashmap.
  24. */
  25. typedef void *any_t;
  26. /*
  27. * PFany is a pointer to a function that can take two any_t arguments
  28. * and return an integer. Returns status code..
  29. */
  30. typedef int (*PFany)(any_t, any_t);
  31. /*
  32. * map_t is a pointer to an internally maintained data structure.
  33. * Clients of this package do not need to know how hashmaps are
  34. * represented. They see and manipulate only map_t's.
  35. */
  36. typedef any_t map_t;
  37. /*
  38. * Return an empty hashmap. Returns NULL if empty.
  39. */
  40. extern map_t hashmap_new();
  41. /*
  42. * Iteratively call f with argument (item, data) for
  43. * each element data in the hashmap. The function must
  44. * return a map status code. If it returns anything other
  45. * than MAP_OK the traversal is terminated. f must
  46. * not reenter any hashmap functions, or deadlock may arise.
  47. */
  48. extern int hashmap_iterate(map_t in, PFany f, any_t item);
  49. /*
  50. * Add an element to the hashmap. Return MAP_OK or MAP_OMEM.
  51. */
  52. extern int hashmap_put(map_t in, char* key, any_t value);
  53. /*
  54. * Get an element from the hashmap. Return MAP_OK or MAP_MISSING.
  55. */
  56. extern int hashmap_get(map_t in, char* key, any_t *arg);
  57. /*
  58. * Remove an element from the hashmap. Return MAP_OK or MAP_MISSING.
  59. */
  60. extern int hashmap_remove(map_t in, char* key);
  61. /*
  62. * Get any element. Return MAP_OK or MAP_MISSING.
  63. * remove - should the element be removed from the hashmap
  64. */
  65. extern int hashmap_get_one(map_t in, any_t *arg, int remove);
  66. /*
  67. * Free the hashmap
  68. */
  69. extern void hashmap_free(map_t in);
  70. /*
  71. * Get the current size of a hashmap
  72. */
  73. extern int hashmap_length(map_t in);
  74. /*
  75. * hashmap_operation operation
  76. */
  77. extern int hashmap_operation(int type, map_t in, char* key, any_t value, any_t *arg);
  78. #endif /* HASHMAP_H_ */