Elijah Newren | 15db4e7 | 2023-02-24 00:09:23 +0000 | [diff] [blame] | 1 | #include "git-compat-util.h" |
Elijah Newren | d1cbe1e | 2023-04-22 20:17:20 +0000 | [diff] [blame] | 2 | #include "hash.h" |
Jonathan Tan | 9e6fabde | 2017-09-29 15:54:22 -0700 | [diff] [blame] | 3 | #include "oidmap.h" |
| 4 | |
Ævar Arnfjörð Bjarmason | 5cf88fd | 2022-08-25 19:09:48 +0200 | [diff] [blame] | 5 | static int oidmap_neq(const void *hashmap_cmp_fn_data UNUSED, |
Eric Wong | 939af16 | 2019-10-06 23:30:37 +0000 | [diff] [blame] | 6 | const struct hashmap_entry *e1, |
| 7 | const struct hashmap_entry *e2, |
Jeff King | cc00e5c | 2018-08-28 17:22:55 -0400 | [diff] [blame] | 8 | const void *keydata) |
Jonathan Tan | 9e6fabde | 2017-09-29 15:54:22 -0700 | [diff] [blame] | 9 | { |
Eric Wong | 939af16 | 2019-10-06 23:30:37 +0000 | [diff] [blame] | 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 | |
Jonathan Tan | 9e6fabde | 2017-09-29 15:54:22 -0700 | [diff] [blame] | 15 | if (keydata) |
Eric Wong | 939af16 | 2019-10-06 23:30:37 +0000 | [diff] [blame] | 16 | return !oideq(&a->oid, (const struct object_id *) keydata); |
| 17 | return !oideq(&a->oid, &b->oid); |
Jonathan Tan | 9e6fabde | 2017-09-29 15:54:22 -0700 | [diff] [blame] | 18 | } |
| 19 | |
Jonathan Tan | 9e6fabde | 2017-09-29 15:54:22 -0700 | [diff] [blame] | 20 | void oidmap_init(struct oidmap *map, size_t initial_size) |
| 21 | { |
Jeff King | cc00e5c | 2018-08-28 17:22:55 -0400 | [diff] [blame] | 22 | hashmap_init(&map->map, oidmap_neq, NULL, initial_size); |
Jonathan Tan | 9e6fabde | 2017-09-29 15:54:22 -0700 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | void oidmap_free(struct oidmap *map, int free_entries) |
| 26 | { |
| 27 | if (!map) |
| 28 | return; |
Eric Wong | c8e424c | 2019-10-06 23:30:40 +0000 | [diff] [blame] | 29 | |
| 30 | /* TODO: make oidmap itself not depend on struct layouts */ |
Elijah Newren | 6da1a25 | 2020-11-02 18:55:05 +0000 | [diff] [blame] | 31 | hashmap_clear_(&map->map, free_entries ? 0 : -1); |
Jonathan Tan | 9e6fabde | 2017-09-29 15:54:22 -0700 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | void *oidmap_get(const struct oidmap *map, const struct object_id *key) |
| 35 | { |
Brandon Williams | e2a5a02 | 2017-12-22 15:27:29 -0800 | [diff] [blame] | 36 | if (!map->map.cmpfn) |
| 37 | return NULL; |
| 38 | |
Junio C Hamano | c62bff2 | 2019-07-19 11:30:19 -0700 | [diff] [blame] | 39 | return hashmap_get_from_hash(&map->map, oidhash(key), key); |
Jonathan Tan | 9e6fabde | 2017-09-29 15:54:22 -0700 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | void *oidmap_remove(struct oidmap *map, const struct object_id *key) |
| 43 | { |
| 44 | struct hashmap_entry entry; |
Brandon Williams | e2a5a02 | 2017-12-22 15:27:29 -0800 | [diff] [blame] | 45 | |
| 46 | if (!map->map.cmpfn) |
| 47 | oidmap_init(map, 0); |
| 48 | |
Junio C Hamano | c62bff2 | 2019-07-19 11:30:19 -0700 | [diff] [blame] | 49 | hashmap_entry_init(&entry, oidhash(key)); |
Jonathan Tan | 9e6fabde | 2017-09-29 15:54:22 -0700 | [diff] [blame] | 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; |
Brandon Williams | e2a5a02 | 2017-12-22 15:27:29 -0800 | [diff] [blame] | 56 | |
| 57 | if (!map->map.cmpfn) |
| 58 | oidmap_init(map, 0); |
| 59 | |
Junio C Hamano | c62bff2 | 2019-07-19 11:30:19 -0700 | [diff] [blame] | 60 | hashmap_entry_init(&to_put->internal_entry, oidhash(&to_put->oid)); |
Eric Wong | 26b455f | 2019-10-06 23:30:32 +0000 | [diff] [blame] | 61 | return hashmap_put(&map->map, &to_put->internal_entry); |
Jonathan Tan | 9e6fabde | 2017-09-29 15:54:22 -0700 | [diff] [blame] | 62 | } |