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