The eleventh batch for 2.15
[git/git.git] / oidset.c
1 #include "cache.h"
2 #include "oidset.h"
3
4 struct oidset_entry {
5 struct hashmap_entry hash;
6 struct object_id oid;
7 };
8
9 static int oidset_hashcmp(const void *unused_cmp_data,
10 const void *va, const void *vb,
11 const void *vkey)
12 {
13 const struct oidset_entry *a = va, *b = vb;
14 const struct object_id *key = vkey;
15 return oidcmp(&a->oid, key ? key : &b->oid);
16 }
17
18 int oidset_contains(const struct oidset *set, const struct object_id *oid)
19 {
20 struct hashmap_entry key;
21
22 if (!set->map.cmpfn)
23 return 0;
24
25 hashmap_entry_init(&key, sha1hash(oid->hash));
26 return !!hashmap_get(&set->map, &key, oid);
27 }
28
29 int oidset_insert(struct oidset *set, const struct object_id *oid)
30 {
31 struct oidset_entry *entry;
32
33 if (!set->map.cmpfn)
34 hashmap_init(&set->map, oidset_hashcmp, NULL, 0);
35
36 if (oidset_contains(set, oid))
37 return 1;
38
39 entry = xmalloc(sizeof(*entry));
40 hashmap_entry_init(&entry->hash, sha1hash(oid->hash));
41 oidcpy(&entry->oid, oid);
42
43 hashmap_add(&set->map, entry);
44 return 0;
45 }
46
47 void oidset_clear(struct oidset *set)
48 {
49 hashmap_free(&set->map, 1);
50 }