3 ms·
This is true, but your second point is difficult to do in C. You would have to have a set template and a separate lookup template to search the set based on a k
by ludocode 6y ago
This is true, but your second point is difficult to do in C. You would have to have a set template and a separate lookup template to search the set based on a key type. This is actually how heterogeneous lookup is implemented in C++: it's a template within a template. This is not a big deal in C++ because templates are instantiated automatically but we're instantiating templates manually in C. It's also only available as of C++14 and only on ordered containers, not hash tables.
It's more straightforward to combine these templates: make map the fundamental type, but make values contain their keys and make the key type default to the value type if omitted. This way it's both a map and a set. My own C hash table template does it this way [1]: it stores only a value type and not keys, but there is an optional separate key type used for lookups, hashes and comparisons. To make it a map, you provide both a key comparison expression and an expression to convert values to keys.
[1]: https://github.com/ludocode/pottery/tree/master/include/pottery/open_hash_map https://github.com/ludocode/pottery/tree/master/include/pott...