mirror of
https://gitee.com/mirrors_PX4/PX4-Autopilot.git
synced 2026-10-15 16:13:33 +08:00
Multiset<>::forEach()
This commit is contained in:
@@ -135,15 +135,50 @@ private:
|
||||
struct IndexPredicate : ::uavcan::Noncopyable
|
||||
{
|
||||
unsigned index;
|
||||
IndexPredicate(unsigned target_index) : index(target_index) { }
|
||||
bool operator()(const T&) { return index--==0; }
|
||||
IndexPredicate(unsigned target_index)
|
||||
: index(target_index)
|
||||
{ }
|
||||
|
||||
bool operator()(const T&)
|
||||
{
|
||||
return index-- == 0;
|
||||
}
|
||||
};
|
||||
|
||||
struct ComparingPredicate
|
||||
{
|
||||
const T& reference;
|
||||
ComparingPredicate(const T& ref) : reference(ref) { }
|
||||
bool operator()(const T& sample) { return reference == sample; }
|
||||
|
||||
ComparingPredicate(const T& ref)
|
||||
: reference(ref)
|
||||
{ }
|
||||
|
||||
bool operator()(const T& sample)
|
||||
{
|
||||
return reference == sample;
|
||||
}
|
||||
};
|
||||
|
||||
template<typename Operator>
|
||||
struct OperatorToFalsePredicateAdapter : ::uavcan::Noncopyable
|
||||
{
|
||||
const typename ParameterType<Operator>::Type oper;
|
||||
|
||||
OperatorToFalsePredicateAdapter(typename ParameterType<Operator>::Type o)
|
||||
: oper(o)
|
||||
{ }
|
||||
|
||||
bool operator()(T& item)
|
||||
{
|
||||
oper(item);
|
||||
return false;
|
||||
}
|
||||
|
||||
bool operator()(const T& item) const
|
||||
{
|
||||
oper(item);
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
protected:
|
||||
@@ -255,6 +290,26 @@ public:
|
||||
return const_cast<MultisetBase<T>*>(this)->find<Predicate>(predicate);
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls Operator for each item of the set.
|
||||
* Operator prototype:
|
||||
* void (T& item)
|
||||
* void (const T& item) - const overload
|
||||
*/
|
||||
template <typename Operator>
|
||||
void forEach(Operator oper)
|
||||
{
|
||||
OperatorToFalsePredicateAdapter<Operator> adapter(oper);
|
||||
(void)find<OperatorToFalsePredicateAdapter<Operator>&>(adapter);
|
||||
}
|
||||
|
||||
template <typename Operator>
|
||||
void forEach(Operator oper) const
|
||||
{
|
||||
const OperatorToFalsePredicateAdapter<Operator> adapter(oper);
|
||||
(void)find<const OperatorToFalsePredicateAdapter<Operator>&>(adapter);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an item located at the specified position from the beginning.
|
||||
* Note that addition and removal operations invalidate indices.
|
||||
|
||||
@@ -49,6 +49,20 @@ struct NoncopyableWithCounter : uavcan::Noncopyable
|
||||
|
||||
int NoncopyableWithCounter::num_objects = 0;
|
||||
|
||||
template <typename T>
|
||||
struct SummationOperator : uavcan::Noncopyable
|
||||
{
|
||||
T accumulator;
|
||||
SummationOperator() : accumulator() { }
|
||||
void operator()(const T& x) { accumulator += x; }
|
||||
};
|
||||
|
||||
struct ClearingOperator
|
||||
{
|
||||
template <typename T>
|
||||
void operator()(T& x) const { x = T(); }
|
||||
};
|
||||
|
||||
|
||||
TEST(Multiset, Basic)
|
||||
{
|
||||
@@ -62,6 +76,8 @@ TEST(Multiset, Basic)
|
||||
typedef Multiset<std::string, 2> MultisetType;
|
||||
std::auto_ptr<MultisetType> mset(new MultisetType(poolmgr));
|
||||
|
||||
typedef SummationOperator<std::string> StringConcatenationOperator;
|
||||
|
||||
// Empty
|
||||
mset->removeFirst("foo");
|
||||
ASSERT_EQ(0, pool.getNumUsedBlocks());
|
||||
@@ -80,6 +96,12 @@ TEST(Multiset, Basic)
|
||||
ASSERT_TRUE(*mset->getByIndex(0) == "1");
|
||||
ASSERT_TRUE(*mset->getByIndex(1) == "2");
|
||||
|
||||
{
|
||||
StringConcatenationOperator op;
|
||||
mset->forEach<StringConcatenationOperator&>(op);
|
||||
ASSERT_EQ("12", op.accumulator);
|
||||
}
|
||||
|
||||
// Dynamic addition
|
||||
ASSERT_EQ("3", *mset->emplace("3"));
|
||||
ASSERT_EQ("3", *mset->getByIndex(2));
|
||||
@@ -107,6 +129,13 @@ TEST(Multiset, Basic)
|
||||
ASSERT_EQ("4", *mset->find(FindPredicate("4")));
|
||||
ASSERT_FALSE(mset->find(FindPredicate("nonexistent")));
|
||||
|
||||
{
|
||||
StringConcatenationOperator op;
|
||||
mset->forEach<StringConcatenationOperator&>(op);
|
||||
std::cout << "Accumulator: " << op.accumulator << std::endl;
|
||||
ASSERT_EQ(4, op.accumulator.size());
|
||||
}
|
||||
|
||||
// Removing one static; ordering will be preserved
|
||||
mset->removeFirst("1");
|
||||
mset->removeFirst("foo"); // There's no such thing anyway
|
||||
@@ -165,6 +194,20 @@ TEST(Multiset, Basic)
|
||||
}
|
||||
}
|
||||
|
||||
// Clearing all strings
|
||||
{
|
||||
StringConcatenationOperator op;
|
||||
mset->forEach<StringConcatenationOperator&>(op);
|
||||
std::cout << "Accumulator before clearing: " << op.accumulator << std::endl;
|
||||
}
|
||||
mset->forEach(ClearingOperator());
|
||||
{
|
||||
StringConcatenationOperator op;
|
||||
mset->forEach<StringConcatenationOperator&>(op);
|
||||
std::cout << "Accumulator after clearing: " << op.accumulator << std::endl;
|
||||
ASSERT_TRUE(op.accumulator.empty());
|
||||
}
|
||||
|
||||
// Making sure the memory will be released
|
||||
mset.reset();
|
||||
ASSERT_EQ(0, pool.getNumUsedBlocks());
|
||||
@@ -241,6 +284,19 @@ TEST(Multiset, PrimitiveKey)
|
||||
ASSERT_FALSE(mset->getByIndex(5));
|
||||
ASSERT_FALSE(mset->getByIndex(1000));
|
||||
#endif
|
||||
|
||||
// Summation and clearing
|
||||
{
|
||||
SummationOperator<int> summation_operator;
|
||||
mset->forEach<SummationOperator<int>&>(summation_operator);
|
||||
ASSERT_EQ(1 + 2 + 3 + 4, summation_operator.accumulator);
|
||||
}
|
||||
mset->forEach(ClearingOperator());
|
||||
{
|
||||
SummationOperator<int> summation_operator;
|
||||
mset->forEach<SummationOperator<int>&>(summation_operator);
|
||||
ASSERT_EQ(0, summation_operator.accumulator);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user