add length analyzer

This commit is contained in:
Paul Pan 2023-11-03 21:06:09 +08:00
parent 78cd9404c5
commit 3e70825234
3 changed files with 18 additions and 0 deletions

View File

@ -58,6 +58,7 @@ include_directories(password-analyzer PRIVATE ${CMAKE_BINARY_DIR}/external)
set(COMMON_SOURCES set(COMMON_SOURCES
defs.hpp defs.hpp
utils.cpp utils.cpp
stat_length.cpp
) )
set(COMMON_LIBRARIES set(COMMON_LIBRARIES
magic_enum::magic_enum magic_enum::magic_enum

View File

@ -21,4 +21,5 @@ bool is_alpha(char c);
std::generator<std::string> passwords(const DataSource &source); std::generator<std::string> passwords(const DataSource &source);
void stat_length(const DataSource &source);
#endif // DEFS_HPP #endif // DEFS_HPP

16
stat_length.cpp Normal file
View File

@ -0,0 +1,16 @@
#include <algorithm>
#include <map>
#include <vector>
#include "defs.hpp"
void stat_length(const DataSource &source) {
spdlog::info("stat_length({})", magic_enum::enum_name(source));
std::map<size_t, size_t> stat;
for (auto const &password : passwords(source)) ++stat[password.size()];
std::vector<std::pair<size_t, size_t>> vec(stat.begin(), stat.end());
std::sort(vec.begin(), vec.end(), [](auto const &lhs, auto const &rhs) { return lhs.second > rhs.second; });
for (auto const &[length, count] : vec | std::views::take(10)) spdlog::info("{}: {}", length, count);
}