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