resonance 1.0.1
Loading...
Searching...
No Matches
tickobjvector.h
Go to the documentation of this file.
1#pragma once
2
3#include <algorithm>
4#include <vector>
5
6#include "mid/tickobj.h"
7
8// Searches and sorted insertion over a vector of TickObj ordered by song position.
9//
10// Each template below was emitted once per element type and per translation unit that used it, and
11// one declaration stands for several addresses. The instantiations share their instruction shapes
12// exactly and differ only in the comparator and helper addresses they call. The element type of
13// each instantiation is not recorded where the call sites have not settled it.
14//
15// The comparators are called through a function pointer rather than inlined, as std::upper_bound
16// and std::lower_bound do with a comparator passed as a plain function. Each search inlines the
17// standard algorithm and calls the comparator out of line.
18
39template <typename T>
40bool TickObjLess(TickObj<T> first, TickObj<T> second) {
41 return first.mPosition.mTick < second.mPosition.mTick;
42}
43
59template <typename T>
60bool TickObjBefore(int nTick, TickObj<T> entry) {
61 return nTick < entry.mPosition.mTick;
62}
63
75template <typename T>
76bool TickObjAfter(TickObj<T> entry, int nTick) {
77 return entry.mPosition.mTick < nTick;
78}
79
93template <typename T>
94typename std::vector<TickObj<T> >::iterator UpperBoundByEntry(std::vector<TickObj<T> > &values,
95 const TickObj<T> &value) {
96 return std::upper_bound(values.begin(), values.end(), value, TickObjLess<T>);
97}
98
112template <typename T>
113typename std::vector<TickObj<T> >::iterator LowerBoundByEntry(std::vector<TickObj<T> > &values,
114 const TickObj<T> &value) {
115 return std::lower_bound(values.begin(), values.end(), value, TickObjLess<T>);
116}
117
133template <typename T>
134typename std::vector<TickObj<T> >::const_iterator
135UpperBoundByTick(const std::vector<TickObj<T> > &values, int nTick) {
136 return std::upper_bound(values.begin(), values.end(), nTick, TickObjBefore<T>);
137}
138
148template <typename T>
149typename std::vector<TickObj<T> >::const_iterator
150LowerBoundByTick(const std::vector<TickObj<T> > &values, int nTick) {
151 return std::lower_bound(values.begin(), values.end(), nTick, TickObjAfter<T>);
152}
153
169template <typename T>
170typename std::vector<TickObj<T> >::const_iterator
171FindAtOrBefore(const std::vector<TickObj<T> > &values, int nTick) {
172 if (values.begin() == values.end()) {
173 return values.end();
174 }
175 const auto upper = UpperBoundByTick(values, nTick);
176 if (upper == values.begin()) {
177 return values.end();
178 }
179 return upper - 1;
180}
181
191template <typename T>
192typename std::vector<TickObj<T> >::const_iterator
193FindAtOrAfter(const std::vector<TickObj<T> > &values, int nTick) {
194 if (values.begin() == values.end()) {
195 return values.end();
196 }
197 return LowerBoundByTick(values, nTick);
198}
199
216template <typename T>
217void InsertSorted(std::vector<TickObj<T> > &values, const TickObj<T> &value) {
218 if (values.size() != 0 && !(value.mPosition.mTick < values.back().mPosition.mTick)) {
219 values.push_back(value);
220 return;
221 }
222 values.insert(UpperBoundByEntry(values, value), value);
223}
224
236template <typename T>
237void SetAtTick(std::vector<TickObj<T> > &values, const T &value, int nTick) {
238 TickObj<T> entry;
239 entry.mPosition.mTick = nTick;
240 entry.mValue = value;
241 typename std::vector<TickObj<T> >::iterator it = LowerBoundByEntry(values, entry);
242 if (it != values.end() && it->mPosition.mTick == nTick) {
243 *it = entry;
244 return;
245 }
246 values.insert(it, entry);
247}
248
259template <typename T>
260void InsertAtLowerBound(std::vector<TickObj<T> > &values, const TickObj<T> &value) {
261 values.insert(LowerBoundByEntry(values, value), value);
262}
263
277template <typename T>
278void InsertAtTick(std::vector<TickObj<T> > &values, const T &value, int nTick) {
279 TickObj<T> entry;
280 entry.mPosition.mTick = nTick;
281 entry.mValue = value;
282 InsertSorted(values, entry);
283}
int mTick
The position, in MIDI ticks at 480 per quarter note.
Definition tick.h:153
One value at one song position.
Definition tickobj.h:20
T mValue
Definition tickobj.h:22
Sch::Tick mPosition
Definition tickobj.h:21
std::vector< TickObj< T > >::const_iterator UpperBoundByTick(const std::vector< TickObj< T > > &values, int nTick)
Find the first entry a song position strictly precedes.
Definition tickobjvector.h:135
bool TickObjAfter(TickObj< T > entry, int nTick)
Report whether an entry comes strictly before a song position.
Definition tickobjvector.h:76
std::vector< TickObj< T > >::const_iterator LowerBoundByTick(const std::vector< TickObj< T > > &values, int nTick)
Find the first entry at or after a song position.
Definition tickobjvector.h:150
void InsertSorted(std::vector< TickObj< T > > &values, const TickObj< T > &value)
Insert an entry at its sorted place, after every entry at the same position.
Definition tickobjvector.h:217
std::vector< TickObj< T > >::iterator LowerBoundByEntry(std::vector< TickObj< T > > &values, const TickObj< T > &value)
Find the first entry a value does not follow.
Definition tickobjvector.h:113
std::vector< TickObj< T > >::const_iterator FindAtOrAfter(const std::vector< TickObj< T > > &values, int nTick)
Find the first entry at or after a song position.
Definition tickobjvector.h:193
void InsertAtLowerBound(std::vector< TickObj< T > > &values, const TickObj< T > &value)
Insert an entry before every entry at the same position, always searching first.
Definition tickobjvector.h:260
std::vector< TickObj< T > >::iterator UpperBoundByEntry(std::vector< TickObj< T > > &values, const TickObj< T > &value)
Find the first entry a value strictly precedes.
Definition tickobjvector.h:94
bool TickObjLess(TickObj< T > first, TickObj< T > second)
Order two entries by song position.
Definition tickobjvector.h:40
void InsertAtTick(std::vector< TickObj< T > > &values, const T &value, int nTick)
Insert a value at a song position, after every entry at the same position.
Definition tickobjvector.h:278
void SetAtTick(std::vector< TickObj< T > > &values, const T &value, int nTick)
Store a value at a song position, overwriting the first entry already at that position.
Definition tickobjvector.h:237
bool TickObjBefore(int nTick, TickObj< T > entry)
Report whether a song position comes strictly before an entry.
Definition tickobjvector.h:60
std::vector< TickObj< T > >::const_iterator FindAtOrBefore(const std::vector< TickObj< T > > &values, int nTick)
Find the last entry at or before a song position.
Definition tickobjvector.h:171