THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

Git Source Code Mirror - This is a publish-only repository and all pull requests are ignored. Please follow Documentation/SubmittingPatches procedure for any of your improvements.
git
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
2.3KB

  1. #ifndef OIDMAP_H
  2. #define OIDMAP_H
  3. #include "cache.h"
  4. #include "hashmap.h"
  5. /*
  6. * struct oidmap_entry is a structure representing an entry in the hash table,
  7. * which must be used as first member of user data structures.
  8. *
  9. * Users should set the oid field. oidmap_put() will populate the
  10. * internal_entry field.
  11. */
  12. struct oidmap_entry {
  13. /* For internal use only */
  14. struct hashmap_entry internal_entry;
  15. struct object_id oid;
  16. };
  17. struct oidmap {
  18. struct hashmap map;
  19. };
  20. #define OIDMAP_INIT { { NULL } }
  21. /*
  22. * Initializes an oidmap structure.
  23. *
  24. * `map` is the oidmap to initialize.
  25. *
  26. * If the total number of entries is known in advance, the `initial_size`
  27. * parameter may be used to preallocate a sufficiently large table and thus
  28. * prevent expensive resizing. If 0, the table is dynamically resized.
  29. */
  30. void oidmap_init(struct oidmap *map, size_t initial_size);
  31. /*
  32. * Frees an oidmap structure and allocated memory.
  33. *
  34. * If `free_entries` is true, each oidmap_entry in the map is freed as well
  35. * using stdlibs free().
  36. */
  37. void oidmap_free(struct oidmap *map, int free_entries);
  38. /*
  39. * Returns the oidmap entry for the specified oid, or NULL if not found.
  40. */
  41. void *oidmap_get(const struct oidmap *map,
  42. const struct object_id *key);
  43. /*
  44. * Adds or replaces an oidmap entry.
  45. *
  46. * ((struct oidmap_entry *) entry)->internal_entry will be populated by this
  47. * function.
  48. *
  49. * Returns the replaced entry, or NULL if not found (i.e. the entry was added).
  50. */
  51. void *oidmap_put(struct oidmap *map, void *entry);
  52. /*
  53. * Removes an oidmap entry matching the specified oid.
  54. *
  55. * Returns the removed entry, or NULL if not found.
  56. */
  57. void *oidmap_remove(struct oidmap *map, const struct object_id *key);
  58. struct oidmap_iter {
  59. struct hashmap_iter h_iter;
  60. };
  61. static inline void oidmap_iter_init(struct oidmap *map, struct oidmap_iter *iter)
  62. {
  63. hashmap_iter_init(&map->map, &iter->h_iter);
  64. }
  65. static inline void *oidmap_iter_next(struct oidmap_iter *iter)
  66. {
  67. /* TODO: this API could be reworked to do compile-time type checks */
  68. return (void *)hashmap_iter_next(&iter->h_iter);
  69. }
  70. static inline void *oidmap_iter_first(struct oidmap *map,
  71. struct oidmap_iter *iter)
  72. {
  73. oidmap_iter_init(map, iter);
  74. /* TODO: this API could be reworked to do compile-time type checks */
  75. return (void *)oidmap_iter_next(iter);
  76. }
  77. #endif