Chris@16: // Chris@16: //======================================================================= Chris@16: // Copyright 1997, 1998, 1999, 2000 University of Notre Dame. Chris@16: // Authors: Andrew Lumsdaine, Lie-Quan Lee, Jeremy G. Siek Chris@16: // Chris@16: // Distributed under the Boost Software License, Version 1.0. (See Chris@16: // accompanying file LICENSE_1_0.txt or copy at Chris@16: // http://www.boost.org/LICENSE_1_0.txt) Chris@16: //======================================================================= Chris@16: // Chris@16: #ifndef BOOST_MUTABLE_QUEUE_HPP Chris@16: #define BOOST_MUTABLE_QUEUE_HPP Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: namespace boost { Chris@16: Chris@16: // The mutable queue whose elements are indexed Chris@16: // Chris@16: // This adaptor provides a special kind of priority queue that has Chris@16: // and update operation. This allows the ordering of the items to Chris@16: // change. After the ordering criteria for item x changes, one must Chris@16: // call the Q.update(x) Chris@16: // Chris@16: // In order to efficiently find x in the queue, a functor must be Chris@16: // provided to map value_type to a unique ID, which the Chris@16: // mutable_queue will then use to map to the location of the Chris@16: // item. The ID's generated must be between 0 and N, where N is the Chris@16: // value passed to the constructor of mutable_queue Chris@16: Chris@16: template , Chris@16: class Comp = std::less, Chris@16: class ID = identity_property_map > Chris@16: class mutable_queue { Chris@16: public: Chris@16: typedef IndexedType value_type; Chris@16: typedef typename RandomAccessContainer::size_type size_type; Chris@16: protected: Chris@16: typedef typename RandomAccessContainer::iterator iterator; Chris@16: #if !defined BOOST_NO_STD_ITERATOR_TRAITS Chris@16: typedef array_binary_tree_node Node; Chris@16: #else Chris@16: typedef array_binary_tree_node Node; Chris@16: #endif Chris@16: typedef compare_array_node Compare; Chris@16: typedef std::vector IndexArray; Chris@16: public: Chris@16: typedef Compare value_compare; Chris@16: typedef ID id_generator; Chris@16: Chris@16: mutable_queue(size_type n, const Comp& x, const ID& _id) Chris@16: : index_array(n), comp(x), id(_id) { Chris@16: c.reserve(n); Chris@16: } Chris@16: template Chris@16: mutable_queue(ForwardIterator first, ForwardIterator last, Chris@16: const Comp& x, const ID& _id) Chris@16: : index_array(std::distance(first, last)), comp(x), id(_id) Chris@16: { Chris@16: while( first != last ) { Chris@16: push(*first); Chris@16: ++first; Chris@16: } Chris@16: } Chris@16: Chris@16: bool empty() const { return c.empty(); } Chris@16: Chris@16: void pop() { Chris@16: value_type tmp = c.back(); Chris@16: c.back() = c.front(); Chris@16: c.front() = tmp; Chris@16: Chris@16: size_type id_f = get(id, c.back()); Chris@16: size_type id_b = get(id, tmp); Chris@16: size_type i = index_array[ id_b ]; Chris@16: index_array[ id_b ] = index_array[ id_f ]; Chris@16: index_array[ id_f ] = i; Chris@16: Chris@16: c.pop_back(); Chris@16: Node node(c.begin(), c.end(), c.begin(), id); Chris@16: down_heap(node, comp, index_array); Chris@16: } Chris@16: void push(const IndexedType& x) { Chris@16: c.push_back(x); Chris@16: /*set index-array*/ Chris@16: index_array[ get(id, x) ] = c.size()-1; Chris@16: Node node(c.begin(), c.end(), c.end() - 1, id); Chris@16: up_heap(node, comp, index_array); Chris@16: } Chris@16: Chris@16: void update(const IndexedType& x) { Chris@16: size_type current_pos = index_array[ get(id, x) ]; Chris@16: c[current_pos] = x; Chris@16: Chris@16: Node node(c.begin(), c.end(), c.begin()+current_pos, id); Chris@16: update_heap(node, comp, index_array); Chris@16: } Chris@16: Chris@16: value_type& front() { return c.front(); } Chris@16: value_type& top() { return c.front(); } Chris@16: Chris@16: const value_type& front() const { return c.front(); } Chris@16: const value_type& top() const { return c.front(); } Chris@16: Chris@16: size_type size() const { return c.size(); } Chris@16: Chris@16: void clear() { c.clear(); } Chris@16: Chris@16: #if 0 Chris@16: // dwa 2003/7/11 - I don't know what compiler is supposed to Chris@16: // be able to compile this, but is_heap is not standard!! Chris@16: bool test() { Chris@16: return std::is_heap(c.begin(), c.end(), Comp()); Chris@16: } Chris@16: #endif Chris@16: Chris@16: protected: Chris@16: IndexArray index_array; Chris@16: Compare comp; Chris@16: RandomAccessContainer c; Chris@16: ID id; Chris@16: }; Chris@16: Chris@16: Chris@16: } Chris@16: Chris@16: #endif // BOOST_MUTABLE_QUEUE_HPP