t7503: add tests for pre-merge-hook
[git/mjg.git] / oidmap.c
blob6d6e840d037657721a5aa975e2567c8415547d93
1 #include "cache.h"
2 #include "oidmap.h"
4 static int oidmap_neq(const void *hashmap_cmp_fn_data,
5 const void *entry, const void *entry_or_key,
6 const void *keydata)
8 const struct oidmap_entry *entry_ = entry;
9 if (keydata)
10 return !oideq(&entry_->oid, (const struct object_id *) keydata);
11 return !oideq(&entry_->oid,
12 &((const struct oidmap_entry *) entry_or_key)->oid);
15 void oidmap_init(struct oidmap *map, size_t initial_size)
17 hashmap_init(&map->map, oidmap_neq, NULL, initial_size);
20 void oidmap_free(struct oidmap *map, int free_entries)
22 if (!map)
23 return;
24 hashmap_free(&map->map, free_entries);
27 void *oidmap_get(const struct oidmap *map, const struct object_id *key)
29 if (!map->map.cmpfn)
30 return NULL;
32 return hashmap_get_from_hash(&map->map, oidhash(key), key);
35 void *oidmap_remove(struct oidmap *map, const struct object_id *key)
37 struct hashmap_entry entry;
39 if (!map->map.cmpfn)
40 oidmap_init(map, 0);
42 hashmap_entry_init(&entry, oidhash(key));
43 return hashmap_remove(&map->map, &entry, key);
46 void *oidmap_put(struct oidmap *map, void *entry)
48 struct oidmap_entry *to_put = entry;
50 if (!map->map.cmpfn)
51 oidmap_init(map, 0);
53 hashmap_entry_init(&to_put->internal_entry, oidhash(&to_put->oid));
54 return hashmap_put(&map->map, to_put);