mirror of
https://github.com/git-for-windows/git.git
synced 2026-03-18 07:33:26 -05:00
In the `git add -i` command, we show unique prefixes of the commands and files, to give an indication what prefix would select them. Naturally, the C implementation looks a lot different than the Perl implementation: in Perl, a trie is much easier implemented, while we already have a pretty neat hashmap implementation in C that we use for the purpose of storing (not necessarily unique) prefixes. The idea: for each item that we add, we generate prefixes starting with the first letter, then the first two letters, then three, etc, until we find a prefix that is unique (or until the prefix length would be longer than we want). If we encounter a previously-unique prefix on the way, we adjust that item's prefix to make it unique again (or we mark it as having no unique prefix if we failed to find one). These partial prefixes are stored in a hash map (for quick lookup times). To make sure that this function works as expected, we add a test using a special-purpose test helper that was added for that purpose. Note: We expect the list of prefix items to be passed in as a list of pointers rather than as regular list to avoid having to copy information (the actual items will most likely contain more information than just the name and the length of the unique prefix, but passing in `struct prefix_item *` would not allow for that). Signed-off-by: Slavica Djukic <slawica92@hotmail.com> Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
41 lines
944 B
C
41 lines
944 B
C
#ifndef PREFIX_MAP_H
|
|
#define PREFIX_MAP_H
|
|
|
|
#include "hashmap.h"
|
|
|
|
struct prefix_item {
|
|
const char *name;
|
|
size_t prefix_length;
|
|
};
|
|
|
|
struct prefix_map_entry {
|
|
struct hashmap_entry e;
|
|
const char *name;
|
|
size_t prefix_length;
|
|
/* if item is NULL, the prefix is not unique */
|
|
struct prefix_item *item;
|
|
};
|
|
|
|
struct prefix_map {
|
|
struct hashmap map;
|
|
int min_length, max_length;
|
|
};
|
|
|
|
/*
|
|
* Find unique prefixes in a given list of strings.
|
|
*
|
|
* Typically, the `struct prefix_item` information will be but a field in the
|
|
* actual item struct; For this reason, the `list` parameter is specified as a
|
|
* list of pointers to the items.
|
|
*
|
|
* The `min_length`/`max_length` parameters define what length the unique
|
|
* prefixes should have.
|
|
*
|
|
* If no unique prefix could be found for a given item, its `prefix_length`
|
|
* will be set to 0.
|
|
*/
|
|
void find_unique_prefixes(struct prefix_item **list, size_t nr,
|
|
int min_length, int max_length);
|
|
|
|
#endif
|