🌐 AI搜索 & 代理 主页
blob: c9d0f6d3cc8b99959d8637dcbf8ecb235021104e [file] [log] [blame]
Jeff King29c2bd52017-02-08 20:53:071#ifndef OIDSET_H
2#define OIDSET_H
3
René Scharfe8b2f8cb2018-10-04 15:13:064#include "hashmap.h"
5#include "khash.h"
Jonathan Tan9e6fabde2017-09-29 22:54:226
Jeff King29c2bd52017-02-08 20:53:077/**
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:
10 *
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.
14 *
15 * 2. The per-unique-oid memory footprint is slightly higher due to hash
16 * table overhead.
17 */
18
René Scharfe8b2f8cb2018-10-04 15:13:0619static inline unsigned int oid_hash(struct object_id oid)
20{
21 return sha1hash(oid.hash);
22}
23
24static inline int oid_equal(struct object_id a, struct object_id b)
25{
26 return oideq(&a, &b);
27}
28
29KHASH_INIT(oid, struct object_id, int, 0, oid_hash, oid_equal)
30
Jeff King29c2bd52017-02-08 20:53:0731/**
32 * A single oidset; should be zero-initialized (or use OIDSET_INIT).
33 */
34struct oidset {
René Scharfe8b2f8cb2018-10-04 15:13:0635 kh_oid_t set;
Jeff King29c2bd52017-02-08 20:53:0736};
37
René Scharfe8b2f8cb2018-10-04 15:13:0638#define OIDSET_INIT { { 0 } }
Jeff King29c2bd52017-02-08 20:53:0739
Jeff Hostetlerc3a9ad32017-11-21 20:58:4940
René Scharfe8c84ae62018-10-04 15:14:3741/**
42 * Initialize the oidset structure `set`.
43 *
44 * If `initial_size` is bigger than 0 then preallocate to allow inserting
45 * the specified number of elements without further allocations.
46 */
47void oidset_init(struct oidset *set, size_t initial_size);
Jeff Hostetlerc3a9ad32017-11-21 20:58:4948
Jeff King29c2bd52017-02-08 20:53:0749/**
50 * Returns true iff `set` contains `oid`.
51 */
52int oidset_contains(const struct oidset *set, const struct object_id *oid);
53
54/**
55 * Insert the oid into the set; a copy is made, so "oid" does not need
56 * to persist after this function is called.
57 *
58 * Returns 1 if the oid was already in the set, 0 otherwise. This can be used
59 * to perform an efficient check-and-add.
60 */
61int oidset_insert(struct oidset *set, const struct object_id *oid);
62
63/**
Jeff Hostetlerc3a9ad32017-11-21 20:58:4964 * Remove the oid from the set.
65 *
66 * Returns 1 if the oid was present in the set, 0 otherwise.
67 */
68int oidset_remove(struct oidset *set, const struct object_id *oid);
69
70/**
Jeff King29c2bd52017-02-08 20:53:0771 * Remove all entries from the oidset, freeing any resources associated with
72 * it.
73 */
74void oidset_clear(struct oidset *set);
75
Jeff Hostetlerc3a9ad32017-11-21 20:58:4976struct oidset_iter {
René Scharfe8b2f8cb2018-10-04 15:13:0677 kh_oid_t *set;
78 khiter_t iter;
Jeff Hostetlerc3a9ad32017-11-21 20:58:4979};
80
81static inline void oidset_iter_init(struct oidset *set,
82 struct oidset_iter *iter)
83{
René Scharfe8b2f8cb2018-10-04 15:13:0684 iter->set = &set->set;
85 iter->iter = kh_begin(iter->set);
Jeff Hostetlerc3a9ad32017-11-21 20:58:4986}
87
88static inline struct object_id *oidset_iter_next(struct oidset_iter *iter)
89{
René Scharfe8b2f8cb2018-10-04 15:13:0690 for (; iter->iter != kh_end(iter->set); iter->iter++) {
91 if (kh_exist(iter->set, iter->iter))
92 return &kh_key(iter->set, iter->iter++);
93 }
94 return NULL;
Jeff Hostetlerc3a9ad32017-11-21 20:58:4995}
96
97static inline struct object_id *oidset_iter_first(struct oidset *set,
98 struct oidset_iter *iter)
99{
100 oidset_iter_init(set, iter);
101 return oidset_iter_next(iter);
102}
103
Jeff King29c2bd52017-02-08 20:53:07104#endif /* OIDSET_H */