Mercurial > hg > svcore
diff base/EventSeries.cpp @ 1632:0890c10e5129 single-point
Add some more handy methods
author | Chris Cannam |
---|---|
date | Tue, 12 Mar 2019 14:52:11 +0000 |
parents | b2048f350906 |
children | 6ac92836cd86 |
line wrap: on
line diff
--- a/base/EventSeries.cpp Tue Mar 12 14:14:00 2019 +0000 +++ b/base/EventSeries.cpp Tue Mar 12 14:52:11 2019 +0000 @@ -24,7 +24,7 @@ EventSeries::count() const { if (m_events.size() > INT_MAX) { - throw std::runtime_error("too many events"); + throw std::logic_error("too many events"); } return int(m_events.size()); } @@ -271,6 +271,45 @@ return cover; } +bool +EventSeries::getEventPreceding(const Event &e, Event &preceding) const +{ + auto pitr = lower_bound(m_events.begin(), m_events.end(), e); + if (pitr == m_events.end() || *pitr != e) { + return false; + } + if (pitr == m_events.begin()) { + return false; + } + --pitr; + preceding = *pitr; + return true; +} + +bool +EventSeries::getEventFollowing(const Event &e, Event &following) const +{ + auto pitr = lower_bound(m_events.begin(), m_events.end(), e); + if (pitr == m_events.end() || *pitr != e) { + return false; + } + ++pitr; + if (pitr == m_events.end()) { + return false; + } + following = *pitr; + return true; +} + +Event +EventSeries::getEventByIndex(int index) const +{ + if (index < 0 || index >= count()) { + throw std::logic_error("index out of range"); + } + return m_events[index]; +} + void EventSeries::toXml(QTextStream &out, QString indent,