1 // Copyright (C) 2004-2006 The Trustees of Indiana University.
3 // Use, modification and distribution is subject to the Boost Software
4 // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
7 // Authors: Douglas Gregor
9 #ifndef BOOST_FILTERED_QUEUE_HPP
10 #define BOOST_FILTERED_QUEUE_HPP
12 #ifndef BOOST_GRAPH_USE_MPI
13 #error "Parallel BGL files should not be included unless <boost/graph/use_mpi.hpp> has been included"
20 /** Queue adaptor that filters elements pushed into the queue
21 * according to some predicate.
23 template<typename Buffer, typename Predicate>
27 typedef Buffer buffer_type;
28 typedef Predicate predicate_type;
29 typedef typename Buffer::value_type value_type;
30 typedef typename Buffer::size_type size_type;
33 * Constructs a new filtered queue with an initial buffer and a
36 * @param buffer the initial buffer
37 * @param pred the predicate
40 filtered_queue(const buffer_type& buffer = buffer_type(),
41 const predicate_type& pred = predicate_type())
42 : buffer(buffer), pred(pred) {}
44 /** Push a value into the queue.
46 * If the predicate returns @c true for @p x, pushes @p x into the
49 void push(const value_type& x) { if (pred(x)) buffer.push(x); }
51 /** Pop the front element off the buffer.
55 void pop() { buffer.pop(); }
57 /** Retrieve the front (top) element in the buffer.
61 value_type& top() { return buffer.top(); }
66 const value_type& top() const { return buffer.top(); }
68 /** Determine the number of elements in the buffer. */
69 size_type size() const { return buffer.size(); }
71 /** Determine if the buffer is empty. */
72 bool empty() const { return buffer.empty(); }
74 /** Get a reference to the underlying buffer. */
75 buffer_type& base() { return buffer; }
76 const buffer_type& base() const { return buffer; }
78 /** Swap the contents of this with @p other. */
79 void swap(filtered_queue& other)
82 swap(buffer, other.buffer);
83 swap(pred, other.pred);
91 /** Create a filtered queue. */
92 template<typename Buffer, typename Predicate>
93 inline filtered_queue<Buffer, Predicate>
94 make_filtered_queue(const Buffer& buffer, const Predicate& pred)
95 { return filtered_queue<Buffer, Predicate>(buffer, pred); }
97 /** Swap a filtered_queue. */
98 template<typename Buffer, typename Predicate>
100 swap(filtered_queue<Buffer, Predicate>& x,
101 filtered_queue<Buffer, Predicate>& y)
106 } // end namespace boost
108 #endif // BOOST_FILTERED_QUEUE_HPP