oidset.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. #ifndef OIDSET_H
  2. #define OIDSET_H
  3. #include "khash.h"
  4. /**
  5. * This API is similar to oid-array, in that it maintains a set of object ids
  6. * in a memory-efficient way. The major differences are:
  7. *
  8. * 1. It uses a hash, so we can do online duplicate removal, rather than
  9. * sort-and-uniq at the end. This can reduce memory footprint if you have
  10. * a large list of oids with many duplicates.
  11. *
  12. * 2. The per-unique-oid memory footprint is slightly higher due to hash
  13. * table overhead.
  14. */
  15. /**
  16. * A single oidset; should be zero-initialized (or use OIDSET_INIT).
  17. */
  18. struct oidset {
  19. kh_oid_set_t set;
  20. };
  21. #define OIDSET_INIT { { 0 } }
  22. /**
  23. * Initialize the oidset structure `set`.
  24. *
  25. * If `initial_size` is bigger than 0 then preallocate to allow inserting
  26. * the specified number of elements without further allocations.
  27. */
  28. void oidset_init(struct oidset *set, size_t initial_size);
  29. /**
  30. * Returns true iff `set` contains `oid`.
  31. */
  32. int oidset_contains(const struct oidset *set, const struct object_id *oid);
  33. /**
  34. * Insert the oid into the set; a copy is made, so "oid" does not need
  35. * to persist after this function is called.
  36. *
  37. * Returns 1 if the oid was already in the set, 0 otherwise. This can be used
  38. * to perform an efficient check-and-add.
  39. */
  40. int oidset_insert(struct oidset *set, const struct object_id *oid);
  41. /**
  42. * Remove the oid from the set.
  43. *
  44. * Returns 1 if the oid was present in the set, 0 otherwise.
  45. */
  46. int oidset_remove(struct oidset *set, const struct object_id *oid);
  47. /**
  48. * Returns the number of oids in the set.
  49. */
  50. int oidset_size(struct oidset *set);
  51. /**
  52. * Remove all entries from the oidset, freeing any resources associated with
  53. * it.
  54. */
  55. void oidset_clear(struct oidset *set);
  56. /**
  57. * Add the contents of the file 'path' to an initialized oidset. Each line is
  58. * an unabbreviated object name. Comments begin with '#', and trailing comments
  59. * are allowed. Leading whitespace and empty or white-space only lines are
  60. * ignored.
  61. */
  62. void oidset_parse_file(struct oidset *set, const char *path);
  63. /*
  64. * Similar to the above, but with a callback which can (1) return non-zero to
  65. * signal displeasure with the object and (2) replace object ID with something
  66. * else (meant to be used to "peel").
  67. */
  68. typedef int (*oidset_parse_tweak_fn)(struct object_id *, void *);
  69. void oidset_parse_file_carefully(struct oidset *set, const char *path,
  70. oidset_parse_tweak_fn fn, void *cbdata);
  71. struct oidset_iter {
  72. kh_oid_set_t *set;
  73. khiter_t iter;
  74. };
  75. static inline void oidset_iter_init(struct oidset *set,
  76. struct oidset_iter *iter)
  77. {
  78. iter->set = &set->set;
  79. iter->iter = kh_begin(iter->set);
  80. }
  81. static inline struct object_id *oidset_iter_next(struct oidset_iter *iter)
  82. {
  83. for (; iter->iter != kh_end(iter->set); iter->iter++) {
  84. if (kh_exist(iter->set, iter->iter))
  85. return &kh_key(iter->set, iter->iter++);
  86. }
  87. return NULL;
  88. }
  89. static inline struct object_id *oidset_iter_first(struct oidset *set,
  90. struct oidset_iter *iter)
  91. {
  92. oidset_iter_init(set, iter);
  93. return oidset_iter_next(iter);
  94. }
  95. #endif /* OIDSET_H */