Skip to content
Snippets Groups Projects
Commit 1b008f81 authored by Mathieu Valois's avatar Mathieu Valois
Browse files

proper reverse order using comparator from STL

parent 4806d489
No related branches found
No related tags found
No related merge requests found
Pipeline #6263 passed
......@@ -18,6 +18,7 @@
#include <fstream>
#include <unordered_map>
#include <iomanip>
#include <utility>
inline float percentage(const float& num, const float& den){
return 100 * num / den;
......@@ -36,11 +37,11 @@ using MapIterator = typename std::map<K, V>::const_iterator;
* @return ordered map
*/
template<typename A>
std::multimap<uint64_t, A> flip_map(const std::unordered_map<A, uint64_t> & src) {
std::multimap<uint64_t, A> dst;
std::map<uint64_t, A, std::greater<uint64_t>> flip_map(const std::unordered_map<A, uint64_t> & src) {
std::map<uint64_t, A, std::greater<uint64_t>> dst;
for(UnorderedMapIterator<A, uint64_t> it = src.begin(); it != src.end(); ++it)
dst.insert(std::pair<uint64_t, A>(it->second, it->first));
dst.insert(std::make_pair(it->second, it->first));
return dst;
}
......@@ -82,18 +83,15 @@ void readResult(const uint64_t & res, const Type& carac, int & count, const uint
template<typename Type>
void showMap(const std::unordered_map<Type, uint64_t> & stats, const int & top, const uint64_t & total_counter, const int & hiderare, int & count) {
count = 0;
std::multimap<uint64_t, Type> reverse = flip_map<Type>(stats);
MapIterator<uint64_t, Type> it;
for(it = reverse.end(); it != reverse.begin(); it--) {
if (it == reverse.end()) continue;
readResult<Type>(it->first, it->second, count, total_counter, hiderare);
std::map<uint64_t, Type, std::greater<uint64_t>> reverse = flip_map<Type>(stats);
std::pair<uint64_t, Type> it;
for(std::pair<uint64_t, Type> it : reverse) {
readResult<Type>(it.first, it.second, count, total_counter, hiderare);
if (top != -1 && count == top) break;
}
if (count != top) {
readResult<Type>(it->first, it->second, count, total_counter, hiderare);
readResult<Type>(it.first, it.second, count, total_counter, hiderare);
}
}
......
......@@ -112,11 +112,6 @@ int Statsgen::generate_stats() {
pthread_t threads[MAX_THREADS];
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
// split stdin into nbThread files on disk
if (is_stdin){
string line;
......@@ -160,7 +155,6 @@ int Statsgen::generate_stats() {
}
void *status;
pthread_attr_destroy(&attr);
for(int i = 0; i < nbThread; i++ ) {
int rc = pthread_join(threads[i], &status);
if (rc) {
......@@ -233,14 +227,13 @@ void Statsgen::print_stats() {
cout << "\nStatistics relative to advancedmask: \n" << endl;
showMap(stats_advancedmasks, top, total_counter, hiderare, count);
if (outfile_name != ""){
if (! outfile_name.empty()){
locale::global(locale("C"));
ofstream outfile_stream(outfile_name);
multimap<uint64_t, string> reverse = flip_map<string>(stats_advancedmasks);
for(auto it=reverse.end();it!=reverse.begin();it--){
if (it == reverse.end()) continue;
if(it->second == "othermasks") continue;
outfile_stream << it->second << "," << it->first << endl;
map<uint64_t, string, greater<uint64_t>> reverse = flip_map(stats_advancedmasks);
for(pair<uint64_t, string> it : reverse){
if(it.second == "othermasks") continue;
outfile_stream << it.second << "," << it.first << endl;
}
outfile_stream.close();
}
......
......@@ -162,24 +162,22 @@ double MainWindow::initGraphicalStats(QBarSeries * barLength, QPieSeries * pieCh
percentageSecurity = percentage(stats.getNbSecurePasswords(), total);
/* LENGTH HISTOGRAM */
multimap<uint64_t, int> reverseL = flip_map<int>(stats.getStatsLength());
map<uint64_t, int, greater<uint64_t>> reverseL = flip_map<int>(stats.getStatsLength());
double percentageL;
double maxPercLength = 0;
uint64_t nbHideL = 0;
barLength->clear();
MapIterator<uint64_t, int> itL;
for(itL = reverseL.end(); itL != reverseL.begin(); itL--) {
if (itL == reverseL.end()) continue;
for(pair<uint64_t, int> itL : reverseL) {
percentageL = percentage(itL->first, total);
percentageL = percentage(itL.first, total);
maxPercLength = percentageL > maxPercLength ? percentageL : maxPercLength;
if (percentageL >= perc_other) {
QBarSet *set = new QBarSet(QString::number(itL->second));
QBarSet *set = new QBarSet(QString::number(itL.second));
*set << percentageL;
barLength->append(set);
} else {
nbHideL += itL->first;
nbHideL += itL.first;
}
}
......@@ -189,55 +187,50 @@ double MainWindow::initGraphicalStats(QBarSeries * barLength, QPieSeries * pieCh
/* CHARSET PIECHART */
multimap<uint64_t, string> reverseC = flip_map<string>(stats.getStatsCharsets());
map<uint64_t, string, greater<uint64_t>> reverseC = flip_map(stats.getStatsCharsets());
uint64_t top = 0;
uint64_t nbHideC = 0;
pieCharset->clear();
MapIterator<uint64_t, string> itC;
for(itC = reverseC.end(); itC != reverseC.begin(); itC--) {
if (itC == reverseC.end()) continue;
for(pair<uint64_t, string> itC : reverseC) {
top++;
if (top <= display_charsets) {
pieCharset->append(QString::fromStdString(itC->second), itC->first);
pieCharset->append(QString::fromStdString(itC.second), itC.first);
} else {
nbHideC += itC->first;
nbHideC += itC.first;
}
}
pieCharset->append("Other charsets", nbHideC);
/* SIMPLE PIECHART */
multimap<uint64_t, string> reverseS = flip_map<string>(stats.getStatsSimple());
map<uint64_t, string, greater<uint64_t>> reverseS = flip_map(stats.getStatsSimple());
uint64_t top_simple = 0;
uint64_t nbHideS = 0;
pieSimple->clear();
MapIterator<uint64_t, string> itS;
for(itS = reverseS.end(); itS != reverseS.begin(); itS--) {
if (itS == reverseS.end()) continue;
for(pair<uint64_t, string> itS : reverseS) {
top_simple++;
if (top_simple <= display_simples) {
pieSimple->append(QString::fromStdString(itS->second), itS->first);
pieSimple->append(QString::fromStdString(itS.second), itS.first);
} else {
nbHideS += itS->first;
nbHideS += itS.first;
}
}
pieSimple->append("Other Masks", nbHideS);
/* ADVANCED PIECHART */
multimap<uint64_t, string> reverseA = flip_map<string>(stats.getStatsAdvanced());
map<uint64_t, string, greater<uint64_t>> reverseA = flip_map(stats.getStatsAdvanced());
uint64_t top_advanced = 0;
uint64_t nbHideA = 0;
pieAdvanced->clear();
MapIterator<uint64_t, string> itA;
for(itA = reverseA.end(); itA != reverseA.begin(); itA--) {
if (itA == reverseA.end()) continue;
for(pair<uint64_t, string> itA : reverseA) {
top_advanced++;
if (top_advanced <= display_advanced) {
pieAdvanced->append(QString::fromStdString(itA->second), itA->first);
pieAdvanced->append(QString::fromStdString(itA.second), itA.first);
} else {
nbHideA += itA->first;
nbHideA += itA.first;
}
}
pieAdvanced->append("Other Masks", nbHideA);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment