deba@1724: /* -*- C++ -*- deba@1724: * lemon/linear_heap.h - Part of LEMON, a generic C++ optimization library deba@1724: * deba@1724: * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport deba@1724: * (Egervary Research Group on Combinatorial Optimization, EGRES). deba@1724: * deba@1724: * Permission to use, modify and distribute this software is granted deba@1724: * provided that this copyright notice appears in all copies. For deba@1724: * precise terms see the accompanying LICENSE file. deba@1724: * deba@1724: * This software is provided "AS IS" with no warranty of any kind, deba@1724: * express or implied, and with no claim as to its suitability for any deba@1724: * purpose. deba@1724: * deba@1724: */ deba@1724: deba@1724: #ifndef LEMON_LINEAR_HEAP_H deba@1724: #define LEMON_LINEAR_HEAP_H deba@1724: deba@1724: ///\ingroup auxdat deba@1724: ///\file deba@1724: ///\brief Binary Heap implementation. deba@1724: deba@1724: #include deba@1724: #include deba@1724: #include deba@1724: deba@1724: namespace lemon { deba@1724: deba@1724: /// \addtogroup auxdat deba@1724: /// @{ deba@1724: deba@1724: /// \brief A Linear Heap implementation. deba@1724: /// deba@1724: /// This class implements the \e linear \e heap data structure. A \e heap deba@1724: /// is a data structure for storing items with specified values called \e deba@1724: /// priorities in such a way that finding the item with minimum priority is deba@1724: /// efficient. The linear heap is very simple implementation, it can store deba@1724: /// only integer priorities and it stores for each priority in the [0..C] deba@1724: /// range a list of items. So it should be used only when the priorities deba@1724: /// are small. It is not intended to use as dijkstra heap. deba@1724: /// deba@1724: /// \param _Item Type of the items to be stored. deba@1724: /// \param _ItemIntMap A read and writable Item int map, used internally deba@1724: /// to handle the cross references. deba@1724: /// \param minimize If the given parameter is true then the heap gives back deba@1724: /// the lowest priority. deba@1724: template deba@1724: class LinearHeap { deba@1724: deba@1724: public: deba@1724: typedef _Item Item; deba@1724: typedef int Prio; deba@1724: typedef std::pair Pair; deba@1724: typedef _ItemIntMap ItemIntMap; deba@1724: deba@1724: /// \brief Type to represent the items states. deba@1724: /// deba@1724: /// Each Item element have a state associated to it. It may be "in heap", deba@1724: /// "pre heap" or "post heap". The latter two are indifferent from the deba@1724: /// heap's point of view, but may be useful to the user. deba@1724: /// deba@1724: /// The ItemIntMap \e should be initialized in such way that it maps deba@1724: /// PRE_HEAP (-1) to any element to be put in the heap... deba@1724: enum state_enum { deba@1724: IN_HEAP = 0, deba@1724: PRE_HEAP = -1, deba@1724: POST_HEAP = -2 deba@1724: }; deba@1724: deba@1724: public: deba@1724: /// \brief The constructor. deba@1724: /// deba@1724: /// The constructor. deba@1724: /// \param _index should be given to the constructor, since it is used deba@1724: /// internally to handle the cross references. The value of the map deba@1724: /// should be PRE_HEAP (-1) for each element. deba@1724: explicit LinearHeap(ItemIntMap &_index) : index(_index), minimal(0) {} deba@1724: deba@1724: /// The number of items stored in the heap. deba@1724: /// deba@1724: /// \brief Returns the number of items stored in the heap. deba@1724: int size() const { return data.size(); } deba@1724: deba@1724: /// \brief Checks if the heap stores no items. deba@1724: /// deba@1724: /// Returns \c true if and only if the heap stores no items. deba@1724: bool empty() const { return data.empty(); } deba@1724: deba@1724: /// \brief Make empty this heap. deba@1724: /// deba@1724: /// Make empty this heap. deba@1724: void clear() { deba@1724: for (int i = 0; i < (int)data.size(); ++i) { deba@1724: index[data[i].item] = -2; deba@1724: } deba@1724: data.clear(); first.clear(); minimal = 0; deba@1724: } deba@1724: deba@1724: private: deba@1724: deba@1724: void relocate_last(int idx) { deba@1724: if (idx + 1 < (int)data.size()) { deba@1724: data[idx] = data.back(); deba@1724: if (data[idx].prev != -1) { deba@1724: data[data[idx].prev].next = idx; deba@1724: } else { deba@1724: first[data[idx].value] = idx; deba@1724: } deba@1724: if (data[idx].next != -1) { deba@1724: data[data[idx].next].prev = idx; deba@1724: } deba@1724: index[data[idx].item] = idx; deba@1724: } deba@1724: data.pop_back(); deba@1724: } deba@1724: deba@1724: void unlace(int idx) { deba@1724: if (data[idx].prev != -1) { deba@1724: data[data[idx].prev].next = data[idx].next; deba@1724: } else { deba@1724: first[data[idx].value] = data[idx].next; deba@1724: } deba@1724: if (data[idx].next != -1) { deba@1724: data[data[idx].next].prev = data[idx].prev; deba@1724: } deba@1724: } deba@1724: deba@1724: void lace(int idx) { deba@1724: if ((int)first.size() <= data[idx].value) { deba@1724: first.resize(data[idx].value + 1, -1); deba@1724: } deba@1724: data[idx].next = first[data[idx].value]; deba@1724: if (data[idx].next != -1) { deba@1724: data[data[idx].next].prev = idx; deba@1724: } deba@1724: first[data[idx].value] = idx; deba@1724: data[idx].prev = -1; deba@1724: } deba@1724: deba@1724: public: deba@1724: /// \brief Insert a pair of item and priority into the heap. deba@1724: /// deba@1724: /// Adds \c p.first to the heap with priority \c p.second. deba@1724: /// \param p The pair to insert. deba@1724: void push(const Pair& p) { deba@1724: push(p.first, p.second); deba@1724: } deba@1724: deba@1724: /// \brief Insert an item into the heap with the given priority. deba@1724: /// deba@1724: /// Adds \c i to the heap with priority \c p. deba@1724: /// \param i The item to insert. deba@1724: /// \param p The priority of the item. deba@1724: void push(const Item &i, const Prio &p) { deba@1724: int idx = data.size(); deba@1724: index[i] = idx; deba@1724: data.push_back(LinearItem(i, p)); deba@1724: lace(idx); deba@1724: if (p < minimal) { deba@1724: minimal = p; deba@1724: } deba@1724: } deba@1724: deba@1724: /// \brief Returns the item with minimum priority relative to \c Compare. deba@1724: /// deba@1724: /// This method returns the item with minimum priority relative to \c deba@1724: /// Compare. deba@1724: /// \pre The heap must be nonempty. deba@1724: Item top() const { deba@1724: while (first[minimal] == -1) { deba@1724: ++minimal; deba@1724: } deba@1724: return data[first[minimal]].item; deba@1724: } deba@1724: deba@1724: /// \brief Returns the minimum priority relative to \c Compare. deba@1724: /// deba@1724: /// It returns the minimum priority relative to \c Compare. deba@1724: /// \pre The heap must be nonempty. deba@1724: Prio prio() const { deba@1724: while (first[minimal] == -1) { deba@1724: ++minimal; deba@1724: } deba@1724: return minimal; deba@1724: } deba@1724: deba@1724: /// \brief Deletes the item with minimum priority relative to \c Compare. deba@1724: /// deba@1724: /// This method deletes the item with minimum priority relative to \c deba@1724: /// Compare from the heap. deba@1724: /// \pre The heap must be non-empty. deba@1724: void pop() { deba@1724: while (first[minimal] == -1) { deba@1724: ++minimal; deba@1724: } deba@1724: int idx = first[minimal]; deba@1724: index[data[idx].item] = -2; deba@1724: unlace(idx); deba@1724: relocate_last(idx); deba@1724: } deba@1724: deba@1724: /// \brief Deletes \c i from the heap. deba@1724: /// deba@1724: /// This method deletes item \c i from the heap, if \c i was deba@1724: /// already stored in the heap. deba@1724: /// \param i The item to erase. deba@1724: void erase(const Item &i) { deba@1724: int idx = index[i]; deba@1724: index[data[idx].item] = -2; deba@1724: unlace(idx); deba@1724: relocate_last(idx); deba@1724: } deba@1724: deba@1724: deba@1724: /// \brief Returns the priority of \c i. deba@1724: /// deba@1724: /// This function returns the priority of item \c i. deba@1724: /// \pre \c i must be in the heap. deba@1724: /// \param i The item. deba@1724: Prio operator[](const Item &i) const { deba@1724: int idx = index[i]; deba@1724: return data[idx].value; deba@1724: } deba@1724: deba@1724: /// \brief \c i gets to the heap with priority \c p independently deba@1724: /// if \c i was already there. deba@1724: /// deba@1724: /// This method calls \ref push(\c i, \c p) if \c i is not stored deba@1724: /// in the heap and sets the priority of \c i to \c p otherwise. deba@1724: /// \param i The item. deba@1724: /// \param p The priority. deba@1724: void set(const Item &i, const Prio &p) { deba@1724: int idx = index[i]; deba@1724: if (idx < 0) { deba@1724: push(i,p); deba@1724: } else if (p > data[idx].value) { deba@1724: increase(i, p); deba@1724: } else { deba@1724: decrease(i, p); deba@1724: } deba@1724: } deba@1724: deba@1724: /// \brief Decreases the priority of \c i to \c p. deba@1724: deba@1724: /// This method decreases the priority of item \c i to \c p. deba@1724: /// \pre \c i must be stored in the heap with priority at least \c deba@1724: /// p relative to \c Compare. deba@1724: /// \param i The item. deba@1724: /// \param p The priority. deba@1724: void decrease(const Item &i, const Prio &p) { deba@1724: int idx = index[i]; deba@1724: unlace(idx); deba@1724: data[idx].value = p; deba@1724: if (p < minimal) { deba@1724: minimal = p; deba@1724: } deba@1724: lace(idx); deba@1724: } deba@1724: deba@1724: /// \brief Increases the priority of \c i to \c p. deba@1724: /// deba@1724: /// This method sets the priority of item \c i to \c p. deba@1724: /// \pre \c i must be stored in the heap with priority at most \c deba@1724: /// p relative to \c Compare. deba@1724: /// \param i The item. deba@1724: /// \param p The priority. deba@1724: void increase(const Item &i, const Prio &p) { deba@1724: int idx = index[i]; deba@1724: unlace(idx); deba@1724: data[idx].value = p; deba@1724: lace(idx); deba@1724: } deba@1724: deba@1724: /// \brief Returns if \c item is in, has already been in, or has deba@1724: /// never been in the heap. deba@1724: /// deba@1724: /// This method returns PRE_HEAP if \c item has never been in the deba@1724: /// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP deba@1724: /// otherwise. In the latter case it is possible that \c item will deba@1724: /// get back to the heap again. deba@1724: /// \param i The item. deba@1724: state_enum state(const Item &i) const { deba@1724: int idx = index[i]; deba@1724: if (idx >= 0) idx = 0; deba@1724: return state_enum(idx); deba@1724: } deba@1724: deba@1724: private: deba@1724: deba@1724: struct LinearItem { deba@1724: LinearItem(const Item& _item, int _value) deba@1724: : item(_item), value(_value) {} deba@1724: deba@1724: Item item; deba@1724: int value; deba@1724: deba@1724: int prev, next; deba@1724: }; deba@1724: deba@1724: ItemIntMap& index; deba@1724: std::vector first; deba@1724: std::vector data; deba@1724: mutable int minimal; deba@1724: deba@1724: }; // class LinearHeap deba@1724: deba@1724: deba@1724: template deba@1724: class LinearHeap<_Item, _ItemIntMap, false> { deba@1724: deba@1724: public: deba@1724: typedef _Item Item; deba@1724: typedef int Prio; deba@1724: typedef std::pair Pair; deba@1724: typedef _ItemIntMap ItemIntMap; deba@1724: deba@1724: enum state_enum { deba@1724: IN_HEAP = 0, deba@1724: PRE_HEAP = -1, deba@1724: POST_HEAP = -2 deba@1724: }; deba@1724: deba@1724: public: deba@1724: deba@1724: explicit LinearHeap(ItemIntMap &_index) : index(_index), maximal(-1) {} deba@1724: deba@1724: int size() const { return data.size(); } deba@1724: bool empty() const { return data.empty(); } deba@1724: deba@1724: void clear() { deba@1724: for (int i = 0; i < (int)data.size(); ++i) { deba@1724: index[data[i].item] = -2; deba@1724: } deba@1724: data.clear(); first.clear(); maximal = -1; deba@1724: } deba@1724: deba@1724: private: deba@1724: deba@1724: void relocate_last(int idx) { deba@1724: if (idx + 1 != (int)data.size()) { deba@1724: data[idx] = data.back(); deba@1724: if (data[idx].prev != -1) { deba@1724: data[data[idx].prev].next = idx; deba@1724: } else { deba@1724: first[data[idx].value] = idx; deba@1724: } deba@1724: if (data[idx].next != -1) { deba@1724: data[data[idx].next].prev = idx; deba@1724: } deba@1724: index[data[idx].item] = idx; deba@1724: } deba@1724: data.pop_back(); deba@1724: } deba@1724: deba@1724: void unlace(int idx) { deba@1724: if (data[idx].prev != -1) { deba@1724: data[data[idx].prev].next = data[idx].next; deba@1724: } else { deba@1724: first[data[idx].value] = data[idx].next; deba@1724: } deba@1724: if (data[idx].next != -1) { deba@1724: data[data[idx].next].prev = data[idx].prev; deba@1724: } deba@1724: } deba@1724: deba@1724: void lace(int idx) { deba@1724: if ((int)first.size() <= data[idx].value) { deba@1724: first.resize(data[idx].value + 1, -1); deba@1724: } deba@1724: data[idx].next = first[data[idx].value]; deba@1724: if (data[idx].next != -1) { deba@1724: data[data[idx].next].prev = idx; deba@1724: } deba@1724: first[data[idx].value] = idx; deba@1724: data[idx].prev = -1; deba@1724: } deba@1724: deba@1724: public: deba@1724: deba@1724: void push(const Pair& p) { deba@1724: push(p.first, p.second); deba@1724: } deba@1724: deba@1724: void push(const Item &i, const Prio &p) { deba@1724: int idx = data.size(); deba@1724: index[i] = idx; deba@1724: data.push_back(LinearItem(i, p)); deba@1724: lace(idx); deba@1724: if (data[idx].value > maximal) { deba@1724: maximal = data[idx].value; deba@1724: } deba@1724: } deba@1724: deba@1724: Item top() const { deba@1724: while (first[maximal] == -1) { deba@1724: --maximal; deba@1724: } deba@1724: return data[first[maximal]].item; deba@1724: } deba@1724: deba@1724: Prio prio() const { deba@1724: while (first[maximal] == -1) { deba@1724: --maximal; deba@1724: } deba@1724: return maximal; deba@1724: } deba@1724: deba@1724: void pop() { deba@1724: while (first[maximal] == -1) { deba@1724: --maximal; deba@1724: } deba@1724: int idx = first[maximal]; deba@1724: index[data[idx].item] = -2; deba@1724: unlace(idx); deba@1724: relocate_last(idx); deba@1724: } deba@1724: deba@1724: void erase(const Item &i) { deba@1724: int idx = index[i]; deba@1724: index[data[idx].item] = -2; deba@1724: unlace(idx); deba@1724: relocate_last(idx); deba@1724: } deba@1724: deba@1724: Prio operator[](const Item &i) const { deba@1724: int idx = index[i]; deba@1724: return data[idx].value; deba@1724: } deba@1724: deba@1724: void set(const Item &i, const Prio &p) { deba@1724: int idx = index[i]; deba@1724: if (idx < 0) { deba@1724: push(i,p); deba@1724: } else if (p > data[idx].value) { deba@1724: decrease(i, p); deba@1724: } else { deba@1724: increase(i, p); deba@1724: } deba@1724: } deba@1724: deba@1724: void decrease(const Item &i, const Prio &p) { deba@1724: int idx = index[i]; deba@1724: unlace(idx); deba@1724: data[idx].value = p; deba@1724: if (p > maximal) { deba@1724: maximal = p; deba@1724: } deba@1724: lace(idx); deba@1724: } deba@1724: deba@1724: void increase(const Item &i, const Prio &p) { deba@1724: int idx = index[i]; deba@1724: unlace(idx); deba@1724: data[idx].value = p; deba@1724: lace(idx); deba@1724: } deba@1724: deba@1724: state_enum state(const Item &i) const { deba@1724: int idx = index[i]; deba@1724: if (idx >= 0) idx = 0; deba@1724: return state_enum(idx); deba@1724: } deba@1724: deba@1724: private: deba@1724: deba@1724: struct LinearItem { deba@1724: LinearItem(const Item& _item, int _value) deba@1724: : item(_item), value(_value) {} deba@1724: deba@1724: Item item; deba@1724: int value; deba@1724: deba@1724: int prev, next; deba@1724: }; deba@1724: deba@1724: ItemIntMap& index; deba@1724: std::vector first; deba@1724: std::vector data; deba@1724: mutable int maximal; deba@1724: deba@1724: }; // class LinearHeap deba@1724: deba@1724: } deba@1724: deba@1724: #endif