]> git.ipfire.org Git - thirdparty/git.git/blame - oidset.h
commit-graph.c: don't use discarded graph_name in error
[thirdparty/git.git] / oidset.h
CommitLineData
29c2bd5f
JK
1#ifndef OIDSET_H
2#define OIDSET_H
3
8b2f8cbc
RS
4#include "hashmap.h"
5#include "khash.h"
9e6fabde 6
29c2bd5f
JK
7/**
8 * This API is similar to sha1-array, in that it maintains a set of object ids
9 * in a memory-efficient way. The major differences are:
10 *
11 * 1. It uses a hash, so we can do online duplicate removal, rather than
12 * sort-and-uniq at the end. This can reduce memory footprint if you have
13 * a large list of oids with many duplicates.
14 *
15 * 2. The per-unique-oid memory footprint is slightly higher due to hash
16 * table overhead.
17 */
18
19/**
20 * A single oidset; should be zero-initialized (or use OIDSET_INIT).
21 */
22struct oidset {
8fbb558a 23 kh_oid_set_t set;
29c2bd5f
JK
24};
25
8b2f8cbc 26#define OIDSET_INIT { { 0 } }
29c2bd5f 27
c3a9ad31 28
8c84ae65
RS
29/**
30 * Initialize the oidset structure `set`.
31 *
32 * If `initial_size` is bigger than 0 then preallocate to allow inserting
33 * the specified number of elements without further allocations.
34 */
35void oidset_init(struct oidset *set, size_t initial_size);
c3a9ad31 36
29c2bd5f
JK
37/**
38 * Returns true iff `set` contains `oid`.
39 */
40int oidset_contains(const struct oidset *set, const struct object_id *oid);
41
42/**
43 * Insert the oid into the set; a copy is made, so "oid" does not need
44 * to persist after this function is called.
45 *
46 * Returns 1 if the oid was already in the set, 0 otherwise. This can be used
47 * to perform an efficient check-and-add.
48 */
49int oidset_insert(struct oidset *set, const struct object_id *oid);
50
c3a9ad31
JH
51/**
52 * Remove the oid from the set.
53 *
54 * Returns 1 if the oid was present in the set, 0 otherwise.
55 */
56int oidset_remove(struct oidset *set, const struct object_id *oid);
57
f4781068
TB
58/**
59 * Returns the number of oids in the set.
60 */
61int oidset_size(struct oidset *set);
62
29c2bd5f
JK
63/**
64 * Remove all entries from the oidset, freeing any resources associated with
65 * it.
66 */
67void oidset_clear(struct oidset *set);
68
f93895f8
BR
69/**
70 * Add the contents of the file 'path' to an initialized oidset. Each line is
71 * an unabbreviated object name. Comments begin with '#', and trailing comments
72 * are allowed. Leading whitespace and empty or white-space only lines are
73 * ignored.
74 */
75void oidset_parse_file(struct oidset *set, const char *path);
76
c3a9ad31 77struct oidset_iter {
8fbb558a 78 kh_oid_set_t *set;
8b2f8cbc 79 khiter_t iter;
c3a9ad31
JH
80};
81
82static inline void oidset_iter_init(struct oidset *set,
83 struct oidset_iter *iter)
84{
8b2f8cbc
RS
85 iter->set = &set->set;
86 iter->iter = kh_begin(iter->set);
c3a9ad31
JH
87}
88
89static inline struct object_id *oidset_iter_next(struct oidset_iter *iter)
90{
8b2f8cbc
RS
91 for (; iter->iter != kh_end(iter->set); iter->iter++) {
92 if (kh_exist(iter->set, iter->iter))
93 return &kh_key(iter->set, iter->iter++);
94 }
95 return NULL;
c3a9ad31
JH
96}
97
98static inline struct object_id *oidset_iter_first(struct oidset *set,
99 struct oidset_iter *iter)
100{
101 oidset_iter_init(set, iter);
102 return oidset_iter_next(iter);
103}
104
29c2bd5f 105#endif /* OIDSET_H */