void handleGetNodeInfoResponse(const ServiceCallResult<protocol::GetNodeInfo>& result) { Entry& entry = getEntry(result.getCallID().server_node_id); if (result.isSuccessful()) { /* * Updating the uptime here allows to properly handle a corner case where the service response arrives * after the device has restarted and published its new NodeStatus (although it's unlikely to happen). */ entry.uptime_sec = result.getResponse().status.uptime_sec; entry.request_needed = false; listeners_.forEach(NodeInfoRetrievedHandlerCaller(result.getCallID().server_node_id, result.getResponse())); } else { if (num_attempts_ != UnlimitedRequestAttempts) { entry.num_attempts_made++; if (entry.num_attempts_made >= num_attempts_) { entry.request_needed = false; listeners_.forEach(GenericHandlerCaller<NodeID>(&INodeInfoListener::handleNodeInfoUnavailable, result.getCallID().server_node_id)); } } } }
void combine(const Multiset& ms1, const Multiset& ms2, Multiset& result) { result = ms1; for (int k = 0; k < ms2.uniqueSize(); k++) //loop through ms2 and insert into result { ItemType x; int n = ms2.get(k, x); for (int count = n; count > 0; count--) //insert all iterations of value result.insert(x); } }
virtual void handleNodeStatusChange(const NodeStatusChangeEvent& event) { const bool was_offline = !event.old_status.known || (event.old_status.status_code == protocol::NodeStatus::STATUS_OFFLINE); const bool offline_now = !event.status.known || (event.status.status_code == protocol::NodeStatus::STATUS_OFFLINE); if (was_offline || offline_now) { Entry& entry = getEntry(event.node_id); entry.request_needed = !offline_now; entry.num_attempts_made = 0; UAVCAN_TRACE("NodeInfoRetriever", "Offline status change: node ID %d, request needed: %d", int(event.node_id.get()), int(entry.request_needed)); if (entry.request_needed) { startTimerIfNotRunning(); } } listeners_.forEach( GenericHandlerCaller<const NodeStatusChangeEvent&>(&INodeInfoListener::handleNodeStatusChange, event)); }
void subtract(const Multiset& ms1, const Multiset& ms2, Multiset& result) { if (&ms2 == &result) //If ms2 and result are the same object, then modifications to result change ms2, so a copy of ms2 has to be made { Multiset temp = ms2; result = ms1; //temp and result are NOT the same object, so temp remains the same for each iteration of the loop for (int i = 0; i < temp.uniqueSize(); i++) { ItemType item; int count = temp.get(i, item); for (int j = 0; j < count; j++) result.erase(item); } } else //ms2 and result are NOT the same object, so ms2 remains the same for each iteration of the loop { result = ms1; //If ms1 and result are the same object, nothing happens for (int i = 0; i < ms2.uniqueSize(); i++) { ItemType item; int count = ms2.get(i, item); for (int j = 0; j < count; j++) result.erase(item); } } }
void subtract(const Multiset& ms1, const Multiset& ms2, Multiset& result) { //make sure result is empty if (result.size() != 0) { Multiset temp; result = temp; } for (int m1 = 0; m1 < ms1.uniqueSize(); m1++) { ItemType m1_val; int n1 = ms1.get(m1, m1_val); for (int m2 = 0; m2 < ms2.uniqueSize(); m2++) { ItemType m2_val; int n2 = ms2.get(m2, m2_val); if (m1_val == m2_val && n1 > n2) { for (int count = n1-n2; count > 0; count--) //insert n1-n2 number of iterations result.insert(m1_val); } } } }
void test() { Multiset ulms; assert(ulms.insert(20)); assert(ulms.insert(10)); assert(ulms.insert(20)); assert(ulms.insert(30)); assert(ulms.insert(20)); assert(ulms.insert(10)); assert(ulms.size() == 6 && ulms.uniqueSize() == 3); assert(ulms.count(10) == 2); assert(ulms.count(20) == 3); assert(ulms.count(30) == 1); assert(ulms.count(40) == 0); }
void combine(const Multiset& ms1, const Multiset& ms2, Multiset& result) { bool addFirst = false; //addFirst indicates whether the contents of ms1 should be added to result bool addSecond = false; //addSecond does the same, but for ms2 if (&ms1 == &result) //As result is ms1, only need to add ms2 to result to combine the two addSecond = true; else if (&ms2 == &result) //As result is ms2, only need to add ms1 to result to combine the two addFirst = true; else //Empty out result, as it may have contained something prior to this { int size = result.uniqueSize(); for (int i = 0; i < size; i++) { ItemType item; result.get(0, item); result.eraseAll(item); } addFirst = true; addSecond = true; } if (addFirst) //ms1 and result are NOT the same object, so ms1.uniqueSize() remains the same for each iteration of the loop { for (int j = 0; j < ms1.uniqueSize(); j++) { ItemType item; int count = ms1.get(j, item); for (int k = 0; k < count; k++) result.insert(item); } } if (addSecond) //ms2 and result are NOT the same object, so ms2.uniqueSize() remains the same for each iteration of the loop { for (int j = 0; j < ms2.uniqueSize(); j++) { ItemType item; int count = ms2.get(j, item); for (int k = 0; k < count; k++) result.insert(item); } } }
int main() { Multiset<int> mi; mi.insert(7); // OK Multiset<string> ms; ms.insert("http://www.symantec.com"); // OK Multiset<URL> mu; mu.insert(URL("http://www.symantec.com")); // error! }
virtual void handleNodeStatusMessage(const ReceivedDataStructure<protocol::NodeStatus>& msg) { Entry& entry = getEntry(msg.getSrcNodeID()); if (msg.uptime_sec < entry.uptime_sec) { entry.request_needed = true; entry.num_attempts_made = 0; startTimerIfNotRunning(); } entry.uptime_sec = msg.uptime_sec; entry.updated_since_last_attempt = true; listeners_.forEach(GenericHandlerCaller<const ReceivedDataStructure<protocol::NodeStatus>&>( &INodeInfoListener::handleNodeStatusMessage, msg)); }
int main(void) { Multiset ms; ms.insert("fennel"); ms.insert("fennel"); ms.insert("fenugreek"); ms.insert("fennel"); for (int k = 0; k < ms.uniqueSize(); k++) { string x; int n = ms.get(k, x); cout << x << " occurs " << n << " times." << endl; } }
inline void test_multiset_insert(Multiset c) { using phx::arg_names::arg1; using phx::arg_names::arg2; using phx::arg_names::arg3; typename Multiset::value_type const value = *c.begin(); typename Multiset::iterator c_begin = c.begin(); std::size_t old_size = c.size(); // wrapper for // iterator insert(iterator where, const value_type& val); typename Multiset::iterator it = phx::insert(arg1, arg2, arg3)(c, c_begin, value); if (test(*it != value || c.size() != old_size + 1)) { cerr << "Failed " << typeid(Multiset).name() << " test_multiset_insert 1\n"; return; } // wrapper for // iterator insert(const value_type& val); typename Multiset::value_type const value2(1400); it = phx::insert(arg1, arg2)(c, value2); if (test(it == c.end())) { cerr << "Failed " << typeid(Multiset).name() << " test_multiset_insert 2\n"; return; } // wrapper for // template<class InIt> // void insert(InIt first, InIt last); Multiset const const_c = build_assoc<Multiset>(); typename Multiset::size_type size = c.size(); phx::insert(arg1, const_c.begin(), const_c.end())(c); if (test(c.size() != size + const_c.size())) { cerr << "Failed " << typeid(Multiset).name() << " test_multiset_insert 3\n"; return; } }