Merge branch 'ms/doc-revision-illustration-fix'
[alt-git.git] / oidset.h
blob3a2d9d1115a45e47a93fa3950ad91a0eb1af4c7c
1 #ifndef OIDSET_H
2 #define OIDSET_H
4 #include "khash.h"
6 /**
7 * This API is similar to oid-array, in that it maintains a set of object ids
8 * in a memory-efficient way. The major differences are:
10 * 1. It uses a hash, so we can do online duplicate removal, rather than
11 * sort-and-uniq at the end. This can reduce memory footprint if you have
12 * a large list of oids with many duplicates.
14 * 2. The per-unique-oid memory footprint is slightly higher due to hash
15 * table overhead.
18 /**
19 * A single oidset; should be zero-initialized (or use OIDSET_INIT).
21 struct oidset {
22 kh_oid_set_t set;
25 #define OIDSET_INIT { { 0 } }
28 /**
29 * Initialize the oidset structure `set`.
31 * If `initial_size` is bigger than 0 then preallocate to allow inserting
32 * the specified number of elements without further allocations.
34 void oidset_init(struct oidset *set, size_t initial_size);
36 /**
37 * Returns true iff `set` contains `oid`.
39 int oidset_contains(const struct oidset *set, const struct object_id *oid);
41 /**
42 * Insert the oid into the set; a copy is made, so "oid" does not need
43 * to persist after this function is called.
45 * Returns 1 if the oid was already in the set, 0 otherwise. This can be used
46 * to perform an efficient check-and-add.
48 int oidset_insert(struct oidset *set, const struct object_id *oid);
50 /**
51 * Remove the oid from the set.
53 * Returns 1 if the oid was present in the set, 0 otherwise.
55 int oidset_remove(struct oidset *set, const struct object_id *oid);
57 /**
58 * Remove all entries from the oidset, freeing any resources associated with
59 * it.
61 void oidset_clear(struct oidset *set);
63 /**
64 * Add the contents of the file 'path' to an initialized oidset. Each line is
65 * an unabbreviated object name. Comments begin with '#', and trailing comments
66 * are allowed. Leading whitespace and empty or white-space only lines are
67 * ignored.
69 void oidset_parse_file(struct oidset *set, const char *path);
71 struct oidset_iter {
72 kh_oid_set_t *set;
73 khiter_t iter;
76 static inline void oidset_iter_init(struct oidset *set,
77 struct oidset_iter *iter)
79 iter->set = &set->set;
80 iter->iter = kh_begin(iter->set);
83 static inline struct object_id *oidset_iter_next(struct oidset_iter *iter)
85 for (; iter->iter != kh_end(iter->set); iter->iter++) {
86 if (kh_exist(iter->set, iter->iter))
87 return &kh_key(iter->set, iter->iter++);
89 return NULL;
92 static inline struct object_id *oidset_iter_first(struct oidset *set,
93 struct oidset_iter *iter)
95 oidset_iter_init(set, iter);
96 return oidset_iter_next(iter);
99 #endif /* OIDSET_H */