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