COIN-OR::LEMON - Graph Library

Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • lemon/bin_heap.h

    r631 r209  
    33 * This file is a part of LEMON, a generic C++ optimization library.
    44 *
    5  * Copyright (C) 2003-2009
     5 * Copyright (C) 2003-2008
    66 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
    77 * (Egervary Research Group on Combinatorial Optimization, EGRES).
     
    3434  ///\brief A Binary Heap implementation.
    3535  ///
    36   ///This class implements the \e binary \e heap data structure.
    37   ///
    38   ///A \e heap is a data structure for storing items with specified values
    39   ///called \e priorities in such a way that finding the item with minimum
    40   ///priority is efficient. \c Comp specifies the ordering of the priorities.
    41   ///In a heap one can change the priority of an item, add or erase an
    42   ///item, etc.
     36  ///This class implements the \e binary \e heap data structure. A \e heap
     37  ///is a data structure for storing items with specified values called \e
     38  ///priorities in such a way that finding the item with minimum priority is
     39  ///efficient. \c Compare specifies the ordering of the priorities. In a heap
     40  ///one can change the priority of an item, add or erase an item, etc.
    4341  ///
    44   ///\tparam PR Type of the priority of the items.
    45   ///\tparam IM A read and writable item map with int values, used internally
     42  ///\tparam _Prio Type of the priority of the items.
     43  ///\tparam _ItemIntMap A read and writable Item int map, used internally
    4644  ///to handle the cross references.
    47   ///\tparam Comp A functor class for the ordering of the priorities.
    48   ///The default is \c std::less<PR>.
     45  ///\tparam _Compare A class for the ordering of the priorities. The
     46  ///default is \c std::less<_Prio>.
    4947  ///
    5048  ///\sa FibHeap
    5149  ///\sa Dijkstra
    52   template <typename PR, typename IM, typename Comp = std::less<PR> >
     50  template <typename _Prio, typename _ItemIntMap,
     51            typename _Compare = std::less<_Prio> >
    5352  class BinHeap {
    5453
    5554  public:
    5655    ///\e
    57     typedef IM ItemIntMap;
    58     ///\e
    59     typedef PR Prio;
     56    typedef _ItemIntMap ItemIntMap;
     57    ///\e
     58    typedef _Prio Prio;
    6059    ///\e
    6160    typedef typename ItemIntMap::Key Item;
     
    6362    typedef std::pair<Item,Prio> Pair;
    6463    ///\e
    65     typedef Comp Compare;
     64    typedef _Compare Compare;
    6665
    6766    /// \brief Type to represent the items states.
     
    7170    /// heap's point of view, but may be useful to the user.
    7271    ///
    73     /// The item-int map must be initialized in such way that it assigns
    74     /// \c PRE_HEAP (<tt>-1</tt>) to any element to be put in the heap.
     72    /// The ItemIntMap \e should be initialized in such way that it maps
     73    /// PRE_HEAP (-1) to any element to be put in the heap...
    7574    enum State {
    76       IN_HEAP = 0,    ///< = 0.
    77       PRE_HEAP = -1,  ///< = -1.
    78       POST_HEAP = -2  ///< = -2.
     75      IN_HEAP = 0,
     76      PRE_HEAP = -1,
     77      POST_HEAP = -2
    7978    };
    8079
    8180  private:
    82     std::vector<Pair> _data;
    83     Compare _comp;
    84     ItemIntMap &_iim;
     81    std::vector<Pair> data;
     82    Compare comp;
     83    ItemIntMap &iim;
    8584
    8685  public:
     
    8887    ///
    8988    /// The constructor.
    90     /// \param map should be given to the constructor, since it is used
    91     /// internally to handle the cross references. The value of the map
    92     /// must be \c PRE_HEAP (<tt>-1</tt>) for every item.
    93     explicit BinHeap(ItemIntMap &map) : _iim(map) {}
    94 
    95     /// \brief The constructor.
    96     ///
    97     /// The constructor.
    98     /// \param map should be given to the constructor, since it is used
     89    /// \param _iim should be given to the constructor, since it is used
    9990    /// internally to handle the cross references. The value of the map
    10091    /// should be PRE_HEAP (-1) for each element.
    101     ///
    102     /// \param comp The comparator function object.
    103     BinHeap(ItemIntMap &map, const Compare &comp)
    104       : _iim(map), _comp(comp) {}
     92    explicit BinHeap(ItemIntMap &_iim) : iim(_iim) {}
     93
     94    /// \brief The constructor.
     95    ///
     96    /// The constructor.
     97    /// \param _iim should be given to the constructor, since it is used
     98    /// internally to handle the cross references. The value of the map
     99    /// should be PRE_HEAP (-1) for each element.
     100    ///
     101    /// \param _comp The comparator function object.
     102    BinHeap(ItemIntMap &_iim, const Compare &_comp)
     103      : iim(_iim), comp(_comp) {}
    105104
    106105
     
    108107    ///
    109108    /// \brief Returns the number of items stored in the heap.
    110     int size() const { return _data.size(); }
     109    int size() const { return data.size(); }
    111110
    112111    /// \brief Checks if the heap stores no items.
    113112    ///
    114113    /// Returns \c true if and only if the heap stores no items.
    115     bool empty() const { return _data.empty(); }
     114    bool empty() const { return data.empty(); }
    116115
    117116    /// \brief Make empty this heap.
     
    122121    /// each item to \c PRE_HEAP.
    123122    void clear() {
    124       _data.clear();
     123      data.clear();
    125124    }
    126125
     
    130129    static int second_child(int i) { return 2*i+2; }
    131130    bool less(const Pair &p1, const Pair &p2) const {
    132       return _comp(p1.second, p2.second);
     131      return comp(p1.second, p2.second);
    133132    }
    134133
    135134    int bubble_up(int hole, Pair p) {
    136135      int par = parent(hole);
    137       while( hole>0 && less(p,_data[par]) ) {
    138         move(_data[par],hole);
     136      while( hole>0 && less(p,data[par]) ) {
     137        move(data[par],hole);
    139138        hole = par;
    140139        par = parent(hole);
     
    147146      int child = second_child(hole);
    148147      while(child < length) {
    149         if( less(_data[child-1], _data[child]) ) {
     148        if( less(data[child-1], data[child]) ) {
    150149          --child;
    151150        }
    152         if( !less(_data[child], p) )
     151        if( !less(data[child], p) )
    153152          goto ok;
    154         move(_data[child], hole);
     153        move(data[child], hole);
    155154        hole = child;
    156155        child = second_child(hole);
    157156      }
    158157      child--;
    159       if( child<length && less(_data[child], p) ) {
    160         move(_data[child], hole);
     158      if( child<length && less(data[child], p) ) {
     159        move(data[child], hole);
    161160        hole=child;
    162161      }
     
    167166
    168167    void move(const Pair &p, int i) {
    169       _data[i] = p;
    170       _iim.set(p.first, i);
     168      data[i] = p;
     169      iim.set(p.first, i);
    171170    }
    172171
     
    177176    /// \param p The pair to insert.
    178177    void push(const Pair &p) {
    179       int n = _data.size();
    180       _data.resize(n+1);
     178      int n = data.size();
     179      data.resize(n+1);
    181180      bubble_up(n, p);
    182181    }
     
    195194    /// \pre The heap must be nonempty.
    196195    Item top() const {
    197       return _data[0].first;
     196      return data[0].first;
    198197    }
    199198
     
    203202    /// \pre The heap must be nonempty.
    204203    Prio prio() const {
    205       return _data[0].second;
     204      return data[0].second;
    206205    }
    207206
     
    212211    /// \pre The heap must be non-empty.
    213212    void pop() {
    214       int n = _data.size()-1;
    215       _iim.set(_data[0].first, POST_HEAP);
     213      int n = data.size()-1;
     214      iim.set(data[0].first, POST_HEAP);
    216215      if (n > 0) {
    217         bubble_down(0, _data[n], n);
    218       }
    219       _data.pop_back();
     216        bubble_down(0, data[n], n);
     217      }
     218      data.pop_back();
    220219    }
    221220
     
    226225    /// \pre The item should be in the heap.
    227226    void erase(const Item &i) {
    228       int h = _iim[i];
    229       int n = _data.size()-1;
    230       _iim.set(_data[h].first, POST_HEAP);
     227      int h = iim[i];
     228      int n = data.size()-1;
     229      iim.set(data[h].first, POST_HEAP);
    231230      if( h < n ) {
    232         if ( bubble_up(h, _data[n]) == h) {
    233           bubble_down(h, _data[n], n);
     231        if ( bubble_up(h, data[n]) == h) {
     232          bubble_down(h, data[n], n);
    234233        }
    235234      }
    236       _data.pop_back();
     235      data.pop_back();
    237236    }
    238237
     
    241240    ///
    242241    /// This function returns the priority of item \c i.
    243     /// \param i The item.
    244242    /// \pre \c i must be in the heap.
     243    /// \param i The item.
    245244    Prio operator[](const Item &i) const {
    246       int idx = _iim[i];
    247       return _data[idx].second;
     245      int idx = iim[i];
     246      return data[idx].second;
    248247    }
    249248
     
    256255    /// \param p The priority.
    257256    void set(const Item &i, const Prio &p) {
    258       int idx = _iim[i];
     257      int idx = iim[i];
    259258      if( idx < 0 ) {
    260259        push(i,p);
    261260      }
    262       else if( _comp(p, _data[idx].second) ) {
     261      else if( comp(p, data[idx].second) ) {
    263262        bubble_up(idx, Pair(i,p));
    264263      }
    265264      else {
    266         bubble_down(idx, Pair(i,p), _data.size());
     265        bubble_down(idx, Pair(i,p), data.size());
    267266      }
    268267    }
     
    271270    ///
    272271    /// This method decreases the priority of item \c i to \c p.
    273     /// \param i The item.
    274     /// \param p The priority.
    275272    /// \pre \c i must be stored in the heap with priority at least \c
    276273    /// p relative to \c Compare.
     274    /// \param i The item.
     275    /// \param p The priority.
    277276    void decrease(const Item &i, const Prio &p) {
    278       int idx = _iim[i];
     277      int idx = iim[i];
    279278      bubble_up(idx, Pair(i,p));
    280279    }
     
    283282    ///
    284283    /// This method sets the priority of item \c i to \c p.
    285     /// \param i The item.
    286     /// \param p The priority.
    287284    /// \pre \c i must be stored in the heap with priority at most \c
    288285    /// p relative to \c Compare.
     286    /// \param i The item.
     287    /// \param p The priority.
    289288    void increase(const Item &i, const Prio &p) {
    290       int idx = _iim[i];
    291       bubble_down(idx, Pair(i,p), _data.size());
     289      int idx = iim[i];
     290      bubble_down(idx, Pair(i,p), data.size());
    292291    }
    293292
     
    301300    /// \param i The item.
    302301    State state(const Item &i) const {
    303       int s = _iim[i];
     302      int s = iim[i];
    304303      if( s>=0 )
    305304        s=0;
     
    321320          erase(i);
    322321        }
    323         _iim[i] = st;
     322        iim[i] = st;
    324323        break;
    325324      case IN_HEAP:
     
    335334    /// with the same prioriority as prevoiusly the \c i item.
    336335    void replace(const Item& i, const Item& j) {
    337       int idx = _iim[i];
    338       _iim.set(i, _iim[j]);
    339       _iim.set(j, idx);
    340       _data[idx].first = j;
     336      int idx = iim[i];
     337      iim.set(i, iim[j]);
     338      iim.set(j, idx);
     339      data[idx].first = j;
    341340    }
    342341
Note: See TracChangeset for help on using the changeset viewer.