use COPY_ARRAY for copying arrays
[git/raj.git] / oidset.h
blob14f18f791fea19301a41b650b860b0b432241e7a
1 #ifndef OIDSET_H
2 #define OIDSET_H
4 #include "hashmap.h"
5 #include "khash.h"
7 /**
8 * This API is similar to sha1-array, in that it maintains a set of object ids
9 * in a memory-efficient way. The major differences are:
11 * 1. It uses a hash, so we can do online duplicate removal, rather than
12 * sort-and-uniq at the end. This can reduce memory footprint if you have
13 * a large list of oids with many duplicates.
15 * 2. The per-unique-oid memory footprint is slightly higher due to hash
16 * table overhead.
19 /**
20 * A single oidset; should be zero-initialized (or use OIDSET_INIT).
22 struct oidset {
23 kh_oid_t set;
26 #define OIDSET_INIT { { 0 } }
29 /**
30 * Initialize the oidset structure `set`.
32 * If `initial_size` is bigger than 0 then preallocate to allow inserting
33 * the specified number of elements without further allocations.
35 void oidset_init(struct oidset *set, size_t initial_size);
37 /**
38 * Returns true iff `set` contains `oid`.
40 int oidset_contains(const struct oidset *set, const struct object_id *oid);
42 /**
43 * Insert the oid into the set; a copy is made, so "oid" does not need
44 * to persist after this function is called.
46 * Returns 1 if the oid was already in the set, 0 otherwise. This can be used
47 * to perform an efficient check-and-add.
49 int oidset_insert(struct oidset *set, const struct object_id *oid);
51 /**
52 * Remove the oid from the set.
54 * Returns 1 if the oid was present in the set, 0 otherwise.
56 int oidset_remove(struct oidset *set, const struct object_id *oid);
58 /**
59 * Remove all entries from the oidset, freeing any resources associated with
60 * it.
62 void oidset_clear(struct oidset *set);
64 struct oidset_iter {
65 kh_oid_t *set;
66 khiter_t iter;
69 static inline void oidset_iter_init(struct oidset *set,
70 struct oidset_iter *iter)
72 iter->set = &set->set;
73 iter->iter = kh_begin(iter->set);
76 static inline struct object_id *oidset_iter_next(struct oidset_iter *iter)
78 for (; iter->iter != kh_end(iter->set); iter->iter++) {
79 if (kh_exist(iter->set, iter->iter))
80 return &kh_key(iter->set, iter->iter++);
82 return NULL;
85 static inline struct object_id *oidset_iter_first(struct oidset *set,
86 struct oidset_iter *iter)
88 oidset_iter_init(set, iter);
89 return oidset_iter_next(iter);
92 #endif /* OIDSET_H */