]>
Commit | Line | Data |
---|---|---|
15db4e7f | 1 | #include "git-compat-util.h" |
d1cbe1e6 | 2 | #include "hash.h" |
9e6fabde JT |
3 | #include "oidmap.h" |
4 | ||
5cf88fd8 | 5 | static int oidmap_neq(const void *hashmap_cmp_fn_data UNUSED, |
939af16e EW |
6 | const struct hashmap_entry *e1, |
7 | const struct hashmap_entry *e2, | |
cc00e5ce | 8 | const void *keydata) |
9e6fabde | 9 | { |
939af16e EW |
10 | const struct oidmap_entry *a, *b; |
11 | ||
12 | a = container_of(e1, const struct oidmap_entry, internal_entry); | |
13 | b = container_of(e2, const struct oidmap_entry, internal_entry); | |
14 | ||
9e6fabde | 15 | if (keydata) |
939af16e EW |
16 | return !oideq(&a->oid, (const struct object_id *) keydata); |
17 | return !oideq(&a->oid, &b->oid); | |
9e6fabde JT |
18 | } |
19 | ||
9e6fabde JT |
20 | void oidmap_init(struct oidmap *map, size_t initial_size) |
21 | { | |
cc00e5ce | 22 | hashmap_init(&map->map, oidmap_neq, NULL, initial_size); |
9e6fabde JT |
23 | } |
24 | ||
25 | void oidmap_free(struct oidmap *map, int free_entries) | |
26 | { | |
27 | if (!map) | |
28 | return; | |
c8e424c9 EW |
29 | |
30 | /* TODO: make oidmap itself not depend on struct layouts */ | |
6da1a258 | 31 | hashmap_clear_(&map->map, free_entries ? 0 : -1); |
9e6fabde JT |
32 | } |
33 | ||
34 | void *oidmap_get(const struct oidmap *map, const struct object_id *key) | |
35 | { | |
e2a5a028 BW |
36 | if (!map->map.cmpfn) |
37 | return NULL; | |
38 | ||
c62bff2c | 39 | return hashmap_get_from_hash(&map->map, oidhash(key), key); |
9e6fabde JT |
40 | } |
41 | ||
42 | void *oidmap_remove(struct oidmap *map, const struct object_id *key) | |
43 | { | |
44 | struct hashmap_entry entry; | |
e2a5a028 BW |
45 | |
46 | if (!map->map.cmpfn) | |
47 | oidmap_init(map, 0); | |
48 | ||
c62bff2c | 49 | hashmap_entry_init(&entry, oidhash(key)); |
9e6fabde JT |
50 | return hashmap_remove(&map->map, &entry, key); |
51 | } | |
52 | ||
53 | void *oidmap_put(struct oidmap *map, void *entry) | |
54 | { | |
55 | struct oidmap_entry *to_put = entry; | |
e2a5a028 BW |
56 | |
57 | if (!map->map.cmpfn) | |
58 | oidmap_init(map, 0); | |
59 | ||
c62bff2c | 60 | hashmap_entry_init(&to_put->internal_entry, oidhash(&to_put->oid)); |
26b455f2 | 61 | return hashmap_put(&map->map, &to_put->internal_entry); |
9e6fabde | 62 | } |