filtered_queue.hpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Copyright (C) 2004-2006 The Trustees of Indiana University.
  2. // Use, modification and distribution is subject to the Boost Software
  3. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. // Authors: Douglas Gregor
  6. // Andrew Lumsdaine
  7. #ifndef BOOST_FILTERED_QUEUE_HPP
  8. #define BOOST_FILTERED_QUEUE_HPP
  9. #ifndef BOOST_GRAPH_USE_MPI
  10. #error "Parallel BGL files should not be included unless <boost/graph/use_mpi.hpp> has been included"
  11. #endif
  12. #include <algorithm>
  13. namespace boost {
  14. /** Queue adaptor that filters elements pushed into the queue
  15. * according to some predicate.
  16. */
  17. template<typename Buffer, typename Predicate>
  18. class filtered_queue
  19. {
  20. public:
  21. typedef Buffer buffer_type;
  22. typedef Predicate predicate_type;
  23. typedef typename Buffer::value_type value_type;
  24. typedef typename Buffer::size_type size_type;
  25. /**
  26. * Constructs a new filtered queue with an initial buffer and a
  27. * predicate.
  28. *
  29. * @param buffer the initial buffer
  30. * @param pred the predicate
  31. */
  32. explicit
  33. filtered_queue(const buffer_type& buffer = buffer_type(),
  34. const predicate_type& pred = predicate_type())
  35. : buffer(buffer), pred(pred) {}
  36. /** Push a value into the queue.
  37. *
  38. * If the predicate returns @c true for @p x, pushes @p x into the
  39. * buffer.
  40. */
  41. void push(const value_type& x) { if (pred(x)) buffer.push(x); }
  42. /** Pop the front element off the buffer.
  43. *
  44. * @pre @c !empty()
  45. */
  46. void pop() { buffer.pop(); }
  47. /** Retrieve the front (top) element in the buffer.
  48. *
  49. * @pre @c !empty()
  50. */
  51. value_type& top() { return buffer.top(); }
  52. /**
  53. * \overload
  54. */
  55. const value_type& top() const { return buffer.top(); }
  56. /** Determine the number of elements in the buffer. */
  57. size_type size() const { return buffer.size(); }
  58. /** Determine if the buffer is empty. */
  59. bool empty() const { return buffer.empty(); }
  60. /** Get a reference to the underlying buffer. */
  61. buffer_type& base() { return buffer; }
  62. const buffer_type& base() const { return buffer; }
  63. /** Swap the contents of this with @p other. */
  64. void swap(filtered_queue& other)
  65. {
  66. using std::swap;
  67. swap(buffer, other.buffer);
  68. swap(pred, other.pred);
  69. }
  70. private:
  71. buffer_type buffer;
  72. predicate_type pred;
  73. };
  74. /** Create a filtered queue. */
  75. template<typename Buffer, typename Predicate>
  76. inline filtered_queue<Buffer, Predicate>
  77. make_filtered_queue(const Buffer& buffer, const Predicate& pred)
  78. { return filtered_queue<Buffer, Predicate>(buffer, pred); }
  79. /** Swap a filtered_queue. */
  80. template<typename Buffer, typename Predicate>
  81. inline void
  82. swap(filtered_queue<Buffer, Predicate>& x,
  83. filtered_queue<Buffer, Predicate>& y)
  84. {
  85. x.swap(y);
  86. }
  87. } // end namespace boost
  88. #endif // BOOST_FILTERED_QUEUE_HPP