1
0
Fork 0
mirror of https://github.com/NixOS/nix synced 2025-07-05 08:11:47 +02:00

Use transparent comparators for std::set<std::string> (NFC)

This patch finally applies the transition to std::less<>,
which is a transparent comparator. There's no functional
change and string lookups in sets are now more efficient
and don't produce temporaries (e.g. set.find(std::string_view{"key"})).
This commit is contained in:
Sergei Zimmerman 2025-05-02 17:43:02 +00:00
parent 5278cd2396
commit ebb836d499
No known key found for this signature in database
GPG key ID: A9B0B557CA632325
12 changed files with 66 additions and 40 deletions

View file

@ -90,8 +90,8 @@ struct json_avoids_null<std::vector<T>> : std::true_type {};
template<typename T>
struct json_avoids_null<std::list<T>> : std::true_type {};
template<typename T>
struct json_avoids_null<std::set<T>> : std::true_type {};
template<typename T, typename Compare>
struct json_avoids_null<std::set<T, Compare>> : std::true_type {};
template<typename K, typename V>
struct json_avoids_null<std::map<K, V>> : std::true_type {};

View file

@ -5,13 +5,13 @@
namespace nix {
template<typename T>
std::vector<T> topoSort(std::set<T> items,
std::function<std::set<T>(const T &)> getChildren,
template<typename T, typename Compare>
std::vector<T> topoSort(std::set<T, Compare> items,
std::function<std::set<T, Compare>(const T &)> getChildren,
std::function<Error(const T &, const T &)> makeCycleError)
{
std::vector<T> sorted;
std::set<T> visited, parents;
decltype(items) visited, parents;
std::function<void(const T & path, const T * parent)> dfsVisit;
@ -21,7 +21,7 @@ std::vector<T> topoSort(std::set<T> items,
if (!visited.insert(path).second) return;
parents.insert(path);
std::set<T> references = getChildren(path);
auto references = getChildren(path);
for (auto & i : references)
/* Don't traverse into items that don't exist in our starting set. */

View file

@ -12,17 +12,35 @@
namespace nix {
typedef std::list<std::string> Strings;
typedef std::set<std::string> StringSet;
typedef std::map<std::string, std::string> StringMap;
typedef std::map<std::string, std::string> StringPairs;
/**
* Alias to ordered set container with transparent comparator.
*
* Used instead of std::set<std::string> to use C++14 N3657 [1]
* heterogenous lookup consistently across the whole codebase.
* Transparent comparators get rid of creation of unnecessary
* temporary variables when looking up keys by `std::string_view`
* or C-style `const char *` strings.
*
* [1]: https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3657.htm
*/
using StringSet = std::set<std::string, std::less<>>;
/**
* Paths are just strings.
*/
typedef std::string Path;
typedef std::string_view PathView;
typedef std::list<Path> Paths;
typedef std::set<Path> PathSet;
/**
* Alias to an ordered set of `Path`s. Uses transparent comparator.
*
* @see StringSet
*/
using PathSet = std::set<Path, std::less<>>;
typedef std::vector<std::pair<std::string, std::string>> Headers;