oidmap: map with OID as key
[git/git.git] / oidmap.c
CommitLineData
9e6fabde
JT
1#include "cache.h"
2#include "oidmap.h"
3
4static int cmpfn(const void *hashmap_cmp_fn_data,
5 const void *entry, const void *entry_or_key,
6 const void *keydata)
7{
8 const struct oidmap_entry *entry_ = entry;
9 if (keydata)
10 return oidcmp(&entry_->oid, (const struct object_id *) keydata);
11 return oidcmp(&entry_->oid,
12 &((const struct oidmap_entry *) entry_or_key)->oid);
13}
14
15static int hash(const struct object_id *oid)
16{
17 int hash;
18 memcpy(&hash, oid->hash, sizeof(hash));
19 return hash;
20}
21
22void oidmap_init(struct oidmap *map, size_t initial_size)
23{
24 hashmap_init(&map->map, cmpfn, NULL, initial_size);
25}
26
27void oidmap_free(struct oidmap *map, int free_entries)
28{
29 if (!map)
30 return;
31 hashmap_free(&map->map, free_entries);
32}
33
34void *oidmap_get(const struct oidmap *map, const struct object_id *key)
35{
36 return hashmap_get_from_hash(&map->map, hash(key), key);
37}
38
39void *oidmap_remove(struct oidmap *map, const struct object_id *key)
40{
41 struct hashmap_entry entry;
42 hashmap_entry_init(&entry, hash(key));
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;
49 hashmap_entry_init(&to_put->internal_entry, hash(&to_put->oid));
50 return hashmap_put(&map->map, to_put);
51}