sha1-array.con commit Merge branch 'bc/doc-interpret-trailers-grammofix' into maint (dfc20a5)
   1#include "cache.h"
   2#include "sha1-array.h"
   3#include "sha1-lookup.h"
   4
   5void oid_array_append(struct oid_array *array, const struct object_id *oid)
   6{
   7        ALLOC_GROW(array->oid, array->nr + 1, array->alloc);
   8        oidcpy(&array->oid[array->nr++], oid);
   9        array->sorted = 0;
  10}
  11
  12static int void_hashcmp(const void *a, const void *b)
  13{
  14        return oidcmp(a, b);
  15}
  16
  17static void oid_array_sort(struct oid_array *array)
  18{
  19        QSORT(array->oid, array->nr, void_hashcmp);
  20        array->sorted = 1;
  21}
  22
  23static const unsigned char *sha1_access(size_t index, void *table)
  24{
  25        struct object_id *array = table;
  26        return array[index].hash;
  27}
  28
  29int oid_array_lookup(struct oid_array *array, const struct object_id *oid)
  30{
  31        if (!array->sorted)
  32                oid_array_sort(array);
  33        return sha1_pos(oid->hash, array->oid, array->nr, sha1_access);
  34}
  35
  36void oid_array_clear(struct oid_array *array)
  37{
  38        FREE_AND_NULL(array->oid);
  39        array->nr = 0;
  40        array->alloc = 0;
  41        array->sorted = 0;
  42}
  43
  44int oid_array_for_each_unique(struct oid_array *array,
  45                                for_each_oid_fn fn,
  46                                void *data)
  47{
  48        int i;
  49
  50        if (!array->sorted)
  51                oid_array_sort(array);
  52
  53        for (i = 0; i < array->nr; i++) {
  54                int ret;
  55                if (i > 0 && !oidcmp(array->oid + i, array->oid + i - 1))
  56                        continue;
  57                ret = fn(array->oid + i, data);
  58                if (ret)
  59                        return ret;
  60        }
  61        return 0;
  62}