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