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(
|
||||
const std::vector<bool>& merge) {
|
||||
gttic(Cluster::mergeChildren);
|
||||
size_t nrChildren = children.size();
|
||||
|
||||
// Count how many keys, factors and children we'll end up with
|
||||
size_t nrKeys = orderedFrontalKeys.size();
|
||||
size_t nrFactors = factors.size();
|
||||
size_t nrNewChildren = 0;
|
||||
// Loop over children
|
||||
for (size_t i = 0; i < nrChildren; ++i) {
|
||||
size_t i = 0;
|
||||
BOOST_FOREACH(const sharedNode& child, children) {
|
||||
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();
|
||||
nrFactors += child->factors.size();
|
||||
nrNewChildren += child->children.size();
|
||||
} else {
|
||||
nrNewChildren += 1; // we keep the child
|
||||
}
|
||||
++i;
|
||||
}
|
||||
|
||||
// now reserve space, and really merge
|
||||
orderedFrontalKeys.reserve(nrKeys);
|
||||
factors.reserve(nrFactors);
|
||||
typename Node::Children newChildren;
|
||||
newChildren.reserve(nrNewChildren);
|
||||
// Loop over newChildren
|
||||
for (size_t i = 0; i < nrChildren; ++i) {
|
||||
// Check if we should merge the i^th child
|
||||
sharedNode child = children[i];
|
||||
// newChildren.reserve(nrNewChildren);
|
||||
i = 0;
|
||||
BOOST_FOREACH(const sharedNode& child, children) {
|
||||
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..
|
||||
orderedFrontalKeys.insert(orderedFrontalKeys.end(),
|
||||
child->orderedFrontalKeys.rbegin(), child->orderedFrontalKeys.rend());
|
||||
|
|
@ -174,6 +168,7 @@ void ClusterTree<BAYESTREE, GRAPH>::Cluster::mergeChildren(
|
|||
} else {
|
||||
newChildren.push_back(child); // we keep the child
|
||||
}
|
||||
++i;
|
||||
}
|
||||
children = newChildren;
|
||||
std::reverse(orderedFrontalKeys.begin(), orderedFrontalKeys.end());
|
||||
|
|
|
|||
|
|
@ -37,7 +37,7 @@ public:
|
|||
struct Cluster {
|
||||
typedef Ordering Keys;
|
||||
typedef FastVector<sharedFactor> Factors;
|
||||
typedef FastVector<boost::shared_ptr<Cluster> > Children;
|
||||
typedef FastList<boost::shared_ptr<Cluster> > Children;
|
||||
|
||||
Cluster() {
|
||||
}
|
||||
|
|
|
|||
|
|
@ -104,15 +104,15 @@ struct ConstructorTraversalData {
|
|||
|
||||
// decide which children to merge, as index into children
|
||||
std::vector<bool> merge(nrChildren, false);
|
||||
size_t myNrFrontals = 1;
|
||||
for (size_t i = 0; i < nrChildren; ++i) {
|
||||
size_t myNrFrontals = 1, i = 0;
|
||||
BOOST_FOREACH(const sharedNode& child, node->children) {
|
||||
// Check if we should merge the i^th child
|
||||
if (myNrParents + myNrFrontals == childConditionals[i]->nrParents()) {
|
||||
sharedNode child = node->children[i];
|
||||
// Increment number of frontal variables
|
||||
myNrFrontals += child->orderedFrontalKeys.size();
|
||||
merge[i] = true;
|
||||
}
|
||||
++i;
|
||||
}
|
||||
|
||||
// now really merge
|
||||
|
|
@ -145,7 +145,10 @@ JunctionTree<BAYESTREE, GRAPH>::JunctionTree(
|
|||
Data::ConstructorTraversalVisitorPostAlg2);
|
||||
|
||||
// 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
|
||||
Base::remainingFactors_ = eliminationTree.remainingFactors();
|
||||
|
|
|
|||
|
|
@ -103,15 +103,15 @@ TEST( GaussianJunctionTreeB, constructor2 )
|
|||
GaussianJunctionTree::sharedNode x324 = actual.roots().front();
|
||||
LONGS_EQUAL(2, x324->children.size());
|
||||
#if defined(__APPLE__) // tie-breaking seems different :-(
|
||||
GaussianJunctionTree::sharedNode x1 = x324->children[0];
|
||||
GaussianJunctionTree::sharedNode x56 = x324->children[1];
|
||||
GaussianJunctionTree::sharedNode x1 = x324->children.front();
|
||||
GaussianJunctionTree::sharedNode x56 = x324->children.back();
|
||||
#else
|
||||
GaussianJunctionTree::sharedNode x1 = x324->children[1];
|
||||
GaussianJunctionTree::sharedNode x56 = x324->children[0];
|
||||
GaussianJunctionTree::sharedNode x1 = x324->children.back();
|
||||
GaussianJunctionTree::sharedNode x56 = x324->children.front();
|
||||
#endif
|
||||
LONGS_EQUAL(0, x1->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());
|
||||
|
||||
EXPECT(assert_equal(o324, x324->orderedFrontalKeys));
|
||||
|
|
|
|||
Loading…
Reference in New Issue