Extents.h
Go to the documentation of this file.
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 
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  template <typename Alloc>
63  bool sample(const std::vector<T, Alloc> &ff) {
64  bool changed = false;
65  for (auto f: ff) {
66  if (sample(f)) {
67  changed = true;
68  }
69  }
70  return changed;
71  }
72  bool sample(const Extents &r) {
73  bool changed = false;
74  if (isSet()) {
75  if (r.m_min < m_min) { m_min = r.m_min; changed = true; }
76  if (r.m_max > m_max) { m_max = r.m_max; changed = true; }
77  } else {
78  m_min = r.m_min;
79  m_max = r.m_max;
80  changed = true;
81  }
82  return changed;
83  }
84 
85  T getMin() const { return m_min; }
86  T getMax() const { return m_max; }
87 
88 private:
89  T m_min;
90  T m_max;
91 };
92 
93 #endif
bool operator==(const Extents &r)
Definition: Extents.h:31
bool sample(const Extents &r)
Definition: Extents.h:72
Maintain a min and max value, and update them when supplied a new data point.
Definition: Extents.h:25
void reset()
Definition: Extents.h:46
T getMax() const
Definition: Extents.h:86
bool sample(const std::vector< T, Alloc > &ff)
Definition: Extents.h:63
T m_max
Definition: Extents.h:90
Extents(T min, T max)
Definition: Extents.h:29
bool operator!=(const Extents &r)
Definition: Extents.h:34
T getMin() const
Definition: Extents.h:85
bool isSet() const
Definition: Extents.h:38
T m_min
Definition: Extents.h:89
Extents()
Definition: Extents.h:28
bool sample(T f)
Definition: Extents.h:51