diff --git a/gtsam/discrete/TableFactor.cpp b/gtsam/discrete/TableFactor.cpp index 22548da07..bf9662e34 100644 --- a/gtsam/discrete/TableFactor.cpp +++ b/gtsam/discrete/TableFactor.cpp @@ -252,41 +252,12 @@ DecisionTreeFactor TableFactor::operator*(const DecisionTreeFactor& f) const { DecisionTreeFactor TableFactor::toDecisionTreeFactor() const { DiscreteKeys dkeys = discreteKeys(); - // Record key assignment and value pairs in pair_table. - // The assignments are stored in descending order of keys so that the order of - // the values matches what is expected by a DecisionTree. - // This is why we reverse the keys and then - // query for the key value/assignment. - DiscreteKeys rdkeys(dkeys.rbegin(), dkeys.rend()); - std::vector> pair_table; + std::vector table; for (auto i = 0; i < sparse_table_.size(); i++) { - std::stringstream ss; - for (auto&& [key, _] : rdkeys) { - ss << keyValueForIndex(key, i); - } - // k will be in reverse key order already - uint64_t k; - ss >> k; - pair_table.push_back(std::make_pair(k, sparse_table_.coeff(i))); + table.push_back(sparse_table_.coeff(i)); } - // Sort the pair_table (of assignment-value pairs) based on assignment so we - // get values in reverse key order. - std::sort( - pair_table.begin(), pair_table.end(), - [](const std::pair& a, - const std::pair& b) { return a.first < b.first; }); - - // Create the table vector by extracting the values from pair_table. - // The pair_table has already been sorted in the desired order, - // so the values will be in descending key order. - std::vector table; - std::for_each(pair_table.begin(), pair_table.end(), - [&table](const std::pair& pair) { - table.push_back(pair.second); - }); - - AlgebraicDecisionTree tree(rdkeys, table); + AlgebraicDecisionTree tree(dkeys, table); DecisionTreeFactor f(dkeys, tree); return f; }