comparison base/EventSeries.cpp @ 1653:eaad70939848 single-point

Add nearest-event-matching search
author Chris Cannam
date Tue, 19 Mar 2019 14:24:05 +0000
parents 513192aa9b03
children 26aa42fd60e9
comparison
equal deleted inserted replaced
1652:08bed13d3a26 1653:eaad70939848
399 } 399 }
400 following = *pitr; 400 following = *pitr;
401 return true; 401 return true;
402 } 402 }
403 403
404 bool
405 EventSeries::getNearestEventMatching(sv_frame_t startSearchAt,
406 std::function<bool(const Event &)> predicate,
407 Direction direction,
408 Event &found) const
409 {
410 auto pitr = lower_bound(m_events.begin(), m_events.end(),
411 Event(startSearchAt));
412
413 while (true) {
414
415 if (direction == Backward) {
416 if (pitr == m_events.begin()) {
417 break;
418 } else {
419 --pitr;
420 }
421 } else {
422 if (pitr == m_events.end()) {
423 break;
424 }
425 }
426
427 const Event &e = *pitr;
428 if (predicate(e)) {
429 found = e;
430 return true;
431 }
432
433 if (direction == Forward) {
434 ++pitr;
435 }
436 }
437
438 return false;
439 }
440
404 Event 441 Event
405 EventSeries::getEventByIndex(int index) const 442 EventSeries::getEventByIndex(int index) const
406 { 443 {
407 if (index < 0 || index >= count()) { 444 if (index < 0 || index >= count()) {
408 throw std::logic_error("index out of range"); 445 throw std::logic_error("index out of range");