mirror of
https://github.com/git/git.git
synced 2025-12-12 20:36:24 +01:00
decorate: clean up and document API
Improve the names of the identifiers in decorate.h, document them, and add an example of how to use these functions. The example is compiled and run as part of the test suite. Signed-off-by: Jonathan Tan <jonathantanmy@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:
committed by
Junio C Hamano
parent
95ec6b1b33
commit
ddd3e31242
28
decorate.c
28
decorate.c
@@ -14,20 +14,20 @@ static unsigned int hash_obj(const struct object *obj, unsigned int n)
|
||||
static void *insert_decoration(struct decoration *n, const struct object *base, void *decoration)
|
||||
{
|
||||
int size = n->size;
|
||||
struct object_decoration *hash = n->hash;
|
||||
struct decoration_entry *entries = n->entries;
|
||||
unsigned int j = hash_obj(base, size);
|
||||
|
||||
while (hash[j].base) {
|
||||
if (hash[j].base == base) {
|
||||
void *old = hash[j].decoration;
|
||||
hash[j].decoration = decoration;
|
||||
while (entries[j].base) {
|
||||
if (entries[j].base == base) {
|
||||
void *old = entries[j].decoration;
|
||||
entries[j].decoration = decoration;
|
||||
return old;
|
||||
}
|
||||
if (++j >= size)
|
||||
j = 0;
|
||||
}
|
||||
hash[j].base = base;
|
||||
hash[j].decoration = decoration;
|
||||
entries[j].base = base;
|
||||
entries[j].decoration = decoration;
|
||||
n->nr++;
|
||||
return NULL;
|
||||
}
|
||||
@@ -36,24 +36,23 @@ static void grow_decoration(struct decoration *n)
|
||||
{
|
||||
int i;
|
||||
int old_size = n->size;
|
||||
struct object_decoration *old_hash = n->hash;
|
||||
struct decoration_entry *old_entries = n->entries;
|
||||
|
||||
n->size = (old_size + 1000) * 3 / 2;
|
||||
n->hash = xcalloc(n->size, sizeof(struct object_decoration));
|
||||
n->entries = xcalloc(n->size, sizeof(struct decoration_entry));
|
||||
n->nr = 0;
|
||||
|
||||
for (i = 0; i < old_size; i++) {
|
||||
const struct object *base = old_hash[i].base;
|
||||
void *decoration = old_hash[i].decoration;
|
||||
const struct object *base = old_entries[i].base;
|
||||
void *decoration = old_entries[i].decoration;
|
||||
|
||||
if (!decoration)
|
||||
continue;
|
||||
insert_decoration(n, base, decoration);
|
||||
}
|
||||
free(old_hash);
|
||||
free(old_entries);
|
||||
}
|
||||
|
||||
/* Add a decoration pointer, return any old one */
|
||||
void *add_decoration(struct decoration *n, const struct object *obj,
|
||||
void *decoration)
|
||||
{
|
||||
@@ -64,7 +63,6 @@ void *add_decoration(struct decoration *n, const struct object *obj,
|
||||
return insert_decoration(n, obj, decoration);
|
||||
}
|
||||
|
||||
/* Lookup a decoration pointer */
|
||||
void *lookup_decoration(struct decoration *n, const struct object *obj)
|
||||
{
|
||||
unsigned int j;
|
||||
@@ -74,7 +72,7 @@ void *lookup_decoration(struct decoration *n, const struct object *obj)
|
||||
return NULL;
|
||||
j = hash_obj(obj, n->size);
|
||||
for (;;) {
|
||||
struct object_decoration *ref = n->hash + j;
|
||||
struct decoration_entry *ref = n->entries + j;
|
||||
if (ref->base == obj)
|
||||
return ref->decoration;
|
||||
if (!ref->base)
|
||||
|
||||
Reference in New Issue
Block a user