The sixteenth batch
[git/gitster.git] / oidmap.h
blob67fb32290f1c494a36be653a908f37b58f5e6202
1 #ifndef OIDMAP_H
2 #define OIDMAP_H
4 #include "hash.h"
5 #include "hashmap.h"
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.
11 * Users should set the oid field. oidmap_put() will populate the
12 * internal_entry field.
14 struct oidmap_entry {
15 /* For internal use only */
16 struct hashmap_entry internal_entry;
18 struct object_id oid;
21 struct oidmap {
22 struct hashmap map;
25 #define OIDMAP_INIT { { NULL } }
28 * Initializes an oidmap structure.
30 * `map` is the oidmap to initialize.
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.
36 void oidmap_init(struct oidmap *map, size_t initial_size);
39 * Clear an oidmap, freeing any allocated memory. The map is empty and
40 * can be reused without another explicit init.
42 * If `free_entries` is true, each oidmap_entry in the map is freed as well
43 * using stdlibs free().
45 void oidmap_clear(struct oidmap *map, int free_entries);
48 * Returns the oidmap entry for the specified oid, or NULL if not found.
50 void *oidmap_get(const struct oidmap *map,
51 const struct object_id *key);
54 * Adds or replaces an oidmap entry.
56 * ((struct oidmap_entry *) entry)->internal_entry will be populated by this
57 * function.
59 * Returns the replaced entry, or NULL if not found (i.e. the entry was added).
61 void *oidmap_put(struct oidmap *map, void *entry);
64 * Removes an oidmap entry matching the specified oid.
66 * Returns the removed entry, or NULL if not found.
68 void *oidmap_remove(struct oidmap *map, const struct object_id *key);
70 static inline unsigned int oidmap_get_size(struct oidmap *map)
72 return hashmap_get_size(&map->map);
75 struct oidmap_iter {
76 struct hashmap_iter h_iter;
79 static inline void oidmap_iter_init(struct oidmap *map, struct oidmap_iter *iter)
81 hashmap_iter_init(&map->map, &iter->h_iter);
84 static inline void *oidmap_iter_next(struct oidmap_iter *iter)
86 /* TODO: this API could be reworked to do compile-time type checks */
87 return (void *)hashmap_iter_next(&iter->h_iter);
90 static inline void *oidmap_iter_first(struct oidmap *map,
91 struct oidmap_iter *iter)
93 oidmap_iter_init(map, iter);
94 /* TODO: this API could be reworked to do compile-time type checks */
95 return (void *)oidmap_iter_next(iter);
98 #endif