]>
git.proxmox.com Git - libgit2.git/blob - src/idxmap.c
2 * Copyright (C) the libgit2 contributors. All rights reserved.
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.
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
17 __KHASH_TYPE(idx
, const git_index_entry
*, git_index_entry
*)
18 __KHASH_TYPE(idxicase
, const git_index_entry
*, git_index_entry
*)
20 /* This is __ac_X31_hash_string but with tolower and it takes the entry's stage into account */
21 static kh_inline khint_t
idxentry_hash(const git_index_entry
*e
)
23 const char *s
= e
->path
;
24 khint_t h
= (khint_t
)git__tolower(*s
);
25 if (h
) for (++s
; *s
; ++s
) h
= (h
<< 5) - h
+ (khint_t
)git__tolower(*s
);
26 return h
+ GIT_INDEX_ENTRY_STAGE(e
);
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)
32 __KHASH_IMPL(idx
, static kh_inline
, const git_index_entry
*, git_index_entry
*, 1, idxentry_hash
, idxentry_equal
)
33 __KHASH_IMPL(idxicase
, static kh_inline
, const git_index_entry
*, git_index_entry
*, 1, idxentry_hash
, idxentry_icase_equal
)
35 int git_idxmap_new(git_idxmap
**out
)
38 GIT_ERROR_CHECK_ALLOC(*out
);
43 int git_idxmap_icase_new(git_idxmap_icase
**out
)
45 *out
= kh_init(idxicase
);
46 GIT_ERROR_CHECK_ALLOC(*out
);
51 void git_idxmap_free(git_idxmap
*map
)
56 void git_idxmap_icase_free(git_idxmap_icase
*map
)
58 kh_destroy(idxicase
, map
);
61 void git_idxmap_clear(git_idxmap
*map
)
66 void git_idxmap_icase_clear(git_idxmap_icase
*map
)
68 kh_clear(idxicase
, map
);
71 int git_idxmap_resize(git_idxmap
*map
, size_t size
)
73 if (!git__is_uint32(size
) ||
74 kh_resize(idx
, map
, (khiter_t
)size
) < 0) {
81 int git_idxmap_icase_resize(git_idxmap_icase
*map
, size_t size
)
83 if (!git__is_uint32(size
) ||
84 kh_resize(idxicase
, map
, (khiter_t
)size
) < 0) {
91 void *git_idxmap_get(git_idxmap
*map
, const git_index_entry
*key
)
93 size_t idx
= kh_get(idx
, map
, key
);
94 if (idx
== kh_end(map
) || !kh_exist(map
, idx
))
96 return kh_val(map
, idx
);
99 int git_idxmap_set(git_idxmap
*map
, const git_index_entry
*key
, void *value
)
104 idx
= kh_put(idx
, map
, key
, &rval
);
109 kh_key(map
, idx
) = key
;
111 kh_val(map
, idx
) = value
;
116 int git_idxmap_icase_set(git_idxmap_icase
*map
, const git_index_entry
*key
, void *value
)
121 idx
= kh_put(idxicase
, map
, key
, &rval
);
126 kh_key(map
, idx
) = key
;
128 kh_val(map
, idx
) = value
;
133 void *git_idxmap_icase_get(git_idxmap_icase
*map
, const git_index_entry
*key
)
135 size_t idx
= kh_get(idxicase
, map
, key
);
136 if (idx
== kh_end(map
) || !kh_exist(map
, idx
))
138 return kh_val(map
, idx
);
141 int git_idxmap_delete(git_idxmap
*map
, const git_index_entry
*key
)
143 khiter_t idx
= kh_get(idx
, map
, key
);
144 if (idx
== kh_end(map
))
145 return GIT_ENOTFOUND
;
146 kh_del(idx
, map
, idx
);
150 int git_idxmap_icase_delete(git_idxmap_icase
*map
, const git_index_entry
*key
)
152 khiter_t idx
= kh_get(idxicase
, map
, key
);
153 if (idx
== kh_end(map
))
154 return GIT_ENOTFOUND
;
155 kh_del(idxicase
, map
, idx
);