]>
Commit | Line | Data |
---|---|---|
c0f4a011 CMN |
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" | |
eae0bfdc | 11 | |
c0f4a011 CMN |
12 | #include "git2/types.h" |
13 | ||
22a2d3d5 | 14 | /** A map with `off64_t`s as key. */ |
ac3d33df | 15 | typedef struct kh_off_s git_offmap; |
c0f4a011 | 16 | |
22a2d3d5 UG |
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 | */ | |
94af9155 | 34 | void git_offmap_free(git_offmap *map); |
22a2d3d5 UG |
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 | */ | |
cf6124d6 | 45 | void git_offmap_clear(git_offmap *map); |
c0f4a011 | 46 | |
22a2d3d5 UG |
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); | |
c0f4a011 | 54 | |
22a2d3d5 UG |
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); | |
c0f4a011 | 63 | |
22a2d3d5 UG |
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); | |
c0f4a011 | 78 | |
22a2d3d5 UG |
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); | |
c0f4a011 | 92 | |
22a2d3d5 UG |
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); | |
c0f4a011 | 101 | |
22a2d3d5 UG |
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); | |
ac3d33df | 122 | |
22a2d3d5 UG |
123 | #define git_offmap_foreach(h, kvar, vvar, code) { size_t __i = 0; \ |
124 | while (git_offmap_iterate((void **) &(vvar), h, &__i, &(kvar)) == 0) { \ | |
ac3d33df JK |
125 | code; \ |
126 | } } | |
127 | ||
22a2d3d5 UG |
128 | #define git_offmap_foreach_value(h, vvar, code) { size_t __i = 0; \ |
129 | while (git_offmap_iterate((void **) &(vvar), h, &__i, NULL) == 0) { \ | |
ac3d33df JK |
130 | code; \ |
131 | } } | |
c0f4a011 CMN |
132 | |
133 | #endif |