]>
Commit | Line | Data |
---|---|---|
29c2bd5f JK |
1 | #ifndef OIDSET_H |
2 | #define OIDSET_H | |
3 | ||
8b2f8cbc | 4 | #include "khash.h" |
9e6fabde | 5 | |
29c2bd5f | 6 | /** |
0740d0a5 | 7 | * This API is similar to oid-array, in that it maintains a set of object ids |
29c2bd5f JK |
8 | * in a memory-efficient way. The major differences are: |
9 | * | |
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. | |
13 | * | |
14 | * 2. The per-unique-oid memory footprint is slightly higher due to hash | |
15 | * table overhead. | |
16 | */ | |
17 | ||
18 | /** | |
19 | * A single oidset; should be zero-initialized (or use OIDSET_INIT). | |
20 | */ | |
21 | struct oidset { | |
8fbb558a | 22 | kh_oid_set_t set; |
29c2bd5f JK |
23 | }; |
24 | ||
8b2f8cbc | 25 | #define OIDSET_INIT { { 0 } } |
29c2bd5f | 26 | |
c3a9ad31 | 27 | |
8c84ae65 RS |
28 | /** |
29 | * Initialize the oidset structure `set`. | |
30 | * | |
31 | * If `initial_size` is bigger than 0 then preallocate to allow inserting | |
32 | * the specified number of elements without further allocations. | |
33 | */ | |
34 | void oidset_init(struct oidset *set, size_t initial_size); | |
c3a9ad31 | 35 | |
29c2bd5f JK |
36 | /** |
37 | * Returns true iff `set` contains `oid`. | |
38 | */ | |
39 | int oidset_contains(const struct oidset *set, const struct object_id *oid); | |
40 | ||
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. | |
44 | * | |
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. | |
47 | */ | |
48 | int oidset_insert(struct oidset *set, const struct object_id *oid); | |
49 | ||
c3a9ad31 JH |
50 | /** |
51 | * Remove the oid from the set. | |
52 | * | |
53 | * Returns 1 if the oid was present in the set, 0 otherwise. | |
54 | */ | |
55 | int oidset_remove(struct oidset *set, const struct object_id *oid); | |
56 | ||
f4781068 TB |
57 | /** |
58 | * Returns the number of oids in the set. | |
59 | */ | |
60 | int oidset_size(struct oidset *set); | |
61 | ||
29c2bd5f JK |
62 | /** |
63 | * Remove all entries from the oidset, freeing any resources associated with | |
64 | * it. | |
65 | */ | |
66 | void oidset_clear(struct oidset *set); | |
67 | ||
f93895f8 BR |
68 | /** |
69 | * Add the contents of the file 'path' to an initialized oidset. Each line is | |
70 | * an unabbreviated object name. Comments begin with '#', and trailing comments | |
71 | * are allowed. Leading whitespace and empty or white-space only lines are | |
72 | * ignored. | |
73 | */ | |
74 | void oidset_parse_file(struct oidset *set, const char *path); | |
75 | ||
c3a9ad31 | 76 | struct oidset_iter { |
8fbb558a | 77 | kh_oid_set_t *set; |
8b2f8cbc | 78 | khiter_t iter; |
c3a9ad31 JH |
79 | }; |
80 | ||
81 | static inline void oidset_iter_init(struct oidset *set, | |
82 | struct oidset_iter *iter) | |
83 | { | |
8b2f8cbc RS |
84 | iter->set = &set->set; |
85 | iter->iter = kh_begin(iter->set); | |
c3a9ad31 JH |
86 | } |
87 | ||
88 | static inline struct object_id *oidset_iter_next(struct oidset_iter *iter) | |
89 | { | |
8b2f8cbc RS |
90 | for (; iter->iter != kh_end(iter->set); iter->iter++) { |
91 | if (kh_exist(iter->set, iter->iter)) | |
92 | return &kh_key(iter->set, iter->iter++); | |
93 | } | |
94 | return NULL; | |
c3a9ad31 JH |
95 | } |
96 | ||
97 | static inline struct object_id *oidset_iter_first(struct oidset *set, | |
98 | struct oidset_iter *iter) | |
99 | { | |
100 | oidset_iter_init(set, iter); | |
101 | return oidset_iter_next(iter); | |
102 | } | |
103 | ||
29c2bd5f | 104 | #endif /* OIDSET_H */ |