]>
Commit | Line | Data |
---|---|---|
01fed0a8 | 1 | /* |
359fc2d2 | 2 | * Copyright (C) the libgit2 contributors. All rights reserved. |
01fed0a8 RB |
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 | */ | |
c2b67043 RB |
7 | #ifndef INCLUDE_oidmap_h__ |
8 | #define INCLUDE_oidmap_h__ | |
01fed0a8 RB |
9 | |
10 | #include "common.h" | |
eae0bfdc | 11 | |
01fed0a8 RB |
12 | #include "git2/oid.h" |
13 | ||
0c9c969a | 14 | /** A map with `git_oid`s as key. */ |
ac3d33df | 15 | typedef struct kh_oid_s git_oidmap; |
01fed0a8 | 16 | |
0c9c969a UG |
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 | */ | |
94af9155 | 34 | void git_oidmap_free(git_oidmap *map); |
0c9c969a 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 | */ | |
659f5d07 | 45 | void git_oidmap_clear(git_oidmap *map); |
01fed0a8 | 46 | |
0c9c969a 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 | */ | |
659f5d07 | 53 | size_t git_oidmap_size(git_oidmap *map); |
de1e81aa | 54 | |
0c9c969a 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_oidmap_get(git_oidmap *map, const git_oid *key); | |
036daa59 | 63 | |
0c9c969a 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_oidmap_set(git_oidmap *map, const git_oid *key, void *value); | |
de1e81aa | 78 | |
0c9c969a 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_oidmap_delete(git_oidmap *map, const git_oid *key); | |
f31cb45a | 92 | |
0c9c969a 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_oidmap_exists(git_oidmap *map, const git_oid *key); | |
de1e81aa | 101 | |
0c9c969a 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_oidmap_iterate(void **value, git_oidmap *map, size_t *iter, const git_oid **key); | |
3a728fb5 | 122 | |
0c9c969a UG |
123 | #define git_oidmap_foreach_value(h, vvar, code) { size_t __i = 0; \ |
124 | while (git_oidmap_iterate((void **) &(vvar), h, &__i, NULL) == 0) { \ | |
ac3d33df JK |
125 | code; \ |
126 | } } | |
64e46dc3 | 127 | |
01fed0a8 | 128 | #endif |