mutable_queue.hpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. {
  23. // The mutable queue whose elements are indexed
  24. //
  25. // This adaptor provides a special kind of priority queue that has
  26. // and update operation. This allows the ordering of the items to
  27. // change. After the ordering criteria for item x changes, one must
  28. // call the Q.update(x)
  29. //
  30. // In order to efficiently find x in the queue, a functor must be
  31. // provided to map value_type to a unique ID, which the
  32. // mutable_queue will then use to map to the location of the
  33. // item. The ID's generated must be between 0 and N, where N is the
  34. // value passed to the constructor of mutable_queue
  35. template < class IndexedType,
  36. class RandomAccessContainer = std::vector< IndexedType >,
  37. class Comp = std::less< typename RandomAccessContainer::value_type >,
  38. class ID = identity_property_map >
  39. class mutable_queue
  40. {
  41. public:
  42. typedef IndexedType value_type;
  43. typedef typename RandomAccessContainer::size_type size_type;
  44. protected:
  45. typedef typename RandomAccessContainer::iterator iterator;
  46. #if !defined BOOST_NO_STD_ITERATOR_TRAITS
  47. typedef array_binary_tree_node< iterator, ID > Node;
  48. #else
  49. typedef array_binary_tree_node< iterator, value_type, ID > Node;
  50. #endif
  51. typedef compare_array_node< RandomAccessContainer, Comp > Compare;
  52. typedef std::vector< size_type > IndexArray;
  53. public:
  54. typedef Compare value_compare;
  55. typedef ID id_generator;
  56. mutable_queue(size_type n, const Comp& x, const ID& _id)
  57. : index_array(n), comp(x), id(_id)
  58. {
  59. c.reserve(n);
  60. }
  61. template < class ForwardIterator >
  62. mutable_queue(ForwardIterator first, ForwardIterator last, const Comp& x,
  63. const ID& _id)
  64. : index_array(std::distance(first, last)), comp(x), id(_id)
  65. {
  66. while (first != last)
  67. {
  68. push(*first);
  69. ++first;
  70. }
  71. }
  72. bool empty() const { return c.empty(); }
  73. void pop()
  74. {
  75. value_type tmp = c.back();
  76. c.back() = c.front();
  77. c.front() = tmp;
  78. size_type id_f = get(id, c.back());
  79. size_type id_b = get(id, tmp);
  80. size_type i = index_array[id_b];
  81. index_array[id_b] = index_array[id_f];
  82. index_array[id_f] = i;
  83. c.pop_back();
  84. Node node(c.begin(), c.end(), c.begin(), id);
  85. down_heap(node, comp, index_array);
  86. }
  87. void push(const IndexedType& x)
  88. {
  89. c.push_back(x);
  90. /*set index-array*/
  91. index_array[get(id, x)] = c.size() - 1;
  92. Node node(c.begin(), c.end(), c.end() - 1, id);
  93. up_heap(node, comp, index_array);
  94. }
  95. void update(const IndexedType& x)
  96. {
  97. size_type current_pos = index_array[get(id, x)];
  98. c[current_pos] = x;
  99. Node node(c.begin(), c.end(), c.begin() + current_pos, id);
  100. update_heap(node, comp, index_array);
  101. }
  102. value_type& front() { return c.front(); }
  103. value_type& top() { return c.front(); }
  104. const value_type& front() const { return c.front(); }
  105. const value_type& top() const { return c.front(); }
  106. size_type size() const { return c.size(); }
  107. void clear() { c.clear(); }
  108. #if 0
  109. // dwa 2003/7/11 - I don't know what compiler is supposed to
  110. // be able to compile this, but is_heap is not standard!!
  111. bool test() {
  112. return std::is_heap(c.begin(), c.end(), Comp());
  113. }
  114. #endif
  115. protected:
  116. IndexArray index_array;
  117. Compare comp;
  118. RandomAccessContainer c;
  119. ID id;
  120. };
  121. }
  122. #endif // BOOST_MUTABLE_QUEUE_HPP