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
24template <typename T>
25std::vector<std::pair<T, unsigned int>> counts_to_descending_list(const std::map<T, unsigned int> &counts)
26{
27 std::vector<std::pair<T, unsigned int>> result;
28 result.reserve(counts.size());
29 for (const auto &[key, count] : counts) {
30 result.emplace_back(key, count);
31 }
32
33 // Sort in descending order by count (second element)
34 std::sort(result.begin(), result.end(), [](const auto &a, const auto &b) { return a.second > b.second; });
35
36 return result;
37}
38
39} // 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.