source src/idxmap.c
Line | Flow | Count | Block(s) | Source |
---|---|---|---|---|
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 "idxmap.h" | ||
9 | - | |||
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(idx, const git_index_entry *, git_index_entry *) | ||
18 | - | __KHASH_TYPE(idxicase, const git_index_entry *, git_index_entry *) | ||
19 | - | |||
20 | - | /* This is __ac_X31_hash_string but with tolower and it takes the entry's stage into account */ | ||
21 | 123845 | 2 | static kh_inline khint_t idxentry_hash(const git_index_entry *e) | |
22 | - | { | ||
23 | 123845 | 2 | const char *s = e->path; | |
24 | 123845 | 2 | khint_t h = (khint_t)git__tolower(*s); | |
25 | 2884665 | 2-5 | if (h) for (++s ; *s; ++s) h = (h << 5) - h + (khint_t)git__tolower(*s); | |
26 | 123845 | 6 | return h + GIT_INDEX_ENTRY_STAGE(e); | |
27 | - | } | ||
28 | - | |||
29 | - | #define idxentry_equal(a, b) (GIT_INDEX_ENTRY_STAGE(a) == GIT_INDEX_ENTRY_STAGE(b) && strcmp(a->path, b->path) == 0) | ||
30 | - | #define idxentry_icase_equal(a, b) (GIT_INDEX_ENTRY_STAGE(a) == GIT_INDEX_ENTRY_STAGE(b) && strcasecmp(a->path, b->path) == 0) | ||
31 | - | |||
32 | 53021 | 2,2,2,2,2,2,2 | __KHASH_IMPL(idx, static kh_inline, const git_index_entry *, git_index_entry *, 1, idxentry_hash, idxentry_equal) | |
33 | 453 | 2,2,2,2,2,2,2 | __KHASH_IMPL(idxicase, static kh_inline, const git_index_entry *, git_index_entry *, 1, idxentry_hash, idxentry_icase_equal) | |
34 | - | |||
35 | 2869 | 2 | int git_idxmap_new(git_idxmap **out) | |
36 | - | { | ||
37 | 2869 | 2 | *out = kh_init(idx); | |
38 | 2868 | 3,4 | GIT_ERROR_CHECK_ALLOC(*out); | |
39 | - | |||
40 | 2868 | 5 | return 0; | |
41 | - | } | ||
42 | - | |||
43 | ##### | 2 | int git_idxmap_icase_new(git_idxmap_icase **out) | |
44 | - | { | ||
45 | ##### | 2 | *out = kh_init(idxicase); | |
46 | ##### | 3,4 | GIT_ERROR_CHECK_ALLOC(*out); | |
47 | - | |||
48 | ##### | 5 | return 0; | |
49 | - | } | ||
50 | - | |||
51 | 2869 | 2 | void git_idxmap_free(git_idxmap *map) | |
52 | - | { | ||
53 | 2869 | 2 | kh_destroy(idx, map); | |
54 | 2869 | 3 | } | |
55 | - | |||
56 | ##### | 2 | void git_idxmap_icase_free(git_idxmap_icase *map) | |
57 | - | { | ||
58 | ##### | 2 | kh_destroy(idxicase, map); | |
59 | ##### | 3 | } | |
60 | - | |||
61 | 4972 | 2 | void git_idxmap_clear(git_idxmap *map) | |
62 | - | { | ||
63 | 4972 | 2 | kh_clear(idx, map); | |
64 | 4972 | 3 | } | |
65 | - | |||
66 | ##### | 2 | void git_idxmap_icase_clear(git_idxmap_icase *map) | |
67 | - | { | ||
68 | ##### | 2 | kh_clear(idxicase, map); | |
69 | ##### | 3 | } | |
70 | - | |||
71 | 2448 | 2 | int git_idxmap_resize(git_idxmap *map, size_t size) | |
72 | - | { | ||
73 | 2448 | 2,3,5 | if (!git__is_uint32(size) || | |
74 | 2448 | 4 | kh_resize(idx, map, (khiter_t)size) < 0) { | |
75 | ##### | 6 | git_error_set_oom(); | |
76 | ##### | 7 | return -1; | |
77 | - | } | ||
78 | 2448 | 8 | return 0; | |
79 | - | } | ||
80 | - | |||
81 | 1 | 2 | int git_idxmap_icase_resize(git_idxmap_icase *map, size_t size) | |
82 | - | { | ||
83 | 1 | 2,3,5 | if (!git__is_uint32(size) || | |
84 | 1 | 4 | kh_resize(idxicase, map, (khiter_t)size) < 0) { | |
85 | ##### | 6 | git_error_set_oom(); | |
86 | ##### | 7 | return -1; | |
87 | - | } | ||
88 | 1 | 8 | return 0; | |
89 | - | } | ||
90 | - | |||
91 | 6638 | 2 | void *git_idxmap_get(git_idxmap *map, const git_index_entry *key) | |
92 | - | { | ||
93 | 6638 | 2 | size_t idx = kh_get(idx, map, key); | |
94 | 6638 | 3,4 | if (idx == kh_end(map) || !kh_exist(map, idx)) | |
95 | 1190 | 5 | return NULL; | |
96 | 5448 | 6 | return kh_val(map, idx); | |
97 | - | } | ||
98 | - | |||
99 | 43881 | 2 | int git_idxmap_set(git_idxmap *map, const git_index_entry *key, void *value) | |
100 | - | { | ||
101 | - | size_t idx; | ||
102 | - | int rval; | ||
103 | - | |||
104 | 43881 | 2 | idx = kh_put(idx, map, key, &rval); | |
105 | 43891 | 3 | if (rval < 0) | |
106 | 1 | 4 | return -1; | |
107 | - | |||
108 | 43890 | 5 | if (rval == 0) | |
109 | 2 | 6 | kh_key(map, idx) = key; | |
110 | - | |||
111 | 43890 | 7 | kh_val(map, idx) = value; | |
112 | - | |||
113 | 43890 | 7 | return 0; | |
114 | - | } | ||
115 | - | |||
116 | 110 | 2 | int git_idxmap_icase_set(git_idxmap_icase *map, const git_index_entry *key, void *value) | |
117 | - | { | ||
118 | - | size_t idx; | ||
119 | - | int rval; | ||
120 | - | |||
121 | 110 | 2 | idx = kh_put(idxicase, map, key, &rval); | |
122 | 110 | 3 | if (rval < 0) | |
123 | ##### | 4 | return -1; | |
124 | - | |||
125 | 110 | 5 | if (rval == 0) | |
126 | ##### | 6 | kh_key(map, idx) = key; | |
127 | - | |||
128 | 110 | 7 | kh_val(map, idx) = value; | |
129 | - | |||
130 | 110 | 7 | return 0; | |
131 | - | } | ||
132 | - | |||
133 | 4 | 2 | void *git_idxmap_icase_get(git_idxmap_icase *map, const git_index_entry *key) | |
134 | - | { | ||
135 | 4 | 2 | size_t idx = kh_get(idxicase, map, key); | |
136 | 4 | 3,4 | if (idx == kh_end(map) || !kh_exist(map, idx)) | |
137 | ##### | 5 | return NULL; | |
138 | 4 | 6 | return kh_val(map, idx); | |
139 | - | } | ||
140 | - | |||
141 | 46383 | 2 | int git_idxmap_delete(git_idxmap *map, const git_index_entry *key) | |
142 | - | { | ||
143 | 46383 | 2 | khiter_t idx = kh_get(idx, map, key); | |
144 | 46383 | 3 | if (idx == kh_end(map)) | |
145 | 44511 | 4 | return GIT_ENOTFOUND; | |
146 | 1872 | 5 | kh_del(idx, map, idx); | |
147 | 1872 | 6 | return 0; | |
148 | - | } | ||
149 | - | |||
150 | 449 | 2 | int git_idxmap_icase_delete(git_idxmap_icase *map, const git_index_entry *key) | |
151 | - | { | ||
152 | 449 | 2 | khiter_t idx = kh_get(idxicase, map, key); | |
153 | 449 | 3 | if (idx == kh_end(map)) | |
154 | 449 | 4 | return GIT_ENOTFOUND; | |
155 | ##### | 5 | kh_del(idxicase, map, idx); | |
156 | ##### | 6 | return 0; | |
157 | - | } |