- Migrated util.cpp compressIds to CmdIDs::compressIds, as this is the only
  place it is used.
This commit is contained in:
Paul Beckingham 2014-09-07 16:54:59 -04:00
parent 8a31ccabdc
commit dd6399aba7
2 changed files with 58 additions and 1 deletions

View file

@ -31,7 +31,6 @@
#include <Filter.h> #include <Filter.h>
#include <main.h> #include <main.h>
#include <text.h> #include <text.h>
#include <util.h>
#include <i18n.h> #include <i18n.h>
#include <CmdIDs.h> #include <CmdIDs.h>
@ -73,6 +72,61 @@ int CmdIDs::execute (std::string& output)
return 0; return 0;
} }
////////////////////////////////////////////////////////////////////////////////
// The vector must be sorted first. This is a modified version of the run-
// length encoding algorithm.
//
// This function converts the vector:
//
// [1, 3, 4, 6, 7, 8, 9, 11]
//
// to ths string:
//
// 1,3-4,6-9,11
//
std::string CmdIDs::compressIds (const std::vector <int>& ids)
{
std::stringstream result;
int range_start = 0;
int range_end = 0;
for (unsigned int i = 0; i < ids.size (); ++i)
{
if (i + 1 == ids.size ())
{
if (result.str ().length ())
result << ",";
if (range_start < range_end)
result << ids[range_start] << "-" << ids[range_end];
else
result << ids[range_start];
}
else
{
if (ids[range_end] + 1 == ids[i + 1])
{
++range_end;
}
else
{
if (result.str ().length ())
result << ",";
if (range_start < range_end)
result << ids[range_start] << "-" << ids[range_end];
else
result << ids[range_start];
range_start = range_end = i + 1;
}
}
}
return result.str ();
}
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////
CmdCompletionIds::CmdCompletionIds () CmdCompletionIds::CmdCompletionIds ()
{ {

View file

@ -35,6 +35,9 @@ class CmdIDs : public Command
public: public:
CmdIDs (); CmdIDs ();
int execute (std::string&); int execute (std::string&);
private:
std::string compressIds (const std::vector <int>&);
}; };
class CmdCompletionIds : public Command class CmdCompletionIds : public Command