hashmap.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * Copyright (C) 2007 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /**
  17. * Hash map.
  18. */
  19. #ifndef __HASHMAP_H
  20. #define __HASHMAP_H
  21. #include <stdbool.h>
  22. #include <stdlib.h>
  23. #ifdef __cplusplus
  24. extern "C" {
  25. #endif
  26. /** A hash map. */
  27. typedef struct Hashmap Hashmap;
  28. /**
  29. * Creates a new hash map. Returns NULL if memory allocation fails.
  30. *
  31. * @param initialCapacity number of expected entries
  32. * @param hash function which hashes keys
  33. * @param equals function which compares keys for equality
  34. */
  35. Hashmap* hashmapCreate(size_t initialCapacity,
  36. int (*hash)(intptr_t key), int (*equals)(intptr_t keyA, intptr_t keyB));
  37. /**
  38. * Frees the hash map. Does not free the keys or values themselves.
  39. */
  40. void hashmapFree(Hashmap* map);
  41. /**
  42. * Hashes the memory pointed to by key with the given size. Useful for
  43. * implementing hash functions.
  44. */
  45. int hashmapHash(intptr_t key, size_t keySize);
  46. /**
  47. * Puts value for the given key in the map. Returns pre-existing value if
  48. * any.
  49. *
  50. * If memory allocation fails, this function returns NULL, the map's size
  51. * does not increase, and errno is set to ENOMEM.
  52. */
  53. void* hashmapPut(Hashmap* map, intptr_t key, void* value);
  54. /**
  55. * Gets a value from the map. Returns NULL if no entry for the given key is
  56. * found or if the value itself is NULL.
  57. */
  58. void* hashmapGet(Hashmap* map, intptr_t key);
  59. /**
  60. * Returns true if the map contains an entry for the given key.
  61. */
  62. int hashmapContainsKey(Hashmap* map, intptr_t key);
  63. /**
  64. * Gets the value for a key. If a value is not found, this function gets a
  65. * value and creates an entry using the given callback.
  66. *
  67. * If memory allocation fails, the callback is not called, this function
  68. * returns NULL, and errno is set to ENOMEM.
  69. */
  70. void* hashmapMemoize(Hashmap* map, intptr_t key,
  71. void* (*initialValue)(intptr_t key, void* context), void* context);
  72. /**
  73. * Removes an entry from the map. Returns the removed value or NULL if no
  74. * entry was present.
  75. */
  76. void* hashmapRemove(Hashmap* map, intptr_t key);
  77. /**
  78. * Gets the number of entries in this map.
  79. */
  80. size_t hashmapSize(Hashmap* map);
  81. /**
  82. * Invokes the given callback on each entry in the map. Stops iterating if
  83. * the callback returns false.
  84. */
  85. void hashmapForEach(Hashmap* map,
  86. bool (*callback)(intptr_t key, void* value, void* context),
  87. void* context);
  88. /**
  89. * Concurrency support.
  90. */
  91. /**
  92. * Locks the hash map so only the current thread can access it.
  93. */
  94. void hashmapLock(Hashmap* map);
  95. /**
  96. * Unlocks the hash map so other threads can access it.
  97. */
  98. void hashmapUnlock(Hashmap* map);
  99. /**
  100. * Key utilities.
  101. */
  102. /**
  103. * Hashes int keys. 'key' is a pointer to int.
  104. */
  105. int hashmapIntHash(intptr_t key);
  106. /**
  107. * Compares two int keys for equality.
  108. */
  109. int hashmapIntEquals(intptr_t keyA, intptr_t keyB);
  110. /**
  111. * For debugging.
  112. */
  113. /**
  114. * Gets current capacity.
  115. */
  116. size_t hashmapCurrentCapacity(Hashmap* map);
  117. /**
  118. * Counts the number of entry collisions.
  119. */
  120. size_t hashmapCountCollisions(Hashmap* map);
  121. #ifdef __cplusplus
  122. }
  123. #endif
  124. #endif /* __HASHMAP_H */