Skip to content

Commit

Permalink
Re #7822. Add a method to clear all but the last value.
Browse files Browse the repository at this point in the history
This will modify a TimeSeriesProperty to keep only the last entry. Note
that 'last' means the last added, unless the property has been sorted
first.
  • Loading branch information
RussellTaylor committed Oct 1, 2013
1 parent dfb629c commit 769ff9f
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ class ITimeSeriesProperty
virtual int realSize() const = 0;
/// Deletes the series of values in the property
virtual void clear() = 0;
/// Deletes all but the 'last entry' in the property
virtual void clearOutdated() = 0;

/// Virtual destructor
virtual ~ITimeSeriesProperty() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,8 @@ namespace Mantid

/// Deletes the series of values in the property
void clear();
/// Deletes all but the 'last entry' in the property
void clearOutdated();
/// Clears and creates a TimeSeriesProperty from these parameters
void create(const Kernel::DateAndTime &start_time, const std::vector<double> & time_sec, const std::vector<TYPE> & new_values);
/// Clears and creates a TimeSeriesProperty from these parameters
Expand Down
17 changes: 17 additions & 0 deletions Code/Mantid/Framework/Kernel/src/TimeSeriesProperty.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1068,6 +1068,23 @@ namespace Mantid
m_filterApplied = false;
}

/** Clears out all but the last value in the property.
* The last value is the last entry in the m_values vector - no sorting is
* done or checked for to ensure that the last value is the most recent in time.
* It is up to the client to call sort() first if this is a requirement.
*/
template<typename TYPE>
void TimeSeriesProperty<TYPE>::clearOutdated()
{
if ( realSize() > 1 )
{
auto lastValue = m_values.back();
clear();
m_values.push_back(lastValue);
m_size = 1;
}
}

/**
* Clears and creates a TimeSeriesProperty from these parameters:
* @param start_time :: The reference time as a boost::posix_time::ptime value
Expand Down
35 changes: 33 additions & 2 deletions Code/Mantid/Framework/Kernel/test/TimeSeriesPropertyTest.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "MantidKernel/CPUTimer.h"
#include "algorithm"
#include <vector>
#include <boost/scoped_ptr.hpp>

using namespace Mantid::Kernel;

Expand Down Expand Up @@ -965,19 +966,49 @@ class TimeSeriesPropertyTest : public CxxTest::TestSuite

void test_clear()
{
TimeSeriesProperty<int> * p = new TimeSeriesProperty<int>("aProp");
boost::scoped_ptr<TimeSeriesProperty<int>> p(new TimeSeriesProperty<int>("aProp"));
p->addValue("2007-11-30T16:17:00",1);

TS_ASSERT_EQUALS( p->size(), 1);
TS_ASSERT_EQUALS( p->realSize(), 1);

ITimeSeriesProperty * pi = p;
ITimeSeriesProperty * pi = p.get();
TS_ASSERT_THROWS_NOTHING( pi->clear() );

TS_ASSERT_EQUALS( p->size(), 0);
TS_ASSERT_EQUALS( p->realSize(), 0);
}

void test_clearOutdated()
{
boost::scoped_ptr<TimeSeriesProperty<int>> p(new TimeSeriesProperty<int>("aProp"));
p->addValue("2007-11-30T16:17:00",99);

ITimeSeriesProperty * pi = p.get();
TS_ASSERT_THROWS_NOTHING( pi->clearOutdated() );
// No change
TS_ASSERT_EQUALS( p->size(), 1);
TS_ASSERT_EQUALS( p->realSize(), 1);
TS_ASSERT_EQUALS( p->lastValue(), 99);

DateAndTime t("2007-11-30T15:17:00");
p->addValue(t,88);
TS_ASSERT_EQUALS( p->size(), 2);

TS_ASSERT_THROWS_NOTHING( pi->clearOutdated() );
TS_ASSERT_EQUALS( p->size(), 1);
TS_ASSERT_EQUALS( p->realSize(), 1);
// Note that it kept the last-added entry even though its time is earlier
TS_ASSERT_EQUALS( p->lastTime(), t);
TS_ASSERT_EQUALS( p->firstValue(), 88);

TimeSeriesProperty<double> pp("empty");
TS_ASSERT_THROWS_NOTHING( pp.clearOutdated() );
// No change
TS_ASSERT_EQUALS( pp.size(), 0);
TS_ASSERT_EQUALS( pp.realSize(), 0);
}

/*
* Test 2 create() functions by creating 3 properties in different approaches.
*/
Expand Down

0 comments on commit 769ff9f

Please sign in to comment.