1 /*
2  * Copyright (C) the libgit2 contributors. All rights reserved.
3  *
4  * This file is part of libgit2, distributed under the GNU GPL v2 with
5  * a Linking Exception. For full terms see the included COPYING file.
6  */
7 #ifndef INCLUDE_oidmap_h__
8 #define INCLUDE_oidmap_h__
9 
10 #include "common.h"
11 
12 #include "git2/oid.h"
13 
14 /** A map with `git_oid`s as key. */
15 typedef struct kh_oid_s git_oidmap;
16 
17 /**
18  * Allocate a new OID map.
19  *
20  * @param out Pointer to the map that shall be allocated.
21  * @return 0 on success, an error code if allocation has failed.
22  */
23 int git_oidmap_new(git_oidmap **out);
24 
25 /**
26  * Free memory associated with the map.
27  *
28  * Note that this function will _not_ free values added to this
29  * map.
30  *
31  * @param map Pointer to the map that is to be free'd. May be
32  *            `NULL`.
33  */
34 void git_oidmap_free(git_oidmap *map);
35 
36 /**
37  * Clear all entries from the map.
38  *
39  * This function will remove all entries from the associated map.
40  * Memory associated with it will not be released, though.
41  *
42  * @param map Pointer to the map that shall be cleared. May be
43  *            `NULL`.
44  */
45 void git_oidmap_clear(git_oidmap *map);
46 
47 /**
48  * Return the number of elements in the map.
49  *
50  * @parameter map map containing the elements
51  * @return number of elements in the map
52  */
53 size_t git_oidmap_size(git_oidmap *map);
54 
55 /**
56  * Return value associated with the given key.
57  *
58  * @param map map to search key in
59  * @param key key to search for
60  * @return value associated with the given key or NULL if the key was not found
61  */
62 void *git_oidmap_get(git_oidmap *map, const git_oid *key);
63 
64 /**
65  * Set the entry for key to value.
66  *
67  * If the map has no corresponding entry for the given key, a new
68  * entry will be created with the given value. If an entry exists
69  * already, its value will be updated to match the given value.
70  *
71  * @param map map to create new entry in
72  * @param key key to set
73  * @param value value to associate the key with; may be NULL
74  * @return zero if the key was successfully set, a negative error
75  *         code otherwise
76  */
77 int git_oidmap_set(git_oidmap *map, const git_oid *key, void *value);
78 
79 /**
80  * Delete an entry from the map.
81  *
82  * Delete the given key and its value from the map. If no such
83  * key exists, this will do nothing.
84  *
85  * @param map map to delete key in
86  * @param key key to delete
87  * @return `0` if the key has been deleted, GIT_ENOTFOUND if no
88  *         such key was found, a negative code in case of an
89  *         error
90  */
91 int git_oidmap_delete(git_oidmap *map, const git_oid *key);
92 
93 /**
94  * Check whether a key exists in the given map.
95  *
96  * @param map map to query for the key
97  * @param key key to search for
98  * @return 0 if the key has not been found, 1 otherwise
99  */
100 int git_oidmap_exists(git_oidmap *map, const git_oid *key);
101 
102 /**
103  * Iterate over entries of the map.
104  *
105  * This functions allows to iterate over all key-value entries of
106  * the map. The current position is stored in the `iter` variable
107  * and should be initialized to `0` before the first call to this
108  * function.
109  *
110  * @param map map to iterate over
111  * @param value pointer to the variable where to store the current
112  *            value. May be NULL.
113  * @param iter iterator storing the current position. Initialize
114  *             with zero previous to the first call.
115  * @param key pointer to the variable where to store the current
116  *            key. May be NULL.
117  * @return `0` if the next entry was correctly retrieved.
118  *        GIT_ITEROVER if no entries are left. A negative error
119  *        code otherwise.
120  */
121 int git_oidmap_iterate(void **value, git_oidmap *map, size_t *iter, const git_oid **key);
122 
123 #define git_oidmap_foreach_value(h, vvar, code) { size_t __i = 0;		\
124 	while (git_oidmap_iterate((void **) &(vvar), h, &__i, NULL) == 0) {	\
125 		code;								\
126 	} }
127 
128 #endif
129