comparison base/Extents.h @ 1650:bbfb5a1e4b84 single-point

Make MagnitudeRange generic, in the form of Extents
author Chris Cannam
date Mon, 18 Mar 2019 09:37:46 +0000
parents
children 1b688ab5f1b3
comparison
equal deleted inserted replaced
1649:1cc9a0d4b1b6 1650:bbfb5a1e4b84
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
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2 of the
11 License, or (at your option) any later version. See the file
12 COPYING included with this distribution for more information.
13 */
14
15 #ifndef SV_EXTENTS_H
16 #define SV_EXTENTS_H
17
18 #include <vector>
19
20 /**
21 * Maintain a min and max value, and update them when supplied a new
22 * data point.
23 */
24 template <typename T>
25 class Extents
26 {
27 public:
28 Extents() : m_min(T()), m_max(T()) { }
29 Extents(T min, T max) : m_min(min), m_max(max) { }
30
31 bool operator==(const Extents &r) {
32 return r.m_min == m_min && r.m_max == m_max;
33 }
34 bool operator!=(const Extents &r) {
35 return !(*this == r);
36 }
37
38 bool isSet() const {
39 return (m_min != T() || m_max != T());
40 }
41 void set(T min, T max) {
42 m_min = min;
43 m_max = max;
44 if (m_max < m_min) m_max = m_min;
45 }
46 void reset() {
47 m_min = T();
48 m_max = T();
49 }
50
51 bool sample(T f) {
52 bool changed = false;
53 if (isSet()) {
54 if (f < m_min) { m_min = f; changed = true; }
55 if (f > m_max) { m_max = f; changed = true; }
56 } else {
57 m_max = m_min = f;
58 changed = true;
59 }
60 return changed;
61 }
62 bool sample(const std::vector<T> &ff) {
63 bool changed = false;
64 for (auto f: ff) {
65 if (sample(f)) {
66 changed = true;
67 }
68 }
69 return changed;
70 }
71 bool sample(const Extents &r) {
72 bool changed = false;
73 if (isSet()) {
74 if (r.m_min < m_min) { m_min = r.m_min; changed = true; }
75 if (r.m_max > m_max) { m_max = r.m_max; changed = true; }
76 } else {
77 m_min = r.m_min;
78 m_max = r.m_max;
79 changed = true;
80 }
81 return changed;
82 }
83
84 T getMin() const { return m_min; }
85 T getMax() const { return m_max; }
86
87 private:
88 T m_min;
89 T m_max;
90 };
91
92 #endif