annotate base/Extents.h @ 1675:6804af71b7be osc-script

Write path points
author Chris Cannam
date Thu, 28 Mar 2019 10:39:02 +0000
parents bbfb5a1e4b84
children 1b688ab5f1b3
rev   line source
Chris@1650 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@1650 2
Chris@1650 3 /*
Chris@1650 4 Sonic Visualiser
Chris@1650 5 An audio file viewer and annotation editor.
Chris@1650 6 Centre for Digital Music, Queen Mary, University of London.
Chris@1650 7
Chris@1650 8 This program is free software; you can redistribute it and/or
Chris@1650 9 modify it under the terms of the GNU General Public License as
Chris@1650 10 published by the Free Software Foundation; either version 2 of the
Chris@1650 11 License, or (at your option) any later version. See the file
Chris@1650 12 COPYING included with this distribution for more information.
Chris@1650 13 */
Chris@1650 14
Chris@1650 15 #ifndef SV_EXTENTS_H
Chris@1650 16 #define SV_EXTENTS_H
Chris@1650 17
Chris@1650 18 #include <vector>
Chris@1650 19
Chris@1650 20 /**
Chris@1650 21 * Maintain a min and max value, and update them when supplied a new
Chris@1650 22 * data point.
Chris@1650 23 */
Chris@1650 24 template <typename T>
Chris@1650 25 class Extents
Chris@1650 26 {
Chris@1650 27 public:
Chris@1650 28 Extents() : m_min(T()), m_max(T()) { }
Chris@1650 29 Extents(T min, T max) : m_min(min), m_max(max) { }
Chris@1650 30
Chris@1650 31 bool operator==(const Extents &r) {
Chris@1650 32 return r.m_min == m_min && r.m_max == m_max;
Chris@1650 33 }
Chris@1650 34 bool operator!=(const Extents &r) {
Chris@1650 35 return !(*this == r);
Chris@1650 36 }
Chris@1650 37
Chris@1650 38 bool isSet() const {
Chris@1650 39 return (m_min != T() || m_max != T());
Chris@1650 40 }
Chris@1650 41 void set(T min, T max) {
Chris@1650 42 m_min = min;
Chris@1650 43 m_max = max;
Chris@1650 44 if (m_max < m_min) m_max = m_min;
Chris@1650 45 }
Chris@1650 46 void reset() {
Chris@1650 47 m_min = T();
Chris@1650 48 m_max = T();
Chris@1650 49 }
Chris@1650 50
Chris@1650 51 bool sample(T f) {
Chris@1650 52 bool changed = false;
Chris@1650 53 if (isSet()) {
Chris@1650 54 if (f < m_min) { m_min = f; changed = true; }
Chris@1650 55 if (f > m_max) { m_max = f; changed = true; }
Chris@1650 56 } else {
Chris@1650 57 m_max = m_min = f;
Chris@1650 58 changed = true;
Chris@1650 59 }
Chris@1650 60 return changed;
Chris@1650 61 }
Chris@1650 62 bool sample(const std::vector<T> &ff) {
Chris@1650 63 bool changed = false;
Chris@1650 64 for (auto f: ff) {
Chris@1650 65 if (sample(f)) {
Chris@1650 66 changed = true;
Chris@1650 67 }
Chris@1650 68 }
Chris@1650 69 return changed;
Chris@1650 70 }
Chris@1650 71 bool sample(const Extents &r) {
Chris@1650 72 bool changed = false;
Chris@1650 73 if (isSet()) {
Chris@1650 74 if (r.m_min < m_min) { m_min = r.m_min; changed = true; }
Chris@1650 75 if (r.m_max > m_max) { m_max = r.m_max; changed = true; }
Chris@1650 76 } else {
Chris@1650 77 m_min = r.m_min;
Chris@1650 78 m_max = r.m_max;
Chris@1650 79 changed = true;
Chris@1650 80 }
Chris@1650 81 return changed;
Chris@1650 82 }
Chris@1650 83
Chris@1650 84 T getMin() const { return m_min; }
Chris@1650 85 T getMax() const { return m_max; }
Chris@1650 86
Chris@1650 87 private:
Chris@1650 88 T m_min;
Chris@1650 89 T m_max;
Chris@1650 90 };
Chris@1650 91
Chris@1650 92 #endif