Skip to content

Commit 5d795b3

Browse files
pks-tgitster
authored andcommitted
oidset: introduce oidset_equal()
Introduce a new function that allows the caller to verify whether two oidsets contain the exact same object IDs. Note that this change requires us to change `oidset_iter_init()` to accept a `const struct oidset`. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent b67b2d9 commit 5d795b3

2 files changed

Lines changed: 23 additions & 2 deletions

File tree

oidset.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,22 @@ int oidset_contains(const struct oidset *set, const struct object_id *oid)
1616
return pos != kh_end(&set->set);
1717
}
1818

19+
bool oidset_equal(const struct oidset *a, const struct oidset *b)
20+
{
21+
struct oidset_iter iter;
22+
struct object_id *a_oid;
23+
24+
if (oidset_size(a) != oidset_size(b))
25+
return false;
26+
27+
oidset_iter_init(a, &iter);
28+
while ((a_oid = oidset_iter_next(&iter)))
29+
if (!oidset_contains(b, a_oid))
30+
return false;
31+
32+
return true;
33+
}
34+
1935
int oidset_insert(struct oidset *set, const struct object_id *oid)
2036
{
2137
int added;

oidset.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ void oidset_init(struct oidset *set, size_t initial_size);
3838
*/
3939
int oidset_contains(const struct oidset *set, const struct object_id *oid);
4040

41+
/**
42+
* Returns true iff `a` and `b` contain the exact same OIDs.
43+
*/
44+
bool oidset_equal(const struct oidset *a, const struct oidset *b);
45+
4146
/**
4247
* Insert the oid into the set; a copy is made, so "oid" does not need
4348
* to persist after this function is called.
@@ -94,11 +99,11 @@ void oidset_parse_file_carefully(struct oidset *set, const char *path,
9499
oidset_parse_tweak_fn fn, void *cbdata);
95100

96101
struct oidset_iter {
97-
kh_oid_set_t *set;
102+
const kh_oid_set_t *set;
98103
khiter_t iter;
99104
};
100105

101-
static inline void oidset_iter_init(struct oidset *set,
106+
static inline void oidset_iter_init(const struct oidset *set,
102107
struct oidset_iter *iter)
103108
{
104109
iter->set = &set->set;

0 commit comments

Comments
 (0)