]>
Commit | Line | Data |
---|---|---|
659f5d07 PS |
1 | /* |
2 | * Copyright (C) the libgit2 contributors. All rights reserved. | |
3 | * | |
4 | * This file is part of libgit2, distributed under the GNU GPL v2 with | |
5 | * a Linking Exception. For full terms see the included COPYING file. | |
6 | */ | |
7 | ||
8 | #include "oidmap.h" | |
9 | ||
ac3d33df JK |
10 | #define kmalloc git__malloc |
11 | #define kcalloc git__calloc | |
12 | #define krealloc git__realloc | |
13 | #define kreallocarray git__reallocarray | |
14 | #define kfree git__free | |
15 | #include "khash.h" | |
16 | ||
17 | __KHASH_TYPE(oid, const git_oid *, void *) | |
18 | ||
0d716905 PS |
19 | GIT_INLINE(khint_t) git_oidmap_hash(const git_oid *oid) |
20 | { | |
21 | khint_t h; | |
22 | memcpy(&h, oid, sizeof(khint_t)); | |
23 | return h; | |
24 | } | |
25 | ||
659f5d07 PS |
26 | __KHASH_IMPL(oid, static kh_inline, const git_oid *, void *, 1, git_oidmap_hash, git_oid_equal) |
27 | ||
0c9c969a | 28 | int git_oidmap_new(git_oidmap **out) |
659f5d07 | 29 | { |
0c9c969a UG |
30 | *out = kh_init(oid); |
31 | GIT_ERROR_CHECK_ALLOC(*out); | |
32 | ||
33 | return 0; | |
659f5d07 PS |
34 | } |
35 | ||
94af9155 | 36 | void git_oidmap_free(git_oidmap *map) |
659f5d07 PS |
37 | { |
38 | kh_destroy(oid, map); | |
39 | } | |
40 | ||
41 | void git_oidmap_clear(git_oidmap *map) | |
42 | { | |
43 | kh_clear(oid, map); | |
44 | } | |
45 | ||
46 | size_t git_oidmap_size(git_oidmap *map) | |
47 | { | |
48 | return kh_size(map); | |
49 | } | |
50 | ||
0c9c969a | 51 | void *git_oidmap_get(git_oidmap *map, const git_oid *key) |
659f5d07 | 52 | { |
0c9c969a UG |
53 | size_t idx = kh_get(oid, map, key); |
54 | if (idx == kh_end(map) || !kh_exist(map, idx)) | |
55 | return NULL; | |
56 | return kh_val(map, idx); | |
659f5d07 PS |
57 | } |
58 | ||
0c9c969a | 59 | int git_oidmap_set(git_oidmap *map, const git_oid *key, void *value) |
659f5d07 | 60 | { |
0c9c969a UG |
61 | size_t idx; |
62 | int rval; | |
659f5d07 | 63 | |
0c9c969a UG |
64 | idx = kh_put(oid, map, key, &rval); |
65 | if (rval < 0) | |
66 | return -1; | |
659f5d07 | 67 | |
0c9c969a UG |
68 | if (rval == 0) |
69 | kh_key(map, idx) = key; | |
659f5d07 | 70 | |
659f5d07 | 71 | kh_val(map, idx) = value; |
0c9c969a UG |
72 | |
73 | return 0; | |
659f5d07 PS |
74 | } |
75 | ||
0c9c969a | 76 | int git_oidmap_delete(git_oidmap *map, const git_oid *key) |
659f5d07 | 77 | { |
0c9c969a UG |
78 | khiter_t idx = kh_get(oid, map, key); |
79 | if (idx == kh_end(map)) | |
80 | return GIT_ENOTFOUND; | |
659f5d07 | 81 | kh_del(oid, map, idx); |
0c9c969a | 82 | return 0; |
659f5d07 PS |
83 | } |
84 | ||
0c9c969a | 85 | int git_oidmap_exists(git_oidmap *map, const git_oid *key) |
659f5d07 | 86 | { |
0c9c969a | 87 | return kh_get(oid, map, key) != kh_end(map); |
659f5d07 PS |
88 | } |
89 | ||
0c9c969a | 90 | int git_oidmap_iterate(void **value, git_oidmap *map, size_t *iter, const git_oid **key) |
659f5d07 | 91 | { |
0c9c969a | 92 | size_t i = *iter; |
659f5d07 | 93 | |
0c9c969a UG |
94 | while (i < map->n_buckets && !kh_exist(map, i)) |
95 | i++; | |
659f5d07 | 96 | |
0c9c969a UG |
97 | if (i >= map->n_buckets) |
98 | return GIT_ITEROVER; | |
ac3d33df | 99 | |
0c9c969a UG |
100 | if (key) |
101 | *key = kh_key(map, i); | |
102 | if (value) | |
103 | *value = kh_value(map, i); | |
104 | *iter = ++i; | |
ac3d33df | 105 | |
0c9c969a | 106 | return 0; |
ac3d33df | 107 | } |