THIS IS A TEST INSTANCE ONLY! REPOSITORIES CAN BE DELETED AT ANY TIME!

Git Source Code Mirror - This is a publish-only repository and all pull requests are ignored. Please follow Documentation/SubmittingPatches procedure for any of your improvements.
git
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.

97 lines
1.9KB

  1. #include "cache.h"
  2. #include "sha1-array.h"
  3. #include "sha1-lookup.h"
  4. void oid_array_append(struct oid_array *array, const struct object_id *oid)
  5. {
  6. ALLOC_GROW(array->oid, array->nr + 1, array->alloc);
  7. oidcpy(&array->oid[array->nr++], oid);
  8. array->sorted = 0;
  9. }
  10. static int void_hashcmp(const void *a, const void *b)
  11. {
  12. return oidcmp(a, b);
  13. }
  14. static void oid_array_sort(struct oid_array *array)
  15. {
  16. QSORT(array->oid, array->nr, void_hashcmp);
  17. array->sorted = 1;
  18. }
  19. static const unsigned char *sha1_access(size_t index, void *table)
  20. {
  21. struct object_id *array = table;
  22. return array[index].hash;
  23. }
  24. int oid_array_lookup(struct oid_array *array, const struct object_id *oid)
  25. {
  26. if (!array->sorted)
  27. oid_array_sort(array);
  28. return sha1_pos(oid->hash, array->oid, array->nr, sha1_access);
  29. }
  30. void oid_array_clear(struct oid_array *array)
  31. {
  32. FREE_AND_NULL(array->oid);
  33. array->nr = 0;
  34. array->alloc = 0;
  35. array->sorted = 0;
  36. }
  37. int oid_array_for_each(struct oid_array *array,
  38. for_each_oid_fn fn,
  39. void *data)
  40. {
  41. int i;
  42. /* No oid_array_sort() here! See the api-oid-array.txt docs! */
  43. for (i = 0; i < array->nr; i++) {
  44. int ret = fn(array->oid + i, data);
  45. if (ret)
  46. return ret;
  47. }
  48. return 0;
  49. }
  50. int oid_array_for_each_unique(struct oid_array *array,
  51. for_each_oid_fn fn,
  52. void *data)
  53. {
  54. int i;
  55. if (!array->sorted)
  56. oid_array_sort(array);
  57. for (i = 0; i < array->nr; i++) {
  58. int ret;
  59. if (i > 0 && oideq(array->oid + i, array->oid + i - 1))
  60. continue;
  61. ret = fn(array->oid + i, data);
  62. if (ret)
  63. return ret;
  64. }
  65. return 0;
  66. }
  67. void oid_array_filter(struct oid_array *array,
  68. for_each_oid_fn want,
  69. void *cb_data)
  70. {
  71. unsigned nr = array->nr, src, dst;
  72. struct object_id *oids = array->oid;
  73. for (src = dst = 0; src < nr; src++) {
  74. if (want(&oids[src], cb_data)) {
  75. if (src != dst)
  76. oidcpy(&oids[dst], &oids[src]);
  77. dst++;
  78. }
  79. }
  80. array->nr = dst;
  81. }