You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
81 lines
2.4 KiB
81 lines
2.4 KiB
8 years ago
|
oid-array API
|
||
14 years ago
|
==============
|
||
|
|
||
8 years ago
|
The oid-array API provides storage and manipulation of sets of object
|
||
14 years ago
|
identifiers. The emphasis is on storage and processing efficiency,
|
||
|
making them suitable for large lists. Note that the ordering of items is
|
||
|
not preserved over some operations.
|
||
|
|
||
|
Data Structures
|
||
|
---------------
|
||
|
|
||
8 years ago
|
`struct oid_array`::
|
||
14 years ago
|
|
||
8 years ago
|
A single array of object IDs. This should be initialized by
|
||
|
assignment from `OID_ARRAY_INIT`. The `oid` member contains
|
||
14 years ago
|
the actual data. The `nr` member contains the number of items in
|
||
|
the set. The `alloc` and `sorted` members are used internally,
|
||
|
and should not be needed by API callers.
|
||
|
|
||
|
Functions
|
||
|
---------
|
||
|
|
||
8 years ago
|
`oid_array_append`::
|
||
|
Add an item to the set. The object ID will be placed at the end of
|
||
14 years ago
|
the array (but note that some operations below may lose this
|
||
|
ordering).
|
||
|
|
||
8 years ago
|
`oid_array_lookup`::
|
||
|
Perform a binary search of the array for a specific object ID.
|
||
14 years ago
|
If found, returns the offset (in number of elements) of the
|
||
8 years ago
|
object ID. If not found, returns a negative integer. If the array
|
||
|
is not sorted, this function has the side effect of sorting it.
|
||
14 years ago
|
|
||
8 years ago
|
`oid_array_clear`::
|
||
14 years ago
|
Free all memory associated with the array and return it to the
|
||
|
initial, empty state.
|
||
|
|
||
8 years ago
|
`oid_array_for_each_unique`::
|
||
14 years ago
|
Efficiently iterate over each unique element of the list,
|
||
|
executing the callback function for each one. If the array is
|
||
8 years ago
|
not sorted, this function has the side effect of sorting it. If
|
||
|
the callback returns a non-zero value, the iteration ends
|
||
|
immediately and the callback's return is propagated; otherwise,
|
||
|
0 is returned.
|
||
14 years ago
|
|
||
|
Examples
|
||
|
--------
|
||
|
|
||
|
-----------------------------------------
|
||
8 years ago
|
int print_callback(const struct object_id *oid,
|
||
14 years ago
|
void *data)
|
||
|
{
|
||
8 years ago
|
printf("%s\n", oid_to_hex(oid));
|
||
8 years ago
|
return 0; /* always continue */
|
||
14 years ago
|
}
|
||
|
|
||
|
void some_func(void)
|
||
|
{
|
||
8 years ago
|
struct sha1_array hashes = OID_ARRAY_INIT;
|
||
|
struct object_id oid;
|
||
14 years ago
|
|
||
|
/* Read objects into our set */
|
||
8 years ago
|
while (read_object_from_stdin(oid.hash))
|
||
|
oid_array_append(&hashes, &oid);
|
||
14 years ago
|
|
||
|
/* Check if some objects are in our set */
|
||
8 years ago
|
while (read_object_from_stdin(oid.hash)) {
|
||
|
if (oid_array_lookup(&hashes, &oid) >= 0)
|
||
14 years ago
|
printf("it's in there!\n");
|
||
|
|
||
|
/*
|
||
|
* Print the unique set of objects. We could also have
|
||
|
* avoided adding duplicate objects in the first place,
|
||
|
* but we would end up re-sorting the array repeatedly.
|
||
|
* Instead, this will sort once and then skip duplicates
|
||
|
* in linear time.
|
||
|
*/
|
||
8 years ago
|
oid_array_for_each_unique(&hashes, print_callback, NULL);
|
||
14 years ago
|
}
|
||
|
-----------------------------------------
|