annotate base/MagnitudeRange.h @ 1412:b7a9edee85e0 scale-ticks

Change loop to something that feels more correct, though it makes no difference to the tests here. More tests, one failing.
author Chris Cannam
date Thu, 04 May 2017 08:32:41 +0100
parents 4d0d94ba2ea7
children 48e9f538e6e9
rev   line source
Chris@1186 1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
Chris@1186 2
Chris@1186 3 /*
Chris@1186 4 Sonic Visualiser
Chris@1186 5 An audio file viewer and annotation editor.
Chris@1186 6 Centre for Digital Music, Queen Mary, University of London.
Chris@1186 7 This file copyright 2006 Chris Cannam and QMUL.
Chris@1186 8
Chris@1186 9 This program is free software; you can redistribute it and/or
Chris@1186 10 modify it under the terms of the GNU General Public License as
Chris@1186 11 published by the Free Software Foundation; either version 2 of the
Chris@1186 12 License, or (at your option) any later version. See the file
Chris@1186 13 COPYING included with this distribution for more information.
Chris@1186 14 */
Chris@1186 15
Chris@1186 16 #ifndef MAGNITUDE_RANGE_H
Chris@1186 17 #define MAGNITUDE_RANGE_H
Chris@1186 18
Chris@1194 19 #include <vector>
Chris@1194 20
Chris@1186 21 /**
Chris@1186 22 * Maintain a min and max value, and update them when supplied a new
Chris@1186 23 * data point.
Chris@1186 24 */
Chris@1186 25 class MagnitudeRange
Chris@1186 26 {
Chris@1186 27 public:
Chris@1186 28 MagnitudeRange() : m_min(0), m_max(0) { }
Chris@1199 29 MagnitudeRange(float min, float max) : m_min(min), m_max(max) { }
Chris@1199 30
Chris@1186 31 bool operator==(const MagnitudeRange &r) {
Chris@1186 32 return r.m_min == m_min && r.m_max == m_max;
Chris@1186 33 }
Chris@1199 34 bool operator!=(const MagnitudeRange &r) {
Chris@1199 35 return !(*this == r);
Chris@1199 36 }
Chris@1199 37
Chris@1186 38 bool isSet() const { return (m_min != 0.f || m_max != 0.f); }
Chris@1186 39 void set(float min, float max) {
Chris@1186 40 m_min = min;
Chris@1186 41 m_max = max;
Chris@1186 42 if (m_max < m_min) m_max = m_min;
Chris@1186 43 }
Chris@1186 44 bool sample(float f) {
Chris@1186 45 bool changed = false;
Chris@1186 46 if (isSet()) {
Chris@1186 47 if (f < m_min) { m_min = f; changed = true; }
Chris@1186 48 if (f > m_max) { m_max = f; changed = true; }
Chris@1186 49 } else {
Chris@1186 50 m_max = m_min = f;
Chris@1186 51 changed = true;
Chris@1186 52 }
Chris@1186 53 return changed;
Chris@1194 54 }
Chris@1194 55 bool sample(const std::vector<float> &ff) {
Chris@1194 56 bool changed = false;
Chris@1194 57 for (auto f: ff) {
Chris@1194 58 if (sample(f)) {
Chris@1194 59 changed = true;
Chris@1194 60 }
Chris@1194 61 }
Chris@1194 62 return changed;
Chris@1194 63 }
Chris@1186 64 bool sample(const MagnitudeRange &r) {
Chris@1186 65 bool changed = false;
Chris@1186 66 if (isSet()) {
Chris@1186 67 if (r.m_min < m_min) { m_min = r.m_min; changed = true; }
Chris@1186 68 if (r.m_max > m_max) { m_max = r.m_max; changed = true; }
Chris@1186 69 } else {
Chris@1186 70 m_min = r.m_min;
Chris@1186 71 m_max = r.m_max;
Chris@1186 72 changed = true;
Chris@1186 73 }
Chris@1186 74 return changed;
Chris@1186 75 }
Chris@1186 76 float getMin() const { return m_min; }
Chris@1186 77 float getMax() const { return m_max; }
Chris@1186 78 private:
Chris@1186 79 float m_min;
Chris@1186 80 float m_max;
Chris@1186 81 };
Chris@1186 82
Chris@1186 83 #endif