COIN-OR::LEMON - Graph Library

Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • lemon/radix_heap.h

    r730 r758  
    2020#define LEMON_RADIX_HEAP_H
    2121
    22 ///\ingroup auxdat
     22///\ingroup heaps
    2323///\file
    24 ///\brief Radix Heap implementation.
     24///\brief Radix heap implementation.
    2525
    2626#include <vector>
     
    3030
    3131
    32   /// \ingroup auxdata
     32  /// \ingroup heaps
    3333  ///
    34   /// \brief A Radix Heap implementation.
     34  /// \brief Radix heap data structure.
    3535  ///
    36   /// This class implements the \e radix \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. This heap type can store only items with \e int priority.
    40   /// In a heap one can change the priority of an item, add or erase an
    41   /// item, but the priority cannot be decreased under the last removed
    42   /// item's priority.
     36  /// This class implements the \e radix \e heap data structure.
     37  /// It practically conforms to the \ref concepts::Heap "heap concept",
     38  /// but it has some limitations due its special implementation.
     39  /// The type of the priorities must be \c int and the priority of an
     40  /// item cannot be decreased under the priority of the last removed item.
    4341  ///
    44   /// \param IM A read and writable Item int map, used internally
    45   /// to handle the cross references.
    46   ///
    47   /// \see BinHeap
    48   /// \see Dijkstra
     42  /// \tparam IM A read-writable item map with \c int values, used
     43  /// internally to handle the cross references.
    4944  template <typename IM>
    5045  class RadixHeap {
    5146
    5247  public:
    53     typedef typename IM::Key Item;
     48
     49    /// Type of the item-int map.
     50    typedef IM ItemIntMap;
     51    /// Type of the priorities.
    5452    typedef int Prio;
    55     typedef IM ItemIntMap;
     53    /// Type of the items stored in the heap.
     54    typedef typename ItemIntMap::Key Item;
    5655
    5756    /// \brief Exception thrown by RadixHeap.
    5857    ///
    59     /// This Exception is thrown when a smaller priority
    60     /// is inserted into the \e RadixHeap then the last time erased.
     58    /// This exception is thrown when an item is inserted into a
     59    /// RadixHeap with a priority smaller than the last erased one.
    6160    /// \see RadixHeap
    62 
    63     class UnderFlowPriorityError : public Exception {
     61    class PriorityUnderflowError : public Exception {
    6462    public:
    6563      virtual const char* what() const throw() {
    66         return "lemon::RadixHeap::UnderFlowPriorityError";
     64        return "lemon::RadixHeap::PriorityUnderflowError";
    6765      }
    6866    };
    6967
    70     /// \brief Type to represent the items states.
    71     ///
    72     /// Each Item element have a state associated to it. It may be "in heap",
    73     /// "pre heap" or "post heap". The latter two are indifferent from the
     68    /// \brief Type to represent the states of the items.
     69    ///
     70    /// Each item has a state associated to it. It can be "in heap",
     71    /// "pre-heap" or "post-heap". The latter two are indifferent from the
    7472    /// heap's point of view, but may be useful to the user.
    7573    ///
    76     /// The ItemIntMap \e should be initialized in such way that it maps
    77     /// PRE_HEAP (-1) to any element to be put in the heap...
     74    /// The item-int map must be initialized in such way that it assigns
     75    /// \c PRE_HEAP (<tt>-1</tt>) to any element to be put in the heap.
    7876    enum State {
    79       IN_HEAP = 0,
    80       PRE_HEAP = -1,
    81       POST_HEAP = -2
     77      IN_HEAP = 0,    ///< = 0.
     78      PRE_HEAP = -1,  ///< = -1.
     79      POST_HEAP = -2  ///< = -2.
    8280    };
    8381
     
    9795    };
    9896
    99     std::vector<RadixItem> data;
    100     std::vector<RadixBox> boxes;
     97    std::vector<RadixItem> _data;
     98    std::vector<RadixBox> _boxes;
    10199
    102100    ItemIntMap &_iim;
    103101
    104 
    105102  public:
    106     /// \brief The constructor.
    107     ///
    108     /// The constructor.
    109     ///
    110     /// \param map It should be given to the constructor, since it is used
    111     /// internally to handle the cross references. The value of the map
    112     /// should be PRE_HEAP (-1) for each element.
    113     ///
    114     /// \param minimal The initial minimal value of the heap.
    115     /// \param capacity It determines the initial capacity of the heap.
    116     RadixHeap(ItemIntMap &map, int minimal = 0, int capacity = 0)
    117       : _iim(map) {
    118       boxes.push_back(RadixBox(minimal, 1));
    119       boxes.push_back(RadixBox(minimal + 1, 1));
    120       while (lower(boxes.size() - 1, capacity + minimal - 1)) {
     103
     104    /// \brief Constructor.
     105    ///
     106    /// Constructor.
     107    /// \param map A map that assigns \c int values to the items.
     108    /// It is used internally to handle the cross references.
     109    /// The assigned value must be \c PRE_HEAP (<tt>-1</tt>) for each item.
     110    /// \param minimum The initial minimum value of the heap.
     111    /// \param capacity The initial capacity of the heap.
     112    RadixHeap(ItemIntMap &map, int minimum = 0, int capacity = 0)
     113      : _iim(map)
     114    {
     115      _boxes.push_back(RadixBox(minimum, 1));
     116      _boxes.push_back(RadixBox(minimum + 1, 1));
     117      while (lower(_boxes.size() - 1, capacity + minimum - 1)) {
    121118        extend();
    122119      }
    123120    }
    124121
    125     /// The number of items stored in the heap.
    126     ///
    127     /// \brief Returns the number of items stored in the heap.
    128     int size() const { return data.size(); }
    129     /// \brief Checks if the heap stores no items.
    130     ///
    131     /// Returns \c true if and only if the heap stores no items.
    132     bool empty() const { return data.empty(); }
    133 
    134     /// \brief Make empty this heap.
    135     ///
    136     /// Make empty this heap. It does not change the cross reference
    137     /// map.  If you want to reuse a heap what is not surely empty you
    138     /// should first clear the heap and after that you should set the
    139     /// cross reference map for each item to \c PRE_HEAP.
    140     void clear(int minimal = 0, int capacity = 0) {
    141       data.clear(); boxes.clear();
    142       boxes.push_back(RadixBox(minimal, 1));
    143       boxes.push_back(RadixBox(minimal + 1, 1));
    144       while (lower(boxes.size() - 1, capacity + minimal - 1)) {
     122    /// \brief The number of items stored in the heap.
     123    ///
     124    /// This function returns the number of items stored in the heap.
     125    int size() const { return _data.size(); }
     126
     127    /// \brief Check if the heap is empty.
     128    ///
     129    /// This function returns \c true if the heap is empty.
     130    bool empty() const { return _data.empty(); }
     131
     132    /// \brief Make the heap empty.
     133    ///
     134    /// This functon makes the heap empty.
     135    /// It does not change the cross reference map. If you want to reuse
     136    /// a heap that is not surely empty, you should first clear it and
     137    /// then you should set the cross reference map to \c PRE_HEAP
     138    /// for each item.
     139    /// \param minimum The minimum value of the heap.
     140    /// \param capacity The capacity of the heap.
     141    void clear(int minimum = 0, int capacity = 0) {
     142      _data.clear(); _boxes.clear();
     143      _boxes.push_back(RadixBox(minimum, 1));
     144      _boxes.push_back(RadixBox(minimum + 1, 1));
     145      while (lower(_boxes.size() - 1, capacity + minimum - 1)) {
    145146        extend();
    146147      }
     
    150151
    151152    bool upper(int box, Prio pr) {
    152       return pr < boxes[box].min;
     153      return pr < _boxes[box].min;
    153154    }
    154155
    155156    bool lower(int box, Prio pr) {
    156       return pr >= boxes[box].min + boxes[box].size;
    157     }
    158 
    159     /// \brief Remove item from the box list.
     157      return pr >= _boxes[box].min + _boxes[box].size;
     158    }
     159
     160    // Remove item from the box list
    160161    void remove(int index) {
    161       if (data[index].prev >= 0) {
    162         data[data[index].prev].next = data[index].next;
     162      if (_data[index].prev >= 0) {
     163        _data[_data[index].prev].next = _data[index].next;
    163164      } else {
    164         boxes[data[index].box].first = data[index].next;
    165       }
    166       if (data[index].next >= 0) {
    167         data[data[index].next].prev = data[index].prev;
    168       }
    169     }
    170 
    171     /// \brief Insert item into the box list.
     165        _boxes[_data[index].box].first = _data[index].next;
     166      }
     167      if (_data[index].next >= 0) {
     168        _data[_data[index].next].prev = _data[index].prev;
     169      }
     170    }
     171
     172    // Insert item into the box list
    172173    void insert(int box, int index) {
    173       if (boxes[box].first == -1) {
    174         boxes[box].first = index;
    175         data[index].next = data[index].prev = -1;
     174      if (_boxes[box].first == -1) {
     175        _boxes[box].first = index;
     176        _data[index].next = _data[index].prev = -1;
    176177      } else {
    177         data[index].next = boxes[box].first;
    178         data[boxes[box].first].prev = index;
    179         data[index].prev = -1;
    180         boxes[box].first = index;
    181       }
    182       data[index].box = box;
    183     }
    184 
    185     /// \brief Add a new box to the box list.
     178        _data[index].next = _boxes[box].first;
     179        _data[_boxes[box].first].prev = index;
     180        _data[index].prev = -1;
     181        _boxes[box].first = index;
     182      }
     183      _data[index].box = box;
     184    }
     185
     186    // Add a new box to the box list
    186187    void extend() {
    187       int min = boxes.back().min + boxes.back().size;
    188       int bs = 2 * boxes.back().size;
    189       boxes.push_back(RadixBox(min, bs));
    190     }
    191 
    192     /// \brief Move an item up into the proper box.
    193     void bubble_up(int index) {
    194       if (!lower(data[index].box, data[index].prio)) return;
     188      int min = _boxes.back().min + _boxes.back().size;
     189      int bs = 2 * _boxes.back().size;
     190      _boxes.push_back(RadixBox(min, bs));
     191    }
     192
     193    // Move an item up into the proper box.
     194    void bubbleUp(int index) {
     195      if (!lower(_data[index].box, _data[index].prio)) return;
    195196      remove(index);
    196       int box = findUp(data[index].box, data[index].prio);
     197      int box = findUp(_data[index].box, _data[index].prio);
    197198      insert(box, index);
    198199    }
    199200
    200     /// \brief Find up the proper box for the item with the given prio.
     201    // Find up the proper box for the item with the given priority
    201202    int findUp(int start, int pr) {
    202203      while (lower(start, pr)) {
    203         if (++start == int(boxes.size())) {
     204        if (++start == int(_boxes.size())) {
    204205          extend();
    205206        }
     
    208209    }
    209210
    210     /// \brief Move an item down into the proper box.
    211     void bubble_down(int index) {
    212       if (!upper(data[index].box, data[index].prio)) return;
     211    // Move an item down into the proper box
     212    void bubbleDown(int index) {
     213      if (!upper(_data[index].box, _data[index].prio)) return;
    213214      remove(index);
    214       int box = findDown(data[index].box, data[index].prio);
     215      int box = findDown(_data[index].box, _data[index].prio);
    215216      insert(box, index);
    216217    }
    217218
    218     /// \brief Find up the proper box for the item with the given prio.
     219    // Find down the proper box for the item with the given priority
    219220    int findDown(int start, int pr) {
    220221      while (upper(start, pr)) {
    221         if (--start < 0) throw UnderFlowPriorityError();
     222        if (--start < 0) throw PriorityUnderflowError();
    222223      }
    223224      return start;
    224225    }
    225226
    226     /// \brief Find the first not empty box.
     227    // Find the first non-empty box
    227228    int findFirst() {
    228229      int first = 0;
    229       while (boxes[first].first == -1) ++first;
     230      while (_boxes[first].first == -1) ++first;
    230231      return first;
    231232    }
    232233
    233     /// \brief Gives back the minimal prio of the box.
     234    // Gives back the minimum priority of the given box
    234235    int minValue(int box) {
    235       int min = data[boxes[box].first].prio;
    236       for (int k = boxes[box].first; k != -1; k = data[k].next) {
    237         if (data[k].prio < min) min = data[k].prio;
     236      int min = _data[_boxes[box].first].prio;
     237      for (int k = _boxes[box].first; k != -1; k = _data[k].next) {
     238        if (_data[k].prio < min) min = _data[k].prio;
    238239      }
    239240      return min;
    240241    }
    241242
    242     /// \brief Rearrange the items of the heap and makes the
    243     /// first box not empty.
     243    // Rearrange the items of the heap and make the first box non-empty
    244244    void moveDown() {
    245245      int box = findFirst();
     
    247247      int min = minValue(box);
    248248      for (int i = 0; i <= box; ++i) {
    249         boxes[i].min = min;
    250         min += boxes[i].size;
    251       }
    252       int curr = boxes[box].first, next;
     249        _boxes[i].min = min;
     250        min += _boxes[i].size;
     251      }
     252      int curr = _boxes[box].first, next;
    253253      while (curr != -1) {
    254         next = data[curr].next;
    255         bubble_down(curr);
     254        next = _data[curr].next;
     255        bubbleDown(curr);
    256256        curr = next;
    257257      }
    258258    }
    259259
    260     void relocate_last(int index) {
    261       if (index != int(data.size()) - 1) {
    262         data[index] = data.back();
    263         if (data[index].prev != -1) {
    264           data[data[index].prev].next = index;
     260    void relocateLast(int index) {
     261      if (index != int(_data.size()) - 1) {
     262        _data[index] = _data.back();
     263        if (_data[index].prev != -1) {
     264          _data[_data[index].prev].next = index;
    265265        } else {
    266           boxes[data[index].box].first = index;
     266          _boxes[_data[index].box].first = index;
    267267        }
    268         if (data[index].next != -1) {
    269           data[data[index].next].prev = index;
     268        if (_data[index].next != -1) {
     269          _data[_data[index].next].prev = index;
    270270        }
    271         _iim[data[index].item] = index;
    272       }
    273       data.pop_back();
     271        _iim[_data[index].item] = index;
     272      }
     273      _data.pop_back();
    274274    }
    275275
     
    278278    /// \brief Insert an item into the heap with the given priority.
    279279    ///
    280     /// Adds \c i to the heap with priority \c p.
     280    /// This function inserts the given item into the heap with the
     281    /// given priority.
    281282    /// \param i The item to insert.
    282283    /// \param p The priority of the item.
     284    /// \pre \e i must not be stored in the heap.
     285    /// \warning This method may throw an \c UnderFlowPriorityException.
    283286    void push(const Item &i, const Prio &p) {
    284       int n = data.size();
     287      int n = _data.size();
    285288      _iim.set(i, n);
    286       data.push_back(RadixItem(i, p));
    287       while (lower(boxes.size() - 1, p)) {
     289      _data.push_back(RadixItem(i, p));
     290      while (lower(_boxes.size() - 1, p)) {
    288291        extend();
    289292      }
    290       int box = findDown(boxes.size() - 1, p);
     293      int box = findDown(_boxes.size() - 1, p);
    291294      insert(box, n);
    292295    }
    293296
    294     /// \brief Returns the item with minimum priority.
    295     ///
    296     /// This method returns the item with minimum priority.
    297     /// \pre The heap must be nonempty.
     297    /// \brief Return the item having minimum priority.
     298    ///
     299    /// This function returns the item having minimum priority.
     300    /// \pre The heap must be non-empty.
    298301    Item top() const {
    299302      const_cast<RadixHeap<ItemIntMap>&>(*this).moveDown();
    300       return data[boxes[0].first].item;
    301     }
    302 
    303     /// \brief Returns the minimum priority.
    304     ///
    305     /// It returns the minimum priority.
    306     /// \pre The heap must be nonempty.
     303      return _data[_boxes[0].first].item;
     304    }
     305
     306    /// \brief The minimum priority.
     307    ///
     308    /// This function returns the minimum priority.
     309    /// \pre The heap must be non-empty.
    307310    Prio prio() const {
    308311      const_cast<RadixHeap<ItemIntMap>&>(*this).moveDown();
    309       return data[boxes[0].first].prio;
     312      return _data[_boxes[0].first].prio;
    310313     }
    311314
    312     /// \brief Deletes the item with minimum priority.
    313     ///
    314     /// This method deletes the item with minimum priority.
     315    /// \brief Remove the item having minimum priority.
     316    ///
     317    /// This function removes the item having minimum priority.
    315318    /// \pre The heap must be non-empty.
    316319    void pop() {
    317320      moveDown();
    318       int index = boxes[0].first;
    319       _iim[data[index].item] = POST_HEAP;
     321      int index = _boxes[0].first;
     322      _iim[_data[index].item] = POST_HEAP;
    320323      remove(index);
    321       relocate_last(index);
    322     }
    323 
    324     /// \brief Deletes \c i from the heap.
    325     ///
    326     /// This method deletes item \c i from the heap, if \c i was
    327     /// already stored in the heap.
    328     /// \param i The item to erase.
     324      relocateLast(index);
     325    }
     326
     327    /// \brief Remove the given item from the heap.
     328    ///
     329    /// This function removes the given item from the heap if it is
     330    /// already stored.
     331    /// \param i The item to delete.
     332    /// \pre \e i must be in the heap.
    329333    void erase(const Item &i) {
    330334      int index = _iim[i];
    331335      _iim[i] = POST_HEAP;
    332336      remove(index);
    333       relocate_last(index);
     337      relocateLast(index);
    334338   }
    335339
    336     /// \brief Returns the priority of \c i.
    337     ///
    338     /// This function returns the priority of item \c i.
    339     /// \pre \c i must be in the heap.
    340     /// \param i The item.
     340    /// \brief The priority of the given item.
     341    ///
     342    /// This function returns the priority of the given item.
     343    /// \param i The item.
     344    /// \pre \e i must be in the heap.
    341345    Prio operator[](const Item &i) const {
    342346      int idx = _iim[i];
    343       return data[idx].prio;
    344     }
    345 
    346     /// \brief \c i gets to the heap with priority \c p independently
    347     /// if \c i was already there.
    348     ///
    349     /// This method calls \ref push(\c i, \c p) if \c i is not stored
    350     /// in the heap and sets the priority of \c i to \c p otherwise.
    351     /// It may throw an \e UnderFlowPriorityException.
     347      return _data[idx].prio;
     348    }
     349
     350    /// \brief Set the priority of an item or insert it, if it is
     351    /// not stored in the heap.
     352    ///
     353    /// This method sets the priority of the given item if it is
     354    /// already stored in the heap. Otherwise it inserts the given
     355    /// item into the heap with the given priority.
    352356    /// \param i The item.
    353357    /// \param p The priority.
     358    /// \pre \e i must be in the heap.
     359    /// \warning This method may throw an \c UnderFlowPriorityException.
    354360    void set(const Item &i, const Prio &p) {
    355361      int idx = _iim[i];
     
    357363        push(i, p);
    358364      }
    359       else if( p >= data[idx].prio ) {
    360         data[idx].prio = p;
    361         bubble_up(idx);
     365      else if( p >= _data[idx].prio ) {
     366        _data[idx].prio = p;
     367        bubbleUp(idx);
    362368      } else {
    363         data[idx].prio = p;
    364         bubble_down(idx);
    365       }
    366     }
    367 
    368 
    369     /// \brief Decreases the priority of \c i to \c p.
    370     ///
    371     /// This method decreases the priority of item \c i to \c p.
    372     /// \pre \c i must be stored in the heap with priority at least \c p, and
    373     /// \c should be greater or equal to the last removed item's priority.
     369        _data[idx].prio = p;
     370        bubbleDown(idx);
     371      }
     372    }
     373
     374    /// \brief Decrease the priority of an item to the given value.
     375    ///
     376    /// This function decreases the priority of an item to the given value.
    374377    /// \param i The item.
    375378    /// \param p The priority.
     379    /// \pre \e i must be stored in the heap with priority at least \e p.
     380    /// \warning This method may throw an \c UnderFlowPriorityException.
    376381    void decrease(const Item &i, const Prio &p) {
    377382      int idx = _iim[i];
    378       data[idx].prio = p;
    379       bubble_down(idx);
    380     }
    381 
    382     /// \brief Increases the priority of \c i to \c p.
    383     ///
    384     /// This method sets the priority of item \c i to \c p.
    385     /// \pre \c i must be stored in the heap with priority at most \c p
     383      _data[idx].prio = p;
     384      bubbleDown(idx);
     385    }
     386
     387    /// \brief Increase the priority of an item to the given value.
     388    ///
     389    /// This function increases the priority of an item to the given value.
    386390    /// \param i The item.
    387391    /// \param p The priority.
     392    /// \pre \e i must be stored in the heap with priority at most \e p.
    388393    void increase(const Item &i, const Prio &p) {
    389394      int idx = _iim[i];
    390       data[idx].prio = p;
    391       bubble_up(idx);
    392     }
    393 
    394     /// \brief Returns if \c item is in, has already been in, or has
    395     /// never been in the heap.
    396     ///
    397     /// This method returns PRE_HEAP if \c item has never been in the
    398     /// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP
    399     /// otherwise. In the latter case it is possible that \c item will
    400     /// get back to the heap again.
     395      _data[idx].prio = p;
     396      bubbleUp(idx);
     397    }
     398
     399    /// \brief Return the state of an item.
     400    ///
     401    /// This method returns \c PRE_HEAP if the given item has never
     402    /// been in the heap, \c IN_HEAP if it is in the heap at the moment,
     403    /// and \c POST_HEAP otherwise.
     404    /// In the latter case it is possible that the item will get back
     405    /// to the heap again.
    401406    /// \param i The item.
    402407    State state(const Item &i) const {
     
    406411    }
    407412
    408     /// \brief Sets the state of the \c item in the heap.
    409     ///
    410     /// Sets the state of the \c item in the heap. It can be used to
    411     /// manually clear the heap when it is important to achive the
    412     /// better time complexity.
     413    /// \brief Set the state of an item in the heap.
     414    ///
     415    /// This function sets the state of the given item in the heap.
     416    /// It can be used to manually clear the heap when it is important
     417    /// to achive better time complexity.
    413418    /// \param i The item.
    414419    /// \param st The state. It should not be \c IN_HEAP.
Note: See TracChangeset for help on using the changeset viewer.