fix a bug when expanding potential

This commit is contained in:
Tiago Gomes 2012-04-20 14:50:14 +01:00
parent 09697b8461
commit 0ffa409a80

View File

@ -583,10 +583,10 @@ Parfactor::expandPotential (
unsigned newRange, unsigned newRange,
const vector<unsigned>& sumIndexes) const vector<unsigned>& sumIndexes)
{ {
unsigned size = (params_.size() / ranges_[fIdx]) * newRange; unsigned newSize = (params_.size() / ranges_[fIdx]) * newRange;
Params copy = params_; Params copy = params_;
params_.clear(); params_.clear();
params_.reserve (size); params_.reserve (newSize);
unsigned prod = 1; unsigned prod = 1;
vector<unsigned> offsets_ (ranges_.size()); vector<unsigned> offsets_ (ranges_.size());
@ -598,27 +598,29 @@ Parfactor::expandPotential (
unsigned index = 0; unsigned index = 0;
ranges_[fIdx] = newRange; ranges_[fIdx] = newRange;
vector<unsigned> indices (ranges_.size(), 0); vector<unsigned> indices (ranges_.size(), 0);
for (unsigned k = 0; k < size; k++) { for (unsigned k = 0; k < newSize; k++) {
assert (index < copy.size());
params_.push_back (copy[index]); params_.push_back (copy[index]);
for (int i = ranges_.size() - 1; i >= 0; i--) { for (int i = ranges_.size() - 1; i >= 0; i--) {
indices[i] ++; indices[i] ++;
if (i == fIdx) { if (i == fIdx) {
assert (indices[i] - 1 < sumIndexes.size()); if (indices[i] != ranges_[i]) {
int diff = sumIndexes[indices[i]] - sumIndexes[indices[i] - 1]; int diff = sumIndexes[indices[i]] - sumIndexes[indices[i] - 1];
index += diff * offsets_[i];
} else {
index += offsets_[i];
}
if (indices[i] != ranges_[i]) {
break;
} else {
if (i == fIdx) {
int diff = sumIndexes[0] - sumIndexes[indices[i]];
index += diff * offsets_[i]; index += diff * offsets_[i];
break;
} else { } else {
index -= offsets_[i] * ranges_[i]; // last index contains the old range minus 1
index -= sumIndexes.back() * offsets_[i];
indices[i] = 0;
}
} else {
if (indices[i] != ranges_[i]) {
index += offsets_[i];
break;
} else {
index -= (ranges_[i] - 1) * offsets_[i];
indices[i] = 0;
} }
indices[i] = 0;
} }
} }
} }