Essentia  2.1-beta5-dev
essentiautil.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2006-2016 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 #ifdef OS_WIN32
161 int mkstemp(char *tmpl);
162 #endif // OS_WIN32
163 
164 
165 
166 
171 template <typename T>
172 inline void fastcopy(T* dest, const T* src, int n) {
173  for (int i=0; i<n; i++) {
174  *dest++ = *src++;
175  }
176 }
177 
178 template <>
179 inline void fastcopy<Real>(Real* dest, const Real* src, int n) {
180  memcpy(dest, src, n*sizeof(Real));
181 }
182 
183 template <>
184 inline void fastcopy<StereoSample>(StereoSample* dest, const StereoSample* src, int n) {
185  memcpy(dest, src, n*sizeof(StereoSample));
186 }
187 
188 template <>
189 inline void fastcopy<int>(int* dest, const int* src, int n) {
190  memcpy(dest, src, n*sizeof(int));
191 }
192 
193 // overload for iterators, which allow us to fastcopy(dest.begin(), src.begin(), 0) and not crash
194 inline void fastcopy(std::vector<Real>::iterator dest, std::vector<Real>::const_iterator src, int n) {
195  // need to test this because otherwise it is not legal to dereference the iterator
196  if (n > 0) {
197  fastcopy(&*dest, &*src, n);
198  }
199 }
200 
201 inline void fastcopy(std::vector<StereoSample>::iterator dest, std::vector<StereoSample>::const_iterator src, int n) {
202  if (n > 0) {
203  fastcopy(&*dest, &*src, n);
204  }
205 }
206 
207 } // namespace essentia
208 
209 #endif // ESSENTIA_UTILS_H
int dim1() const
Definition: tnt_array2d.h:231
bool isValid(const T &value)
Definition: essentiautil.h:117
Definition: types.h:128
void fastcopy< StereoSample >(StereoSample *dest, const StereoSample *src, int n)
Definition: essentiautil.h:184
void fastcopy(T *dest, const T *src, int n)
Definition: essentiautil.h:172
void fastcopy< Real >(Real *dest, const Real *src, int n)
Definition: essentiautil.h:179
#define ARRAY_SIZE(arr)
Definition: essentiautil.h:36
Definition: tnt_array2d.h:37
bool contains(const std::vector< T > &v, const T &elem)
Definition: essentiautil.h:81
std::vector< T > arrayToVector(const Array &array)
Definition: essentiautil.h:49
int dim2() const
Definition: tnt_array2d.h:234
float Real
Definition: types.h:68
Definition: types.h:351
int indexOf(const std::vector< T > &v, const T &elem)
Definition: essentiautil.h:65
Definition: algorithm.h:28
int size() const
Definition: types.h:132
const T & right() const
Definition: types.h:357
const T & left() const
Definition: types.h:356
void fastcopy< int >(int *dest, const int *src, int n)
Definition: essentiautil.h:189