]> git.ipfire.org Git - thirdparty/git.git/blob - oidmap.c
hashmap: use *_entry APIs for iteration
[thirdparty/git.git] / oidmap.c
1 #include "cache.h"
2 #include "oidmap.h"
3
4 static int oidmap_neq(const void *hashmap_cmp_fn_data,
5 const struct hashmap_entry *e1,
6 const struct hashmap_entry *e2,
7 const void *keydata)
8 {
9 const struct oidmap_entry *a, *b;
10
11 a = container_of(e1, const struct oidmap_entry, internal_entry);
12 b = container_of(e2, const struct oidmap_entry, internal_entry);
13
14 if (keydata)
15 return !oideq(&a->oid, (const struct object_id *) keydata);
16 return !oideq(&a->oid, &b->oid);
17 }
18
19 void oidmap_init(struct oidmap *map, size_t initial_size)
20 {
21 hashmap_init(&map->map, oidmap_neq, NULL, initial_size);
22 }
23
24 void oidmap_free(struct oidmap *map, int free_entries)
25 {
26 if (!map)
27 return;
28 hashmap_free(&map->map, free_entries);
29 }
30
31 void *oidmap_get(const struct oidmap *map, const struct object_id *key)
32 {
33 if (!map->map.cmpfn)
34 return NULL;
35
36 return hashmap_get_from_hash(&map->map, oidhash(key), key);
37 }
38
39 void *oidmap_remove(struct oidmap *map, const struct object_id *key)
40 {
41 struct hashmap_entry entry;
42
43 if (!map->map.cmpfn)
44 oidmap_init(map, 0);
45
46 hashmap_entry_init(&entry, oidhash(key));
47 return hashmap_remove(&map->map, &entry, key);
48 }
49
50 void *oidmap_put(struct oidmap *map, void *entry)
51 {
52 struct oidmap_entry *to_put = entry;
53
54 if (!map->map.cmpfn)
55 oidmap_init(map, 0);
56
57 hashmap_entry_init(&to_put->internal_entry, oidhash(&to_put->oid));
58 return hashmap_put(&map->map, &to_put->internal_entry);
59 }