Switch to list - made code be container-agnostic
parent
d34c1808a8
commit
e2d49922d2
|
|
@ -128,38 +128,32 @@ template<class BAYESTREE, class GRAPH>
|
||||||
void ClusterTree<BAYESTREE, GRAPH>::Cluster::mergeChildren(
|
void ClusterTree<BAYESTREE, GRAPH>::Cluster::mergeChildren(
|
||||||
const std::vector<bool>& merge) {
|
const std::vector<bool>& merge) {
|
||||||
gttic(Cluster::mergeChildren);
|
gttic(Cluster::mergeChildren);
|
||||||
size_t nrChildren = children.size();
|
|
||||||
|
|
||||||
// Count how many keys, factors and children we'll end up with
|
// Count how many keys, factors and children we'll end up with
|
||||||
size_t nrKeys = orderedFrontalKeys.size();
|
size_t nrKeys = orderedFrontalKeys.size();
|
||||||
size_t nrFactors = factors.size();
|
size_t nrFactors = factors.size();
|
||||||
size_t nrNewChildren = 0;
|
size_t nrNewChildren = 0;
|
||||||
// Loop over children
|
// Loop over children
|
||||||
for (size_t i = 0; i < nrChildren; ++i) {
|
size_t i = 0;
|
||||||
|
BOOST_FOREACH(const sharedNode& child, children) {
|
||||||
if (merge[i]) {
|
if (merge[i]) {
|
||||||
// Get a reference to the i, adjusting the index to account for children
|
|
||||||
// previously merged and removed from the i list.
|
|
||||||
sharedNode child = children[i];
|
|
||||||
nrKeys += child->orderedFrontalKeys.size();
|
nrKeys += child->orderedFrontalKeys.size();
|
||||||
nrFactors += child->factors.size();
|
nrFactors += child->factors.size();
|
||||||
nrNewChildren += child->children.size();
|
nrNewChildren += child->children.size();
|
||||||
} else {
|
} else {
|
||||||
nrNewChildren += 1; // we keep the child
|
nrNewChildren += 1; // we keep the child
|
||||||
}
|
}
|
||||||
|
++i;
|
||||||
}
|
}
|
||||||
|
|
||||||
// now reserve space, and really merge
|
// now reserve space, and really merge
|
||||||
orderedFrontalKeys.reserve(nrKeys);
|
orderedFrontalKeys.reserve(nrKeys);
|
||||||
factors.reserve(nrFactors);
|
factors.reserve(nrFactors);
|
||||||
typename Node::Children newChildren;
|
typename Node::Children newChildren;
|
||||||
newChildren.reserve(nrNewChildren);
|
// newChildren.reserve(nrNewChildren);
|
||||||
// Loop over newChildren
|
i = 0;
|
||||||
for (size_t i = 0; i < nrChildren; ++i) {
|
BOOST_FOREACH(const sharedNode& child, children) {
|
||||||
// Check if we should merge the i^th child
|
|
||||||
sharedNode child = children[i];
|
|
||||||
if (merge[i]) {
|
if (merge[i]) {
|
||||||
// Get a reference to the i, adjusting the index to account for newChildren
|
|
||||||
// previously merged and removed from the i list.
|
|
||||||
// Merge keys. For efficiency, we add keys in reverse order at end, calling reverse after..
|
// Merge keys. For efficiency, we add keys in reverse order at end, calling reverse after..
|
||||||
orderedFrontalKeys.insert(orderedFrontalKeys.end(),
|
orderedFrontalKeys.insert(orderedFrontalKeys.end(),
|
||||||
child->orderedFrontalKeys.rbegin(), child->orderedFrontalKeys.rend());
|
child->orderedFrontalKeys.rbegin(), child->orderedFrontalKeys.rend());
|
||||||
|
|
@ -174,6 +168,7 @@ void ClusterTree<BAYESTREE, GRAPH>::Cluster::mergeChildren(
|
||||||
} else {
|
} else {
|
||||||
newChildren.push_back(child); // we keep the child
|
newChildren.push_back(child); // we keep the child
|
||||||
}
|
}
|
||||||
|
++i;
|
||||||
}
|
}
|
||||||
children = newChildren;
|
children = newChildren;
|
||||||
std::reverse(orderedFrontalKeys.begin(), orderedFrontalKeys.end());
|
std::reverse(orderedFrontalKeys.begin(), orderedFrontalKeys.end());
|
||||||
|
|
|
||||||
|
|
@ -37,7 +37,7 @@ public:
|
||||||
struct Cluster {
|
struct Cluster {
|
||||||
typedef Ordering Keys;
|
typedef Ordering Keys;
|
||||||
typedef FastVector<sharedFactor> Factors;
|
typedef FastVector<sharedFactor> Factors;
|
||||||
typedef FastVector<boost::shared_ptr<Cluster> > Children;
|
typedef FastList<boost::shared_ptr<Cluster> > Children;
|
||||||
|
|
||||||
Cluster() {
|
Cluster() {
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -104,15 +104,15 @@ struct ConstructorTraversalData {
|
||||||
|
|
||||||
// decide which children to merge, as index into children
|
// decide which children to merge, as index into children
|
||||||
std::vector<bool> merge(nrChildren, false);
|
std::vector<bool> merge(nrChildren, false);
|
||||||
size_t myNrFrontals = 1;
|
size_t myNrFrontals = 1, i = 0;
|
||||||
for (size_t i = 0; i < nrChildren; ++i) {
|
BOOST_FOREACH(const sharedNode& child, node->children) {
|
||||||
// Check if we should merge the i^th child
|
// Check if we should merge the i^th child
|
||||||
if (myNrParents + myNrFrontals == childConditionals[i]->nrParents()) {
|
if (myNrParents + myNrFrontals == childConditionals[i]->nrParents()) {
|
||||||
sharedNode child = node->children[i];
|
|
||||||
// Increment number of frontal variables
|
// Increment number of frontal variables
|
||||||
myNrFrontals += child->orderedFrontalKeys.size();
|
myNrFrontals += child->orderedFrontalKeys.size();
|
||||||
merge[i] = true;
|
merge[i] = true;
|
||||||
}
|
}
|
||||||
|
++i;
|
||||||
}
|
}
|
||||||
|
|
||||||
// now really merge
|
// now really merge
|
||||||
|
|
@ -145,7 +145,10 @@ JunctionTree<BAYESTREE, GRAPH>::JunctionTree(
|
||||||
Data::ConstructorTraversalVisitorPostAlg2);
|
Data::ConstructorTraversalVisitorPostAlg2);
|
||||||
|
|
||||||
// Assign roots from the dummy node
|
// Assign roots from the dummy node
|
||||||
Base::roots_ = rootData.myJTNode->children;
|
typedef typename JunctionTree<BAYESTREE, GRAPH>::Node Node;
|
||||||
|
const typename Node::Children& children = rootData.myJTNode->children;
|
||||||
|
Base::roots_.reserve(children.size());
|
||||||
|
Base::roots_.insert(Base::roots_.begin(), children.begin(), children.end());
|
||||||
|
|
||||||
// Transfer remaining factors from elimination tree
|
// Transfer remaining factors from elimination tree
|
||||||
Base::remainingFactors_ = eliminationTree.remainingFactors();
|
Base::remainingFactors_ = eliminationTree.remainingFactors();
|
||||||
|
|
|
||||||
|
|
@ -103,15 +103,15 @@ TEST( GaussianJunctionTreeB, constructor2 )
|
||||||
GaussianJunctionTree::sharedNode x324 = actual.roots().front();
|
GaussianJunctionTree::sharedNode x324 = actual.roots().front();
|
||||||
LONGS_EQUAL(2, x324->children.size());
|
LONGS_EQUAL(2, x324->children.size());
|
||||||
#if defined(__APPLE__) // tie-breaking seems different :-(
|
#if defined(__APPLE__) // tie-breaking seems different :-(
|
||||||
GaussianJunctionTree::sharedNode x1 = x324->children[0];
|
GaussianJunctionTree::sharedNode x1 = x324->children.front();
|
||||||
GaussianJunctionTree::sharedNode x56 = x324->children[1];
|
GaussianJunctionTree::sharedNode x56 = x324->children.back();
|
||||||
#else
|
#else
|
||||||
GaussianJunctionTree::sharedNode x1 = x324->children[1];
|
GaussianJunctionTree::sharedNode x1 = x324->children.back();
|
||||||
GaussianJunctionTree::sharedNode x56 = x324->children[0];
|
GaussianJunctionTree::sharedNode x56 = x324->children.front();
|
||||||
#endif
|
#endif
|
||||||
LONGS_EQUAL(0, x1->children.size());
|
LONGS_EQUAL(0, x1->children.size());
|
||||||
LONGS_EQUAL(1, x56->children.size());
|
LONGS_EQUAL(1, x56->children.size());
|
||||||
GaussianJunctionTree::sharedNode x7 = x56->children[0];
|
GaussianJunctionTree::sharedNode x7 = x56->children.front();
|
||||||
LONGS_EQUAL(0, x7->children.size());
|
LONGS_EQUAL(0, x7->children.size());
|
||||||
|
|
||||||
EXPECT(assert_equal(o324, x324->orderedFrontalKeys));
|
EXPECT(assert_equal(o324, x324->orderedFrontalKeys));
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue