Essentia  2.1-beta6-dev
pool.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_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  std::map<std::string, Tensor<Real> > _poolSingleTensorReal;
105 
106  // maps for vectors of values:
108  PoolOf(std::vector<Real>) _poolVectorReal;
109  PoolOf(std::string) _poolString;
110  PoolOf(std::vector<std::string>) _poolVectorString;
114 
115  // WARNING: this function assumes that all sub-pools are locked
116  std::vector<std::string> descriptorNamesNoLocking() const;
117 
122  void validateKey(const std::string& name);
123 
124 
125  public:
126 
131 
154  void add(const std::string& name, const Real& value, bool validityCheck = false);
155 
157  void add(const std::string& name, const std::vector<Real>& value, bool validityCheck = false);
158 
160  void add(const std::string& name, const std::string& value, bool validityCheck = false);
161 
163  void add(const std::string& name, const std::vector<std::string>& value, bool validityCheck = false);
164 
166  void add(const std::string& name, const TNT::Array2D<Real>& value, bool validityCheck = false);
167 
169  void add(const std::string& name, const Tensor<Real>& value, bool validityCheck = false);
170 
172  void add(const std::string& name, const StereoSample& value, bool validityCheck = false);
173 
179  template <typename T>
180  void append(const std::string& name, const std::vector<T>& values);
181 
202  void set(const std::string& name, const Real& value, bool validityCheck=false);
203 
205  void set(const std::string& name, const std::vector<Real>& value, bool validityCheck=false);
206 
208  void set(const std::string& name, const std::string& value, bool validityCheck=false);
209 
211  void set(const std::string& name, const std::vector<std::string>& value, bool validityCheck=false);
212 
214  void set(const std::string& name, const Tensor<Real>& value, bool validityCheck=false);
215 
236  void merge(Pool& p, const std::string& type="");
237 
243  void merge(const std::string& name, const std::vector<Real>& value, const std::string& type="");
244 
246  void merge(const std::string& name, const std::vector<std::vector<Real> >& value, const std::string& type="");
247 
249  void merge(const std::string& name, const std::vector<std::string>& value, const std::string& type="");
250 
252  void merge(const std::string& name, const std::vector<std::vector<std::string> >& value, const std::string& type="");
253 
255  void merge(const std::string& name, const std::vector<TNT::Array2D<Real> >& value, const std::string& type="");
256 
258  void merge(const std::string& name, const std::vector<Tensor<Real> >& value, const std::string& type="");
259 
261  void merge(const std::string& name, const std::vector<StereoSample>& value, const std::string& type="");
262 
264  void mergeSingle(const std::string& name, const Real& value, const std::string& type="");
266  void mergeSingle(const std::string& name, const std::vector<Real>& value, const std::string& type="");
268  void mergeSingle(const std::string& name, const std::string& value, const std::string& type="");
270  void mergeSingle(const std::string& name, const std::vector<std::string>& value, const std::string& type="");
272  void mergeSingle(const std::string& name, const Tensor<Real> & value, const std::string& type="");
279  void remove(const std::string& name);
280 
287  void removeNamespace(const std::string& ns);
288 
294  template <typename T>
295  const T& value(const std::string& name) const;
296 
302  template <typename T>
303  bool contains(const std::string& name) const;
304 
308  std::vector<std::string> descriptorNames() const;
309 
314  std::vector<std::string> descriptorNames(const std::string& ns) const;
315 
320  const PoolOf(Real)& getRealPool() const { return _poolReal; }
321 
326  const PoolOf(std::vector<Real>)& getVectorRealPool() const { return _poolVectorReal; }
327 
332  const PoolOf(std::string)& getStringPool() const { return _poolString; }
333 
338  const PoolOf(std::vector<std::string>)& getVectorStringPool() const { return _poolVectorString; }
339 
345 
351 
357 
362  const std::map<std::string, Real>& getSingleRealPool() const { return _poolSingleReal; }
363 
368  const std::map<std::string, std::string>& getSingleStringPool() const { return _poolSingleString; }
369 
374  const std::map<std::string, std::vector<Real> >& getSingleVectorRealPool() const { return _poolSingleVectorReal; }
375 
380  const std::map<std::string, std::vector<std::string> >& getSingleVectorStringPool() const { return _poolSingleVectorString; }
381 
386  const std::map<std::string, Tensor<Real> >& getSingleTensorRealPool() const { return _poolSingleTensorReal; }
387 
392  void checkIntegrity() const;
393 
397  void clear();
398 
403  bool isSingleValue(const std::string& name);
404 };
405 
406 
407 // make doxygen skip the macros
409 
410 // T& Pool::value(const DescriptorName& name)
411 #define SPECIALIZE_VALUE(type, tname) \
412 template <> \
413 inline const type& Pool::value(const std::string& name) const { \
414  MutexLocker lock(mutex##tname); \
415  std::map<std::string,type >::const_iterator result = _pool##tname.find(name);\
416  if (result == _pool##tname.end()) { \
417  std::ostringstream msg; \
418  msg << "Descriptor name '" << name << "' of type " \
419  << nameOfType(typeid(type)) << " not found"; \
420  throw EssentiaException(msg); \
421  } \
422  return result->second; \
423 }
424 
425 SPECIALIZE_VALUE(Real, SingleReal);
426 SPECIALIZE_VALUE(std::string, SingleString);
427 //SPECIALIZE_VALUE(std::vector<std::string>, String);
428 SPECIALIZE_VALUE(std::vector<std::vector<Real> >, VectorReal);
429 SPECIALIZE_VALUE(std::vector<std::vector<std::string> >, VectorString);
430 SPECIALIZE_VALUE(std::vector<TNT::Array2D<Real> >, Array2DReal);
431 SPECIALIZE_VALUE(std::vector<Tensor<Real> >, TensorReal);
432 SPECIALIZE_VALUE(Tensor<Real>, SingleTensorReal);
433 SPECIALIZE_VALUE(std::vector<StereoSample>, StereoSample);
434 
435 // This value function is not under the macro above because it needs to check
436 // in two separate sub-pools (poolReal and poolSingleVectorReal)
437 template<>
438 inline const std::vector<Real>& Pool::value(const std::string& name) const {
439  std::map<std::string, std::vector<Real> >::const_iterator result;
440  {
441  MutexLocker lock(mutexReal);
442  result = _poolReal.find(name);
443  if (result != _poolReal.end()) {
444  return result->second;
445  }
446  }
447 
448  {
449  MutexLocker lock(mutexSingleVectorReal);
450  result = _poolSingleVectorReal.find(name);
451  if (result != _poolSingleVectorReal.end()) {
452  return result->second;
453  }
454  }
455 
456  std::ostringstream msg;
457  msg << "Descriptor name '" << name << "' of type "
458  << nameOfType(typeid(std::vector<Real>)) << " not found";
459  throw EssentiaException(msg);
460 }
461 
462 // This value function is not under the macro above because it needs to check
463 // in two separate sub-pools (poolString and poolSingleVectorString)
464 template<>
465 inline const std::vector<std::string>& Pool::value(const std::string& name) const {
466  std::map<std::string, std::vector<std::string> >::const_iterator result;
467  {
468  MutexLocker lock(mutexString);
469  result = _poolString.find(name);
470  if (result != _poolString.end()) {
471  return result->second;
472  }
473  }
474 
475  {
476  MutexLocker lock(mutexSingleVectorString);
477  result = _poolSingleVectorString.find(name);
478  if (result != _poolSingleVectorString.end()) {
479  return result->second;
480  }
481  }
482 
483  std::ostringstream msg;
484  msg << "Descriptor name '" << name << "' of type "
485  << nameOfType(typeid(std::vector<std::string>)) << " not found";
486  throw EssentiaException(msg);
487 }
488 
489 // bool Pool::contains(const DescriptorName& name)
490 #define SPECIALIZE_CONTAINS(type, tname) \
491 template <> \
492 inline bool Pool::contains<type>(const std::string& name) const { \
493  MutexLocker lock(mutex##tname); \
494  std::map<std::string,type >::const_iterator result = _pool##tname.find(name);\
495  if (result == _pool##tname.end()) { \
496  return false; \
497  } \
498  return true; \
499 }
500 
501 SPECIALIZE_CONTAINS(Real, SingleReal);
502 SPECIALIZE_CONTAINS(std::string, SingleString);
503 //SPECIALIZE_CONTAINS(std::vector<std::string>, String);
504 SPECIALIZE_CONTAINS(std::vector<std::vector<Real> >, VectorReal);
505 SPECIALIZE_CONTAINS(std::vector<std::vector<std::string> >, VectorString);
506 SPECIALIZE_CONTAINS(std::vector<TNT::Array2D<Real> >, Array2DReal);
507 SPECIALIZE_CONTAINS(std::vector<Tensor<Real> >, TensorReal);
508 SPECIALIZE_CONTAINS(Tensor<Real> , SingleTensorReal);
509 SPECIALIZE_CONTAINS(std::vector<StereoSample>, StereoSample);
510 
511 // This value function is not under the macro above because it needs to check
512 // in two separate sub-pools (poolReal and poolSingleVectorReal)
513 template<>
514 inline bool Pool::contains<std::vector<Real> >(const std::string& name) const {
515  std::map<std::string, std::vector<Real> >::const_iterator result;
516  {
517  MutexLocker lock(mutexReal);
518  result = _poolReal.find(name);
519  if (result != _poolReal.end()) {
520  return true;
521  }
522  }
523 
524  {
525  MutexLocker lock(mutexSingleVectorReal);
526  result = _poolSingleVectorReal.find(name);
527  if (result != _poolSingleVectorReal.end()) {
528  return true;
529  }
530  }
531 
532  return false;
533 }
534 
535 
536 // This value function is not under the macro above because it needs to check
537 // in two separate sub-pools (poolString and poolSingleVectorString)
538 template<>
539 inline bool Pool::contains<std::vector<std::string> >(const std::string& name) const {
540  std::map<std::string, std::vector<std::string> >::const_iterator result;
541  {
542  MutexLocker lock(mutexString);
543  result = _poolString.find(name);
544  if (result != _poolString.end()) {
545  return true;
546  }
547  }
548 
549  {
550  MutexLocker lock(mutexSingleVectorString);
551  result = _poolSingleVectorString.find(name);
552  if (result != _poolSingleVectorString.end()) {
553  return true;
554  }
555  }
556 
557  return false;
558 }
559 
560 // Used to get a lock over all sub-pools, make sure to update this when adding
561 // a new sub-pool
562 #define GLOBAL_LOCK \
563 MutexLocker lockReal(mutexReal); \
564 MutexLocker lockVectorReal(mutexVectorReal); \
565 MutexLocker lockString(mutexString); \
566 MutexLocker lockVectorString(mutexVectorString); \
567 MutexLocker lockArray2DReal(mutexArray2DReal); \
568 MutexLocker lockTensorReal(mutexTensorReal); \
569 MutexLocker lockStereoSample(mutexStereoSample); \
570 MutexLocker lockSingleReal(mutexSingleReal); \
571 MutexLocker lockSingleString(mutexSingleString); \
572 MutexLocker lockSingleVectorReal(mutexSingleVectorReal); \
573 MutexLocker lockSingleVectorString(mutexSingleVectorString);\
574 MutexLocker lockSingleTensorReal(mutexSingleTensorReal);
575 
576 
577 
578 template<typename T>
579 inline void Pool::append(const std::string& name, const std::vector<T>& values) {
580  throw EssentiaException("Pool::append not implemented for type: ", nameOfType(typeid(T)));
581 }
582 
583 #define SPECIALIZE_APPEND(type, tname) \
584 template <> \
585 inline void Pool::append(const std::string& name, const std::vector<type>& values) { \
586  { \
587  MutexLocker lock(mutex##tname); \
588  PoolOf(type)::iterator result = _pool##tname.find(name); \
589  if (result != _pool##tname.end()) { \
590  \
591  std::vector<type>& v = result->second; \
592  int vsize = v.size(); \
593  v.resize(vsize + values.size()); \
594  fastcopy(&v[vsize], &values[0], values.size()); \
595  return; \
596  } \
597  } \
598  \
599  GLOBAL_LOCK \
600  validateKey(name); \
601  _pool##tname[name] = values; \
602 }
603 
604 
605 SPECIALIZE_APPEND(Real, Real);
606 SPECIALIZE_APPEND(std::vector<Real>, VectorReal);
607 SPECIALIZE_APPEND(std::string, String);
608 SPECIALIZE_APPEND(std::vector<std::string>, VectorString);
609 SPECIALIZE_APPEND(StereoSample, StereoSample);
610 
612 
613 } // namespace essentia
614 
615 #endif // ESSENTIA_POOL_H
Definition: threading.h:51
Definition: threading.h:45
Definition: pool.h:96
Mutex mutexReal
Definition: pool.h:127
Mutex mutexTensorReal
Definition: pool.h:130
std::vector< std::string > descriptorNames(const std::string &ns) const
void add(const std::string &name, const TNT::Array2D< Real > &value, bool validityCheck=false)
std::map< std::string, std::vector< Tensor< Real > > > _poolTensorReal
Definition: pool.h:112
void merge(const std::string &name, const std::vector< std::vector< Real > > &value, const std::string &type="")
Merges the values given in value into the current pool's descriptor given by name.
void mergeSingle(const std::string &name, const std::string &value, const std::string &type="")
Merges the values given in value into the current pool's descriptor given by name.
std::map< std::string, std::vector< std::string > > _poolString
Definition: pool.h:109
const std::map< std::string, std::vector< std::string > > & getSingleVectorStringPool() const
Definition: pool.h:380
void merge(const std::string &name, const std::vector< Tensor< Real > > &value, const std::string &type="")
Merges the values given in value into the current pool's descriptor given by name.
void mergeSingle(const std::string &name, const Real &value, const std::string &type="")
Merges the values given in value into the current pool's descriptor given by name.
void append(const std::string &name, const std::vector< T > &values)
const std::map< std::string, std::vector< std::vector< Real > > > & getVectorRealPool() const
Definition: pool.h:326
Mutex mutexString
Definition: pool.h:127
const T & value(const std::string &name) const
std::map< std::string, std::vector< TNT::Array2D< Real > > > _poolArray2DReal
Definition: pool.h:111
const std::map< std::string, std::vector< TNT::Array2D< Real > > > & getArray2DRealPool() const
Definition: pool.h:344
std::map< std::string, std::vector< Real > > _poolSingleVectorReal
Definition: pool.h:102
const std::map< std::string, std::vector< std::string > > & getStringPool() const
Definition: pool.h:332
std::map< std::string, std::vector< std::vector< Real > > > _poolVectorReal
Definition: pool.h:108
void set(const std::string &name, const std::string &value, bool validityCheck=false)
Sets the value of a descriptor name.
std::map< std::string, Tensor< Real > > _poolSingleTensorReal
Definition: pool.h:104
Mutex mutexStereoSample
Definition: pool.h:128
void remove(const std::string &name)
void checkIntegrity() const
void add(const std::string &name, const Real &value, bool validityCheck=false)
std::vector< std::string > descriptorNamesNoLocking() const
Mutex mutexArray2DReal
Definition: pool.h:128
std::map< std::string, std::vector< StereoSample > > _poolStereoSample
Definition: pool.h:113
const std::map< std::string, Real > & getSingleRealPool() const
Definition: pool.h:362
void set(const std::string &name, const Real &value, bool validityCheck=false)
Sets the value of a descriptor name.
std::map< std::string, std::vector< std::string > > _poolSingleVectorString
Definition: pool.h:103
std::map< std::string, std::vector< Real > > _poolReal
Definition: pool.h:107
std::map< std::string, std::string > _poolSingleString
Definition: pool.h:101
void mergeSingle(const std::string &name, const Tensor< Real > &value, const std::string &type="")
Merges the values given in value into the current pool's descriptor given by name.
Mutex mutexSingleTensorReal
Definition: pool.h:130
void add(const std::string &name, const std::string &value, bool validityCheck=false)
void mergeSingle(const std::string &name, const std::vector< std::string > &value, const std::string &type="")
Merges the values given in value into the current pool's descriptor given by name.
void set(const std::string &name, const std::vector< Real > &value, bool validityCheck=false)
Sets the value of a descriptor name.
void merge(const std::string &name, const std::vector< TNT::Array2D< Real > > &value, const std::string &type="")
Merges the values given in value into the current pool's descriptor given by name.
void merge(const std::string &name, const std::vector< std::string > &value, const std::string &type="")
Merges the values given in value into the current pool's descriptor given by name.
void add(const std::string &name, const StereoSample &value, bool validityCheck=false)
void validateKey(const std::string &name)
Mutex mutexSingleReal
Definition: pool.h:129
std::vector< std::string > descriptorNames() const
void mergeSingle(const std::string &name, const std::vector< Real > &value, const std::string &type="")
Merges the values given in value into the current pool's descriptor given by name.
std::map< std::string, Real > _poolSingleReal
Definition: pool.h:100
const std::map< std::string, std::vector< std::vector< std::string > > > & getVectorStringPool() const
Definition: pool.h:338
void set(const std::string &name, const std::vector< std::string > &value, bool validityCheck=false)
Sets the value of a descriptor name.
Mutex mutexVectorReal
Definition: pool.h:127
void set(const std::string &name, const Tensor< Real > &value, bool validityCheck=false)
const std::map< std::string, std::vector< Real > > & getSingleVectorRealPool() const
Definition: pool.h:374
Mutex mutexSingleVectorString
Definition: pool.h:130
Mutex mutexSingleVectorReal
Definition: pool.h:129
void merge(Pool &p, const std::string &type="")
Merges the current pool with the given one p.
void merge(const std::string &name, const std::vector< std::vector< std::string > > &value, const std::string &type="")
Merges the values given in value into the current pool's descriptor given by name.
const std::map< std::string, std::vector< Real > > & getRealPool() const
Definition: pool.h:320
void add(const std::string &name, const std::vector< std::string > &value, bool validityCheck=false)
Mutex mutexVectorString
Definition: pool.h:127
bool isSingleValue(const std::string &name)
void removeNamespace(const std::string &ns)
Mutex mutexSingleString
Definition: pool.h:129
const std::map< std::string, std::string > & getSingleStringPool() const
Definition: pool.h:368
void merge(const std::string &name, const std::vector< StereoSample > &value, const std::string &type="")
Merges the values given in value into the current pool's descriptor given by name.
void merge(const std::string &name, const std::vector< Real > &value, const std::string &type="")
Merges the values given in value into the current pool's descriptor given by name.
bool contains(const std::string &name) const
const std::map< std::string, Tensor< Real > > & getSingleTensorRealPool() const
Definition: pool.h:386
std::map< std::string, std::vector< std::vector< std::string > > > _poolVectorString
Definition: pool.h:110
const std::map< std::string, std::vector< StereoSample > > & getStereoSamplePool() const
Definition: pool.h:356
const std::map< std::string, std::vector< Tensor< Real > > > & getTensorRealPool() const
Definition: pool.h:350
void add(const std::string &name, const Tensor< Real > &value, bool validityCheck=false)
void add(const std::string &name, const std::vector< Real > &value, bool validityCheck=false)
Definition: algorithm.h:28
std::string nameOfType(const std::type_info &type)
Tuple2< Real > StereoSample
Definition: types.h:371
std::string DescriptorName
Definition: pool.h:35
float Real
Definition: types.h:69
Eigen::Tensor< T, 4, Eigen::RowMajor > Tensor
Definition: types.h:384
#define PoolOf(type)
Definition: pool.h:33