idxmap.c 3.54 KB
Newer Older
1 2 3 4 5 6 7 8 9
/*
 * Copyright (C) the libgit2 contributors. All rights reserved.
 *
 * This file is part of libgit2, distributed under the GNU GPL v2 with
 * a Linking Exception. For full terms see the included COPYING file.
 */

#include "idxmap.h"

10 11 12 13 14 15 16 17 18 19
#define kmalloc git__malloc
#define kcalloc git__calloc
#define krealloc git__realloc
#define kreallocarray git__reallocarray
#define kfree git__free
#include "khash.h"

__KHASH_TYPE(idx, const git_index_entry *, git_index_entry *)
__KHASH_TYPE(idxicase, const git_index_entry *, git_index_entry *)

20 21 22 23 24 25
/* This is __ac_X31_hash_string but with tolower and it takes the entry's stage into account */
static kh_inline khint_t idxentry_hash(const git_index_entry *e)
{
	const char *s = e->path;
	khint_t h = (khint_t)git__tolower(*s);
	if (h) for (++s ; *s; ++s) h = (h << 5) - h + (khint_t)git__tolower(*s);
26
	return h + GIT_INDEX_ENTRY_STAGE(e);
27 28
}

29 30
#define idxentry_equal(a, b) (GIT_INDEX_ENTRY_STAGE(a) == GIT_INDEX_ENTRY_STAGE(b) && strcmp(a->path, b->path) == 0)
#define idxentry_icase_equal(a, b) (GIT_INDEX_ENTRY_STAGE(a) == GIT_INDEX_ENTRY_STAGE(b) && strcasecmp(a->path, b->path) == 0)
31 32 33

__KHASH_IMPL(idx, static kh_inline, const git_index_entry *, git_index_entry *, 1, idxentry_hash, idxentry_equal)
__KHASH_IMPL(idxicase, static kh_inline, const git_index_entry *, git_index_entry *, 1, idxentry_hash, idxentry_icase_equal)
34

35
int git_idxmap_new(git_idxmap **out)
36
{
37 38
	*out = kh_init(idx);
	GIT_ERROR_CHECK_ALLOC(*out);
39 40 41 42

	return 0;
}

43
int git_idxmap_icase_new(git_idxmap_icase **out)
44
{
45 46
	*out = kh_init(idxicase);
	GIT_ERROR_CHECK_ALLOC(*out);
47 48 49 50

	return 0;
}

51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
void git_idxmap_free(git_idxmap *map)
{
	kh_destroy(idx, map);
}

void git_idxmap_icase_free(git_idxmap_icase *map)
{
	kh_destroy(idxicase, map);
}

void git_idxmap_clear(git_idxmap *map)
{
	kh_clear(idx, map);
}

void git_idxmap_icase_clear(git_idxmap_icase *map)
{
	kh_clear(idxicase, map);
}

71 72
int git_idxmap_resize(git_idxmap *map, size_t size)
{
73 74
	if (!git__is_uint32(size) ||
	    kh_resize(idx, map, (khiter_t)size) < 0) {
75 76 77 78 79 80 81 82
		git_error_set_oom();
		return -1;
	}
	return 0;
}

int git_idxmap_icase_resize(git_idxmap_icase *map, size_t size)
{
83 84
	if (!git__is_uint32(size) ||
	    kh_resize(idxicase, map, (khiter_t)size) < 0) {
85 86 87 88 89 90
		git_error_set_oom();
		return -1;
	}
	return 0;
}

91 92
void *git_idxmap_get(git_idxmap *map, const git_index_entry *key)
{
93 94
	size_t idx = kh_get(idx, map, key);
	if (idx == kh_end(map) || !kh_exist(map, idx))
95 96 97 98
		return NULL;
	return kh_val(map, idx);
}

99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
int git_idxmap_set(git_idxmap *map, const git_index_entry *key, void *value)
{
	size_t idx;
	int rval;

	idx = kh_put(idx, map, key, &rval);
	if (rval < 0)
		return -1;

	if (rval == 0)
		kh_key(map, idx) = key;

	kh_val(map, idx) = value;

	return 0;
}

int git_idxmap_icase_set(git_idxmap_icase *map, const git_index_entry *key, void *value)
{
	size_t idx;
	int rval;

	idx = kh_put(idxicase, map, key, &rval);
	if (rval < 0)
		return -1;

	if (rval == 0)
		kh_key(map, idx) = key;

	kh_val(map, idx) = value;

	return 0;
}

133 134
void *git_idxmap_icase_get(git_idxmap_icase *map, const git_index_entry *key)
{
135 136
	size_t idx = kh_get(idxicase, map, key);
	if (idx == kh_end(map) || !kh_exist(map, idx))
137 138 139 140
		return NULL;
	return kh_val(map, idx);
}

141 142
int git_idxmap_delete(git_idxmap *map, const git_index_entry *key)
{
143 144
	khiter_t idx = kh_get(idx, map, key);
	if (idx == kh_end(map))
145
		return GIT_ENOTFOUND;
146
	kh_del(idx, map, idx);
147 148 149 150 151
	return 0;
}

int git_idxmap_icase_delete(git_idxmap_icase *map, const git_index_entry *key)
{
152 153
	khiter_t idx = kh_get(idxicase, map, key);
	if (idx == kh_end(map))
154
		return GIT_ENOTFOUND;
155
	kh_del(idxicase, map, idx);
156
	return 0;
157
}