| 1 | #ifndef OIDMAP_H |
| 2 | #define OIDMAP_H |
| 3 | |
| 4 | #include "hash.h" |
| 5 | #include "hashmap.h" |
| 6 | |
| 7 | /* |
| 8 | * struct oidmap_entry is a structure representing an entry in the hash table, |
| 9 | * which must be used as first member of user data structures. |
| 10 | * |
| 11 | * Users should set the oid field. oidmap_put() will populate the |
| 12 | * internal_entry field. |
| 13 | */ |
| 14 | struct oidmap_entry { |
| 15 | /* For internal use only */ |
| 16 | struct hashmap_entry internal_entry; |
| 17 | |
| 18 | struct object_id oid; |
| 19 | }; |
| 20 | |
| 21 | struct oidmap { |
| 22 | struct hashmap map; |
| 23 | }; |
| 24 | |
| 25 | #define OIDMAP_INIT { { NULL } } |
| 26 | |
| 27 | /* |
| 28 | * Initializes an oidmap structure. |
| 29 | * |
| 30 | * `map` is the oidmap to initialize. |
| 31 | * |
| 32 | * If the total number of entries is known in advance, the `initial_size` |
| 33 | * parameter may be used to preallocate a sufficiently large table and thus |
| 34 | * prevent expensive resizing. If 0, the table is dynamically resized. |
| 35 | */ |
| 36 | void oidmap_init(struct oidmap *map, size_t initial_size); |
| 37 | |
| 38 | /* |
| 39 | * Function type for functions that free oidmap entries. |
| 40 | */ |
| 41 | typedef void (*oidmap_free_fn)(void *); |
| 42 | |
| 43 | /* |
| 44 | * Clear an oidmap, freeing any allocated memory. The map is empty and |
| 45 | * can be reused without another explicit init. |
| 46 | * |
| 47 | * The `free_fn`, if not NULL, is called for each oidmap entry in the map |
| 48 | * to free any user data associated with the entry. |
| 49 | */ |
| 50 | void oidmap_clear_with_free(struct oidmap *map, |
| 51 | oidmap_free_fn free_fn); |
| 52 | |
| 53 | /* |
| 54 | * Clear an oidmap, freeing any allocated memory. The map is empty and |
| 55 | * can be reused without another explicit init. |
| 56 | * |
| 57 | * If `free_entries` is true, each oidmap_entry in the map is freed as well |
| 58 | * using stdlibs free(). |
| 59 | */ |
| 60 | void oidmap_clear(struct oidmap *map, int free_entries); |
| 61 | |
| 62 | /* |
| 63 | * Returns the oidmap entry for the specified oid, or NULL if not found. |
| 64 | */ |
| 65 | void *oidmap_get(const struct oidmap *map, |
| 66 | const struct object_id *key); |
| 67 | |
| 68 | /* |
| 69 | * Adds or replaces an oidmap entry. |
| 70 | * |
| 71 | * ((struct oidmap_entry *) entry)->internal_entry will be populated by this |
| 72 | * function. |
| 73 | * |
| 74 | * Returns the replaced entry, or NULL if not found (i.e. the entry was added). |
| 75 | */ |
| 76 | void *oidmap_put(struct oidmap *map, void *entry); |
| 77 | |
| 78 | /* |
| 79 | * Removes an oidmap entry matching the specified oid. |
| 80 | * |
| 81 | * Returns the removed entry, or NULL if not found. |
| 82 | */ |
| 83 | void *oidmap_remove(struct oidmap *map, const struct object_id *key); |
| 84 | |
| 85 | static inline unsigned int oidmap_get_size(struct oidmap *map) |
| 86 | { |
| 87 | return hashmap_get_size(&map->map); |
| 88 | } |
| 89 | |
| 90 | struct oidmap_iter { |
| 91 | struct hashmap_iter h_iter; |
| 92 | }; |
| 93 | |
| 94 | static inline void oidmap_iter_init(struct oidmap *map, struct oidmap_iter *iter) |
| 95 | { |
| 96 | hashmap_iter_init(&map->map, &iter->h_iter); |
| 97 | } |
| 98 | |
| 99 | static inline void *oidmap_iter_next(struct oidmap_iter *iter) |
| 100 | { |
| 101 | /* TODO: this API could be reworked to do compile-time type checks */ |
| 102 | return (void *)hashmap_iter_next(&iter->h_iter); |
| 103 | } |
| 104 | |
| 105 | static inline void *oidmap_iter_first(struct oidmap *map, |
| 106 | struct oidmap_iter *iter) |
| 107 | { |
| 108 | oidmap_iter_init(map, iter); |
| 109 | /* TODO: this API could be reworked to do compile-time type checks */ |
| 110 | return (void *)oidmap_iter_next(iter); |
| 111 | } |
| 112 | |
| 113 | #endif |