oidmap.h 1.38 KB
Newer Older
1
/*
Edward Thomson committed
2
 * Copyright (C) the libgit2 contributors. All rights reserved.
3 4 5 6
 *
 * This file is part of libgit2, distributed under the GNU GPL v2 with
 * a Linking Exception. For full terms see the included COPYING file.
 */
7 8
#ifndef INCLUDE_oidmap_h__
#define INCLUDE_oidmap_h__
9 10 11 12 13 14 15

#include "common.h"
#include "git2/oid.h"

#define kmalloc git__malloc
#define kcalloc git__calloc
#define krealloc git__realloc
16
#define kreallocarray git__reallocarray
17 18 19
#define kfree git__free
#include "khash.h"

20
__KHASH_TYPE(oid, const git_oid *, void *)
21
typedef khash_t(oid) git_oidmap;
22

23
GIT_INLINE(khint_t) git_oidmap_hash(const git_oid *oid)
24
{
25 26
	khint_t h;
	memcpy(&h, oid, sizeof(khint_t));
27 28 29
	return h;
}

30
#define GIT__USE_OIDMAP \
31
	__KHASH_IMPL(oid, static kh_inline, const git_oid *, void *, 1, git_oidmap_hash, git_oid_equal)
32

33 34
#define git_oidmap_alloc() kh_init(oid)
#define git_oidmap_free(h) kh_destroy(oid,h), h = NULL
35

36 37 38 39 40 41 42 43 44 45 46 47 48
#define git_oidmap_lookup_index(h, k) kh_get(oid, h, k)
#define git_oidmap_valid_index(h, idx) (idx != kh_end(h))

#define git_oidmap_value_at(h, idx) kh_val(h, idx)

#define git_oidmap_insert(h, key, val, rval) do { \
	khiter_t __pos = kh_put(oid, h, key, &rval); \
	if (rval >= 0) { \
		if (rval == 0) kh_key(h, __pos) = key; \
		kh_val(h, __pos) = val; \
	} } while (0)

#define git_oidmap_foreach_value kh_foreach_value
49 50 51

#define git_oidmap_size(h) kh_size(h)

52 53
#define git_oidmap_clear(h) kh_clear(oid, h)

54
#endif