Merge branch 'bw/forking-and-threading' into maint
[git/raj.git] / oidset.c
blobac169f05d3ab1bde7391f15ae58743d6ceafbad5
1 #include "cache.h"
2 #include "oidset.h"
4 struct oidset_entry {
5 struct hashmap_entry hash;
6 struct object_id oid;
7 };
9 static int oidset_hashcmp(const void *va, const void *vb,
10 const void *vkey)
12 const struct oidset_entry *a = va, *b = vb;
13 const struct object_id *key = vkey;
14 return oidcmp(&a->oid, key ? key : &b->oid);
17 int oidset_contains(const struct oidset *set, const struct object_id *oid)
19 struct hashmap_entry key;
21 if (!set->map.cmpfn)
22 return 0;
24 hashmap_entry_init(&key, sha1hash(oid->hash));
25 return !!hashmap_get(&set->map, &key, oid);
28 int oidset_insert(struct oidset *set, const struct object_id *oid)
30 struct oidset_entry *entry;
32 if (!set->map.cmpfn)
33 hashmap_init(&set->map, oidset_hashcmp, 0);
35 if (oidset_contains(set, oid))
36 return 1;
38 entry = xmalloc(sizeof(*entry));
39 hashmap_entry_init(&entry->hash, sha1hash(oid->hash));
40 oidcpy(&entry->oid, oid);
42 hashmap_add(&set->map, entry);
43 return 0;
46 void oidset_clear(struct oidset *set)
48 hashmap_free(&set->map, 1);