File indexing completed on 2025-01-18 09:43:32
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #ifndef BOOST_MUTABLE_QUEUE_HPP
0012 #define BOOST_MUTABLE_QUEUE_HPP
0013
0014 #include <vector>
0015 #include <algorithm>
0016 #include <functional>
0017 #include <boost/property_map/property_map.hpp>
0018 #include <boost/pending/mutable_heap.hpp>
0019 #include <boost/pending/is_heap.hpp>
0020 #include <boost/graph/detail/array_binary_tree.hpp>
0021 #include <iterator>
0022
0023 namespace boost
0024 {
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039 template < class IndexedType,
0040 class RandomAccessContainer = std::vector< IndexedType >,
0041 class Comp = std::less< typename RandomAccessContainer::value_type >,
0042 class ID = identity_property_map >
0043 class mutable_queue
0044 {
0045 public:
0046 typedef IndexedType value_type;
0047 typedef typename RandomAccessContainer::size_type size_type;
0048
0049 protected:
0050 typedef typename RandomAccessContainer::iterator iterator;
0051 #if !defined BOOST_NO_STD_ITERATOR_TRAITS
0052 typedef array_binary_tree_node< iterator, ID > Node;
0053 #else
0054 typedef array_binary_tree_node< iterator, value_type, ID > Node;
0055 #endif
0056 typedef compare_array_node< RandomAccessContainer, Comp > Compare;
0057 typedef std::vector< size_type > IndexArray;
0058
0059 public:
0060 typedef Compare value_compare;
0061 typedef ID id_generator;
0062
0063 mutable_queue(size_type n, const Comp& x, const ID& _id)
0064 : index_array(n), comp(x), id(_id)
0065 {
0066 c.reserve(n);
0067 }
0068 template < class ForwardIterator >
0069 mutable_queue(ForwardIterator first, ForwardIterator last, const Comp& x,
0070 const ID& _id)
0071 : index_array(std::distance(first, last)), comp(x), id(_id)
0072 {
0073 while (first != last)
0074 {
0075 push(*first);
0076 ++first;
0077 }
0078 }
0079
0080 bool empty() const { return c.empty(); }
0081
0082 void pop()
0083 {
0084 value_type tmp = c.back();
0085 c.back() = c.front();
0086 c.front() = tmp;
0087
0088 size_type id_f = get(id, c.back());
0089 size_type id_b = get(id, tmp);
0090 size_type i = index_array[id_b];
0091 index_array[id_b] = index_array[id_f];
0092 index_array[id_f] = i;
0093
0094 c.pop_back();
0095 Node node(c.begin(), c.end(), c.begin(), id);
0096 down_heap(node, comp, index_array);
0097 }
0098 void push(const IndexedType& x)
0099 {
0100 c.push_back(x);
0101
0102 index_array[get(id, x)] = c.size() - 1;
0103 Node node(c.begin(), c.end(), c.end() - 1, id);
0104 up_heap(node, comp, index_array);
0105 }
0106
0107 void update(const IndexedType& x)
0108 {
0109 size_type current_pos = index_array[get(id, x)];
0110 c[current_pos] = x;
0111
0112 Node node(c.begin(), c.end(), c.begin() + current_pos, id);
0113 update_heap(node, comp, index_array);
0114 }
0115
0116 value_type& front() { return c.front(); }
0117 value_type& top() { return c.front(); }
0118
0119 const value_type& front() const { return c.front(); }
0120 const value_type& top() const { return c.front(); }
0121
0122 size_type size() const { return c.size(); }
0123
0124 void clear() { c.clear(); }
0125
0126 #if 0
0127
0128
0129 bool test() {
0130 return std::is_heap(c.begin(), c.end(), Comp());
0131 }
0132 #endif
0133
0134 protected:
0135 IndexArray index_array;
0136 Compare comp;
0137 RandomAccessContainer c;
0138 ID id;
0139 };
0140
0141 }
0142
0143 #endif