interplane.Get Omega
Read in your language

git

[Ported from github.com/git/git] Sync with Git 2.56.1

Brought by drakestapleton

branch master Branches Tags History

git / oidset.h

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