17 #ifndef HPP_STATISTICS_DISTRIBUTION_HH 18 # define HPP_STATISTICS_DISTRIBUTION_HH 28 namespace statistics {
29 template <
typename Value_t >
34 typedef typename std::pair < Weight_t, Value_t>
ProbaTPair;
35 typedef typename std::vector < ProbaTPair >::iterator
iterator;
41 assert (values_.size() > 0);
42 Weight_t r = rand() % cumulative_weights_.back();
46 return operator() ((Weight_t)(p * cumulative_weights_.back ()));
49 assert (values_.size() > 0);
50 return values_[dichotomy (w % cumulative_weights_.back())].second;
56 void insert (Value_t v, Weight_t w = 1) {
57 iterator itv = values_.begin ();
58 WeightsIterator itcumul = cumulative_weights_.begin();
60 while (itv != values_.end()) {
66 if (itv == values_.end ()) {
68 cumulative_weights_.push_back (c + w);
69 if (cumulative_weights_.back () >= INT_MAX / 2)
74 while (itv != values_.end()) {
79 if (cumulative_weights_.back () >= INT_MAX / 2)
85 Weight_t
get (
const Value_t& v)
const {
86 const_iterator itv = values_.begin ();
87 while (itv != values_.end()) {
97 return values_.size ();
102 if (values_.empty ())
return std::vector < Proba_t > (0);
103 std::vector < Proba_t > proba (values_.size());
104 Proba_t total = (double)cumulative_weights_.back();
105 for (
size_t i = 0; i < values_.size (); i++)
106 proba[i] = (
Proba_t)values_[i].first / total;
111 std::vector < Value_t >
values ()
const {
112 if (values_.empty ())
return std::vector < Value_t > (0);
113 std::vector < Value_t > v (values_.size());
114 for (
size_t i = 0; i < values_.size (); i++)
115 v[i] = values_[i].second;
121 if (cumulative_weights_.empty ())
return 0;
122 return cumulative_weights_.back ();
129 return values_.begin ();
132 return values_.begin ();
135 return values_.end ();
137 const_iterator
end ()
const {
138 return values_.end ();
142 size_t dichotomy (
const Weight_t& r)
const {
143 if (r < cumulative_weights_[0])
return 0;
144 size_t l = 0, h = values_.size () - 1, m;
147 if (cumulative_weights_[m] <= r)
149 else if (cumulative_weights_[m] > r)
159 iterator itv = values_.begin ();
160 WeightsIterator itcumul = cumulative_weights_.begin();
162 while (itv != values_.end()) {
163 if (itv->first > 1) {
164 itv->first = itv->first >> 1;
172 std::vector < ProbaTPair > values_;
173 std::vector < Weight_t > cumulative_weights_;
175 typedef std::vector < Weight_t >::iterator WeightsIterator;
180 #endif // HPP_STATISTICS_DISTRIBUTION_HH size_t size() const
Return the number of value.
Definition: distribution.hh:96
Value_t operator()() const
Definition: distribution.hh:40
double Proba_t
Definition: fwd.hh:22
std::vector< ProbaTPair >::iterator iterator
Definition: distribution.hh:35
std::vector< Value_t > values() const
Return the values.
Definition: distribution.hh:111
iterator end()
Definition: distribution.hh:134
std::size_t Weight_t
Definition: distribution.hh:33
const_iterator end() const
Definition: distribution.hh:137
iterator begin()
Definition: distribution.hh:128
const_iterator begin() const
Definition: distribution.hh:131
std::pair< Weight_t, Value_t > ProbaTPair
Definition: distribution.hh:34
std::vector< ProbaTPair >::const_iterator const_iterator
Definition: distribution.hh:36
DiscreteDistribution()
Definition: distribution.hh:38
Definition: distribution.hh:30
std::vector< Proba_t > probabilities() const
Return the probabilities.
Definition: distribution.hh:101
Weight_t totalWeight() const
Return the total weight.
Definition: distribution.hh:120
void insert(Value_t v, Weight_t w=1)
Definition: distribution.hh:56