Blame src/idxmap.c

Packit Service 20376f
/*
Packit Service 20376f
 * Copyright (C) the libgit2 contributors. All rights reserved.
Packit Service 20376f
 *
Packit Service 20376f
 * This file is part of libgit2, distributed under the GNU GPL v2 with
Packit Service 20376f
 * a Linking Exception. For full terms see the included COPYING file.
Packit Service 20376f
 */
Packit Service 20376f
Packit Service 20376f
#include "idxmap.h"
Packit Service 20376f
Packit Service 20376f
/* This is __ac_X31_hash_string but with tolower and it takes the entry's stage into account */
Packit Service 20376f
static kh_inline khint_t idxentry_hash(const git_index_entry *e)
Packit Service 20376f
{
Packit Service 20376f
	const char *s = e->path;
Packit Service 20376f
	khint_t h = (khint_t)git__tolower(*s);
Packit Service 20376f
	if (h) for (++s ; *s; ++s) h = (h << 5) - h + (khint_t)git__tolower(*s);
Packit Service 20376f
	return h + GIT_IDXENTRY_STAGE(e);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
#define idxentry_equal(a, b) (GIT_IDXENTRY_STAGE(a) == GIT_IDXENTRY_STAGE(b) && strcmp(a->path, b->path) == 0)
Packit Service 20376f
#define idxentry_icase_equal(a, b) (GIT_IDXENTRY_STAGE(a) == GIT_IDXENTRY_STAGE(b) && strcasecmp(a->path, b->path) == 0)
Packit Service 20376f
Packit Service 20376f
__KHASH_IMPL(idx, static kh_inline, const git_index_entry *, git_index_entry *, 1, idxentry_hash, idxentry_equal)
Packit Service 20376f
__KHASH_IMPL(idxicase, static kh_inline, const git_index_entry *, git_index_entry *, 1, idxentry_hash, idxentry_icase_equal)
Packit Service 20376f
Packit Service 20376f
int git_idxmap_alloc(git_idxmap **map)
Packit Service 20376f
{
Packit Service 20376f
	if ((*map = kh_init(idx)) == NULL) {
Packit Service 20376f
		giterr_set_oom();
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_idxmap_icase_alloc(git_idxmap_icase **map)
Packit Service 20376f
{
Packit Service 20376f
	if ((*map = kh_init(idxicase)) == NULL) {
Packit Service 20376f
		giterr_set_oom();
Packit Service 20376f
		return -1;
Packit Service 20376f
	}
Packit Service 20376f
Packit Service 20376f
	return 0;
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void git_idxmap_insert(git_idxmap *map, const git_index_entry *key, void *value, int *rval)
Packit Service 20376f
{
Packit Service 20376f
	khiter_t idx = kh_put(idx, map, key, rval);
Packit Service 20376f
Packit Service 20376f
	if ((*rval) >= 0) {
Packit Service 20376f
		if ((*rval) == 0)
Packit Service 20376f
			kh_key(map, idx) = key;
Packit Service 20376f
		kh_val(map, idx) = value;
Packit Service 20376f
	}
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void git_idxmap_icase_insert(git_idxmap_icase *map, const git_index_entry *key, void *value, int *rval)
Packit Service 20376f
{
Packit Service 20376f
	khiter_t idx = kh_put(idxicase, map, key, rval);
Packit Service 20376f
Packit Service 20376f
	if ((*rval) >= 0) {
Packit Service 20376f
		if ((*rval) == 0)
Packit Service 20376f
			kh_key(map, idx) = key;
Packit Service 20376f
		kh_val(map, idx) = value;
Packit Service 20376f
	}
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
size_t git_idxmap_lookup_index(git_idxmap *map, const git_index_entry *key)
Packit Service 20376f
{
Packit Service 20376f
	return kh_get(idx, map, key);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
size_t git_idxmap_icase_lookup_index(git_idxmap_icase *map, const git_index_entry *key)
Packit Service 20376f
{
Packit Service 20376f
	return kh_get(idxicase, map, key);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void *git_idxmap_value_at(git_idxmap *map, size_t idx)
Packit Service 20376f
{
Packit Service 20376f
	return kh_val(map, idx);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_idxmap_valid_index(git_idxmap *map, size_t idx)
Packit Service 20376f
{
Packit Service 20376f
	return idx != kh_end(map);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
int git_idxmap_has_data(git_idxmap *map, size_t idx)
Packit Service 20376f
{
Packit Service 20376f
	return kh_exist(map, idx);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void git_idxmap_resize(git_idxmap *map, size_t size)
Packit Service 20376f
{
Packit Service 20376f
	kh_resize(idx, map, size);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void git_idxmap_icase_resize(git_idxmap_icase *map, size_t size)
Packit Service 20376f
{
Packit Service 20376f
	kh_resize(idxicase, map, size);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void git_idxmap_free(git_idxmap *map)
Packit Service 20376f
{
Packit Service 20376f
	kh_destroy(idx, map);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void git_idxmap_clear(git_idxmap *map)
Packit Service 20376f
{
Packit Service 20376f
	kh_clear(idx, map);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void git_idxmap_delete_at(git_idxmap *map, size_t idx)
Packit Service 20376f
{
Packit Service 20376f
	kh_del(idx, map, idx);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void git_idxmap_icase_delete_at(git_idxmap_icase *map, size_t idx)
Packit Service 20376f
{
Packit Service 20376f
	kh_del(idxicase, map, idx);
Packit Service 20376f
}
Packit Service 20376f
Packit Service 20376f
void git_idxmap_delete(git_idxmap *map, const git_index_entry *key)
Packit Service 20376f
{
Packit Service 20376f
	khiter_t idx = git_idxmap_lookup_index(map, key);
Packit Service 20376f
	if (git_idxmap_valid_index(map, idx))
Packit Service 20376f
		git_idxmap_delete_at(map, idx);
Packit Service 20376f
}
Packit Service 20376f
void git_idxmap_icase_delete(git_idxmap_icase *map, const git_index_entry *key)
Packit Service 20376f
{
Packit Service 20376f
	khiter_t idx = git_idxmap_icase_lookup_index(map, key);
Packit Service 20376f
	if (git_idxmap_valid_index((git_idxmap *)map, idx))
Packit Service 20376f
		git_idxmap_icase_delete_at(map, idx);
Packit Service 20376f
}