2012-05-23 14:56:01 +01:00
|
|
|
#include <cassert>
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <numeric>
|
|
|
|
|
|
|
|
#include "Histogram.h"
|
|
|
|
#include "Util.h"
|
|
|
|
|
|
|
|
|
2013-02-08 21:12:46 +00:00
|
|
|
namespace Horus {
|
2013-02-07 23:53:13 +00:00
|
|
|
|
2012-05-23 14:56:01 +01:00
|
|
|
HistogramSet::HistogramSet (unsigned size, unsigned range)
|
|
|
|
{
|
|
|
|
size_ = size;
|
|
|
|
hist_.resize (range, 0);
|
|
|
|
hist_[0] = size;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
HistogramSet::nextHistogram (void)
|
|
|
|
{
|
2012-05-24 22:55:20 +01:00
|
|
|
for (size_t i = hist_.size() - 1; i-- > 0; ) {
|
2012-05-23 14:56:01 +01:00
|
|
|
if (hist_[i] > 0) {
|
|
|
|
hist_[i] --;
|
|
|
|
hist_[i + 1] = maxCount (i + 1);
|
|
|
|
clearAfter (i + 1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2012-05-24 22:55:20 +01:00
|
|
|
assert (std::accumulate (hist_.begin(), hist_.end(), 0)
|
|
|
|
== (int) size_);
|
2012-05-23 14:56:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
unsigned
|
2012-05-24 22:55:20 +01:00
|
|
|
HistogramSet::operator[] (size_t idx) const
|
2012-05-23 14:56:01 +01:00
|
|
|
{
|
|
|
|
assert (idx < hist_.size());
|
|
|
|
return hist_[idx];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
unsigned
|
|
|
|
HistogramSet::nrHistograms (void) const
|
|
|
|
{
|
|
|
|
return HistogramSet::nrHistograms (size_, hist_.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
HistogramSet::reset (void)
|
|
|
|
{
|
|
|
|
std::fill (hist_.begin() + 1, hist_.end(), 0);
|
|
|
|
hist_[0] = size_;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-02-07 13:37:15 +00:00
|
|
|
std::vector<Histogram>
|
2012-05-23 14:56:01 +01:00
|
|
|
HistogramSet::getHistograms (unsigned N, unsigned R)
|
|
|
|
{
|
2012-12-20 23:19:10 +00:00
|
|
|
HistogramSet hs (N, R);
|
2012-05-23 14:56:01 +01:00
|
|
|
unsigned H = hs.nrHistograms();
|
2013-02-07 13:37:15 +00:00
|
|
|
std::vector<Histogram> histograms;
|
2012-05-23 14:56:01 +01:00
|
|
|
histograms.reserve (H);
|
|
|
|
for (unsigned i = 0; i < H; i++) {
|
|
|
|
histograms.push_back (hs.hist_);
|
|
|
|
hs.nextHistogram();
|
|
|
|
}
|
|
|
|
return histograms;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
unsigned
|
|
|
|
HistogramSet::nrHistograms (unsigned N, unsigned R)
|
|
|
|
{
|
2013-02-08 21:01:53 +00:00
|
|
|
return Util::nrCombinations (N + R - 1, R - 1);
|
2012-05-23 14:56:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-05-24 22:55:20 +01:00
|
|
|
size_t
|
2012-05-23 14:56:01 +01:00
|
|
|
HistogramSet::findIndex (
|
|
|
|
const Histogram& h,
|
2013-02-07 13:37:15 +00:00
|
|
|
const std::vector<Histogram>& hists)
|
2012-05-23 14:56:01 +01:00
|
|
|
{
|
2013-02-07 13:37:15 +00:00
|
|
|
std::vector<Histogram>::const_iterator it = std::lower_bound (
|
2012-05-23 14:56:01 +01:00
|
|
|
hists.begin(), hists.end(), h, std::greater<Histogram>());
|
|
|
|
assert (it != hists.end() && *it == h);
|
|
|
|
return std::distance (hists.begin(), it);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-02-07 13:37:15 +00:00
|
|
|
std::vector<double>
|
2012-05-23 14:56:01 +01:00
|
|
|
HistogramSet::getNumAssigns (unsigned N, unsigned R)
|
|
|
|
{
|
|
|
|
HistogramSet hs (N, R);
|
2013-02-08 21:01:53 +00:00
|
|
|
double N_fac = Util::logFactorial (N);
|
2012-05-23 14:56:01 +01:00
|
|
|
unsigned H = hs.nrHistograms();
|
2013-02-07 13:37:15 +00:00
|
|
|
std::vector<double> numAssigns;
|
2012-05-23 14:56:01 +01:00
|
|
|
numAssigns.reserve (H);
|
|
|
|
for (unsigned h = 0; h < H; h++) {
|
|
|
|
double prod = 0.0;
|
|
|
|
for (unsigned r = 0; r < R; r++) {
|
2013-02-08 21:01:53 +00:00
|
|
|
prod += Util::logFactorial (hs[r]);
|
2012-05-23 14:56:01 +01:00
|
|
|
}
|
|
|
|
double res = N_fac - prod;
|
2013-02-08 21:01:53 +00:00
|
|
|
numAssigns.push_back (Globals::logDomain ? res : std::exp (res));
|
2012-05-23 14:56:01 +01:00
|
|
|
hs.nextHistogram();
|
|
|
|
}
|
|
|
|
return numAssigns;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
unsigned
|
2012-05-24 22:55:20 +01:00
|
|
|
HistogramSet::maxCount (size_t idx) const
|
2012-05-23 14:56:01 +01:00
|
|
|
{
|
|
|
|
unsigned sum = 0;
|
2012-05-24 22:55:20 +01:00
|
|
|
for (size_t i = 0; i < idx; i++) {
|
2012-05-23 14:56:01 +01:00
|
|
|
sum += hist_[i];
|
|
|
|
}
|
|
|
|
return size_ - sum;
|
|
|
|
}
|
2012-12-20 23:19:10 +00:00
|
|
|
|
2012-05-23 14:56:01 +01:00
|
|
|
|
|
|
|
|
|
|
|
void
|
2012-05-24 22:55:20 +01:00
|
|
|
HistogramSet::clearAfter (size_t idx)
|
2012-05-23 14:56:01 +01:00
|
|
|
{
|
|
|
|
std::fill (hist_.begin() + idx + 1, hist_.end(), 0);
|
|
|
|
}
|
|
|
|
|
2013-02-06 00:24:02 +00:00
|
|
|
|
|
|
|
|
2013-02-07 22:37:45 +00:00
|
|
|
std::ostream&
|
|
|
|
operator<< (std::ostream& os, const HistogramSet& hs)
|
2013-02-06 00:24:02 +00:00
|
|
|
{
|
|
|
|
os << "#" << hs.hist_;
|
|
|
|
return os;
|
|
|
|
}
|
|
|
|
|
2013-02-08 21:12:46 +00:00
|
|
|
} // namespace Horus
|
2013-02-07 23:53:13 +00:00
|
|
|
|