|
| 1 | +#include "cache.h" |
| 2 | +#include "prefix-map.h" |
| 3 | + |
| 4 | +static int map_cmp(const void *unused_cmp_data, |
| 5 | + const void *entry, |
| 6 | + const void *entry_or_key, |
| 7 | + const void *unused_keydata) |
| 8 | +{ |
| 9 | + const struct prefix_map_entry *a = entry; |
| 10 | + const struct prefix_map_entry *b = entry_or_key; |
| 11 | + |
| 12 | + return a->prefix_length != b->prefix_length || |
| 13 | + strncmp(a->name, b->name, a->prefix_length); |
| 14 | +} |
| 15 | + |
| 16 | +static void add_prefix_entry(struct hashmap *map, const char *name, |
| 17 | + size_t prefix_length, struct prefix_item *item) |
| 18 | +{ |
| 19 | + struct prefix_map_entry *result = xmalloc(sizeof(*result)); |
| 20 | + result->name = name; |
| 21 | + result->prefix_length = prefix_length; |
| 22 | + result->item = item; |
| 23 | + hashmap_entry_init(result, memhash(name, prefix_length)); |
| 24 | + hashmap_add(map, result); |
| 25 | +} |
| 26 | + |
| 27 | +static void init_prefix_map(struct prefix_map *prefix_map, |
| 28 | + int min_prefix_length, int max_prefix_length) |
| 29 | +{ |
| 30 | + hashmap_init(&prefix_map->map, map_cmp, NULL, 0); |
| 31 | + prefix_map->min_length = min_prefix_length; |
| 32 | + prefix_map->max_length = max_prefix_length; |
| 33 | +} |
| 34 | + |
| 35 | +static void add_prefix_item(struct prefix_map *prefix_map, |
| 36 | + struct prefix_item *item) |
| 37 | +{ |
| 38 | + struct prefix_map_entry *e = xmalloc(sizeof(*e)), *e2; |
| 39 | + int j; |
| 40 | + |
| 41 | + e->item = item; |
| 42 | + e->name = e->item->name; |
| 43 | + |
| 44 | + for (j = prefix_map->min_length; j <= prefix_map->max_length; j++) { |
| 45 | + if (!isascii(e->name[j])) { |
| 46 | + free(e); |
| 47 | + break; |
| 48 | + } |
| 49 | + |
| 50 | + e->prefix_length = j; |
| 51 | + hashmap_entry_init(e, memhash(e->name, j)); |
| 52 | + e2 = hashmap_get(&prefix_map->map, e, NULL); |
| 53 | + if (!e2) { |
| 54 | + /* prefix is unique so far */ |
| 55 | + e->item->prefix_length = j; |
| 56 | + hashmap_add(&prefix_map->map, e); |
| 57 | + break; |
| 58 | + } |
| 59 | + |
| 60 | + if (!e2->item) |
| 61 | + continue; /* non-unique prefix */ |
| 62 | + |
| 63 | + if (j != e2->item->prefix_length) |
| 64 | + BUG("unexpected prefix length: %d != %d", |
| 65 | + (int)j, (int)e2->item->prefix_length); |
| 66 | + |
| 67 | + /* skip common prefix */ |
| 68 | + for (; j < prefix_map->max_length && e->name[j]; j++) { |
| 69 | + if (e->item->name[j] != e2->item->name[j]) |
| 70 | + break; |
| 71 | + add_prefix_entry(&prefix_map->map, e->name, j + 1, |
| 72 | + NULL); |
| 73 | + } |
| 74 | + |
| 75 | + /* e2 no longer refers to a unique prefix */ |
| 76 | + if (j < prefix_map->max_length && e2->name[j]) { |
| 77 | + /* found a new unique prefix for e2's item */ |
| 78 | + e2->item->prefix_length = j + 1; |
| 79 | + add_prefix_entry(&prefix_map->map, e2->name, j + 1, |
| 80 | + e2->item); |
| 81 | + } |
| 82 | + else |
| 83 | + e2->item->prefix_length = 0; |
| 84 | + e2->item = NULL; |
| 85 | + |
| 86 | + if (j < prefix_map->max_length && e->name[j]) { |
| 87 | + /* found a unique prefix for the item */ |
| 88 | + e->item->prefix_length = j + 1; |
| 89 | + add_prefix_entry(&prefix_map->map, e->name, j + 1, |
| 90 | + e->item); |
| 91 | + } else { |
| 92 | + /* item has no (short enough) unique prefix */ |
| 93 | + e->item->prefix_length = 0; |
| 94 | + free(e); |
| 95 | + } |
| 96 | + |
| 97 | + break; |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +void find_unique_prefixes(struct prefix_item **list, size_t nr, |
| 102 | + int min_length, int max_length) |
| 103 | +{ |
| 104 | + int i; |
| 105 | + struct prefix_map prefix_map; |
| 106 | + |
| 107 | + init_prefix_map(&prefix_map, min_length, max_length); |
| 108 | + for (i = 0; i < nr; i++) |
| 109 | + add_prefix_item(&prefix_map, list[i]); |
| 110 | + hashmap_free(&prefix_map.map, 1); |
| 111 | +} |
0 commit comments