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.

101 lines
2.6KB

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