Extract project sorting function

This commit is contained in:
mrossinek 2019-01-04 19:19:58 +01:00 committed by Paul Beckingham
parent 7cb341ee05
commit 6a8beed7f5
4 changed files with 59 additions and 61 deletions

View file

@ -112,29 +112,7 @@ int CmdProjects::execute (std::string& output)
// create sorted list of table entries // create sorted list of table entries
std::list <std::pair<std::string, int>> sorted_view; std::list <std::pair<std::string, int>> sorted_view;
for (auto& project : unique) sort_projects (sorted_view, unique);
{
const std::vector <std::string> parents = extractParents (project.first);
if (parents.size ()) {
// if parents exist: store iterator position of last parent
std::list<std::pair<std::string, int>>::iterator parent_pos;
for (auto& parent : parents)
{
parent_pos = std::find_if (sorted_view.begin (), sorted_view.end (),
[&parent](const std::pair<std::string, int>& element) { return element.first == parent; }
);
// if parent does not exist yet: insert into sorted view
if (parent_pos == sorted_view.end()) {
sorted_view.insert (parent_pos, std::make_pair(parent, 1));
}
}
// insert new element below latest parent
sorted_view.insert (++parent_pos, project);
} else {
// if has no parents: simply push to end of list
sorted_view.push_back (project);
}
}
// construct view from sorted list // construct view from sorted list
for (auto& item: sorted_view) { for (auto& item: sorted_view) {

View file

@ -148,53 +148,24 @@ int CmdSummary::execute (std::string& output)
} }
// sort projects into sorted list // sort projects into sorted list
std::list<std::string> sortedProjects; std::list<std::pair<std::string, int>> sortedProjects;
for (auto& i : allProjects) sort_projects (sortedProjects, allProjects);
{
if (showAllProjects || countPending[i.first] > 0)
{
const std::vector <std::string> parents = extractParents (i.first);
if (parents.size ())
{
// if parents exist: store iterator position of last parent
std::list<std::string>::iterator parent_pos;
for (auto& parent : parents)
{
parent_pos = std::find (sortedProjects.begin (), sortedProjects.end (), parent);
// if parent does not exist yet: insert into sorted view
if (parent_pos == sortedProjects.end ())
{
sortedProjects.push_back (parent);
}
}
// insert new element below latest parent
if (parent_pos == sortedProjects.end ()) {
sortedProjects.push_back (i.first);
} else {
sortedProjects.insert (++parent_pos, i.first);
}
} else {
// if has no parents: simply push to end of list
sortedProjects.push_back (i.first);
}
}
}
int barWidth = 30; int barWidth = 30;
// construct view from sorted list // construct view from sorted list
for (auto& i : sortedProjects) for (auto& i : sortedProjects)
{ {
int row = view.addRow (); int row = view.addRow ();
view.set (row, 0, (i == "" view.set (row, 0, (i.first == ""
? "(none)" ? "(none)"
: indentProject (i, " ", '.'))); : indentProject (i.first, " ", '.')));
view.set (row, 1, countPending[i]); view.set (row, 1, countPending[i.first]);
if (counter[i]) if (counter[i.first])
view.set (row, 2, Duration ((int) (sumEntry[i] / (double)counter[i])).formatVague ()); view.set (row, 2, Duration ((int) (sumEntry[i.first] / (double)counter[i.first])).formatVague ());
int c = countCompleted[i]; int c = countCompleted[i.first];
int p = countPending[i]; int p = countPending[i.first];
int completedBar = 0; int completedBar = 0;
if (c + p) if (c + p)
completedBar = (c * barWidth) / (c + p); completedBar = (c * barWidth) / (c + p);

View file

@ -30,6 +30,7 @@
#include <algorithm> #include <algorithm>
#include <string> #include <string>
#include <vector> #include <vector>
#include <list>
#include <map> #include <map>
#include <sys/types.h> #include <sys/types.h>
#include <Context.h> #include <Context.h>
@ -81,6 +82,8 @@ std::string onExpiration (Task&);
// sort.cpp // sort.cpp
void sort_tasks (std::vector <Task>&, std::vector <int>&, const std::string&); void sort_tasks (std::vector <Task>&, std::vector <int>&, const std::string&);
void sort_projects (std::list <std::pair <std::string, int>>& sorted, std::map <std::string, int>& allProjects);
void sort_projects (std::list <std::pair <std::string, int>>& sorted, std::map <std::string, bool>& allProjects);
// legacy.cpp // legacy.cpp
void legacyColumnMap (std::string&); void legacyColumnMap (std::string&);

View file

@ -27,12 +27,15 @@
#include <cmake.h> #include <cmake.h>
#include <algorithm> #include <algorithm>
#include <vector> #include <vector>
#include <list>
#include <map>
#include <string> #include <string>
#include <stdlib.h> #include <stdlib.h>
#include <Context.h> #include <Context.h>
#include <Duration.h> #include <Duration.h>
#include <Task.h> #include <Task.h>
#include <shared.h> #include <shared.h>
#include <util.h>
#include <format.h> #include <format.h>
static std::vector <Task>* global_data = nullptr; static std::vector <Task>* global_data = nullptr;
@ -59,6 +62,49 @@ void sort_tasks (
Context::getContext ().time_sort_us += timer.total_us (); Context::getContext ().time_sort_us += timer.total_us ();
} }
void sort_projects (
std::list <std::pair <std::string, int>>& sorted,
std::map <std::string, int>& allProjects)
{
for (auto& project : allProjects)
{
const std::vector <std::string> parents = extractParents (project.first);
if (parents.size ())
{
// if parents exist: store iterator position of last parent
std::list <std::pair <std::string, int>>::iterator parent_pos;
for (auto& parent : parents)
{
parent_pos = std::find_if (sorted.begin (), sorted.end (),
[&parent](const std::pair <std::string, int>& item) { return item.first == parent; }
);
// if parent does not exist yet: insert into sorted view
if (parent_pos == sorted.end ()) {
sorted.push_back (std::make_pair (parent, 1));
}
}
// insert new element below latest parent
sorted.insert ((parent_pos == sorted.end ()) ? parent_pos : ++parent_pos, project);
} else {
// if has no parents: simply push to end of list
sorted.push_back (project);
}
}
}
void sort_projects (
std::list <std::pair <std::string, int>>& sorted,
std::map <std::string, bool>& allProjects)
{
std::map <std::string, int> allProjectsInt;
for (auto& p : allProjects)
{
allProjectsInt[p.first] = (int) p.second;
}
sort_projects (sorted, allProjectsInt);
}
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
// Re-implementation, using direct Task access instead of data copies that // Re-implementation, using direct Task access instead of data copies that
// require re-parsing. // require re-parsing.