Re: [PATCH 6/9] oid-array: provide a for-loop iterator
- From
Junio C Hamano <gitster@pobox.com>
- Date
- Dec 4, 2020, 21:54 UTC
- Message-ID
- <xmqq1rg5dyp4.fsf@gitster.c.googlers.com>
- In-Reply-To
- <CAPig+cTLs5bkY=Pwg+4tT2KHfVHFjkrNTeS_ygGEwq0cOOJ9wQ@mail.gmail.com>
Eric Sunshine <sunshine@sunshineco.com> writes:
Show 31 quoted lines
> On Fri, Dec 4, 2020 at 1:54 PM Jeff King <peff@peff.net> 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@peff.net>
>> ---
>> 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?
Show 7 quoted lines
>> +{
>> + do {
>> + cur++;
>> + } while (cur < array->nr &&
>> + oideq(array->oid + cur, array->oid + cur - 1));
>> + return cur;
>> +}