Permalink
Browse files

Extract project sorting function

  • Loading branch information...
mrossinek authored and pbeckingham committed Jan 4, 2019
1 parent 7cb341e commit 6a8beed7f5348f1344c4fe549d9cebbf53870132
Showing with 59 additions and 61 deletions.
  1. +1 −23 src/commands/CmdProjects.cpp
  2. +9 −38 src/commands/CmdSummary.cpp
  3. +3 −0 src/main.h
  4. +46 −0 src/sort.cpp
@@ -112,29 +112,7 @@ int CmdProjects::execute (std::string& output)

// create sorted list of table entries
std::list <std::pair<std::string, int>> sorted_view;
for (auto& project : 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);
}
}
sort_projects (sorted_view, unique);

// construct view from sorted list
for (auto& item: sorted_view) {
@@ -148,53 +148,24 @@ int CmdSummary::execute (std::string& output)
}

// sort projects into sorted list
std::list<std::string> sortedProjects;
for (auto& i : 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);
}
}
}
std::list<std::pair<std::string, int>> sortedProjects;
sort_projects (sortedProjects, allProjects);

int barWidth = 30;
// construct view from sorted list
for (auto& i : sortedProjects)
{
int row = view.addRow ();
view.set (row, 0, (i == ""
view.set (row, 0, (i.first == ""
? "(none)"
: indentProject (i, " ", '.')));
: indentProject (i.first, " ", '.')));

view.set (row, 1, countPending[i]);
if (counter[i])
view.set (row, 2, Duration ((int) (sumEntry[i] / (double)counter[i])).formatVague ());
view.set (row, 1, countPending[i.first]);
if (counter[i.first])
view.set (row, 2, Duration ((int) (sumEntry[i.first] / (double)counter[i.first])).formatVague ());

int c = countCompleted[i];
int p = countPending[i];
int c = countCompleted[i.first];
int p = countPending[i.first];
int completedBar = 0;
if (c + p)
completedBar = (c * barWidth) / (c + p);
@@ -30,6 +30,7 @@
#include <algorithm>
#include <string>
#include <vector>
#include <list>
#include <map>
#include <sys/types.h>
#include <Context.h>
@@ -81,6 +82,8 @@ std::string onExpiration (Task&);

// sort.cpp
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
void legacyColumnMap (std::string&);
@@ -27,12 +27,15 @@
#include <cmake.h>
#include <algorithm>
#include <vector>
#include <list>
#include <map>
#include <string>
#include <stdlib.h>
#include <Context.h>
#include <Duration.h>
#include <Task.h>
#include <shared.h>
#include <util.h>
#include <format.h>

static std::vector <Task>* global_data = nullptr;
@@ -59,6 +62,49 @@ void sort_tasks (
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
// require re-parsing.

0 comments on commit 6a8beed

Please sign in to comment.