blob: ba4a5a2cd3a7a233bc9ca2cb7cf4a58a1e5a122c [file] [log] [blame]
Jeff King29c2bd52017-02-08 15:53:07 -05001#ifndef OIDSET_H
2#define OIDSET_H
3
René Scharfe8b2f8cb2018-10-04 17:13:06 +02004#include "khash.h"
Jonathan Tan9e6fabde2017-09-29 15:54:22 -07005
Jeff King29c2bd52017-02-08 15:53:07 -05006/**
Jeff King0740d0a2020-03-30 10:04:13 -04007 * This API is similar to oid-array, in that it maintains a set of object ids
Jeff King29c2bd52017-02-08 15:53:07 -05008 * 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 */
21struct oidset {
Jeff King8fbb5582019-06-20 03:41:28 -040022 kh_oid_set_t set;
Jeff King29c2bd52017-02-08 15:53:07 -050023};
24
René Scharfe8b2f8cb2018-10-04 17:13:06 +020025#define OIDSET_INIT { { 0 } }
Jeff King29c2bd52017-02-08 15:53:07 -050026
Jeff Hostetlerc3a9ad32017-11-21 20:58:49 +000027
René Scharfe8c84ae62018-10-04 17:14:37 +020028/**
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 */
34void oidset_init(struct oidset *set, size_t initial_size);
Jeff Hostetlerc3a9ad32017-11-21 20:58:49 +000035
Jeff King29c2bd52017-02-08 15:53:07 -050036/**
37 * Returns true iff `set` contains `oid`.
38 */
39int 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 */
48int oidset_insert(struct oidset *set, const struct object_id *oid);
49
50/**
Jeff Hostetlerc3a9ad32017-11-21 20:58:49 +000051 * Remove the oid from the set.
52 *
53 * Returns 1 if the oid was present in the set, 0 otherwise.
54 */
55int oidset_remove(struct oidset *set, const struct object_id *oid);
56
57/**
Taylor Blauf4781062020-04-13 22:04:22 -060058 * Returns the number of oids in the set.
59 */
René Scharfe325006f2021-09-11 22:36:40 +020060static inline int oidset_size(const struct oidset *set)
61{
62 return kh_size(&set->set);
63}
Taylor Blauf4781062020-04-13 22:04:22 -060064
65/**
Jeff King29c2bd52017-02-08 15:53:07 -050066 * Remove all entries from the oidset, freeing any resources associated with
67 * it.
68 */
69void oidset_clear(struct oidset *set);
70
Barret Rhodenf93895f2019-05-15 17:44:57 -040071/**
72 * Add the contents of the file 'path' to an initialized oidset. Each line is
73 * an unabbreviated object name. Comments begin with '#', and trailing comments
74 * are allowed. Leading whitespace and empty or white-space only lines are
75 * ignored.
76 */
77void oidset_parse_file(struct oidset *set, const char *path);
78
Junio C Hamano610e2b92020-09-24 21:55:04 -070079/*
80 * Similar to the above, but with a callback which can (1) return non-zero to
81 * signal displeasure with the object and (2) replace object ID with something
82 * else (meant to be used to "peel").
83 */
84typedef int (*oidset_parse_tweak_fn)(struct object_id *, void *);
85void oidset_parse_file_carefully(struct oidset *set, const char *path,
86 oidset_parse_tweak_fn fn, void *cbdata);
87
Jeff Hostetlerc3a9ad32017-11-21 20:58:49 +000088struct oidset_iter {
Jeff King8fbb5582019-06-20 03:41:28 -040089 kh_oid_set_t *set;
René Scharfe8b2f8cb2018-10-04 17:13:06 +020090 khiter_t iter;
Jeff Hostetlerc3a9ad32017-11-21 20:58:49 +000091};
92
93static inline void oidset_iter_init(struct oidset *set,
94 struct oidset_iter *iter)
95{
René Scharfe8b2f8cb2018-10-04 17:13:06 +020096 iter->set = &set->set;
97 iter->iter = kh_begin(iter->set);
Jeff Hostetlerc3a9ad32017-11-21 20:58:49 +000098}
99
100static inline struct object_id *oidset_iter_next(struct oidset_iter *iter)
101{
René Scharfe8b2f8cb2018-10-04 17:13:06 +0200102 for (; iter->iter != kh_end(iter->set); iter->iter++) {
103 if (kh_exist(iter->set, iter->iter))
104 return &kh_key(iter->set, iter->iter++);
105 }
106 return NULL;
Jeff Hostetlerc3a9ad32017-11-21 20:58:49 +0000107}
108
109static inline struct object_id *oidset_iter_first(struct oidset *set,
110 struct oidset_iter *iter)
111{
112 oidset_iter_init(set, iter);
113 return oidset_iter_next(iter);
114}
115
Jeff King29c2bd52017-02-08 15:53:07 -0500116#endif /* OIDSET_H */