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