From 4128088b70d0f0b96620f7248e03d2a42eb6f6d1 Mon Sep 17 00:00:00 2001 From: Paul Davis Date: Sat, 13 Nov 2021 10:00:25 -0700 Subject: [PATCH] temporal: TempoMap::tempo_at() ... templates FTW --- libs/temporal/tempo.cc | 54 ++-------------------------------- libs/temporal/temporal/tempo.h | 8 +++-- 2 files changed, 8 insertions(+), 54 deletions(-) diff --git a/libs/temporal/tempo.cc b/libs/temporal/tempo.cc index a1d564d4da..57918d927b 100644 --- a/libs/temporal/tempo.cc +++ b/libs/temporal/tempo.cc @@ -2855,63 +2855,15 @@ TempoMap::tempo_at (timepos_t const & pos) const return pos.is_beats() ? tempo_at (pos.beats()) : tempo_at (pos.superclocks()); } +template TempoPoint const & -TempoMap::tempo_at (superclock_t sc) const +TempoMap::_tempo_at (TimeType when, Comparator cmp) const { assert (!_tempos.empty()); - Point::sclock_comparator cmp; Tempos::const_iterator prev = _tempos.end(); for (Tempos::const_iterator t = _tempos.begin(); t != _tempos.end(); ++t) { - if (cmp (*t, sc)) { - prev = t; - } else { - break; - } - } - if (prev == _tempos.end()) { - return _tempos.front(); - } - return *prev; -} - - -TempoPoint const & -TempoMap::tempo_at (Beats const & b) const -{ - assert (!_tempos.empty()); - Point::beat_comparator cmp; - - Tempos::const_iterator prev = _tempos.end(); - for (Tempos::const_iterator t = _tempos.begin(); t != _tempos.end(); ++t) { - if (cmp (*t, b)) { - prev = t; - } else { - break; - } - } - if (prev == _tempos.end()) { - return _tempos.front(); - } - return *prev; - - - Tempos::const_iterator t = std::lower_bound (_tempos.begin(), _tempos.end(), b, cmp); - if (t == _tempos.end()) { - return _tempos.front(); - } - return *t; -} - -TempoPoint const & -TempoMap::tempo_at (BBT_Time const & bbt) const -{ - assert (!_tempos.empty()); - Point::bbt_comparator cmp; - - Tempos::const_iterator prev = _tempos.end(); - for (Tempos::const_iterator t = _tempos.begin(); t != _tempos.end(); ++t) { - if (cmp (*t, bbt)) { + if (cmp (*t, when)) { prev = t; } else { break; diff --git a/libs/temporal/temporal/tempo.h b/libs/temporal/temporal/tempo.h index 2806407721..ebf266d58f 100644 --- a/libs/temporal/temporal/tempo.h +++ b/libs/temporal/temporal/tempo.h @@ -763,9 +763,9 @@ class /*LIBTEMPORAL_API*/ TempoMap : public PBD::StatefulDestructible LIBTEMPORAL_API MeterPoint const& meter_at (BBT_Time const & bbt) const { return metric_at (bbt).meter(); } LIBTEMPORAL_API TempoPoint const& tempo_at (timepos_t const & p) const; - LIBTEMPORAL_API TempoPoint const& tempo_at (superclock_t sc) const; - LIBTEMPORAL_API TempoPoint const& tempo_at (Beats const & b) const; - LIBTEMPORAL_API TempoPoint const& tempo_at (BBT_Time const & bbt) const; + LIBTEMPORAL_API TempoPoint const& tempo_at (superclock_t sc) const { return _tempo_at (sc, Point::sclock_comparator()); } + LIBTEMPORAL_API TempoPoint const& tempo_at (Beats const & b) const { return _tempo_at (b, Point::beat_comparator()); } + LIBTEMPORAL_API TempoPoint const& tempo_at (BBT_Time const & bbt) const { return _tempo_at (bbt, Point::bbt_comparator()); } LIBTEMPORAL_API TempoPoint const* previous_tempo (TempoPoint const &) const; @@ -868,6 +868,8 @@ class /*LIBTEMPORAL_API*/ TempoMap : public PBD::StatefulDestructible BBT_Time bbt_at (Beats const &) const; BBT_Time bbt_at (superclock_t sc) const; + template TempoPoint const & _tempo_at (TimeType when, Comparator cmp) const; + template struct const_traits { typedef Points::const_iterator iterator_type; typedef TempoPoint const * tempo_point_type;