]> git.proxmox.com Git - libgit2.git/blob - src/offmap.h
New upstream version 1.4.3+dfsg.1
[libgit2.git] / src / offmap.h
1 /*
2 * Copyright (C) 2012 the libgit2 contributors
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_offmap_h__
8 #define INCLUDE_offmap_h__
9
10 #include "common.h"
11
12 #include "git2/types.h"
13
14 /** A map with `off64_t`s as key. */
15 typedef struct kh_off_s git_offmap;
16
17 /**
18 * Allocate a new `off64_t` 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_offmap_new(git_offmap **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_offmap_free(git_offmap *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_offmap_clear(git_offmap *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_offmap_size(git_offmap *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_offmap_get(git_offmap *map, const off64_t 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_offmap_set(git_offmap *map, const off64_t 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_offmap_delete(git_offmap *map, const off64_t 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_offmap_exists(git_offmap *map, const off64_t 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_offmap_iterate(void **value, git_offmap *map, size_t *iter, off64_t *key);
122
123 #define git_offmap_foreach(h, kvar, vvar, code) { size_t __i = 0; \
124 while (git_offmap_iterate((void **) &(vvar), h, &__i, &(kvar)) == 0) { \
125 code; \
126 } }
127
128 #define git_offmap_foreach_value(h, vvar, code) { size_t __i = 0; \
129 while (git_offmap_iterate((void **) &(vvar), h, &__i, NULL) == 0) { \
130 code; \
131 } }
132
133 #endif