"Slavica Djukic via GitGitGadget" <gitgitgadget@xxxxxxxxx> writes: > 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). OK. I suppose such a machinery that accepts a set of strings and then give us back a set of unique prefix length for each element of the set would be useful in general, even outside the "and choose" part of "list and choose". Nice design. > 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. Somehow the above repeatedly says how special purpose it is redundantly ;-) > diff --git a/prefix-map.h b/prefix-map.h > new file mode 100644 > index 0000000000..ce3b8a4a32 > --- /dev/null > +++ b/prefix-map.h > @@ -0,0 +1,40 @@ > +#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 Looks like a quite sane interface to me. Thanks.