annotate base/EventSeries.h @ 1616:de446dd905e6 single-point

Rework EventSeries to explicitly store counts of events (+ add comments explaining, among other things, why)
author Chris Cannam
date Fri, 08 Mar 2019 10:16:12 +0000
parents 24dc8cb42755
children bdc19a09a1f9
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@1612 19
Chris@1612 20 #include <set>
Chris@1612 21
Chris@1615 22 //#define DEBUG_EVENT_SERIES 1
Chris@1614 23
Chris@1615 24 /**
Chris@1615 25 * Container storing a series of events, with or without durations,
Chris@1616 26 * and supporting the ability to query which events are active at a
Chris@1616 27 * given frame or within a span of frames.
Chris@1616 28 *
Chris@1616 29 * To that end, in addition to the series of events, it stores a
Chris@1616 30 * series of "seams", which are frame positions at which the set of
Chris@1616 31 * simultaneous events changes (i.e. an event of non-zero duration
Chris@1616 32 * starts or ends) associated with a set of the events that are active
Chris@1616 33 * at or from that position. These are updated when an event is added
Chris@1616 34 * or removed.
Chris@1615 35 */
Chris@1615 36 class EventSeries
Chris@1612 37 {
Chris@1612 38 public:
Chris@1615 39 EventSeries() : m_count(0) { }
Chris@1616 40 ~EventSeries() =default;
Chris@1616 41
Chris@1616 42 EventSeries(const EventSeries &) =default;
Chris@1616 43 EventSeries &operator=(const EventSeries &) =default;
Chris@1616 44 EventSeries &operator=(EventSeries &&) =default;
Chris@1616 45
Chris@1616 46 bool operator==(const EventSeries &other) const {
Chris@1616 47 return m_events == other.m_events;
Chris@1616 48 }
Chris@1612 49
Chris@1615 50 void add(const Event &p) {
Chris@1612 51
Chris@1616 52 ++m_events[p];
Chris@1612 53 ++m_count;
Chris@1612 54
Chris@1615 55 if (p.hasDuration()) {
Chris@1612 56 sv_frame_t frame = p.getFrame();
Chris@1612 57 sv_frame_t endFrame = p.getFrame() + p.getDuration();
Chris@1612 58
Chris@1614 59 createSeam(frame);
Chris@1614 60 createSeam(endFrame);
Chris@1616 61
Chris@1616 62 // These calls must both succeed after calling createSeam above
Chris@1616 63 const auto i0 = m_seams.find(frame);
Chris@1616 64 const auto i1 = m_seams.find(endFrame);
Chris@1614 65
Chris@1614 66 for (auto i = i0; i != i1; ++i) {
Chris@1614 67 if (i == m_seams.end()) {
Chris@1615 68 SVCERR << "ERROR: EventSeries::add: "
Chris@1614 69 << "reached end of seam map"
Chris@1614 70 << endl;
Chris@1614 71 break;
Chris@1612 72 }
Chris@1614 73 i->second.insert(p);
Chris@1612 74 }
Chris@1614 75 }
Chris@1612 76
Chris@1615 77 #ifdef DEBUG_EVENT_SERIES
Chris@1614 78 std::cerr << "after add:" << std::endl;
Chris@1615 79 dumpEvents();
Chris@1614 80 dumpSeams();
Chris@1614 81 #endif
Chris@1612 82 }
Chris@1612 83
Chris@1615 84 void remove(const Event &p) {
Chris@1612 85
Chris@1616 86 // If we are removing the last (unique) example of an event,
Chris@1616 87 // then we also need to remove it from the seam map. If this
Chris@1616 88 // is only one of multiple identical events, then we don't.
Chris@1616 89 bool isUnique = false;
Chris@1616 90
Chris@1615 91 auto pitr = m_events.find(p);
Chris@1615 92 if (pitr == m_events.end()) {
Chris@1615 93 return; // we don't know this event
Chris@1612 94 } else {
Chris@1616 95 if (--(pitr->second) == 0) {
Chris@1616 96 isUnique = true;
Chris@1616 97 m_events.erase(pitr);
Chris@1616 98 }
Chris@1612 99 --m_count;
Chris@1612 100 }
Chris@1612 101
Chris@1616 102 if (p.hasDuration() && isUnique) {
Chris@1616 103
Chris@1612 104 sv_frame_t frame = p.getFrame();
Chris@1612 105 sv_frame_t endFrame = p.getFrame() + p.getDuration();
Chris@1612 106
Chris@1616 107 const auto i0 = m_seams.find(frame);
Chris@1616 108 const auto i1 = m_seams.find(endFrame);
Chris@1614 109
Chris@1615 110 #ifdef DEBUG_EVENT_SERIES
Chris@1615 111 // This should be impossible if we found p in m_events above
Chris@1614 112 if (i0 == m_seams.end() || i1 == m_seams.end()) {
Chris@1615 113 SVCERR << "ERROR: EventSeries::remove: either frame " << frame
Chris@1614 114 << " or endFrame " << endFrame
Chris@1615 115 << " for event not found in seam map: event is "
Chris@1612 116 << p.toXmlString() << endl;
Chris@1612 117 }
Chris@1614 118 #endif
Chris@1612 119
Chris@1614 120 for (auto i = i0; i != i1; ++i) {
Chris@1614 121 if (i == m_seams.end()) {
Chris@1615 122 // This can happen only if we have a negative
Chris@1616 123 // duration, which Event forbids
Chris@1616 124 throw std::logic_error("unexpectedly reached end of map");
Chris@1616 125 }
Chris@1616 126
Chris@1616 127 i->second.erase(p);
Chris@1616 128 }
Chris@1616 129
Chris@1616 130 // Tidy up by removing any entries that are now identical
Chris@1616 131 // to their predecessors
Chris@1616 132
Chris@1616 133 std::vector<sv_frame_t> redundant;
Chris@1616 134
Chris@1616 135 auto pitr = m_seams.end();
Chris@1616 136 if (i0 != m_seams.begin()) {
Chris@1616 137 pitr = i0;
Chris@1616 138 --pitr;
Chris@1616 139 }
Chris@1616 140
Chris@1616 141 for (auto i = i0; i != m_seams.end(); ++i) {
Chris@1616 142 if (pitr != m_seams.end() && i->second == pitr->second) {
Chris@1616 143 redundant.push_back(i->first);
Chris@1616 144 }
Chris@1616 145 pitr = i;
Chris@1616 146 if (i == i1) {
Chris@1614 147 break;
Chris@1614 148 }
Chris@1612 149 }
Chris@1612 150
Chris@1616 151 for (sv_frame_t f: redundant) {
Chris@1616 152 m_seams.erase(f);
Chris@1616 153 }
Chris@1612 154 }
Chris@1614 155
Chris@1615 156 #ifdef DEBUG_EVENT_SERIES
Chris@1614 157 std::cerr << "after remove:" << std::endl;
Chris@1615 158 dumpEvents();
Chris@1614 159 dumpSeams();
Chris@1614 160 #endif
Chris@1612 161 }
Chris@1612 162
Chris@1615 163 bool contains(const Event &p) {
Chris@1615 164 return m_events.find(p) != m_events.end();
Chris@1612 165 }
Chris@1612 166
Chris@1612 167 int count() const {
Chris@1612 168 return m_count;
Chris@1612 169 }
Chris@1612 170
Chris@1612 171 bool isEmpty() const {
Chris@1612 172 return m_count == 0;
Chris@1612 173 }
Chris@1612 174
Chris@1612 175 void clear() {
Chris@1615 176 m_events.clear();
Chris@1612 177 m_seams.clear();
Chris@1612 178 m_count = 0;
Chris@1612 179 }
Chris@1612 180
Chris@1612 181 /**
Chris@1616 182 * Retrieve all events any part of which falls within the span in
Chris@1616 183 * frames defined by the given frame f and duration d.
Chris@1616 184 *
Chris@1616 185 * - An event without duration is within the span if its own frame
Chris@1616 186 * is greater than or equal to f and less than f + d.
Chris@1616 187 *
Chris@1616 188 * - An event with duration is within the span if its start frame
Chris@1616 189 * is less than f + d and its start frame plus its duration is
Chris@1616 190 * greater than f.
Chris@1616 191 *
Chris@1616 192 * Note that getEventsSpanning(f, 0) is not equivalent to
Chris@1616 193 * getEventsCovering(f) - they have different behaviour in the
Chris@1616 194 * case of events starting exactly at f, which are included in the
Chris@1616 195 * latter but not the former.
Chris@1616 196 */
Chris@1616 197 EventVector getEventsSpanning(sv_frame_t f, sv_frame_t d) const {
Chris@1616 198 EventVector span;
Chris@1616 199
Chris@1616 200 sv_frame_t start = f;
Chris@1616 201 sv_frame_t end = f + d;
Chris@1616 202
Chris@1616 203 // first find any zero-duration events
Chris@1616 204
Chris@1616 205 auto pitr = m_events.lower_bound(Event(start, QString()));
Chris@1616 206 while (pitr != m_events.end() && pitr->first.getFrame() < end) {
Chris@1616 207 if (!pitr->first.hasDuration()) {
Chris@1616 208 for (int i = 0; i < pitr->second; ++i) {
Chris@1616 209 span.push_back(pitr->first);
Chris@1616 210 }
Chris@1616 211 }
Chris@1616 212 ++pitr;
Chris@1616 213 }
Chris@1616 214
Chris@1616 215 // now any non-zero-duration ones from the seam map
Chris@1616 216
Chris@1616 217 std::set<Event> found;
Chris@1616 218 auto sitr = m_seams.lower_bound(start);
Chris@1616 219 if (sitr == m_seams.end() || sitr->first > start) {
Chris@1616 220 if (sitr != m_seams.begin()) {
Chris@1616 221 --sitr;
Chris@1616 222 }
Chris@1616 223 }
Chris@1616 224 while (sitr != m_seams.end() && sitr->first < end) {
Chris@1616 225 for (const auto &p: sitr->second) {
Chris@1616 226 found.insert(p);
Chris@1616 227 }
Chris@1616 228 ++sitr;
Chris@1616 229 }
Chris@1616 230 for (const auto &p: found) {
Chris@1616 231 int n = m_events.at(p);
Chris@1616 232 if (n < 1) {
Chris@1616 233 throw std::logic_error("event is in seams but not events");
Chris@1616 234 }
Chris@1616 235 for (int i = 0; i < n; ++i) {
Chris@1616 236 span.push_back(p);
Chris@1616 237 }
Chris@1616 238 }
Chris@1616 239
Chris@1616 240 return span;
Chris@1616 241 }
Chris@1616 242
Chris@1616 243 /**
Chris@1616 244 * Retrieve all events that cover the given frame. An event without
Chris@1616 245 * duration covers a frame if its own frame is equal to it. An event
Chris@1616 246 * with duration covers a frame if its start frame is less than or
Chris@1612 247 * equal to it and its end frame (start + duration) is greater
Chris@1612 248 * than it.
Chris@1612 249 */
Chris@1616 250 EventVector getEventsCovering(sv_frame_t frame) const {
Chris@1616 251 EventVector cover;
Chris@1614 252
Chris@1615 253 // first find any zero-duration events
Chris@1616 254
Chris@1615 255 auto pitr = m_events.lower_bound(Event(frame, QString()));
Chris@1616 256 while (pitr != m_events.end() && pitr->first.getFrame() == frame) {
Chris@1616 257 if (!pitr->first.hasDuration()) {
Chris@1616 258 for (int i = 0; i < pitr->second; ++i) {
Chris@1616 259 cover.push_back(pitr->first);
Chris@1614 260 }
Chris@1614 261 }
Chris@1616 262 ++pitr;
Chris@1614 263 }
Chris@1614 264
Chris@1614 265 // now any non-zero-duration ones from the seam map
Chris@1616 266
Chris@1614 267 auto sitr = m_seams.lower_bound(frame);
Chris@1614 268 if (sitr == m_seams.end() || sitr->first > frame) {
Chris@1614 269 if (sitr != m_seams.begin()) {
Chris@1614 270 --sitr;
Chris@1614 271 }
Chris@1614 272 }
Chris@1614 273 if (sitr != m_seams.end() && sitr->first <= frame) {
Chris@1616 274 for (const auto &p: sitr->second) {
Chris@1616 275 int n = m_events.at(p);
Chris@1616 276 if (n < 1) {
Chris@1616 277 throw std::logic_error("event is in seams but not events");
Chris@1616 278 }
Chris@1616 279 for (int i = 0; i < n; ++i) {
Chris@1616 280 cover.push_back(p);
Chris@1616 281 }
Chris@1614 282 }
Chris@1614 283 }
Chris@1614 284
Chris@1616 285 return cover;
Chris@1612 286 }
Chris@1612 287
Chris@1612 288 private:
Chris@1616 289 /**
Chris@1616 290 * Total number of events in the series. Will exceed
Chris@1616 291 * m_events.size() if the series contains duplicate events.
Chris@1616 292 */
Chris@1612 293 int m_count;
Chris@1614 294
Chris@1616 295 /**
Chris@1616 296 * The (ordered) Events map acts as a list of all the events in
Chris@1616 297 * the series. For reasons of backward compatibility, we have to
Chris@1616 298 * handle series containing multiple instances of identical
Chris@1616 299 * events; the int indexed against each event records the number
Chris@1616 300 * of instances of that event. We do this in preference to using a
Chris@1616 301 * multiset, in order to support the case in which we have
Chris@1616 302 * obtained a set of distinct events (from the seam container
Chris@1616 303 * below) and then need to return the correct number of each.
Chris@1616 304 *
Chris@1616 305 * Because events are immutable, we never have to move one to a
Chris@1616 306 * different "key" in this map - we only add or delete them or
Chris@1616 307 * change their counts.
Chris@1616 308 */
Chris@1616 309 typedef std::map<Event, int> Events;
Chris@1616 310 Events m_events;
Chris@1614 311
Chris@1616 312 /**
Chris@1616 313 * The FrameEventMap maps from frame number to a set of events. In
Chris@1616 314 * the seam map this is used to represent the events that are
Chris@1616 315 * active at that frame, either because they begin at that frame
Chris@1616 316 * or because they are continuing from an earlier frame. There is
Chris@1616 317 * an entry here for each frame at which an event starts or ends,
Chris@1616 318 * with the event appearing in all entries from its start time
Chris@1616 319 * onward and disappearing again at its end frame.
Chris@1616 320 *
Chris@1616 321 * Only events with duration appear in this map; point events
Chris@1616 322 * appear only in m_events.
Chris@1616 323 */
Chris@1616 324 typedef std::map<sv_frame_t, std::set<Event>> FrameEventMap;
Chris@1616 325 FrameEventMap m_seams;
Chris@1614 326
Chris@1614 327 /** Create a seam at the given frame, copying from the prior seam
Chris@1614 328 * if there is one. If a seam already exists at the given frame,
Chris@1614 329 * leave it untouched.
Chris@1614 330 */
Chris@1614 331 void createSeam(sv_frame_t frame) {
Chris@1614 332 auto itr = m_seams.lower_bound(frame);
Chris@1614 333 if (itr == m_seams.end() || itr->first > frame) {
Chris@1614 334 if (itr != m_seams.begin()) {
Chris@1614 335 --itr;
Chris@1614 336 }
Chris@1614 337 }
Chris@1614 338 if (itr == m_seams.end()) {
Chris@1614 339 m_seams[frame] = {};
Chris@1614 340 } else if (itr->first < frame) {
Chris@1614 341 m_seams[frame] = itr->second;
Chris@1614 342 } else if (itr->first > frame) { // itr must be begin()
Chris@1614 343 m_seams[frame] = {};
Chris@1614 344 }
Chris@1614 345 }
Chris@1614 346
Chris@1615 347 #ifdef DEBUG_EVENT_SERIES
Chris@1615 348 void dumpEvents() const {
Chris@1615 349 std::cerr << "EVENTS [" << std::endl;
Chris@1616 350 for (const auto &i: m_events) {
Chris@1616 351 std::cerr << i.first.toXmlString(" ");
Chris@1614 352 }
Chris@1614 353 std::cerr << "]" << std::endl;
Chris@1614 354 }
Chris@1614 355
Chris@1614 356 void dumpSeams() const {
Chris@1614 357 std::cerr << "SEAMS [" << std::endl;
Chris@1614 358 for (const auto &s: m_seams) {
Chris@1614 359 std::cerr << " " << s.first << " -> {" << std::endl;
Chris@1614 360 for (const auto &p: s.second) {
Chris@1614 361 std::cerr << p.toXmlString(" ");
Chris@1614 362 }
Chris@1614 363 std::cerr << " }" << std::endl;
Chris@1614 364 }
Chris@1614 365 std::cerr << "]" << std::endl;
Chris@1614 366 }
Chris@1614 367 #endif
Chris@1612 368 };
Chris@1612 369
Chris@1612 370 #endif