annotate base/EventSeries.h @ 1654:26aa42fd60e9 single-point

Add overspill to events-within search
author Chris Cannam
date Wed, 20 Mar 2019 11:12:54 +0000
parents eaad70939848
children e4084bc60fe8
rev   line source
Chris@1612 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@1612 2
Chris@1612 3 /*
Chris@1612 4 Sonic Visualiser
Chris@1612 5 An audio file viewer and annotation editor.
Chris@1612 6 Centre for Digital Music, Queen Mary, University of London.
Chris@1612 7
Chris@1612 8 This program is free software; you can redistribute it and/or
Chris@1612 9 modify it under the terms of the GNU General Public License as
Chris@1612 10 published by the Free Software Foundation; either version 2 of the
Chris@1612 11 License, or (at your option) any later version. See the file
Chris@1612 12 COPYING included with this distribution for more information.
Chris@1612 13 */
Chris@1612 14
Chris@1615 15 #ifndef SV_EVENT_SERIES_H
Chris@1615 16 #define SV_EVENT_SERIES_H
Chris@1612 17
Chris@1615 18 #include "Event.h"
Chris@1631 19 #include "XmlExportable.h"
Chris@1612 20
Chris@1612 21 #include <set>
Chris@1612 22
Chris@1615 23 //#define DEBUG_EVENT_SERIES 1
Chris@1614 24
Chris@1615 25 /**
Chris@1615 26 * Container storing a series of events, with or without durations,
Chris@1616 27 * and supporting the ability to query which events are active at a
Chris@1616 28 * given frame or within a span of frames.
Chris@1616 29 *
Chris@1616 30 * To that end, in addition to the series of events, it stores a
Chris@1616 31 * series of "seams", which are frame positions at which the set of
Chris@1616 32 * simultaneous events changes (i.e. an event of non-zero duration
Chris@1616 33 * starts or ends) associated with a set of the events that are active
Chris@1616 34 * at or from that position. These are updated when an event is added
Chris@1616 35 * or removed.
Chris@1618 36 *
Chris@1632 37 * This class is highly optimised for inserting events in increasing
Chris@1632 38 * order of start frame. Inserting (or deleting) events in the middle
Chris@1632 39 * does work, and should be acceptable in interactive use, but it is
Chris@1632 40 * very slow in bulk.
Chris@1615 41 */
Chris@1631 42 class EventSeries : public XmlExportable
Chris@1612 43 {
Chris@1612 44 public:
Chris@1640 45 EventSeries() : m_finalDurationlessEventFrame(0) { }
Chris@1616 46 ~EventSeries() =default;
Chris@1616 47
Chris@1616 48 EventSeries(const EventSeries &) =default;
Chris@1616 49 EventSeries &operator=(const EventSeries &) =default;
Chris@1616 50 EventSeries &operator=(EventSeries &&) =default;
Chris@1616 51
Chris@1616 52 bool operator==(const EventSeries &other) const {
Chris@1616 53 return m_events == other.m_events;
Chris@1616 54 }
Chris@1612 55
Chris@1631 56 void clear();
Chris@1632 57 void add(const Event &e);
Chris@1632 58 void remove(const Event &e);
Chris@1632 59 bool contains(const Event &e) const;
Chris@1631 60 bool isEmpty() const;
Chris@1631 61 int count() const;
Chris@1612 62
Chris@1612 63 /**
Chris@1640 64 * Return the frame of the first event in the series. If there are
Chris@1640 65 * no events, return 0.
Chris@1640 66 */
Chris@1640 67 sv_frame_t getStartFrame() const;
Chris@1640 68
Chris@1640 69 /**
Chris@1640 70 * Return the frame plus duration of the event in the series that
Chris@1640 71 * ends last. If there are no events, return 0.
Chris@1640 72 */
Chris@1640 73 sv_frame_t getEndFrame() const;
Chris@1640 74
Chris@1640 75 /**
Chris@1635 76 * Retrieve all events any part of which falls within the range in
Chris@1616 77 * frames defined by the given frame f and duration d.
Chris@1616 78 *
Chris@1635 79 * - An event without duration is spanned by the range if its own
Chris@1635 80 * frame is greater than or equal to f and less than f + d.
Chris@1616 81 *
Chris@1635 82 * - An event with duration is spanned by the range if its start
Chris@1635 83 * frame is less than f + d and its start frame plus its duration
Chris@1635 84 * is greater than f.
Chris@1616 85 *
Chris@1619 86 * Note: Passing a duration of zero is seldom useful here; you
Chris@1619 87 * probably want getEventsCovering instead. getEventsSpanning(f,
Chris@1619 88 * 0) is not equivalent to getEventsCovering(f). The latter
Chris@1619 89 * includes durationless events at f and events starting at f,
Chris@1619 90 * both of which are excluded from the former.
Chris@1616 91 */
Chris@1617 92 EventVector getEventsSpanning(sv_frame_t frame,
Chris@1631 93 sv_frame_t duration) const;
Chris@1616 94
Chris@1616 95 /**
Chris@1635 96 * Retrieve all events falling wholly within the range in frames
Chris@1635 97 * defined by the given frame f and duration d.
Chris@1635 98 *
Chris@1635 99 * - An event without duration is within the range if its own
Chris@1635 100 * frame is greater than or equal to f and less than f + d.
Chris@1635 101 *
Chris@1635 102 * - An event with duration is within the range if its start frame
Chris@1635 103 * is greater than or equal to f and its start frame plus its
Chris@1635 104 * duration is less than or equal to f + d.
Chris@1654 105 *
Chris@1654 106 * If overspill is greater than zero, also include that number of
Chris@1654 107 * additional events (where they exist) both before and after the
Chris@1654 108 * edges of the range.
Chris@1635 109 */
Chris@1635 110 EventVector getEventsWithin(sv_frame_t frame,
Chris@1654 111 sv_frame_t duration,
Chris@1654 112 int overspill = 0) const;
Chris@1635 113
Chris@1635 114 /**
Chris@1638 115 * Retrieve all events starting within the range in frames defined
Chris@1638 116 * by the given frame f and duration d.
Chris@1638 117 *
Chris@1638 118 * - An event without duration starts within the range if its own
Chris@1638 119 * frame is greater than or equal to f and less than f + d.
Chris@1638 120 *
Chris@1638 121 * - An event with duration starts within the range if its start
Chris@1638 122 * frame is greater than or equal to f.
Chris@1638 123 */
Chris@1638 124 EventVector getEventsStartingWithin(sv_frame_t frame,
Chris@1638 125 sv_frame_t duration) const;
Chris@1638 126
Chris@1638 127 /**
Chris@1616 128 * Retrieve all events that cover the given frame. An event without
Chris@1616 129 * duration covers a frame if its own frame is equal to it. An event
Chris@1616 130 * with duration covers a frame if its start frame is less than or
Chris@1612 131 * equal to it and its end frame (start + duration) is greater
Chris@1612 132 * than it.
Chris@1612 133 */
Chris@1631 134 EventVector getEventsCovering(sv_frame_t frame) const;
Chris@1644 135
Chris@1644 136 /**
Chris@1644 137 * Retrieve all events, in their natural order.
Chris@1644 138 */
Chris@1644 139 EventVector getAllEvents() const;
Chris@1635 140
Chris@1631 141 /**
Chris@1632 142 * If e is in the series and is not the first event in it, set
Chris@1632 143 * preceding to the event immediate preceding it according to the
Chris@1632 144 * standard event ordering and return true. Otherwise leave
Chris@1632 145 * preceding unchanged and return false.
Chris@1632 146 *
Chris@1632 147 * If there are multiple events identical to e in the series,
Chris@1632 148 * assume that the event passed in is the first one (i.e. never
Chris@1632 149 * set preceding equal to e).
Chris@1633 150 *
Chris@1633 151 * It is acceptable for preceding to alias e when this is called.
Chris@1632 152 */
Chris@1632 153 bool getEventPreceding(const Event &e, Event &preceding) const;
Chris@1632 154
Chris@1632 155 /**
Chris@1632 156 * If e is in the series and is not the final event in it, set
Chris@1632 157 * following to the event immediate following it according to the
Chris@1632 158 * standard event ordering and return true. Otherwise leave
Chris@1632 159 * following unchanged and return false.
Chris@1632 160 *
Chris@1632 161 * If there are multiple events identical to e in the series,
Chris@1632 162 * assume that the event passed in is the last one (i.e. never set
Chris@1632 163 * following equal to e).
Chris@1633 164 *
Chris@1633 165 * It is acceptable for following to alias e when this is called.
Chris@1632 166 */
Chris@1632 167 bool getEventFollowing(const Event &e, Event &following) const;
Chris@1632 168
Chris@1653 169 enum Direction {
Chris@1653 170 Forward,
Chris@1653 171 Backward
Chris@1653 172 };
Chris@1653 173
Chris@1653 174 /**
Chris@1653 175 * Return the first event for which the given predicate returns
Chris@1653 176 * true, searching events with start frames increasingly far from
Chris@1653 177 * the given frame in the given direction. If the direction is
Chris@1653 178 * Forward then the search includes events starting at the given
Chris@1653 179 * frame, otherwise it does not.
Chris@1653 180 */
Chris@1653 181 bool getNearestEventMatching(sv_frame_t startSearchAt,
Chris@1653 182 std::function<bool(const Event &)> predicate,
Chris@1653 183 Direction direction,
Chris@1653 184 Event &found) const;
Chris@1653 185
Chris@1632 186 /**
Chris@1632 187 * Return the event at the given numerical index in the series,
Chris@1632 188 * where 0 = the first event and count()-1 = the last.
Chris@1632 189 */
Chris@1632 190 Event getEventByIndex(int index) const;
Chris@1640 191
Chris@1640 192 /**
Chris@1640 193 * Return the index of the first event in the series that does not
Chris@1640 194 * compare inferior to the given event. If there is no such event,
Chris@1640 195 * return count().
Chris@1640 196 */
Chris@1640 197 int getIndexForEvent(const Event &e) const;
Chris@1653 198
Chris@1632 199 /**
Chris@1631 200 * Emit to XML as a dataset element.
Chris@1631 201 */
Chris@1631 202 void toXml(QTextStream &out,
Chris@1631 203 QString indent,
Chris@1631 204 QString extraAttributes) const override;
Chris@1631 205
Chris@1612 206 private:
Chris@1616 207 /**
Chris@1631 208 * This vector contains all events in the series, in the normal
Chris@1631 209 * sort order. For backward compatibility we must support series
Chris@1631 210 * containing multiple instances of identical events, so
Chris@1631 211 * consecutive events in this vector will not always be distinct.
Chris@1631 212 * The vector is used in preference to a multiset or map<Event,
Chris@1631 213 * int> in order to allow indexing by "row number" as well as by
Chris@1631 214 * properties such as frame.
Chris@1631 215 *
Chris@1631 216 * Because events are immutable, we do not have to worry about the
Chris@1631 217 * order changing once an event is inserted - we only add or
Chris@1631 218 * delete them.
Chris@1616 219 */
Chris@1631 220 typedef std::vector<Event> Events;
Chris@1616 221 Events m_events;
Chris@1631 222
Chris@1616 223 /**
Chris@1616 224 * The FrameEventMap maps from frame number to a set of events. In
Chris@1616 225 * the seam map this is used to represent the events that are
Chris@1616 226 * active at that frame, either because they begin at that frame
Chris@1616 227 * or because they are continuing from an earlier frame. There is
Chris@1616 228 * an entry here for each frame at which an event starts or ends,
Chris@1616 229 * with the event appearing in all entries from its start time
Chris@1616 230 * onward and disappearing again at its end frame.
Chris@1616 231 *
Chris@1616 232 * Only events with duration appear in this map; point events
Chris@1627 233 * appear only in m_events. Note that unlike m_events, we only
Chris@1627 234 * store one instance of each event here, even if we hold many -
Chris@1627 235 * we refer back to m_events when we need to know how many
Chris@1627 236 * identical copies of a given event we have.
Chris@1616 237 */
Chris@1627 238 typedef std::map<sv_frame_t, std::vector<Event>> FrameEventMap;
Chris@1616 239 FrameEventMap m_seams;
Chris@1614 240
Chris@1640 241 /**
Chris@1640 242 * The frame of the last durationless event we have in the series.
Chris@1640 243 * This is to support a fast-ish getEndFrame(): we can easily keep
Chris@1640 244 * this up-to-date when events are added or removed, and we can
Chris@1640 245 * easily find the end frame of the last with-duration event from
Chris@1640 246 * the seam map, but it's not so easy to continuously update an
Chris@1640 247 * overall end frame or to find the last frame of all events
Chris@1640 248 * without this.
Chris@1640 249 */
Chris@1640 250 sv_frame_t m_finalDurationlessEventFrame;
Chris@1640 251
Chris@1614 252 /** Create a seam at the given frame, copying from the prior seam
Chris@1614 253 * if there is one. If a seam already exists at the given frame,
Chris@1614 254 * leave it untouched.
Chris@1614 255 */
Chris@1614 256 void createSeam(sv_frame_t frame) {
Chris@1625 257 auto itr = m_seams.lower_bound(frame);
Chris@1625 258 if (itr == m_seams.end() || itr->first > frame) {
Chris@1614 259 if (itr != m_seams.begin()) {
Chris@1614 260 --itr;
Chris@1614 261 }
Chris@1614 262 }
Chris@1614 263 if (itr == m_seams.end()) {
Chris@1614 264 m_seams[frame] = {};
Chris@1625 265 } else if (itr->first < frame) {
Chris@1625 266 m_seams[frame] = itr->second;
Chris@1625 267 } else if (itr->first > frame) { // itr must be begin()
Chris@1614 268 m_seams[frame] = {};
Chris@1614 269 }
Chris@1614 270 }
Chris@1614 271
Chris@1627 272 bool seamsEqual(const std::vector<Event> &s1,
Chris@1627 273 const std::vector<Event> &s2) const {
Chris@1627 274
Chris@1627 275 if (s1.size() != s2.size()) {
Chris@1627 276 return false;
Chris@1627 277 }
Chris@1627 278
Chris@1627 279 // precondition: no event appears more than once in s1 or more
Chris@1627 280 // than once in s2
Chris@1627 281
Chris@1627 282 #ifdef DEBUG_EVENT_SERIES
Chris@1627 283 for (int i = 0; in_range_for(s1, i); ++i) {
Chris@1627 284 for (int j = i + 1; in_range_for(s1, j); ++j) {
Chris@1627 285 if (s1[i] == s1[j] || s2[i] == s2[j]) {
Chris@1627 286 throw std::runtime_error
Chris@1627 287 ("debug error: duplicate event in s1 or s2");
Chris@1627 288 }
Chris@1627 289 }
Chris@1627 290 }
Chris@1627 291 #endif
Chris@1627 292
Chris@1627 293 std::set<Event> ee;
Chris@1627 294 for (const auto &e: s1) {
Chris@1627 295 ee.insert(e);
Chris@1627 296 }
Chris@1627 297 for (const auto &e: s2) {
Chris@1627 298 if (ee.find(e) == ee.end()) {
Chris@1627 299 return false;
Chris@1627 300 }
Chris@1627 301 }
Chris@1627 302 return true;
Chris@1627 303 }
Chris@1627 304
Chris@1615 305 #ifdef DEBUG_EVENT_SERIES
Chris@1615 306 void dumpEvents() const {
Chris@1618 307 std::cerr << "EVENTS (" << m_events.size() << ") [" << std::endl;
Chris@1616 308 for (const auto &i: m_events) {
Chris@1631 309 std::cerr << " " << i.toXmlString();
Chris@1614 310 }
Chris@1614 311 std::cerr << "]" << std::endl;
Chris@1614 312 }
Chris@1614 313
Chris@1614 314 void dumpSeams() const {
Chris@1618 315 std::cerr << "SEAMS (" << m_seams.size() << ") [" << std::endl;
Chris@1614 316 for (const auto &s: m_seams) {
Chris@1614 317 std::cerr << " " << s.first << " -> {" << std::endl;
Chris@1614 318 for (const auto &p: s.second) {
Chris@1614 319 std::cerr << p.toXmlString(" ");
Chris@1614 320 }
Chris@1614 321 std::cerr << " }" << std::endl;
Chris@1614 322 }
Chris@1614 323 std::cerr << "]" << std::endl;
Chris@1614 324 }
Chris@1614 325 #endif
Chris@1612 326 };
Chris@1612 327
Chris@1612 328 #endif