Refactored Map<>

This commit is contained in:
Pavel Kirienko 2015-05-16 03:31:18 +03:00
parent 24f0ec56f4
commit eb370b08dd
7 changed files with 46 additions and 46 deletions

View File

@ -146,7 +146,7 @@ class NodeDiscoverer : TimerBase
{
HighestUptimeSearcher searcher;
const NodeID* const out = node_map_.findFirstKey<HighestUptimeSearcher&>(searcher);
const NodeID* const out = node_map_.find<HighestUptimeSearcher&>(searcher);
(void)out;
UAVCAN_ASSERT(out == NULL);

View File

@ -253,7 +253,7 @@ private:
}
}
listeners_.removeWhere(
listeners_.removeAllWhere(
GenericHandlerCaller<const NodeStatusChangeEvent&>(&INodeInfoListener::handleNodeStatusChange, event));
}
@ -271,8 +271,8 @@ private:
entry.uptime_sec = msg.uptime_sec;
entry.updated_since_last_attempt = true;
listeners_.removeWhere(GenericHandlerCaller<const ReceivedDataStructure<protocol::NodeStatus>&>(
&INodeInfoListener::handleNodeStatusMessage, msg));
listeners_.removeAllWhere(GenericHandlerCaller<const ReceivedDataStructure<protocol::NodeStatus>&>(
&INodeInfoListener::handleNodeStatusMessage, msg));
}
void handleGetNodeInfoResponse(const ServiceCallResult<protocol::GetNodeInfo>& result)
@ -287,8 +287,8 @@ private:
*/
entry.uptime_sec = result.getResponse().status.uptime_sec;
entry.request_needed = false;
listeners_.removeWhere(NodeInfoRetrievedHandlerCaller(result.getCallID().server_node_id,
result.getResponse()));
listeners_.removeAllWhere(NodeInfoRetrievedHandlerCaller(result.getCallID().server_node_id,
result.getResponse()));
}
else
{
@ -298,8 +298,8 @@ private:
if (entry.num_attempts_made >= num_attempts_)
{
entry.request_needed = false;
listeners_.removeWhere(GenericHandlerCaller<NodeID>(&INodeInfoListener::handleNodeInfoUnavailable,
result.getCallID().server_node_id));
listeners_.removeAllWhere(GenericHandlerCaller<NodeID>(
&INodeInfoListener::handleNodeInfoUnavailable, result.getCallID().server_node_id));
}
}
}

View File

@ -165,13 +165,13 @@ TransferID* OutgoingTransferRegistry<NumStaticEntries>::accessOrCreate(const Out
template <int NumStaticEntries>
bool OutgoingTransferRegistry<NumStaticEntries>::exists(DataTypeID dtid, TransferType tt) const
{
return NULL != map_.findFirstKey(ExistenceCheckingPredicate(dtid, tt));
return NULL != map_.find(ExistenceCheckingPredicate(dtid, tt));
}
template <int NumStaticEntries>
void OutgoingTransferRegistry<NumStaticEntries>::cleanup(MonotonicTime ts)
{
map_.removeWhere(DeadlineExpiredPredicate(ts));
map_.removeAllWhere(DeadlineExpiredPredicate(ts));
}
}

View File

@ -179,7 +179,7 @@ public:
virtual ~TransferListener()
{
// Map must be cleared before bufmgr is destroyed
receivers_.removeAll();
receivers_.clear();
}
};

View File

@ -108,7 +108,7 @@ private:
const unsigned num_static_entries_;
#endif
KVPair* find(const Key& key);
KVPair* findKey(const Key& key);
#if !UAVCAN_TINY
void optimizeStorage();
@ -117,7 +117,7 @@ private:
struct YesPredicate
{
bool operator()(const Key& k, const Value& v) const { (void)k; (void)v; return true; }
bool operator()(const Key&, const Value&) const { return true; }
};
protected:
@ -137,7 +137,7 @@ protected:
}
#endif
/// Derived class destructor must call removeAll();
/// Derived class destructor must call clear();
~MapBase()
{
UAVCAN_ASSERT(getSize() == 0);
@ -165,7 +165,7 @@ public:
* bool (Key& key, Value& value)
*/
template <typename Predicate>
void removeWhere(Predicate predicate);
void removeAllWhere(Predicate predicate);
/**
* Returns first entry where the predicate returns true.
@ -173,9 +173,12 @@ public:
* bool (const Key& key, const Value& value)
*/
template <typename Predicate>
const Key* findFirstKey(Predicate predicate) const;
const Key* find(Predicate predicate) const;
void removeAll();
/**
* Removes all items.
*/
void clear();
/**
* Returns a key-value pair located at the specified position from the beginning.
@ -185,7 +188,10 @@ public:
KVPair* getByIndex(unsigned index);
const KVPair* getByIndex(unsigned index) const;
bool isEmpty() const;
/**
* Complexity is O(1).
*/
bool isEmpty() const { return find(YesPredicate()) == NULL; }
unsigned getSize() const;
@ -211,7 +217,7 @@ public:
: MapBase<Key, Value>(static_, NumStaticEntries, allocator)
{ }
~Map() { this->removeAll(); }
~Map() { this->clear(); }
#endif // !UAVCAN_TINY
};
@ -229,7 +235,7 @@ public:
#endif
{ }
~Map() { this->removeAll(); }
~Map() { this->clear(); }
};
// ----------------------------------------------------------------------------
@ -238,7 +244,7 @@ public:
* MapBase<>
*/
template <typename Key, typename Value>
typename MapBase<Key, Value>::KVPair* MapBase<Key, Value>::find(const Key& key)
typename MapBase<Key, Value>::KVPair* MapBase<Key, Value>::findKey(const Key& key)
{
#if !UAVCAN_TINY
for (unsigned i = 0; i < num_static_entries_; i++)
@ -348,7 +354,7 @@ template <typename Key, typename Value>
Value* MapBase<Key, Value>::access(const Key& key)
{
UAVCAN_ASSERT(!(key == Key()));
KVPair* const kv = find(key);
KVPair* const kv = findKey(key);
return kv ? &kv->value : NULL;
}
@ -358,7 +364,7 @@ Value* MapBase<Key, Value>::insert(const Key& key, const Value& value)
UAVCAN_ASSERT(!(key == Key()));
remove(key);
KVPair* const kv = find(Key());
KVPair* const kv = findKey(Key());
if (kv)
{
*kv = KVPair(key, value);
@ -379,7 +385,7 @@ template <typename Key, typename Value>
void MapBase<Key, Value>::remove(const Key& key)
{
UAVCAN_ASSERT(!(key == Key()));
KVPair* const kv = find(key);
KVPair* const kv = findKey(key);
if (kv)
{
*kv = KVPair();
@ -392,7 +398,7 @@ void MapBase<Key, Value>::remove(const Key& key)
template <typename Key, typename Value>
template <typename Predicate>
void MapBase<Key, Value>::removeWhere(Predicate predicate)
void MapBase<Key, Value>::removeAllWhere(Predicate predicate)
{
unsigned num_removed = 0;
@ -439,7 +445,7 @@ void MapBase<Key, Value>::removeWhere(Predicate predicate)
template <typename Key, typename Value>
template <typename Predicate>
const Key* MapBase<Key, Value>::findFirstKey(Predicate predicate) const
const Key* MapBase<Key, Value>::find(Predicate predicate) const
{
#if !UAVCAN_TINY
for (unsigned i = 0; i < num_static_entries_; i++)
@ -474,9 +480,9 @@ const Key* MapBase<Key, Value>::findFirstKey(Predicate predicate) const
}
template <typename Key, typename Value>
void MapBase<Key, Value>::removeAll()
void MapBase<Key, Value>::clear()
{
removeWhere(YesPredicate());
removeAllWhere(YesPredicate());
}
template <typename Key, typename Value>
@ -525,12 +531,6 @@ const typename MapBase<Key, Value>::KVPair* MapBase<Key, Value>::getByIndex(unsi
return const_cast<MapBase<Key, Value>*>(this)->getByIndex(index);
}
template <typename Key, typename Value>
bool MapBase<Key, Value>::isEmpty() const
{
return getSize() == 0;
}
template <typename Key, typename Value>
unsigned MapBase<Key, Value>::getSize() const
{

View File

@ -189,7 +189,7 @@ void TransferListenerBase::handleAnonymousTransferReception(const RxFrame& frame
void TransferListenerBase::cleanup(MonotonicTime ts)
{
receivers_.removeWhere(TimedOutReceiverPredicate(ts, bufmgr_));
receivers_.removeAllWhere(TimedOutReceiverPredicate(ts, bufmgr_));
UAVCAN_ASSERT(receivers_.isEmpty() ? bufmgr_.isEmpty() : 1);
}

View File

@ -105,18 +105,18 @@ TEST(Map, Basic)
ASSERT_EQ("D", *map->access("4"));
// Finding some keys
ASSERT_EQ("1", *map->findFirstKey(KeyFindPredicate("1")));
ASSERT_EQ("2", *map->findFirstKey(KeyFindPredicate("2")));
ASSERT_EQ("3", *map->findFirstKey(KeyFindPredicate("3")));
ASSERT_EQ("4", *map->findFirstKey(KeyFindPredicate("4")));
ASSERT_FALSE(map->findFirstKey(KeyFindPredicate("nonexistent_key")));
ASSERT_EQ("1", *map->find(KeyFindPredicate("1")));
ASSERT_EQ("2", *map->find(KeyFindPredicate("2")));
ASSERT_EQ("3", *map->find(KeyFindPredicate("3")));
ASSERT_EQ("4", *map->find(KeyFindPredicate("4")));
ASSERT_FALSE(map->find(KeyFindPredicate("nonexistent_key")));
// Finding some values
ASSERT_EQ("1", *map->findFirstKey(ValueFindPredicate("A")));
ASSERT_EQ("2", *map->findFirstKey(ValueFindPredicate("B")));
ASSERT_EQ("3", *map->findFirstKey(ValueFindPredicate("C")));
ASSERT_EQ("4", *map->findFirstKey(ValueFindPredicate("D")));
ASSERT_FALSE(map->findFirstKey(KeyFindPredicate("nonexistent_value")));
ASSERT_EQ("1", *map->find(ValueFindPredicate("A")));
ASSERT_EQ("2", *map->find(ValueFindPredicate("B")));
ASSERT_EQ("3", *map->find(ValueFindPredicate("C")));
ASSERT_EQ("4", *map->find(ValueFindPredicate("D")));
ASSERT_FALSE(map->find(KeyFindPredicate("nonexistent_value")));
// Removing one static
map->remove("1"); // One of dynamics now migrates to the static storage
@ -176,7 +176,7 @@ TEST(Map, Basic)
// Removing odd values - nearly half of them
ASSERT_EQ(2, map->getNumStaticPairs());
const unsigned num_dynamics_old = map->getNumDynamicPairs();
map->removeWhere(oddValuePredicate);
map->removeAllWhere(oddValuePredicate);
ASSERT_EQ(2, map->getNumStaticPairs());
const unsigned num_dynamics_new = map->getNumDynamicPairs();
std::cout << "Num of dynamic pairs reduced from " << num_dynamics_old << " to " << num_dynamics_new << std::endl;