diff --git a/libuavcan/include/uavcan/protocol/dynamic_node_id_server/node_discoverer.hpp b/libuavcan/include/uavcan/protocol/dynamic_node_id_server/node_discoverer.hpp index 74280ac9f4..03b4145759 100644 --- a/libuavcan/include/uavcan/protocol/dynamic_node_id_server/node_discoverer.hpp +++ b/libuavcan/include/uavcan/protocol/dynamic_node_id_server/node_discoverer.hpp @@ -146,7 +146,7 @@ class NodeDiscoverer : TimerBase { HighestUptimeSearcher searcher; - const NodeID* const out = node_map_.findFirstKey(searcher); + const NodeID* const out = node_map_.find(searcher); (void)out; UAVCAN_ASSERT(out == NULL); diff --git a/libuavcan/include/uavcan/protocol/node_info_retriever.hpp b/libuavcan/include/uavcan/protocol/node_info_retriever.hpp index c2656b645c..29391a15a6 100644 --- a/libuavcan/include/uavcan/protocol/node_info_retriever.hpp +++ b/libuavcan/include/uavcan/protocol/node_info_retriever.hpp @@ -253,7 +253,7 @@ private: } } - listeners_.removeWhere( + listeners_.removeAllWhere( GenericHandlerCaller(&INodeInfoListener::handleNodeStatusChange, event)); } @@ -271,8 +271,8 @@ private: entry.uptime_sec = msg.uptime_sec; entry.updated_since_last_attempt = true; - listeners_.removeWhere(GenericHandlerCaller&>( - &INodeInfoListener::handleNodeStatusMessage, msg)); + listeners_.removeAllWhere(GenericHandlerCaller&>( + &INodeInfoListener::handleNodeStatusMessage, msg)); } void handleGetNodeInfoResponse(const ServiceCallResult& 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(&INodeInfoListener::handleNodeInfoUnavailable, - result.getCallID().server_node_id)); + listeners_.removeAllWhere(GenericHandlerCaller( + &INodeInfoListener::handleNodeInfoUnavailable, result.getCallID().server_node_id)); } } } diff --git a/libuavcan/include/uavcan/transport/outgoing_transfer_registry.hpp b/libuavcan/include/uavcan/transport/outgoing_transfer_registry.hpp index 114cecee22..2dbc508118 100644 --- a/libuavcan/include/uavcan/transport/outgoing_transfer_registry.hpp +++ b/libuavcan/include/uavcan/transport/outgoing_transfer_registry.hpp @@ -165,13 +165,13 @@ TransferID* OutgoingTransferRegistry::accessOrCreate(const Out template bool OutgoingTransferRegistry::exists(DataTypeID dtid, TransferType tt) const { - return NULL != map_.findFirstKey(ExistenceCheckingPredicate(dtid, tt)); + return NULL != map_.find(ExistenceCheckingPredicate(dtid, tt)); } template void OutgoingTransferRegistry::cleanup(MonotonicTime ts) { - map_.removeWhere(DeadlineExpiredPredicate(ts)); + map_.removeAllWhere(DeadlineExpiredPredicate(ts)); } } diff --git a/libuavcan/include/uavcan/transport/transfer_listener.hpp b/libuavcan/include/uavcan/transport/transfer_listener.hpp index 62be67d64b..428ec1489b 100644 --- a/libuavcan/include/uavcan/transport/transfer_listener.hpp +++ b/libuavcan/include/uavcan/transport/transfer_listener.hpp @@ -179,7 +179,7 @@ public: virtual ~TransferListener() { // Map must be cleared before bufmgr is destroyed - receivers_.removeAll(); + receivers_.clear(); } }; diff --git a/libuavcan/include/uavcan/util/map.hpp b/libuavcan/include/uavcan/util/map.hpp index ed5535266d..1f805fa0a2 100644 --- a/libuavcan/include/uavcan/util/map.hpp +++ b/libuavcan/include/uavcan/util/map.hpp @@ -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 - 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 - 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(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 MapBase::KVPair* MapBase::find(const Key& key) +typename MapBase::KVPair* MapBase::findKey(const Key& key) { #if !UAVCAN_TINY for (unsigned i = 0; i < num_static_entries_; i++) @@ -348,7 +354,7 @@ template Value* MapBase::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::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 void MapBase::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::remove(const Key& key) template template -void MapBase::removeWhere(Predicate predicate) +void MapBase::removeAllWhere(Predicate predicate) { unsigned num_removed = 0; @@ -439,7 +445,7 @@ void MapBase::removeWhere(Predicate predicate) template template -const Key* MapBase::findFirstKey(Predicate predicate) const +const Key* MapBase::find(Predicate predicate) const { #if !UAVCAN_TINY for (unsigned i = 0; i < num_static_entries_; i++) @@ -474,9 +480,9 @@ const Key* MapBase::findFirstKey(Predicate predicate) const } template -void MapBase::removeAll() +void MapBase::clear() { - removeWhere(YesPredicate()); + removeAllWhere(YesPredicate()); } template @@ -525,12 +531,6 @@ const typename MapBase::KVPair* MapBase::getByIndex(unsi return const_cast*>(this)->getByIndex(index); } -template -bool MapBase::isEmpty() const -{ - return getSize() == 0; -} - template unsigned MapBase::getSize() const { diff --git a/libuavcan/src/transport/uc_transfer_listener.cpp b/libuavcan/src/transport/uc_transfer_listener.cpp index a48878f0c1..38cdb726eb 100644 --- a/libuavcan/src/transport/uc_transfer_listener.cpp +++ b/libuavcan/src/transport/uc_transfer_listener.cpp @@ -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); } diff --git a/libuavcan/test/util/map.cpp b/libuavcan/test/util/map.cpp index 3f8671ede9..e394415b00 100644 --- a/libuavcan/test/util/map.cpp +++ b/libuavcan/test/util/map.cpp @@ -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;