diff options
| author | Christian Glöckner <cgloeckner@freenet.de> | 2016-03-29 17:43:58 +0200 | 
|---|---|---|
| committer | André Nusser <andre.nusser@googlemail.com> | 2016-03-31 17:49:56 +0200 | 
| commit | 68dc4b912f0a950a9db4987432c559dd1b0a0ebf (patch) | |
| tree | 9a3aa55513aa50ac48be39c0b060f771600fa552 /src | |
| parent | cf9874dfa5b528c0d6184aa5bb04b908272f2dcb (diff) | |
API Refactoring for class Velocity
Diffstat (limited to 'src')
| -rw-r--r-- | src/velocity.cc | 51 | ||||
| -rw-r--r-- | src/velocity.h | 24 | 
2 files changed, 40 insertions, 35 deletions
| diff --git a/src/velocity.cc b/src/velocity.cc index 0ee00df..e08ab9b 100644 --- a/src/velocity.cc +++ b/src/velocity.cc @@ -29,33 +29,40 @@  #include <stdlib.h>  Velocity::Velocity(unsigned int lower, unsigned int upper) +	: lower{lower} +	, upper{upper} +	, samples{}  { -  this->lower = lower; -  this->upper = upper;  } -void Velocity::addSample(Sample *sample, float probability) +void Velocity::addSample(Sample* sample, float probability)  { -  if(samples.find(sample) != samples.end()) { -    samples[sample] += probability; -  } else { -    samples[sample] = probability; -  } +	if(samples.find(sample) != samples.end()) +	{ +		samples[sample] += probability; +	} +	else +	{ +		samples[sample] = probability; +	}  } -Sample *Velocity::getSample() +Sample* Velocity::getSample() const  { -  Sample *sample = NULL; - -  float x = rand.floatInRange(0, 1); -  float sum = 0.0; -   -  Samples::iterator i = samples.begin(); -  while(i != samples.end() && x > sum) { -    sum += i->second; -    sample = i->first; -    i++; -  } - -  return sample; +	Sample* sample{nullptr}; + +	float x = rand.floatInRange(0, 1); +	float sum = 0.0; + +	for (auto const & pair: samples) +	{ +		if (x > sum) +		{ +			break; +		} +		sum += pair.second; +		sample = pair.first; +	} + +	return sample;  } diff --git a/src/velocity.h b/src/velocity.h index c62ddad..41622d3 100644 --- a/src/velocity.h +++ b/src/velocity.h @@ -24,29 +24,27 @@   *  along with DrumGizmo; if not, write to the Free Software   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.   */ -#ifndef __DRUMGIZMO_VELOCITY_H__ -#define __DRUMGIZMO_VELOCITY_H__ +#pragma once  #include <map>  #include "sample.h"  #include "random.h" -class Velocity { +class Velocity +{  public: -  Velocity(unsigned int lower, unsigned int upper); +	Velocity(unsigned int lower, unsigned int upper); -  void addSample(Sample *sample, float probability); -  Sample *getSample(); +	void addSample(Sample* sample, float probability); +	Sample* getSample() const; -  unsigned int lower; -  unsigned int upper; +	unsigned int lower; +	unsigned int upper;  private: -  typedef std::map< Sample *, float > Samples; -  Samples samples; +	typedef std::map<Sample*, float> Samples; +	Samples samples; -  Random rand; +	Random rand;  }; - -#endif/*__DRUMGIZMO_VELOCITY_H__*/ | 
