Porytiles
Loading...
Searching...
No Matches
count_map_to_list.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <algorithm>
4#include <map>
5#include <utility>
6#include <vector>
7
8namespace porytiles {
9
22template <typename T>
23std::vector<std::pair<T, unsigned int>> counts_to_descending_list(const std::map<T, unsigned int> &counts)
24{
25 std::vector<std::pair<T, unsigned int>> result;
26 result.reserve(counts.size());
27 for (const auto &[key, count] : counts) {
28 result.emplace_back(key, count);
29 }
30
31 // Sort in descending order by count (second element)
32 std::sort(result.begin(), result.end(), [](const auto &a, const auto &b) { return a.second > b.second; });
33
34 return result;
35}
36
37} // namespace porytiles
std::vector< std::pair< T, unsigned int > > counts_to_descending_list(const std::map< T, unsigned int > &counts)
Converts a map of counts to a sorted vector of key-count pairs.