oidmap.h 3.71 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

#include "common.h"
11

12 13
#include "git2/oid.h"

14
/** A map with `git_oid`s as key. */
15
typedef struct kh_oid_s git_oidmap;
16

17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
/**
 * Allocate a new OID map.
 *
 * @param out Pointer to the map that shall be allocated.
 * @return 0 on success, an error code if allocation has failed.
 */
int git_oidmap_new(git_oidmap **out);

/**
 * Free memory associated with the map.
 *
 * Note that this function will _not_ free values added to this
 * map.
 *
 * @param map Pointer to the map that is to be free'd. May be
 *            `NULL`.
 */
34
void git_oidmap_free(git_oidmap *map);
35 36 37 38 39 40 41 42 43 44

/**
 * Clear all entries from the map.
 *
 * This function will remove all entries from the associated map.
 * Memory associated with it will not be released, though.
 *
 * @param map Pointer to the map that shall be cleared. May be
 *            `NULL`.
 */
45
void git_oidmap_clear(git_oidmap *map);
46

47 48 49 50 51 52
/**
 * Return the number of elements in the map.
 *
 * @parameter map map containing the elements
 * @return number of elements in the map
 */
53
size_t git_oidmap_size(git_oidmap *map);
54

55 56 57 58 59 60 61 62 63
/**
 * Return value associated with the given key.
 *
 * @param map map to search key in
 * @param key key to search for
 * @return value associated with the given key or NULL if the key was not found
 */
void *git_oidmap_get(git_oidmap *map, const git_oid *key);

64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
/**
 * Set the entry for key to value.
 *
 * If the map has no corresponding entry for the given key, a new
 * entry will be created with the given value. If an entry exists
 * already, its value will be updated to match the given value.
 *
 * @param map map to create new entry in
 * @param key key to set
 * @param value value to associate the key with; may be NULL
 * @return zero if the key was successfully set, a negative error
 *         code otherwise
 */
int git_oidmap_set(git_oidmap *map, const git_oid *key, void *value);

79 80 81 82 83 84 85 86 87 88 89 90 91 92
/**
 * Delete an entry from the map.
 *
 * Delete the given key and its value from the map. If no such
 * key exists, this will do nothing.
 *
 * @param map map to delete key in
 * @param key key to delete
 * @return `0` if the key has been deleted, GIT_ENOTFOUND if no
 *         such key was found, a negative code in case of an
 *         error
 */
int git_oidmap_delete(git_oidmap *map, const git_oid *key);

93 94 95 96 97 98 99 100 101
/**
 * Check whether a key exists in the given map.
 *
 * @param map map to query for the key
 * @param key key to search for
 * @return 0 if the key has not been found, 1 otherwise
 */
int git_oidmap_exists(git_oidmap *map, const git_oid *key);

102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
/**
 * Iterate over entries of the map.
 *
 * This functions allows to iterate over all key-value entries of
 * the map. The current position is stored in the `iter` variable
 * and should be initialized to `0` before the first call to this
 * function.
 *
 * @param map map to iterate over
 * @param value pointer to the variable where to store the current
 *            value. May be NULL.
 * @param iter iterator storing the current position. Initialize
 *             with zero previous to the first call.
 * @param key pointer to the variable where to store the current
 *            key. May be NULL.
 * @return `0` if the next entry was correctly retrieved.
 *        GIT_ITEROVER if no entries are left. A negative error
 *        code otherwise.
 */
int git_oidmap_iterate(void **value, git_oidmap *map, size_t *iter, const git_oid **key);

#define git_oidmap_foreach_value(h, vvar, code) { size_t __i = 0;		\
	while (git_oidmap_iterate((void **) &(vvar), h, &__i, NULL) == 0) {	\
125 126
		code;								\
	} }
127

128
#endif