mirror of
https://github.com/GothenburgBitFactory/timewarrior.git
synced 2025-07-07 20:06:39 +02:00
data: Added merge function for merging ranges
This commit is contained in:
parent
8372feafee
commit
97756a871e
2 changed files with 40 additions and 0 deletions
39
src/data.cpp
39
src/data.cpp
|
@ -30,6 +30,7 @@
|
||||||
#include <Datetime.h>
|
#include <Datetime.h>
|
||||||
#include <Duration.h>
|
#include <Duration.h>
|
||||||
#include <timew.h>
|
#include <timew.h>
|
||||||
|
#include <algorithm>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
@ -430,6 +431,44 @@ std::vector <Interval> flatten (
|
||||||
return all;
|
return all;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
|
// Simply merges a vector of ranges, without data loss.
|
||||||
|
static bool rangeCompare (Range left, Range right)
|
||||||
|
{
|
||||||
|
return left.start < right.start;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::vector <Range> merge (
|
||||||
|
const std::vector <Range>& ranges)
|
||||||
|
{
|
||||||
|
// Short cut.
|
||||||
|
if (ranges.size () < 2)
|
||||||
|
return ranges;
|
||||||
|
|
||||||
|
std::vector <Range> sorted {ranges};
|
||||||
|
std::sort (sorted.begin (), sorted.end (), rangeCompare);
|
||||||
|
|
||||||
|
unsigned int cursor = 0;
|
||||||
|
int merges = 0;
|
||||||
|
for (unsigned int i = 0; i < sorted.size (); ++i)
|
||||||
|
{
|
||||||
|
if (cursor && sorted[cursor - 1].overlap (sorted[i]))
|
||||||
|
{
|
||||||
|
sorted[cursor - 1] = sorted[cursor - 1].combine (sorted[i]);
|
||||||
|
++merges;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
++cursor;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (merges)
|
||||||
|
sorted.resize (ranges.size () - merges);
|
||||||
|
|
||||||
|
return sorted;
|
||||||
|
}
|
||||||
|
|
||||||
////////////////////////////////////////////////////////////////////////////////
|
////////////////////////////////////////////////////////////////////////////////
|
||||||
// Subset both ranges and additions by limits, and combine.
|
// Subset both ranges and additions by limits, and combine.
|
||||||
std::vector <Range> addRanges (
|
std::vector <Range> addRanges (
|
||||||
|
|
|
@ -45,6 +45,7 @@ std::vector <Interval> subset (const Interval&, const std::vector <I
|
||||||
std::vector <Range> subset (const Range&, const std::vector <Range>&);
|
std::vector <Range> subset (const Range&, const std::vector <Range>&);
|
||||||
std::vector <Interval> subset (const Range&, const std::vector <Interval>&);
|
std::vector <Interval> subset (const Range&, const std::vector <Interval>&);
|
||||||
std::vector <Interval> flatten (const Interval&, const std::vector <Range>&);
|
std::vector <Interval> flatten (const Interval&, const std::vector <Range>&);
|
||||||
|
std::vector <Range> merge (const std::vector <Range>&);
|
||||||
std::vector <Range> addRanges (const Range&, const std::vector <Range>&, const std::vector <Range>&);
|
std::vector <Range> addRanges (const Range&, const std::vector <Range>&, const std::vector <Range>&);
|
||||||
std::vector <Range> subtractRanges (const std::vector <Range>&, const std::vector <Range>&);
|
std::vector <Range> subtractRanges (const std::vector <Range>&, const std::vector <Range>&);
|
||||||
Range outerRange (const std::vector <Interval>&);
|
Range outerRange (const std::vector <Interval>&);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue