mutable_queue.hpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. //
  2. //=======================================================================
  3. // Copyright 1997, 1998, 1999, 2000 University of Notre Dame.
  4. // Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek
  5. //
  6. // Distributed under the Boost Software License, Version 1.0. (See
  7. // accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. //=======================================================================
  10. //
  11. #ifndef BOOST_MUTABLE_QUEUE_HPP
  12. #define BOOST_MUTABLE_QUEUE_HPP
  13. #include <vector>
  14. #include <algorithm>
  15. #include <functional>
  16. #include <boost/property_map/property_map.hpp>
  17. #include <boost/pending/mutable_heap.hpp>
  18. #include <boost/pending/is_heap.hpp>
  19. #include <boost/graph/detail/array_binary_tree.hpp>
  20. #include <iterator>
  21. namespace boost {
  22. // The mutable queue whose elements are indexed
  23. //
  24. // This adaptor provides a special kind of priority queue that has
  25. // and update operation. This allows the ordering of the items to
  26. // change. After the ordering criteria for item x changes, one must
  27. // call the Q.update(x)
  28. //
  29. // In order to efficiently find x in the queue, a functor must be
  30. // provided to map value_type to a unique ID, which the
  31. // mutable_queue will then use to map to the location of the
  32. // item. The ID's generated must be between 0 and N, where N is the
  33. // value passed to the constructor of mutable_queue
  34. template <class IndexedType,
  35. class RandomAccessContainer = std::vector<IndexedType>,
  36. class Comp = std::less<typename RandomAccessContainer::value_type>,
  37. class ID = identity_property_map >
  38. class mutable_queue {
  39. public:
  40. typedef IndexedType value_type;
  41. typedef typename RandomAccessContainer::size_type size_type;
  42. protected:
  43. typedef typename RandomAccessContainer::iterator iterator;
  44. #if !defined BOOST_NO_STD_ITERATOR_TRAITS
  45. typedef array_binary_tree_node<iterator, ID> Node;
  46. #else
  47. typedef array_binary_tree_node<iterator, value_type, ID> Node;
  48. #endif
  49. typedef compare_array_node<RandomAccessContainer,Comp> Compare;
  50. typedef std::vector<size_type> IndexArray;
  51. public:
  52. typedef Compare value_compare;
  53. typedef ID id_generator;
  54. mutable_queue(size_type n, const Comp& x, const ID& _id)
  55. : index_array(n), comp(x), id(_id) {
  56. c.reserve(n);
  57. }
  58. template <class ForwardIterator>
  59. mutable_queue(ForwardIterator first, ForwardIterator last,
  60. const Comp& x, const ID& _id)
  61. : index_array(std::distance(first, last)), comp(x), id(_id)
  62. {
  63. while( first != last ) {
  64. push(*first);
  65. ++first;
  66. }
  67. }
  68. bool empty() const { return c.empty(); }
  69. void pop() {
  70. value_type tmp = c.back();
  71. c.back() = c.front();
  72. c.front() = tmp;
  73. size_type id_f = get(id, c.back());
  74. size_type id_b = get(id, tmp);
  75. size_type i = index_array[ id_b ];
  76. index_array[ id_b ] = index_array[ id_f ];
  77. index_array[ id_f ] = i;
  78. c.pop_back();
  79. Node node(c.begin(), c.end(), c.begin(), id);
  80. down_heap(node, comp, index_array);
  81. }
  82. void push(const IndexedType& x) {
  83. c.push_back(x);
  84. /*set index-array*/
  85. index_array[ get(id, x) ] = c.size()-1;
  86. Node node(c.begin(), c.end(), c.end() - 1, id);
  87. up_heap(node, comp, index_array);
  88. }
  89. void update(const IndexedType& x) {
  90. size_type current_pos = index_array[ get(id, x) ];
  91. c[current_pos] = x;
  92. Node node(c.begin(), c.end(), c.begin()+current_pos, id);
  93. update_heap(node, comp, index_array);
  94. }
  95. value_type& front() { return c.front(); }
  96. value_type& top() { return c.front(); }
  97. const value_type& front() const { return c.front(); }
  98. const value_type& top() const { return c.front(); }
  99. size_type size() const { return c.size(); }
  100. void clear() { c.clear(); }
  101. #if 0
  102. // dwa 2003/7/11 - I don't know what compiler is supposed to
  103. // be able to compile this, but is_heap is not standard!!
  104. bool test() {
  105. return std::is_heap(c.begin(), c.end(), Comp());
  106. }
  107. #endif
  108. protected:
  109. IndexArray index_array;
  110. Compare comp;
  111. RandomAccessContainer c;
  112. ID id;
  113. };
  114. }
  115. #endif // BOOST_MUTABLE_QUEUE_HPP