]> git.ipfire.org Git - thirdparty/git.git/blob - sha1-array.c
Merge branch 'jc/log-quiet-fix'
[thirdparty/git.git] / sha1-array.c
1 #include "cache.h"
2 #include "sha1-array.h"
3 #include "sha1-lookup.h"
4
5 void sha1_array_append(struct sha1_array *array, const unsigned char *sha1)
6 {
7 ALLOC_GROW(array->sha1, array->nr + 1, array->alloc);
8 hashcpy(array->sha1[array->nr++], sha1);
9 array->sorted = 0;
10 }
11
12 static int void_hashcmp(const void *a, const void *b)
13 {
14 return hashcmp(a, b);
15 }
16
17 void sha1_array_sort(struct sha1_array *array)
18 {
19 qsort(array->sha1, array->nr, sizeof(*array->sha1), void_hashcmp);
20 array->sorted = 1;
21 }
22
23 static const unsigned char *sha1_access(size_t index, void *table)
24 {
25 unsigned char (*array)[20] = table;
26 return array[index];
27 }
28
29 int sha1_array_lookup(struct sha1_array *array, const unsigned char *sha1)
30 {
31 if (!array->sorted)
32 sha1_array_sort(array);
33 return sha1_pos(sha1, array->sha1, array->nr, sha1_access);
34 }
35
36 void sha1_array_clear(struct sha1_array *array)
37 {
38 free(array->sha1);
39 array->sha1 = NULL;
40 array->nr = 0;
41 array->alloc = 0;
42 array->sorted = 0;
43 }
44
45 void sha1_array_for_each_unique(struct sha1_array *array,
46 for_each_sha1_fn fn,
47 void *data)
48 {
49 int i;
50
51 if (!array->sorted)
52 sha1_array_sort(array);
53
54 for (i = 0; i < array->nr; i++) {
55 if (i > 0 && !hashcmp(array->sha1[i], array->sha1[i-1]))
56 continue;
57 fn(array->sha1[i], data);
58 }
59 }