pack-bitmap.h: remove magic number
[git/git.git] / oidmap.c
CommitLineData
9e6fabde
JT
1#include "cache.h"
2#include "oidmap.h"
3
cc00e5ce
JK
4static int oidmap_neq(const void *hashmap_cmp_fn_data,
5 const void *entry, const void *entry_or_key,
6 const void *keydata)
9e6fabde
JT
7{
8 const struct oidmap_entry *entry_ = entry;
9 if (keydata)
cc00e5ce
JK
10 return !oideq(&entry_->oid, (const struct object_id *) keydata);
11 return !oideq(&entry_->oid,
9e6fabde
JT
12 &((const struct oidmap_entry *) entry_or_key)->oid);
13}
14
9e6fabde
JT
15void oidmap_init(struct oidmap *map, size_t initial_size)
16{
cc00e5ce 17 hashmap_init(&map->map, oidmap_neq, NULL, initial_size);
9e6fabde
JT
18}
19
20void oidmap_free(struct oidmap *map, int free_entries)
21{
22 if (!map)
23 return;
24 hashmap_free(&map->map, free_entries);
25}
26
27void *oidmap_get(const struct oidmap *map, const struct object_id *key)
28{
e2a5a028
BW
29 if (!map->map.cmpfn)
30 return NULL;
31
c62bff2c 32 return hashmap_get_from_hash(&map->map, oidhash(key), key);
9e6fabde
JT
33}
34
35void *oidmap_remove(struct oidmap *map, const struct object_id *key)
36{
37 struct hashmap_entry entry;
e2a5a028
BW
38
39 if (!map->map.cmpfn)
40 oidmap_init(map, 0);
41
c62bff2c 42 hashmap_entry_init(&entry, oidhash(key));
9e6fabde
JT
43 return hashmap_remove(&map->map, &entry, key);
44}
45
46void *oidmap_put(struct oidmap *map, void *entry)
47{
48 struct oidmap_entry *to_put = entry;
e2a5a028
BW
49
50 if (!map->map.cmpfn)
51 oidmap_init(map, 0);
52
c62bff2c 53 hashmap_entry_init(&to_put->internal_entry, oidhash(&to_put->oid));
9e6fabde
JT
54 return hashmap_put(&map->map, to_put);
55}