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