Eric Sunshine <sunshine@xxxxxxxxxxxxxx> writes: > On Fri, Dec 4, 2020 at 1:54 PM Jeff King <peff@xxxxxxxx> wrote: >> [...] >> The caller does have to remember to sort the array first. We could add >> an assertion into the helper that array->sorted is set, but I didn't >> want to complicate what is otherwise a pretty fast code path. >> [...] >> Signed-off-by: Jeff King <peff@xxxxxxxx> >> --- >> diff --git a/oid-array.h b/oid-array.h >> @@ -111,4 +113,24 @@ void oid_array_filter(struct oid_array *array, >> +/** >> + * Find the next unique oid in the array after position "cur". You >> + * can use this to iterate over unique elements, like: >> + * >> + * size_t i; >> + * oid_array_sort(array); >> + * for (i = 0; i < array->nr; i = oid_array_next_unique(array, i)) >> + * printf("%s", oid_to_hex(array->oids[i]); >> + * >> + * Non-unique iteration can just increment with "i++" to visit each element. >> + */ > > Minor: I see that the example code sorts the array first -- which is > necessary, as explained in the commit message -- but I wonder if it is > worth calling out explicitly in the prose: > > Find the next unique oid in the array after position `cur`. > The array must be sorted for this to work. You can use > this to iterate over unique elements like this: > >> +static inline size_t oid_array_next_unique(struct oid_array *array, size_t cur) Perhaps the function can make it clear that it expects to be fed a sorted array in its name, which would be even better? >> +{ >> + do { >> + cur++; >> + } while (cur < array->nr && >> + oideq(array->oid + cur, array->oid + cur - 1)); >> + return cur; >> +}