Added conversion from Values::Filtered to Values::ConstFiltered and bug fix in Values::ConstFiltered

release/4.3a0
Richard Roberts 2012-02-28 16:53:19 +00:00
parent c2947ea633
commit 908f2ac561
2 changed files with 26 additions and 22 deletions

View File

@ -124,20 +124,17 @@ namespace gtsam {
struct _KeyValuePair {
const Key key; ///< The key
ValueType& value; ///< The value
const Key& first; ///< For std::pair compatibility, the key
ValueType& second; ///< For std::pair compatibility, the value
_KeyValuePair(Key _key, ValueType& _value) : key(_key), value(_value), first(key), second(value) {}
_KeyValuePair(Key _key, ValueType& _value) : key(_key), value(_value) {}
};
template<class ValueType>
struct _ConstKeyValuePair {
const Key key; ///< The key
const ValueType& value; ///< The value
const Key& first; ///< For std::pair compatibility, the key
const ValueType& second; ///< For std::pair compatibility, the value
_ConstKeyValuePair(Key _key, const Value& _value) : key(_key), value(_value), first(key), second(value) {}
_ConstKeyValuePair(Key _key, const ValueType& _value) : key(_key), value(_value) {}
_ConstKeyValuePair(const _KeyValuePair<ValueType>& rhs) : key(rhs.key), value(rhs.value) {}
};
public:
@ -150,13 +147,6 @@ namespace gtsam {
public:
typedef _KeyValuePair<ValueType> KeyValuePair;
private:
typedef boost::transformed_range<
KeyValuePair(*)(Values::KeyValuePair key_value),
const boost::filtered_range<
boost::function<bool(const Values::ConstKeyValuePair&)>,
const boost::iterator_range<iterator> > > Base;
public:
/** Returns the number of values in this view */
size_t size() const {
typename Base::iterator it = this->begin();
@ -168,6 +158,12 @@ namespace gtsam {
private:
typedef boost::transformed_range<
KeyValuePair(*)(Values::KeyValuePair key_value),
const boost::filtered_range<
boost::function<bool(const Values::ConstKeyValuePair&)>,
const boost::iterator_range<iterator> > > Base;
Filtered(const Base& base) : Base(base) {}
friend class Values;
@ -182,14 +178,16 @@ namespace gtsam {
public:
typedef _ConstKeyValuePair<ValueType> KeyValuePair;
private:
typedef boost::transformed_range<
KeyValuePair(*)(Values::ConstKeyValuePair key_value),
const boost::filtered_range<
boost::function<bool(const Values::ConstKeyValuePair&)>,
const boost::iterator_range<const_iterator> > > Base;
/** Conversion from Filtered<ValueType> to ConstFiltered<ValueType> */
ConstFiltered(const Filtered<ValueType>& rhs) : Base(
boost::adaptors::transform(
boost::adaptors::filter(
boost::make_iterator_range(
const_iterator(rhs.begin().base().base().base(), &make_const_deref_pair),
const_iterator(rhs.end().base().base().base(), &make_const_deref_pair)),
rhs.begin().base().predicate()),
&castHelper<const ValueType, _ConstKeyValuePair<ValueType>, ConstKeyValuePair>)) {}
public:
/** Returns the number of values in this view */
size_t size() const {
typename Base::const_iterator it = this->begin();
@ -201,6 +199,12 @@ namespace gtsam {
private:
typedef boost::transformed_range<
KeyValuePair(*)(Values::ConstKeyValuePair key_value),
const boost::filtered_range<
boost::function<bool(const Values::ConstKeyValuePair&)>,
const boost::iterator_range<const_iterator> > > Base;
ConstFiltered(const Base& base) : Base(base) {}
friend class Values;

View File

@ -300,9 +300,9 @@ TEST(Values, filter) {
// Filter by type
i = 0;
Values::Filtered<Pose3> pose_filtered = values.filter<Pose3>();
Values::ConstFiltered<Pose3> pose_filtered = values.filter<Pose3>();
EXPECT_LONGS_EQUAL(2, pose_filtered.size());
BOOST_FOREACH(const Values::Filtered<Pose3>::KeyValuePair& key_value, pose_filtered) {
BOOST_FOREACH(const Values::ConstFiltered<Pose3>::KeyValuePair& key_value, pose_filtered) {
if(i == 0) {
EXPECT_LONGS_EQUAL(1, key_value.key);
EXPECT(assert_equal(pose1, key_value.value));