]>
Commit | Line | Data |
---|---|---|
9e6fabde JT |
1 | #include "cache.h" |
2 | #include "oidmap.h" | |
3 | ||
cc00e5ce JK |
4 | static 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 | ||
15 | static int hash(const struct object_id *oid) | |
16 | { | |
17 | int hash; | |
18 | memcpy(&hash, oid->hash, sizeof(hash)); | |
19 | return hash; | |
20 | } | |
21 | ||
22 | void oidmap_init(struct oidmap *map, size_t initial_size) | |
23 | { | |
cc00e5ce | 24 | hashmap_init(&map->map, oidmap_neq, NULL, initial_size); |
9e6fabde JT |
25 | } |
26 | ||
27 | void oidmap_free(struct oidmap *map, int free_entries) | |
28 | { | |
29 | if (!map) | |
30 | return; | |
31 | hashmap_free(&map->map, free_entries); | |
32 | } | |
33 | ||
34 | void *oidmap_get(const struct oidmap *map, const struct object_id *key) | |
35 | { | |
e2a5a028 BW |
36 | if (!map->map.cmpfn) |
37 | return NULL; | |
38 | ||
9e6fabde JT |
39 | return hashmap_get_from_hash(&map->map, hash(key), key); |
40 | } | |
41 | ||
42 | void *oidmap_remove(struct oidmap *map, const struct object_id *key) | |
43 | { | |
44 | struct hashmap_entry entry; | |
e2a5a028 BW |
45 | |
46 | if (!map->map.cmpfn) | |
47 | oidmap_init(map, 0); | |
48 | ||
9e6fabde JT |
49 | hashmap_entry_init(&entry, hash(key)); |
50 | return hashmap_remove(&map->map, &entry, key); | |
51 | } | |
52 | ||
53 | void *oidmap_put(struct oidmap *map, void *entry) | |
54 | { | |
55 | struct oidmap_entry *to_put = entry; | |
e2a5a028 BW |
56 | |
57 | if (!map->map.cmpfn) | |
58 | oidmap_init(map, 0); | |
59 | ||
9e6fabde JT |
60 | hashmap_entry_init(&to_put->internal_entry, hash(&to_put->oid)); |
61 | return hashmap_put(&map->map, to_put); | |
62 | } |