]>
Commit | Line | Data |
---|---|---|
cf6124d6 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 "offmap.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 | ||
0c9c969a | 17 | __KHASH_TYPE(off, off64_t, void *) |
ac3d33df | 18 | |
0c9c969a | 19 | __KHASH_IMPL(off, static kh_inline, off64_t, void *, 1, kh_int64_hash_func, kh_int64_hash_equal) |
cf6124d6 | 20 | |
0c9c969a UG |
21 | |
22 | int git_offmap_new(git_offmap **out) | |
cf6124d6 | 23 | { |
0c9c969a UG |
24 | *out = kh_init(off); |
25 | GIT_ERROR_CHECK_ALLOC(*out); | |
26 | ||
27 | return 0; | |
cf6124d6 PS |
28 | } |
29 | ||
94af9155 | 30 | void git_offmap_free(git_offmap *map) |
cf6124d6 PS |
31 | { |
32 | kh_destroy(off, map); | |
33 | } | |
34 | ||
35 | void git_offmap_clear(git_offmap *map) | |
36 | { | |
37 | kh_clear(off, map); | |
38 | } | |
39 | ||
0c9c969a | 40 | size_t git_offmap_size(git_offmap *map) |
cf6124d6 PS |
41 | { |
42 | return kh_size(map); | |
43 | } | |
44 | ||
0c9c969a | 45 | void *git_offmap_get(git_offmap *map, const off64_t key) |
cf6124d6 | 46 | { |
0c9c969a UG |
47 | size_t idx = kh_get(off, map, key); |
48 | if (idx == kh_end(map) || !kh_exist(map, idx)) | |
49 | return NULL; | |
50 | return kh_val(map, idx); | |
cf6124d6 PS |
51 | } |
52 | ||
0c9c969a | 53 | int git_offmap_set(git_offmap *map, const off64_t key, void *value) |
ac3d33df | 54 | { |
0c9c969a UG |
55 | size_t idx; |
56 | int rval; | |
ac3d33df | 57 | |
0c9c969a UG |
58 | idx = kh_put(off, map, key, &rval); |
59 | if (rval < 0) | |
60 | return -1; | |
ac3d33df | 61 | |
0c9c969a UG |
62 | if (rval == 0) |
63 | kh_key(map, idx) = key; | |
cf6124d6 | 64 | |
cf6124d6 | 65 | kh_val(map, idx) = value; |
0c9c969a UG |
66 | |
67 | return 0; | |
cf6124d6 PS |
68 | } |
69 | ||
0c9c969a | 70 | int git_offmap_delete(git_offmap *map, const off64_t key) |
cf6124d6 | 71 | { |
0c9c969a UG |
72 | khiter_t idx = kh_get(off, map, key); |
73 | if (idx == kh_end(map)) | |
74 | return GIT_ENOTFOUND; | |
cf6124d6 | 75 | kh_del(off, map, idx); |
0c9c969a | 76 | return 0; |
cf6124d6 PS |
77 | } |
78 | ||
0c9c969a | 79 | int git_offmap_exists(git_offmap *map, const off64_t key) |
cf6124d6 | 80 | { |
0c9c969a | 81 | return kh_get(off, map, key) != kh_end(map); |
cf6124d6 PS |
82 | } |
83 | ||
0c9c969a | 84 | int git_offmap_iterate(void **value, git_offmap *map, size_t *iter, off64_t *key) |
cf6124d6 | 85 | { |
0c9c969a | 86 | size_t i = *iter; |
cf6124d6 | 87 | |
0c9c969a UG |
88 | while (i < map->n_buckets && !kh_exist(map, i)) |
89 | i++; | |
cf6124d6 | 90 | |
0c9c969a UG |
91 | if (i >= map->n_buckets) |
92 | return GIT_ITEROVER; | |
ac3d33df | 93 | |
0c9c969a UG |
94 | if (key) |
95 | *key = kh_key(map, i); | |
96 | if (value) | |
97 | *value = kh_value(map, i); | |
98 | *iter = ++i; | |
ac3d33df | 99 | |
0c9c969a | 100 | return 0; |
ac3d33df | 101 | } |