Skip to content

Commit 405c6b1

Browse files
HebaWalygitster
authored andcommitted
sha1-array: move doc to sha1-array.h
Move the documentation from Documentation/technical/api-oid-array.txt to sha1-array.h as it's easier for the developers to find the usage information beside the code instead of looking for it in another doc file. Also documentation/technical/api-oid-array.txt is removed because the information it has is now redundant and it'll be hard to keep it up to date and synchronized with the documentation in the header file. Signed-off-by: Heba Waly <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent d3d7172 commit 405c6b1

File tree

3 files changed

+81
-91
lines changed

3 files changed

+81
-91
lines changed

Documentation/technical/api-oid-array.txt

Lines changed: 0 additions & 90 deletions
This file was deleted.

sha1-array.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ int oid_array_for_each(struct oid_array *array,
4848
{
4949
int i;
5050

51-
/* No oid_array_sort() here! See the api-oid-array.txt docs! */
51+
/* No oid_array_sort() here! See sha1-array.h */
5252

5353
for (i = 0; i < array->nr; i++) {
5454
int ret = fn(array->oid + i, data);

sha1-array.h

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

4+
/**
5+
* The API provides storage and manipulation of sets of object identifiers.
6+
* The emphasis is on storage and processing efficiency, making them suitable
7+
* for large lists. Note that the ordering of items is not preserved over some
8+
* operations.
9+
*
10+
* Examples
11+
* --------
12+
* -----------------------------------------
13+
* int print_callback(const struct object_id *oid,
14+
* void *data)
15+
* {
16+
* printf("%s\n", oid_to_hex(oid));
17+
* return 0; // always continue
18+
* }
19+
*
20+
* void some_func(void)
21+
* {
22+
* struct sha1_array hashes = OID_ARRAY_INIT;
23+
* struct object_id oid;
24+
*
25+
* // Read objects into our set
26+
* while (read_object_from_stdin(oid.hash))
27+
* oid_array_append(&hashes, &oid);
28+
*
29+
* // Check if some objects are in our set
30+
* while (read_object_from_stdin(oid.hash)) {
31+
* if (oid_array_lookup(&hashes, &oid) >= 0)
32+
* printf("it's in there!\n");
33+
*
34+
* // Print the unique set of objects. We could also have
35+
* // avoided adding duplicate objects in the first place,
36+
* // but we would end up re-sorting the array repeatedly.
37+
* // Instead, this will sort once and then skip duplicates
38+
* // in linear time.
39+
*
40+
* oid_array_for_each_unique(&hashes, print_callback, NULL);
41+
* }
42+
*/
43+
44+
/**
45+
* A single array of object IDs. This should be initialized by assignment from
46+
* `OID_ARRAY_INIT`. The `oid` member contains the actual data. The `nr` member
47+
* contains the number of items in the set. The `alloc` and `sorted` members
48+
* are used internally, and should not be needed by API callers.
49+
*/
450
struct oid_array {
551
struct object_id *oid;
652
int nr;
@@ -10,18 +56,52 @@ struct oid_array {
1056

1157
#define OID_ARRAY_INIT { NULL, 0, 0, 0 }
1258

59+
/**
60+
* Add an item to the set. The object ID will be placed at the end of the array
61+
* (but note that some operations below may lose this ordering).
62+
*/
1363
void oid_array_append(struct oid_array *array, const struct object_id *oid);
64+
65+
/**
66+
* Perform a binary search of the array for a specific object ID. If found,
67+
* returns the offset (in number of elements) of the object ID. If not found,
68+
* returns a negative integer. If the array is not sorted, this function has
69+
* the side effect of sorting it.
70+
*/
1471
int oid_array_lookup(struct oid_array *array, const struct object_id *oid);
72+
73+
/**
74+
* Free all memory associated with the array and return it to the initial,
75+
* empty state.
76+
*/
1577
void oid_array_clear(struct oid_array *array);
1678

1779
typedef int (*for_each_oid_fn)(const struct object_id *oid,
1880
void *data);
81+
/**
82+
* Iterate over each element of the list, executing the callback function for
83+
* each one. Does not sort the list, so any custom hash order is retained.
84+
* If the callback returns a non-zero value, the iteration ends immediately
85+
* and the callback's return is propagated; otherwise, 0 is returned.
86+
*/
1987
int oid_array_for_each(struct oid_array *array,
2088
for_each_oid_fn fn,
2189
void *data);
90+
91+
/**
92+
* Iterate over each unique element of the list in sorted order, but otherwise
93+
* behave like `oid_array_for_each`. If the array is not sorted, this function
94+
* has the side effect of sorting it.
95+
*/
2296
int oid_array_for_each_unique(struct oid_array *array,
2397
for_each_oid_fn fn,
2498
void *data);
99+
100+
/**
101+
* Apply the callback function `want` to each entry in the array, retaining
102+
* only the entries for which the function returns true. Preserve the order
103+
* of the entries that are retained.
104+
*/
25105
void oid_array_filter(struct oid_array *array,
26106
for_each_oid_fn want,
27107
void *cbdata);

0 commit comments

Comments
 (0)