strmap.c 2.31 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 "strmap.h"

10
__KHASH_IMPL(str, static kh_inline, const char *, void *, 1, kh_str_hash_func, kh_str_hash_equal)
11 12 13 14 15 16 17 18 19 20 21

int git_strmap_alloc(git_strmap **map)
{
	if ((*map = kh_init(str)) == NULL) {
		giterr_set_oom();
		return -1;
	}

	return 0;
}

22
void git_strmap_free(git_strmap *map)
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
{
	kh_destroy(str, map);
}

void git_strmap_clear(git_strmap *map)
{
	kh_clear(str, map);
}

size_t git_strmap_num_entries(git_strmap *map)
{
	return kh_size(map);
}

size_t git_strmap_lookup_index(git_strmap *map, const char *key)
{
	return kh_get(str, map, key);
}

int git_strmap_valid_index(git_strmap *map, size_t idx)
{
	return idx != kh_end(map);
}

int git_strmap_exists(git_strmap *map, const char *key)
{
	return kh_get(str, map, key) != kh_end(map);
}

int git_strmap_has_data(git_strmap *map, size_t idx)
{
	return kh_exist(map, idx);
}

const char *git_strmap_key(git_strmap *map, size_t idx)
{
	return kh_key(map, idx);
}

void git_strmap_set_key_at(git_strmap *map, size_t idx, char *key)
{
	kh_val(map, idx) = key;
}

void *git_strmap_value_at(git_strmap *map, size_t idx)
{
	return kh_val(map, idx);
}

void git_strmap_set_value_at(git_strmap *map, size_t idx, void *value)
{
	kh_val(map, idx) = value;
}

void git_strmap_delete_at(git_strmap *map, size_t idx)
{
	kh_del(str, map, idx);
}

int git_strmap_put(git_strmap *map, const char *key, int *err)
{
	return kh_put(str, map, key, err);
}

void git_strmap_insert(git_strmap *map, const char *key, void *value, int *rval)
{
	khiter_t idx = kh_put(str, map, key, rval);

	if ((*rval) >= 0) {
		if ((*rval) == 0)
			kh_key(map, idx) = key;
		kh_val(map, idx) = value;
	}
}

void git_strmap_delete(git_strmap *map, const char *key)
{
	khiter_t idx = git_strmap_lookup_index(map, key);
	if (git_strmap_valid_index(map, idx))
		git_strmap_delete_at(map, idx);
}

105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
int git_strmap_next(
	void **data,
	git_strmap_iter* iter,
	git_strmap *map)
{
	if (!map)
		return GIT_ERROR;

	while (*iter != git_strmap_end(map)) {
		if (!(git_strmap_has_data(map, *iter))) {
			++(*iter);
			continue;
		}

		*data = git_strmap_value_at(map, *iter);

		++(*iter);

		return GIT_OK;
	}

	return GIT_ITEROVER;
}