filter and missingKeys
parent
d17215c69b
commit
803dae75f3
|
|
@ -68,28 +68,74 @@ class GTSAM_EXPORT DiscreteValues : public Assignment<Key> {
|
|||
friend std::ostream& operator<<(std::ostream& os, const DiscreteValues& x);
|
||||
|
||||
// insert in base class;
|
||||
std::pair<iterator, bool> insert( const value_type& value ){
|
||||
std::pair<iterator, bool> insert(const value_type& value) {
|
||||
return Base::insert(value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert key-assignment pair.
|
||||
* Throws an invalid_argument exception if
|
||||
* any keys to be inserted are already used. */
|
||||
* @brief Insert key-assignment pair.
|
||||
*
|
||||
* @param assignment The key-assignment pair to insert.
|
||||
* @return DiscreteValues& Reference to the updated DiscreteValues object.
|
||||
* @throws std::invalid_argument if any keys to be inserted are already used.
|
||||
*/
|
||||
DiscreteValues& insert(const std::pair<Key, size_t>& assignment);
|
||||
|
||||
/** Insert all values from \c values. Throws an invalid_argument exception if
|
||||
* any keys to be inserted are already used. */
|
||||
/**
|
||||
* @brief Insert all values from another DiscreteValues object.
|
||||
*
|
||||
* @param values The DiscreteValues object containing values to insert.
|
||||
* @return DiscreteValues& Reference to the updated DiscreteValues object.
|
||||
* @throws std::invalid_argument if any keys to be inserted are already used.
|
||||
*/
|
||||
DiscreteValues& insert(const DiscreteValues& values);
|
||||
|
||||
/** For all key/value pairs in \c values, replace values with corresponding
|
||||
* keys in this object with those in \c values. Throws std::out_of_range if
|
||||
* any keys in \c values are not present in this object. */
|
||||
/**
|
||||
* @brief Update values with corresponding keys from another DiscreteValues
|
||||
* object.
|
||||
*
|
||||
* @param values The DiscreteValues object containing values to update.
|
||||
* @return DiscreteValues& Reference to the updated DiscreteValues object.
|
||||
* @throws std::out_of_range if any keys in values are not present in this
|
||||
* object.
|
||||
*/
|
||||
DiscreteValues& update(const DiscreteValues& values);
|
||||
|
||||
/**
|
||||
* @brief Return a vector of DiscreteValues, one for each possible
|
||||
* combination of values.
|
||||
* @brief Filter values by keys.
|
||||
*
|
||||
* @param keys The keys to filter by.
|
||||
* @return DiscreteValues The filtered DiscreteValues object.
|
||||
*/
|
||||
DiscreteValues filter(const DiscreteKeys& keys) const {
|
||||
DiscreteValues result;
|
||||
for (const auto& [key, _] : keys) {
|
||||
if (auto it = this->find(key); it != this->end())
|
||||
result[key] = it->second;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Return the keys that are not present in the DiscreteValues object.
|
||||
*
|
||||
* @param keys The keys to check for.
|
||||
* @return DiscreteKeys Keys not present in the DiscreteValues object.
|
||||
*/
|
||||
DiscreteKeys missingKeys(const DiscreteKeys& keys) const {
|
||||
DiscreteKeys result;
|
||||
for (const auto& [key, cardinality] : keys) {
|
||||
if (!this->contains(key)) result.emplace_back(key, cardinality);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Return a vector of DiscreteValues, one for each possible combination
|
||||
* of values.
|
||||
*
|
||||
* @param keys The keys to generate the Cartesian product for.
|
||||
* @return std::vector<DiscreteValues> The vector of DiscreteValues.
|
||||
*/
|
||||
static std::vector<DiscreteValues> CartesianProduct(
|
||||
const DiscreteKeys& keys) {
|
||||
|
|
@ -135,14 +181,16 @@ inline std::vector<DiscreteValues> cartesianProduct(const DiscreteKeys& keys) {
|
|||
}
|
||||
|
||||
/// Free version of markdown.
|
||||
std::string GTSAM_EXPORT markdown(const DiscreteValues& values,
|
||||
const KeyFormatter& keyFormatter = DefaultKeyFormatter,
|
||||
const DiscreteValues::Names& names = {});
|
||||
std::string GTSAM_EXPORT
|
||||
markdown(const DiscreteValues& values,
|
||||
const KeyFormatter& keyFormatter = DefaultKeyFormatter,
|
||||
const DiscreteValues::Names& names = {});
|
||||
|
||||
/// Free version of html.
|
||||
std::string GTSAM_EXPORT html(const DiscreteValues& values,
|
||||
const KeyFormatter& keyFormatter = DefaultKeyFormatter,
|
||||
const DiscreteValues::Names& names = {});
|
||||
std::string GTSAM_EXPORT
|
||||
html(const DiscreteValues& values,
|
||||
const KeyFormatter& keyFormatter = DefaultKeyFormatter,
|
||||
const DiscreteValues::Names& names = {});
|
||||
|
||||
// traits
|
||||
template <>
|
||||
|
|
|
|||
|
|
@ -40,6 +40,24 @@ TEST(DiscreteValues, Update) {
|
|||
DiscreteValues(kExample).update({{12, 2}})));
|
||||
}
|
||||
|
||||
/* ************************************************************************* */
|
||||
// Test DiscreteValues::filter
|
||||
TEST(DiscreteValues, Filter) {
|
||||
DiscreteValues values = {{12, 1}, {5, 0}, {13, 2}};
|
||||
DiscreteKeys keys = {{12, 0}, {13, 0}, {99, 0}}; // 99 is missing in values
|
||||
|
||||
EXPECT(assert_equal(DiscreteValues({{12, 1}, {13, 2}}), values.filter(keys)));
|
||||
}
|
||||
|
||||
/* ************************************************************************* */
|
||||
// Test DiscreteValues::missingKeys
|
||||
TEST(DiscreteValues, MissingKeys) {
|
||||
DiscreteValues values = {{12, 1}, {5, 0}};
|
||||
DiscreteKeys keys = {{12, 0}, {5, 0}, {99, 0}, {42, 0}}; // 99 and 42 are missing
|
||||
|
||||
EXPECT(assert_equal(DiscreteKeys({{99, 0}, {42, 0}}), values.missingKeys(keys)));
|
||||
}
|
||||
|
||||
/* ************************************************************************* */
|
||||
// Check markdown representation with a value formatter.
|
||||
TEST(DiscreteValues, markdownWithValueFormatter) {
|
||||
|
|
|
|||
Loading…
Reference in New Issue