Essentia  2.1-beta6-dev
essentiautil.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2021 Music Technology Group - Universitat Pompeu Fabra
3  *
4  * This file is part of Essentia
5  *
6  * Essentia is free software: you can redistribute it and/or modify it under
7  * the terms of the GNU Affero General Public License as published by the Free
8  * Software Foundation (FSF), either version 3 of the License, or (at your
9  * option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the Affero GNU General Public License
17  * version 3 along with this program. If not, see http://www.gnu.org/licenses/
18  */
19 
20 #ifndef ESSENTIA_UTILS_H
21 #define ESSENTIA_UTILS_H
22 
23 #include <vector>
24 #include <cctype>
25 #include <string>
26 #include <cmath> // isinf, isnan
27 #include "utils/tnt/tnt.h"
28 #include "types.h"
29 
30 
31 namespace essentia {
32 
33 
34 // ARRAY_SIZE returns the size of a constant-sized C-style array
35 #ifndef ARRAY_SIZE
36 #define ARRAY_SIZE(arr) (sizeof(arr)/sizeof(*(arr)))
37 #endif
38 
39 
40 // macro used to silence compiler warnings about otherwise valid uses of
41 // "unused" variables (eg: RAII, see MutexLocker for instance)
42 #define NOWARN_UNUSED(expr) do { (void)(expr); } while (0)
43 
44 
48 template <typename T, typename Array>
49 std::vector<T> arrayToVector(const Array& array) {
50  int size = ARRAY_SIZE(array);
51  std::vector<T> result(size);
52  for (int i=0; i<size; i++) {
53  result[i] = array[i];
54  }
55  return result;
56 }
57 
58 
59 
64 template <typename T>
65 int indexOf(const std::vector<T>& v, const T& elem) {
66  const int size = (int)v.size();
67  for (int i=0; i<size; i++) {
68  if (v[i] == elem) return i;
69  }
70 
71  return -1;
72 }
73 
74 
80 template <typename T>
81 bool contains(const std::vector<T>& v, const T& elem) {
82  return (indexOf(v, elem) != -1);
83 }
84 
85 inline bool contains(const std::vector<std::string>& v, const char* str) {
86  return contains(v, std::string(str));
87 }
88 
92 template <typename T, typename U>
93 bool contains(const std::map<T, U>& m, const T& key) {
94  return m.find(key) != m.end();
95 }
96 
97 template <typename T>
98 bool contains(const OrderedMap<T>& m, const std::string& key) {
99  const int size = (int)m.size();
100  for (int i=0; i<size; i++) {
101  if (m[i].first == key) return true;
102  }
103  return false;
104 }
105 
106 template <typename T>
107 bool contains(const OrderedMap<T>& m, const char* key) {
108  return contains(m, std::string(key));
109 }
110 
111 
112 
116 template <typename T>
117 inline bool isValid(const T& value) {
118  if (std::isinf(value) || std::isnan(value)) return false;
119  return true;
120 }
121 
122 template <typename T>
123 inline bool isValid(const Tuple2<T>& value) {
124  if (!isValid(value.left()) ||
125  !isValid(value.right())) {
126  return false;
127  }
128  return true;
129 }
130 
131 inline bool isValid(const std::string& s) {
132  // at the moment all strings are valid
133  return true;
134 }
135 
136 template <typename T>
137 inline bool isValid(const std::vector<T>& v) {
138  typename std::vector<T>::const_iterator it = v.begin();
139  while (it != v.end() && isValid(*it)) ++it;
140  return it == v.end();
141 }
142 
143 template <typename T>
144 inline bool isValid(const std::vector<std::vector<T> >& mat) {
145  typename std::vector<std::vector<T> >::const_iterator it = mat.begin();
146  while (it != mat.end() && isValid(*it)) ++it;
147  return it == mat.end();
148 }
149 
150 template <typename T>
151 inline bool isValid(const TNT::Array2D<T> & mat) {
152  for (int row=0; row<mat.dim1(); ++row) {
153  for (int col=0; col<mat.dim2(); ++col) {
154  if (!isValid(mat[row][col])) return false;
155  }
156  }
157  return true;
158 }
159 
160 template <typename T>
161 inline bool isValid(const Tensor<T>& tensor) {
162  for (const T* i = tensor.data(); i < (tensor.data() + tensor.size()); ++i){
163  if (!isValid(*i)) return false;
164  }
165  return true;
166 }
167 
168 #ifdef OS_WIN32
169 int mkstemp(char *tmpl);
170 #endif // OS_WIN32
171 
172 
173 
174 
179 template <typename T>
180 inline void fastcopy(T* dest, const T* src, int n) {
181  for (int i=0; i<n; i++) {
182  *dest++ = *src++;
183  }
184 }
185 
186 template <>
187 inline void fastcopy<Real>(Real* dest, const Real* src, int n) {
188  memcpy(dest, src, n*sizeof(Real));
189 }
190 
191 template <>
192 inline void fastcopy<StereoSample>(StereoSample* dest, const StereoSample* src, int n) {
193  memcpy(dest, src, n*sizeof(StereoSample));
194 }
195 
196 template <>
197 inline void fastcopy<int>(int* dest, const int* src, int n) {
198  memcpy(dest, src, n*sizeof(int));
199 }
200 
201 // overload for iterators, which allow us to fastcopy(dest.begin(), src.begin(), 0) and not crash
202 inline void fastcopy(std::vector<Real>::iterator dest, std::vector<Real>::const_iterator src, int n) {
203  // need to test this because otherwise it is not legal to dereference the iterator
204  if (n > 0) {
205  fastcopy(&*dest, &*src, n);
206  }
207 }
208 
209 inline void fastcopy(std::vector<StereoSample>::iterator dest, std::vector<StereoSample>::const_iterator src, int n) {
210  if (n > 0) {
211  fastcopy(&*dest, &*src, n);
212  }
213 }
214 
215 } // namespace essentia
216 
217 #endif // ESSENTIA_UTILS_H
Definition: tnt_array2d.h:38
int dim1() const
Definition: tnt_array2d.h:231
int dim2() const
Definition: tnt_array2d.h:234
Definition: types.h:129
int size() const
Definition: types.h:133
Definition: types.h:352
const T & right() const
Definition: types.h:358
const T & left() const
Definition: types.h:357
#define ARRAY_SIZE(arr)
Definition: essentiautil.h:36
Definition: algorithm.h:28
void fastcopy< Real >(Real *dest, const Real *src, int n)
Definition: essentiautil.h:187
void fastcopy< int >(int *dest, const int *src, int n)
Definition: essentiautil.h:197
bool isValid(const T &value)
Definition: essentiautil.h:117
void fastcopy(T *dest, const T *src, int n)
Definition: essentiautil.h:180
std::vector< T > arrayToVector(const Array &array)
Definition: essentiautil.h:49
int indexOf(const std::vector< T > &v, const T &elem)
Definition: essentiautil.h:65
float Real
Definition: types.h:69
void fastcopy< StereoSample >(StereoSample *dest, const StereoSample *src, int n)
Definition: essentiautil.h:192
Eigen::Tensor< T, 4, Eigen::RowMajor > Tensor
Definition: types.h:384
bool contains(const std::vector< T > &v, const T &elem)
Definition: essentiautil.h:81