oidmap.h 2.13 KB
Newer Older
1 2 3
#ifndef OIDMAP_H
#define OIDMAP_H

4
#include "cache.h"
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
#include "hashmap.h"

/*
 * struct oidmap_entry is a structure representing an entry in the hash table,
 * which must be used as first member of user data structures.
 *
 * Users should set the oid field. oidmap_put() will populate the
 * internal_entry field.
 */
struct oidmap_entry {
	/* For internal use only */
	struct hashmap_entry internal_entry;

	struct object_id oid;
};

struct oidmap {
	struct hashmap map;
};

#define OIDMAP_INIT { { NULL } }

/*
 * Initializes an oidmap structure.
 *
 * `map` is the oidmap to initialize.
 *
 * If the total number of entries is known in advance, the `initial_size`
 * parameter may be used to preallocate a sufficiently large table and thus
 * prevent expensive resizing. If 0, the table is dynamically resized.
 */
36
void oidmap_init(struct oidmap *map, size_t initial_size);
37 38 39 40 41 42 43

/*
 * Frees an oidmap structure and allocated memory.
 *
 * If `free_entries` is true, each oidmap_entry in the map is freed as well
 * using stdlibs free().
 */
44
void oidmap_free(struct oidmap *map, int free_entries);
45 46 47 48

/*
 * Returns the oidmap entry for the specified oid, or NULL if not found.
 */
49
void *oidmap_get(const struct oidmap *map,
50
		 const struct object_id *key);
51 52 53 54 55 56 57 58 59

/*
 * Adds or replaces an oidmap entry.
 *
 * ((struct oidmap_entry *) entry)->internal_entry will be populated by this
 * function.
 *
 * Returns the replaced entry, or NULL if not found (i.e. the entry was added).
 */
60
void *oidmap_put(struct oidmap *map, void *entry);
61 62 63 64 65 66

/*
 * Removes an oidmap entry matching the specified oid.
 *
 * Returns the removed entry, or NULL if not found.
 */
67
void *oidmap_remove(struct oidmap *map, const struct object_id *key);
68

69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90

struct oidmap_iter {
	struct hashmap_iter h_iter;
};

static inline void oidmap_iter_init(struct oidmap *map, struct oidmap_iter *iter)
{
	hashmap_iter_init(&map->map, &iter->h_iter);
}

static inline void *oidmap_iter_next(struct oidmap_iter *iter)
{
	return hashmap_iter_next(&iter->h_iter);
}

static inline void *oidmap_iter_first(struct oidmap *map,
				      struct oidmap_iter *iter)
{
	oidmap_iter_init(map, iter);
	return oidmap_iter_next(iter);
}

91
#endif