inline int operator () (EArgs&&... args) // O(n) { if(slots.empty()) return default_return_value; auto it = slots.cbegin(), end = slots.cend(); auto prev = slots.cbefore_begin(); while(it != end) { try { (it->second)(std::forward<EArgs>(args)...); prev = it; ++it; } catch(slot_remove) { it = slots.erase_after(prev); } catch(signal_return e) { return e.get(); } } return default_return_value; }
void removeDupsNoBuffer(std::forward_list<int>& l) { if (l.empty()) return; auto backtrack = l.before_begin(); while (std::next(backtrack) != l.end()) { // Iterate and print values of the list for (int n : l) std::cout << n << '\t'; std::cout << std::endl; auto prev = std::next(backtrack); auto iter = std::next(prev); //std::cout << " prev =" << *prev << ", iter=" << *iter << std::endl; while (iter != l.end()) { if (*iter == *std::next(backtrack)) iter = l.erase_after(prev); else { ++prev; ++iter; } } ++backtrack; } }
void removeDups2(std::forward_list<int>& l) { std::unordered_set<int> s; auto prev = l.before_begin(); auto iter = l.begin(); while (iter != l.end()) { if (s.find(*iter) != s.end()) { iter = l.erase_after(prev); } else { s.insert(*iter); ++iter; ++prev; } } }
inline bool disconnect(connection_type con) // O(n) { auto it = slots.cbegin(), end = slots.cend(); auto prev = slots.cbefore_begin(); while(it != end) { if(it == con) { slots.erase_after(prev); return true; } prev = it; ++it; } return false; }
inline void operator () (Args... args) { if(slots.empty()) return; auto it = slots.begin(), end = slots.end(); auto prev = slots.before_begin(); while(it != end) { try { (*it)(args...); } catch(slot_remove) { slots.erase_after(prev); it = prev; } prev = it; ++it; } }
void LinkedListExercises::RemoveDuplicates2(std::forward_list<int>& rList) { std::unordered_map<int, int> my_table; std::unordered_map<int, int>::const_iterator found_pos; std::forward_list<int>::const_iterator it = rList.begin(); std::forward_list<int>::const_iterator prev_it = rList.begin(); while (it!=rList.end()) { found_pos = my_table.find(*it); if (found_pos != my_table.end()) { it++; //Note: could use it = std::next(it) rList.erase_after(prev_it); } else { my_table.insert(std::make_pair(*it, 1)); prev_it = it; it++; } } }
void bad_erase_after_forward_list2(std::forward_list<int> &FL) { auto i3 = FL.cbegin(), i0 = i3++, i1 = i3++, i2 = i3++; FL.erase_after(i0, i3); *i1; // expected-warning{{Invalidated iterator accessed}} *i2; // expected-warning{{Invalidated iterator accessed}} }
void good_erase_after_forward_list2(std::forward_list<int> &FL) { auto i3 = FL.cbegin(), i0 = i3++, i1 = i3++, i2 = i3++; FL.erase_after(i0, i3); *i0; // no-warning *i3; // no-warning }
void bad_erase_after_forward_list1(std::forward_list<int> &FL) { auto i1 = FL.cbegin(), i0 = i1++; FL.erase_after(i0); *i1; // expected-warning{{Invalidated iterator accessed}} }
void good_erase_after_forward_list1(std::forward_list<int> &FL) { auto i2 = FL.cbegin(), i0 = i2++, i1 = i2++; FL.erase_after(i0); *i0; // no-warning *i2; // no-warning }