Skip to content

Commit 12c4b4c

Browse files
peffgitster
authored andcommitted
oid-array: provide a for-loop iterator
We provide oid_array_for_each_unique() for iterating over the de-duplicated items in an array. But it's awkward to use for two reasons: 1. It uses a callback, which means marshaling arguments into a struct and passing it to the callback with a void parameter. 2. The callback doesn't know the numeric index of the oid we're looking at. This is useful for things like progress meters. Iterating with a for-loop is much more natural for some cases, but the caller has to do the de-duping itself. However, we can provide a small helper to make this easier (see the docstring in the header for an example use). 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. I also considered adding a full iterator type with init/next/end functions (similar to what we have for hashmaps). But it ended up making the callers much harder to read. This version keeps us close to a basic for-loop. Yet another option would be adding an option to sort the array and compact out the duplicates. This would mean iterating over the array an extra time, though that's probably not a big deal (we did just do an O(n log n) sort). But we'd still have to write a for-loop to iterate, so it doesn't really make anything easier for the caller. No new test, since we'll convert the callback iterator (which is covered by t0064, among other callers) to use the new code. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d0482b4 commit 12c4b4c

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

oid-array.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,8 @@ int oid_array_for_each_unique(struct oid_array *array,
6767

6868
oid_array_sort(array);
6969

70-
for (i = 0; i < array->nr; i++) {
71-
int ret;
72-
if (i > 0 && oideq(array->oid + i, array->oid + i - 1))
73-
continue;
74-
ret = fn(array->oid + i, data);
70+
for (i = 0; i < array->nr; i = oid_array_next_unique(array, i)) {
71+
int ret = fn(array->oid + i, data);
7572
if (ret)
7673
return ret;
7774
}

oid-array.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#ifndef OID_ARRAY_H
22
#define OID_ARRAY_H
33

4+
#include "hash.h"
5+
46
/**
57
* The API provides storage and manipulation of sets of object identifiers.
68
* The emphasis is on storage and processing efficiency, making them suitable
@@ -111,4 +113,25 @@ void oid_array_filter(struct oid_array *array,
111113
*/
112114
void oid_array_sort(struct oid_array *array);
113115

116+
/**
117+
* Find the next unique oid in the array after position "cur".
118+
* The array must be sorted for this to work. You can iterate
119+
* over unique elements like this:
120+
*
121+
* size_t i;
122+
* oid_array_sort(array);
123+
* for (i = 0; i < array->nr; i = oid_array_next_unique(array, i))
124+
* printf("%s", oid_to_hex(array->oids[i]);
125+
*
126+
* Non-unique iteration can just increment with "i++" to visit each element.
127+
*/
128+
static inline size_t oid_array_next_unique(struct oid_array *array, size_t cur)
129+
{
130+
do {
131+
cur++;
132+
} while (cur < array->nr &&
133+
oideq(array->oid + cur, array->oid + cur - 1));
134+
return cur;
135+
}
136+
114137
#endif /* OID_ARRAY_H */

0 commit comments

Comments
 (0)