l10n: zh_CN: for git v2.33.0 l10n round 2
[alt-git.git] / oidtree.c
blob580cab8ae27e08285d8a57cf55d95660072dcd53
1 /*
2 * A wrapper around cbtree which stores oids
3 * May be used to replace oid-array for prefix (abbreviation) matches
4 */
5 #include "oidtree.h"
6 #include "alloc.h"
7 #include "hash.h"
9 struct oidtree_iter_data {
10 oidtree_iter fn;
11 void *arg;
12 size_t *last_nibble_at;
13 int algo;
14 uint8_t last_byte;
17 void oidtree_init(struct oidtree *ot)
19 cb_init(&ot->tree);
20 mem_pool_init(&ot->mem_pool, 0);
23 void oidtree_clear(struct oidtree *ot)
25 if (ot) {
26 mem_pool_discard(&ot->mem_pool, 0);
27 oidtree_init(ot);
31 void oidtree_insert(struct oidtree *ot, const struct object_id *oid)
33 struct cb_node *on;
35 if (!oid->algo)
36 BUG("oidtree_insert requires oid->algo");
38 on = mem_pool_alloc(&ot->mem_pool, sizeof(*on) + sizeof(*oid));
39 oidcpy_with_padding((struct object_id *)on->k, oid);
42 * n.b. Current callers won't get us duplicates, here. If a
43 * future caller causes duplicates, there'll be a a small leak
44 * that won't be freed until oidtree_clear. Currently it's not
45 * worth maintaining a free list
47 cb_insert(&ot->tree, on, sizeof(*oid));
51 int oidtree_contains(struct oidtree *ot, const struct object_id *oid)
53 struct object_id k;
54 size_t klen = sizeof(k);
56 oidcpy_with_padding(&k, oid);
58 if (oid->algo == GIT_HASH_UNKNOWN)
59 klen -= sizeof(oid->algo);
61 /* cb_lookup relies on memcmp on the struct, so order matters: */
62 klen += BUILD_ASSERT_OR_ZERO(offsetof(struct object_id, hash) <
63 offsetof(struct object_id, algo));
65 return cb_lookup(&ot->tree, (const uint8_t *)&k, klen) ? 1 : 0;
68 static enum cb_next iter(struct cb_node *n, void *arg)
70 struct oidtree_iter_data *x = arg;
71 const struct object_id *oid = (const struct object_id *)n->k;
73 if (x->algo != GIT_HASH_UNKNOWN && x->algo != oid->algo)
74 return CB_CONTINUE;
76 if (x->last_nibble_at) {
77 if ((oid->hash[*x->last_nibble_at] ^ x->last_byte) & 0xf0)
78 return CB_CONTINUE;
81 return x->fn(oid, x->arg);
84 void oidtree_each(struct oidtree *ot, const struct object_id *oid,
85 size_t oidhexsz, oidtree_iter fn, void *arg)
87 size_t klen = oidhexsz / 2;
88 struct oidtree_iter_data x = { 0 };
89 assert(oidhexsz <= GIT_MAX_HEXSZ);
91 x.fn = fn;
92 x.arg = arg;
93 x.algo = oid->algo;
94 if (oidhexsz & 1) {
95 x.last_byte = oid->hash[klen];
96 x.last_nibble_at = &klen;
98 cb_each(&ot->tree, (const uint8_t *)oid, klen, iter, &x);