I want to remove an item from a queue with a specific value. How to do it? (I'm trying to create a parallel mix of cards and queues, and I'm currently trying to implement this answer )
So, I have a code like this:
#ifndef CONCURRENT_QUEUED_MAP_H #define CONCURRENT_QUEUED_MAP_H #include <map> #include <deque> #include <boost/thread.hpp> #include <boost/thread/locks.hpp> template <class map_t_1, class map_t_2> class concurrent_queued_map { private: std::map<map_t_1, map_t_2> _ds; std::deque<map_t_1> _queue; mutable boost::mutex mut_; public: concurrent_queued_map() {} map_t_2 get(map_t_1 key) { boost::mutex::scoped_lock lock(mut_); return _ds[key]; } map_t_1 put(map_t_1 key, map_t_2 value) { boost::mutex::scoped_lock lock(mut_); _ds.insert(std::pair<map_t_1, map_t_2>(key,value)); _queue.push_back(key); return key; } map_t_2 get_last(map_t_1 key) { boost::mutex::scoped_lock lock(mut_); const map_t_1 k = _queue.front(); return _ds[k]; } void remove_last(map_t_1 key) { boost::mutex::scoped_lock lock(mut_); const map_t_1 k = _queue.front(); _ds.erase(k); _queue.pop_front(); } void remove(map_t_1 key) { boost::mutex::scoped_lock lock(mut_); _queue.erase(std::remove(_queue.begin(), _queue.end(), key), _queue.end()); _ds.erase(k); } int size() { boost::mutex::scoped_lock lock(mut_); return _ds.size(); } }; #endif // CONCURRENT_QUEUED_MAP_H
So what should I do? How to remove from the queue by value? Or could there be any STL or queue-like Boost component? The meaning of this: .front()
, pop_front();
and push_back(key);
as well as support for searching and erasing by value?
source share