BreakingChanges: document removal of grafting
[git/gitster.git] / oid-array.h
blobf60f9af6741dc5acafb61b8a8a1562f49f92a600
1 #ifndef OID_ARRAY_H
2 #define OID_ARRAY_H
4 #include "hash.h"
6 /**
7 * The API provides storage and manipulation of sets of object identifiers.
8 * The emphasis is on storage and processing efficiency, making them suitable
9 * for large lists. Note that the ordering of items is not preserved over some
10 * operations.
12 * Examples
13 * --------
14 * -----------------------------------------
15 * int print_callback(const struct object_id *oid,
16 * void *data)
17 * {
18 * printf("%s\n", oid_to_hex(oid));
19 * return 0; // always continue
20 * }
22 * void some_func(void)
23 * {
24 * struct oid_array hashes = OID_ARRAY_INIT;
25 * struct object_id oid;
27 * // Read objects into our set
28 * while (read_object_from_stdin(oid.hash))
29 * oid_array_append(&hashes, &oid);
31 * // Check if some objects are in our set
32 * while (read_object_from_stdin(oid.hash)) {
33 * if (oid_array_lookup(&hashes, &oid) >= 0)
34 * printf("it's in there!\n");
36 * // Print the unique set of objects. We could also have
37 * // avoided adding duplicate objects in the first place,
38 * // but we would end up re-sorting the array repeatedly.
39 * // Instead, this will sort once and then skip duplicates
40 * // in linear time.
42 * oid_array_for_each_unique(&hashes, print_callback, NULL);
43 * }
46 /**
47 * A single array of object IDs. This should be initialized by assignment from
48 * `OID_ARRAY_INIT`. The `oid` member contains the actual data. The `nr` member
49 * contains the number of items in the set. The `alloc` and `sorted` members
50 * are used internally, and should not be needed by API callers.
52 struct oid_array {
53 struct object_id *oid;
54 size_t nr;
55 size_t alloc;
56 int sorted;
59 #define OID_ARRAY_INIT { 0 }
61 /**
62 * Add an item to the set. The object ID will be placed at the end of the array
63 * (but note that some operations below may lose this ordering).
65 void oid_array_append(struct oid_array *array, const struct object_id *oid);
67 /**
68 * Perform a binary search of the array for a specific object ID. If found,
69 * returns the offset (in number of elements) of the object ID. If not found,
70 * returns a negative integer. If the array is not sorted, this function has
71 * the side effect of sorting it.
73 int oid_array_lookup(struct oid_array *array, const struct object_id *oid);
75 /**
76 * Free all memory associated with the array and return it to the initial,
77 * empty state.
79 void oid_array_clear(struct oid_array *array);
81 typedef int (*for_each_oid_fn)(const struct object_id *oid,
82 void *data);
83 /**
84 * Iterate over each element of the list, executing the callback function for
85 * each one. Does not sort the list, so any custom hash order is retained.
86 * If the callback returns a non-zero value, the iteration ends immediately
87 * and the callback's return is propagated; otherwise, 0 is returned.
89 int oid_array_for_each(struct oid_array *array,
90 for_each_oid_fn fn,
91 void *data);
93 /**
94 * Iterate over each unique element of the list in sorted order, but otherwise
95 * behave like `oid_array_for_each`. If the array is not sorted, this function
96 * has the side effect of sorting it.
98 int oid_array_for_each_unique(struct oid_array *array,
99 for_each_oid_fn fn,
100 void *data);
103 * Apply the callback function `want` to each entry in the array, retaining
104 * only the entries for which the function returns true. Preserve the order
105 * of the entries that are retained.
107 void oid_array_filter(struct oid_array *array,
108 for_each_oid_fn want,
109 void *cbdata);
112 * Sort the array in order of ascending object id.
114 void oid_array_sort(struct oid_array *array);
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:
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]);
126 * Non-unique iteration can just increment with "i++" to visit each element.
128 static inline size_t oid_array_next_unique(struct oid_array *array, size_t cur)
130 do {
131 cur++;
132 } while (cur < array->nr &&
133 oideq(array->oid + cur, array->oid + cur - 1));
134 return cur;
137 #endif /* OID_ARRAY_H */