| Jeff King | 29c2bd5 | 2017-02-08 20:53:07 | [diff] [blame] | 1 | #ifndef OIDSET_H |
| 2 | #define OIDSET_H |
| 3 | |
| René Scharfe | 8b2f8cb | 2018-10-04 15:13:06 | [diff] [blame] | 4 | #include "khash.h" |
| Jonathan Tan | 9e6fabde | 2017-09-29 22:54:22 | [diff] [blame] | 5 | |
| Jeff King | 29c2bd5 | 2017-02-08 20:53:07 | [diff] [blame] | 6 | /** |
| Jeff King | 0740d0a | 2020-03-30 14:04:13 | [diff] [blame] | 7 | * This API is similar to oid-array, in that it maintains a set of object ids |
| Jeff King | 29c2bd5 | 2017-02-08 20:53:07 | [diff] [blame] | 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 { |
| Jeff King | 8fbb558 | 2019-06-20 07:41:28 | [diff] [blame] | 22 | kh_oid_set_t set; |
| Jeff King | 29c2bd5 | 2017-02-08 20:53:07 | [diff] [blame] | 23 | }; |
| 24 | |
| René Scharfe | 8b2f8cb | 2018-10-04 15:13:06 | [diff] [blame] | 25 | #define OIDSET_INIT { { 0 } } |
| Jeff King | 29c2bd5 | 2017-02-08 20:53:07 | [diff] [blame] | 26 | |
| Jeff Hostetler | c3a9ad3 | 2017-11-21 20:58:49 | [diff] [blame] | 27 | |
| René Scharfe | 8c84ae6 | 2018-10-04 15:14:37 | [diff] [blame] | 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); |
| Jeff Hostetler | c3a9ad3 | 2017-11-21 20:58:49 | [diff] [blame] | 35 | |
| Jeff King | 29c2bd5 | 2017-02-08 20:53:07 | [diff] [blame] | 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 | |
| 50 | /** |
| Jeff Hostetler | c3a9ad3 | 2017-11-21 20:58:49 | [diff] [blame] | 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 | |
| 57 | /** |
| Taylor Blau | f478106 | 2020-04-14 04:04:22 | [diff] [blame] | 58 | * Returns the number of oids in the set. |
| 59 | */ |
| 60 | int oidset_size(struct oidset *set); |
| 61 | |
| 62 | /** |
| Jeff King | 29c2bd5 | 2017-02-08 20:53:07 | [diff] [blame] | 63 | * Remove all entries from the oidset, freeing any resources associated with |
| 64 | * it. |
| 65 | */ |
| 66 | void oidset_clear(struct oidset *set); |
| 67 | |
| Barret Rhoden | f93895f | 2019-05-15 21:44:57 | [diff] [blame] | 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 | |
| Junio C Hamano | 610e2b9 | 2020-09-25 04:55:04 | [diff] [blame] | 76 | /* |
| 77 | * Similar to the above, but with a callback which can (1) return non-zero to |
| 78 | * signal displeasure with the object and (2) replace object ID with something |
| 79 | * else (meant to be used to "peel"). |
| 80 | */ |
| 81 | typedef int (*oidset_parse_tweak_fn)(struct object_id *, void *); |
| 82 | void oidset_parse_file_carefully(struct oidset *set, const char *path, |
| 83 | oidset_parse_tweak_fn fn, void *cbdata); |
| 84 | |
| Jeff Hostetler | c3a9ad3 | 2017-11-21 20:58:49 | [diff] [blame] | 85 | struct oidset_iter { |
| Jeff King | 8fbb558 | 2019-06-20 07:41:28 | [diff] [blame] | 86 | kh_oid_set_t *set; |
| René Scharfe | 8b2f8cb | 2018-10-04 15:13:06 | [diff] [blame] | 87 | khiter_t iter; |
| Jeff Hostetler | c3a9ad3 | 2017-11-21 20:58:49 | [diff] [blame] | 88 | }; |
| 89 | |
| 90 | static inline void oidset_iter_init(struct oidset *set, |
| 91 | struct oidset_iter *iter) |
| 92 | { |
| René Scharfe | 8b2f8cb | 2018-10-04 15:13:06 | [diff] [blame] | 93 | iter->set = &set->set; |
| 94 | iter->iter = kh_begin(iter->set); |
| Jeff Hostetler | c3a9ad3 | 2017-11-21 20:58:49 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | static inline struct object_id *oidset_iter_next(struct oidset_iter *iter) |
| 98 | { |
| René Scharfe | 8b2f8cb | 2018-10-04 15:13:06 | [diff] [blame] | 99 | for (; iter->iter != kh_end(iter->set); iter->iter++) { |
| 100 | if (kh_exist(iter->set, iter->iter)) |
| 101 | return &kh_key(iter->set, iter->iter++); |
| 102 | } |
| 103 | return NULL; |
| Jeff Hostetler | c3a9ad3 | 2017-11-21 20:58:49 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | static inline struct object_id *oidset_iter_first(struct oidset *set, |
| 107 | struct oidset_iter *iter) |
| 108 | { |
| 109 | oidset_iter_init(set, iter); |
| 110 | return oidset_iter_next(iter); |
| 111 | } |
| 112 | |
| Jeff King | 29c2bd5 | 2017-02-08 20:53:07 | [diff] [blame] | 113 | #endif /* OIDSET_H */ |