git@vger.kernel.org list mirror (unofficial, one of many)
 help / color / mirror / code / Atom feed
blob 3c5ae4ae0aecb33323792d4a6778fb7f7c7ac1e2 2908 bytes (raw)
name: prefix-map.c 	 # note: path name is non-authoritative(*)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
 
#include "cache.h"
#include "prefix-map.h"

static int map_cmp(const void *unused_cmp_data,
		   const void *entry,
		   const void *entry_or_key,
		   const void *unused_keydata)
{
	const struct prefix_map_entry *a = entry;
	const struct prefix_map_entry *b = entry_or_key;

	return a->prefix_length != b->prefix_length ||
		strncmp(a->name, b->name, a->prefix_length);
}

static void add_prefix_entry(struct hashmap *map, const char *name,
			     size_t prefix_length, struct prefix_item *item)
{
	struct prefix_map_entry *result = xmalloc(sizeof(*result));
	result->name = name;
	result->prefix_length = prefix_length;
	result->item = item;
	hashmap_entry_init(result, memhash(name, prefix_length));
	hashmap_add(map, result);
}

static void init_prefix_map(struct prefix_map *prefix_map,
			    int min_prefix_length, int max_prefix_length)
{
	hashmap_init(&prefix_map->map, map_cmp, NULL, 0);
	prefix_map->min_length = min_prefix_length;
	prefix_map->max_length = max_prefix_length;
}

static void add_prefix_item(struct prefix_map *prefix_map,
			    struct prefix_item *item)
{
	struct prefix_map_entry *e = xmalloc(sizeof(*e)), *e2;
	int j;

	e->item = item;
	e->name = e->item->name;

	for (j = prefix_map->min_length; j <= prefix_map->max_length; j++) {
		if (!isascii(e->name[j])) {
			free(e);
			break;
		}

		e->prefix_length = j;
		hashmap_entry_init(e, memhash(e->name, j));
		e2 = hashmap_get(&prefix_map->map, e, NULL);
		if (!e2) {
			/* prefix is unique so far */
			e->item->prefix_length = j;
			hashmap_add(&prefix_map->map, e);
			break;
		}

		if (!e2->item)
			continue; /* non-unique prefix */

		if (j != e2->item->prefix_length)
			BUG("unexpected prefix length: %d != %d",
			    (int)j, (int)e2->item->prefix_length);

		/* skip common prefix */
		for (; j < prefix_map->max_length && e->name[j]; j++) {
			if (e->item->name[j] != e2->item->name[j])
				break;
			add_prefix_entry(&prefix_map->map, e->name, j + 1,
					 NULL);
		}

		/* e2 no longer refers to a unique prefix */
		if (j < prefix_map->max_length && e2->name[j]) {
			/* found a new unique prefix for e2's item */
			e2->item->prefix_length = j + 1;
			add_prefix_entry(&prefix_map->map, e2->name, j + 1,
					 e2->item);
		}
		else
			e2->item->prefix_length = 0;
		e2->item = NULL;

		if (j < prefix_map->max_length && e->name[j]) {
			/* found a unique prefix for the item */
			e->item->prefix_length = j + 1;
			add_prefix_entry(&prefix_map->map, e->name, j + 1,
					 e->item);
		} else {
			/* item has no (short enough) unique prefix */
			e->item->prefix_length = 0;
			free(e);
		}

		break;
	}
}

void find_unique_prefixes(struct prefix_item **list, size_t nr,
			  int min_length, int max_length)
{
	int i;
	struct prefix_map prefix_map;

	init_prefix_map(&prefix_map, min_length, max_length);
	for (i = 0; i < nr; i++)
		add_prefix_item(&prefix_map, list[i]);
	hashmap_free(&prefix_map.map, 1);
}

debug log:

solving 3c5ae4ae0a ...
found 3c5ae4ae0a in https://public-inbox.org/git/db1ede363645b0620d4924639efe5ec708441aa7.1554917868.git.gitgitgadget@gmail.com/

applying [1/1] https://public-inbox.org/git/db1ede363645b0620d4924639efe5ec708441aa7.1554917868.git.gitgitgadget@gmail.com/
diff --git a/prefix-map.c b/prefix-map.c
new file mode 100644
index 0000000000..3c5ae4ae0a

Checking patch prefix-map.c...
Applied patch prefix-map.c cleanly.

index at:
100644 3c5ae4ae0aecb33323792d4a6778fb7f7c7ac1e2	prefix-map.c

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).