Essentia  2.1-beta5-dev
pool.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_POOL_H
21 #define ESSENTIA_POOL_H
22 
23 #include "types.h"
24 #include "threading.h"
25 #include "utils/tnt/tnt.h"
26 #include "essentiautil.h"
27 
28 namespace essentia {
29 
30 // standard map and not EssentiaMap because we want a new element
31 // to be automatically created when it doesn't exist
33 #define PoolOf(type) std::map<std::string, std::vector<type > >
34 
35 typedef std::string DescriptorName;
36 
96 class Pool {
97 
98  protected:
99  // maps for single values:
100  std::map<std::string, Real> _poolSingleReal;
101  std::map<std::string, std::string> _poolSingleString;
102  std::map<std::string, std::vector<Real> > _poolSingleVectorReal;
103  std::map<std::string, std::vector<std::string> > _poolSingleVectorString;
104 
105  // maps for vectors of values:
107  PoolOf(std::vector<Real>) _poolVectorReal;
108  PoolOf(std::string) _poolString;
109  PoolOf(std::vector<std::string>) _poolVectorString;
112 
113  // WARNING: this function assumes that all sub-pools are locked
114  std::vector<std::string> descriptorNamesNoLocking() const;
115 
120  void validateKey(const std::string& name);
121 
122 
123  public:
124 
128 
151  void add(const std::string& name, const Real& value, bool validityCheck = false);
152 
154  void add(const std::string& name, const std::vector<Real>& value, bool validityCheck = false);
155 
157  void add(const std::string& name, const std::string& value, bool validityCheck = false);
158 
160  void add(const std::string& name, const std::vector<std::string>& value, bool validityCheck = false);
161 
163  void add(const std::string& name, const TNT::Array2D<Real>& value, bool validityCheck = false);
164 
166  void add(const std::string& name, const StereoSample& value, bool validityCheck = false);
167 
173  template <typename T>
174  void append(const std::string& name, const std::vector<T>& values);
175 
196  void set(const std::string& name, const Real& value, bool validityCheck=false);
197 
199  void set(const std::string& name, const std::vector<Real>& value, bool validityCheck=false);
200 
202  void set(const std::string& name, const std::string& value, bool validityCheck=false);
203 
205  void set(const std::string& name, const std::vector<std::string>& value, bool validityCheck=false);
206 
227  void merge(Pool& p, const std::string& type="");
228 
234  void merge(const std::string& name, const std::vector<Real>& value, const std::string& type="");
235 
237  void merge(const std::string& name, const std::vector<std::vector<Real> >& value, const std::string& type="");
238 
240  void merge(const std::string& name, const std::vector<std::string>& value, const std::string& type="");
241 
243  void merge(const std::string& name, const std::vector<std::vector<std::string> >& value, const std::string& type="");
244 
246  void merge(const std::string& name, const std::vector<TNT::Array2D<Real> >& value, const std::string& type="");
247 
249  void merge(const std::string& name, const std::vector<StereoSample>& value, const std::string& type="");
250 
252  void mergeSingle(const std::string& name, const Real& value, const std::string& type="");
254  void mergeSingle(const std::string& name, const std::vector<Real>& value, const std::string& type="");
256  void mergeSingle(const std::string& name, const std::string& value, const std::string& type="");
258  void mergeSingle(const std::string& name, const std::vector<std::string>& value, const std::string& type="");
265  void remove(const std::string& name);
266 
273  void removeNamespace(const std::string& ns);
274 
280  template <typename T>
281  const T& value(const std::string& name) const;
282 
288  template <typename T>
289  bool contains(const std::string& name) const;
290 
294  std::vector<std::string> descriptorNames() const;
295 
300  std::vector<std::string> descriptorNames(const std::string& ns) const;
301 
306  const PoolOf(Real)& getRealPool() const { return _poolReal; }
307 
312  const PoolOf(std::vector<Real>)& getVectorRealPool() const { return _poolVectorReal; }
313 
318  const PoolOf(std::string)& getStringPool() const { return _poolString; }
319 
324  const PoolOf(std::vector<std::string>)& getVectorStringPool() const { return _poolVectorString; }
325 
331 
337 
342  const std::map<std::string, Real>& getSingleRealPool() const { return _poolSingleReal; }
343 
348  const std::map<std::string, std::string>& getSingleStringPool() const { return _poolSingleString; }
349 
354  const std::map<std::string, std::vector<Real> >& getSingleVectorRealPool() const { return _poolSingleVectorReal; }
355 
360  const std::map<std::string, std::vector<std::string> >& getSingleVectorStringPool() const { return _poolSingleVectorString; }
361 
366  void checkIntegrity() const;
367 
371  void clear();
372 
377  bool isSingleValue(const std::string& name);
378 };
379 
380 
381 // make doxygen skip the macros
383 
384 // T& Pool::value(const DescriptorName& name)
385 #define SPECIALIZE_VALUE(type, tname) \
386 template <> \
387 inline const type& Pool::value(const std::string& name) const { \
388  MutexLocker lock(mutex##tname); \
389  std::map<std::string,type >::const_iterator result = _pool##tname.find(name);\
390  if (result == _pool##tname.end()) { \
391  std::ostringstream msg; \
392  msg << "Descriptor name '" << name << "' of type " \
393  << nameOfType(typeid(type)) << " not found"; \
394  throw EssentiaException(msg); \
395  } \
396  return result->second; \
397 }
398 
399 SPECIALIZE_VALUE(Real, SingleReal);
400 SPECIALIZE_VALUE(std::string, SingleString);
401 //SPECIALIZE_VALUE(std::vector<std::string>, String);
402 SPECIALIZE_VALUE(std::vector<std::vector<Real> >, VectorReal);
403 SPECIALIZE_VALUE(std::vector<std::vector<std::string> >, VectorString);
404 SPECIALIZE_VALUE(std::vector<TNT::Array2D<Real> >, Array2DReal);
405 SPECIALIZE_VALUE(std::vector<StereoSample>, StereoSample);
406 
407 // This value function is not under the macro above because it needs to check
408 // in two separate sub-pools (poolReal and poolSingleVectorReal)
409 template<>
410 inline const std::vector<Real>& Pool::value(const std::string& name) const {
411  std::map<std::string, std::vector<Real> >::const_iterator result;
412  {
413  MutexLocker lock(mutexReal);
414  result = _poolReal.find(name);
415  if (result != _poolReal.end()) {
416  return result->second;
417  }
418  }
419 
420  {
422  result = _poolSingleVectorReal.find(name);
423  if (result != _poolSingleVectorReal.end()) {
424  return result->second;
425  }
426  }
427 
428  std::ostringstream msg;
429  msg << "Descriptor name '" << name << "' of type "
430  << nameOfType(typeid(std::vector<Real>)) << " not found";
431  throw EssentiaException(msg);
432 }
433 
434 // This value function is not under the macro above because it needs to check
435 // in two separate sub-pools (poolString and poolSingleVectorString)
436 template<>
437 inline const std::vector<std::string>& Pool::value(const std::string& name) const {
438  std::map<std::string, std::vector<std::string> >::const_iterator result;
439  {
440  MutexLocker lock(mutexString);
441  result = _poolString.find(name);
442  if (result != _poolString.end()) {
443  return result->second;
444  }
445  }
446 
447  {
449  result = _poolSingleVectorString.find(name);
450  if (result != _poolSingleVectorString.end()) {
451  return result->second;
452  }
453  }
454 
455  std::ostringstream msg;
456  msg << "Descriptor name '" << name << "' of type "
457  << nameOfType(typeid(std::vector<std::string>)) << " not found";
458  throw EssentiaException(msg);
459 }
460 
461 // bool Pool::contains(const DescriptorName& name)
462 #define SPECIALIZE_CONTAINS(type, tname) \
463 template <> \
464 inline bool Pool::contains<type>(const std::string& name) const { \
465  MutexLocker lock(mutex##tname); \
466  std::map<std::string,type >::const_iterator result = _pool##tname.find(name);\
467  if (result == _pool##tname.end()) { \
468  return false; \
469  } \
470  return true; \
471 }
472 
473 SPECIALIZE_CONTAINS(Real, SingleReal);
474 SPECIALIZE_CONTAINS(std::string, SingleString);
475 //SPECIALIZE_CONTAINS(std::vector<std::string>, String);
476 SPECIALIZE_CONTAINS(std::vector<std::vector<Real> >, VectorReal);
477 SPECIALIZE_CONTAINS(std::vector<std::vector<std::string> >, VectorString);
478 SPECIALIZE_CONTAINS(std::vector<TNT::Array2D<Real> >, Array2DReal);
479 SPECIALIZE_CONTAINS(std::vector<StereoSample>, StereoSample);
480 
481 // This value function is not under the macro above because it needs to check
482 // in two separate sub-pools (poolReal and poolSingleVectorReal)
483 template<>
484 inline bool Pool::contains<std::vector<Real> >(const std::string& name) const {
485  std::map<std::string, std::vector<Real> >::const_iterator result;
486  {
487  MutexLocker lock(mutexReal);
488  result = _poolReal.find(name);
489  if (result != _poolReal.end()) {
490  return true;
491  }
492  }
493 
494  {
496  result = _poolSingleVectorReal.find(name);
497  if (result != _poolSingleVectorReal.end()) {
498  return true;
499  }
500  }
501 
502  return false;
503 }
504 
505 
506 // This value function is not under the macro above because it needs to check
507 // in two separate sub-pools (poolString and poolSingleVectorString)
508 template<>
509 inline bool Pool::contains<std::vector<std::string> >(const std::string& name) const {
510  std::map<std::string, std::vector<std::string> >::const_iterator result;
511  {
512  MutexLocker lock(mutexString);
513  result = _poolString.find(name);
514  if (result != _poolString.end()) {
515  return true;
516  }
517  }
518 
519  {
521  result = _poolSingleVectorString.find(name);
522  if (result != _poolSingleVectorString.end()) {
523  return true;
524  }
525  }
526 
527  return false;
528 }
529 
530 // Used to get a lock over all sub-pools, make sure to update this when adding
531 // a new sub-pool
532 #define GLOBAL_LOCK \
533 MutexLocker lockReal(mutexReal); \
534 MutexLocker lockVectorReal(mutexVectorReal); \
535 MutexLocker lockString(mutexString); \
536 MutexLocker lockVectorString(mutexVectorString); \
537 MutexLocker lockArray2DReal(mutexArray2DReal); \
538 MutexLocker lockStereoSample(mutexStereoSample); \
539 MutexLocker lockSingleReal(mutexSingleReal); \
540 MutexLocker lockSingleString(mutexSingleString); \
541 MutexLocker lockSingleVectorReal(mutexSingleVectorReal); \
542 MutexLocker lockSingleVectorString(mutexSingleVectorString);
543 
544 
545 
546 template<typename T>
547 inline void Pool::append(const std::string& name, const std::vector<T>& values) {
548  throw EssentiaException("Pool::append not implemented for type: ", nameOfType(typeid(T)));
549 }
550 
551 #define SPECIALIZE_APPEND(type, tname) \
552 template <> \
553 inline void Pool::append(const std::string& name, const std::vector<type>& values) { \
554  { \
555  MutexLocker lock(mutex##tname); \
556  PoolOf(type)::iterator result = _pool##tname.find(name); \
557  if (result != _pool##tname.end()) { \
558  \
559  std::vector<type>& v = result->second; \
560  int vsize = v.size(); \
561  v.resize(vsize + values.size()); \
562  fastcopy(&v[vsize], &values[0], values.size()); \
563  return; \
564  } \
565  } \
566  \
567  GLOBAL_LOCK \
568  validateKey(name); \
569  _pool##tname[name] = values; \
570 }
571 
572 
573 SPECIALIZE_APPEND(Real, Real);
574 SPECIALIZE_APPEND(std::vector<Real>, VectorReal);
575 SPECIALIZE_APPEND(std::string, String);
576 SPECIALIZE_APPEND(std::vector<std::string>, VectorString);
577 SPECIALIZE_APPEND(StereoSample, StereoSample);
578 
580 
581 } // namespace essentia
582 
583 #endif // ESSENTIA_POOL_H
void removeNamespace(const std::string &ns)
std::map< std::string, std::vector< std::vector< Real > > > _poolVectorReal
Definition: pool.h:107
Mutex mutexVectorString
Definition: pool.h:125
const T & value(const std::string &name) const
void append(const std::string &name, const std::vector< T > &values)
const std::map< std::string, std::vector< StereoSample > > & getStereoSamplePool() const
Definition: pool.h:336
const std::map< std::string, std::vector< TNT::Array2D< Real > > > & getArray2DRealPool() const
Definition: pool.h:330
const std::map< std::string, std::vector< std::string > > & getSingleVectorStringPool() const
Definition: pool.h:360
std::map< std::string, std::string > _poolSingleString
Definition: pool.h:101
Mutex mutexVectorReal
Definition: pool.h:125
std::string nameOfType(const std::type_info &type)
const std::map< std::string, std::vector< std::vector< std::string > > > & getVectorStringPool() const
Definition: pool.h:324
void checkIntegrity() const
std::map< std::string, std::vector< TNT::Array2D< Real > > > _poolArray2DReal
Definition: pool.h:110
Mutex mutexSingleVectorString
Definition: pool.h:125
Definition: pool.h:96
std::map< std::string, std::vector< StereoSample > > _poolStereoSample
Definition: pool.h:111
Mutex mutexString
Definition: pool.h:125
void add(const std::string &name, const Real &value, bool validityCheck=false)
std::map< std::string, Real > _poolSingleReal
Definition: pool.h:100
const std::map< std::string, std::vector< Real > > & getRealPool() const
Definition: pool.h:306
#define PoolOf(type)
Definition: pool.h:33
float Real
Definition: types.h:68
std::map< std::string, std::vector< Real > > _poolReal
Definition: pool.h:106
Mutex mutexReal
Definition: pool.h:125
void mergeSingle(const std::string &name, const Real &value, const std::string &type="")
Merges the values given in value into the current pool&#39;s descriptor given by name.
bool isSingleValue(const std::string &name)
const std::map< std::string, Real > & getSingleRealPool() const
Definition: pool.h:342
Mutex mutexSingleString
Definition: pool.h:125
std::vector< std::string > descriptorNamesNoLocking() const
std::map< std::string, std::vector< std::string > > _poolSingleVectorString
Definition: pool.h:103
Definition: types.h:351
Definition: algorithm.h:28
bool contains(const std::string &name) const
Definition: types.h:76
std::map< std::string, std::vector< std::string > > _poolString
Definition: pool.h:108
std::string DescriptorName
Definition: pool.h:35
Mutex mutexStereoSample
Definition: pool.h:125
Mutex mutexArray2DReal
Definition: pool.h:125
Mutex mutexSingleReal
Definition: pool.h:125
Definition: threading.h:51
Mutex mutexSingleVectorReal
Definition: pool.h:125
const std::map< std::string, std::string > & getSingleStringPool() const
Definition: pool.h:348
std::vector< std::string > descriptorNames() const
Definition: threading.h:45
void validateKey(const std::string &name)
const std::map< std::string, std::vector< Real > > & getSingleVectorRealPool() const
Definition: pool.h:354
const std::map< std::string, std::vector< std::vector< Real > > > & getVectorRealPool() const
Definition: pool.h:312
void merge(Pool &p, const std::string &type="")
Merges the current pool with the given one p.
std::map< std::string, std::vector< Real > > _poolSingleVectorReal
Definition: pool.h:102
std::map< std::string, std::vector< std::vector< std::string > > > _poolVectorString
Definition: pool.h:109
const std::map< std::string, std::vector< std::string > > & getStringPool() const
Definition: pool.h:318