lemon/linear_heap.h
changeset 2038 33db14058543
parent 2037 32e4bebee616
child 2039 dacc4ce9474d
     1.1 --- a/lemon/linear_heap.h	Tue Apr 04 17:43:23 2006 +0000
     1.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.3 @@ -1,520 +0,0 @@
     1.4 -/* -*- C++ -*-
     1.5 - *
     1.6 - * This file is a part of LEMON, a generic C++ optimization library
     1.7 - *
     1.8 - * Copyright (C) 2003-2006
     1.9 - * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
    1.10 - * (Egervary Research Group on Combinatorial Optimization, EGRES).
    1.11 - *
    1.12 - * Permission to use, modify and distribute this software is granted
    1.13 - * provided that this copyright notice appears in all copies. For
    1.14 - * precise terms see the accompanying LICENSE file.
    1.15 - *
    1.16 - * This software is provided "AS IS" with no warranty of any kind,
    1.17 - * express or implied, and with no claim as to its suitability for any
    1.18 - * purpose.
    1.19 - *
    1.20 - */
    1.21 -
    1.22 -#ifndef LEMON_LINEAR_HEAP_H
    1.23 -#define LEMON_LINEAR_HEAP_H
    1.24 -
    1.25 -///\ingroup auxdat
    1.26 -///\file
    1.27 -///\brief Binary Heap implementation.
    1.28 -
    1.29 -#include <vector>
    1.30 -#include <utility>
    1.31 -#include <functional>
    1.32 -
    1.33 -namespace lemon {
    1.34 -
    1.35 -  /// \ingroup auxdat
    1.36 -
    1.37 -  /// \brief A Linear Heap implementation.
    1.38 -  ///
    1.39 -  /// This class implements the \e linear \e heap data structure. A \e heap
    1.40 -  /// is a data structure for storing items with specified values called \e
    1.41 -  /// priorities in such a way that finding the item with minimum priority is
    1.42 -  /// efficient. The linear heap is very simple implementation, it can store
    1.43 -  /// only integer priorities and it stores for each priority in the [0..C]
    1.44 -  /// range a list of items. So it should be used only when the priorities
    1.45 -  /// are small. It is not intended to use as dijkstra heap.
    1.46 -  ///
    1.47 -  /// \param _Item Type of the items to be stored.  
    1.48 -  /// \param _ItemIntMap A read and writable Item int map, used internally
    1.49 -  /// to handle the cross references.
    1.50 -  /// \param minimize If the given parameter is true then the heap gives back
    1.51 -  /// the lowest priority. 
    1.52 -  template <typename _Item, typename _ItemIntMap, bool minimize = true >
    1.53 -  class LinearHeap {
    1.54 -
    1.55 -  public:
    1.56 -    typedef _Item Item;
    1.57 -    typedef int Prio;
    1.58 -    typedef std::pair<Item, Prio> Pair;
    1.59 -    typedef _ItemIntMap ItemIntMap;
    1.60 -
    1.61 -    /// \brief Type to represent the items states.
    1.62 -    ///
    1.63 -    /// Each Item element have a state associated to it. It may be "in heap",
    1.64 -    /// "pre heap" or "post heap". The latter two are indifferent from the
    1.65 -    /// heap's point of view, but may be useful to the user.
    1.66 -    ///
    1.67 -    /// The ItemIntMap \e should be initialized in such way that it maps
    1.68 -    /// PRE_HEAP (-1) to any element to be put in the heap...
    1.69 -    enum state_enum {
    1.70 -      IN_HEAP = 0,
    1.71 -      PRE_HEAP = -1,
    1.72 -      POST_HEAP = -2
    1.73 -    };
    1.74 -
    1.75 -  public:
    1.76 -    /// \brief The constructor.
    1.77 -    ///
    1.78 -    /// The constructor.
    1.79 -    /// \param _index should be given to the constructor, since it is used
    1.80 -    /// internally to handle the cross references. The value of the map
    1.81 -    /// should be PRE_HEAP (-1) for each element.
    1.82 -    explicit LinearHeap(ItemIntMap &_index) : index(_index), minimal(0) {}
    1.83 -    
    1.84 -    /// The number of items stored in the heap.
    1.85 -    ///
    1.86 -    /// \brief Returns the number of items stored in the heap.
    1.87 -    int size() const { return data.size(); }
    1.88 -    
    1.89 -    /// \brief Checks if the heap stores no items.
    1.90 -    ///
    1.91 -    /// Returns \c true if and only if the heap stores no items.
    1.92 -    bool empty() const { return data.empty(); }
    1.93 -
    1.94 -    /// \brief Make empty this heap.
    1.95 -    /// 
    1.96 -    /// Make empty this heap.
    1.97 -    void clear() { 
    1.98 -      for (int i = 0; i < (int)data.size(); ++i) {
    1.99 -	index[data[i].item] = -2;
   1.100 -      }
   1.101 -      data.clear(); first.clear(); minimal = 0;
   1.102 -    }
   1.103 -
   1.104 -  private:
   1.105 -
   1.106 -    void relocate_last(int idx) {
   1.107 -      if (idx + 1 < (int)data.size()) {
   1.108 -	data[idx] = data.back();
   1.109 -	if (data[idx].prev != -1) {
   1.110 -	  data[data[idx].prev].next = idx;
   1.111 -	} else {
   1.112 -	  first[data[idx].value] = idx;
   1.113 -	}
   1.114 -	if (data[idx].next != -1) {
   1.115 -	  data[data[idx].next].prev = idx;
   1.116 -	}
   1.117 -	index[data[idx].item] = idx;
   1.118 -      }
   1.119 -      data.pop_back();
   1.120 -    }
   1.121 -
   1.122 -    void unlace(int idx) {
   1.123 -      if (data[idx].prev != -1) {
   1.124 -	data[data[idx].prev].next = data[idx].next;
   1.125 -      } else {
   1.126 -	first[data[idx].value] = data[idx].next;
   1.127 -      }
   1.128 -      if (data[idx].next != -1) {
   1.129 -	data[data[idx].next].prev = data[idx].prev;
   1.130 -      }
   1.131 -    }
   1.132 -
   1.133 -    void lace(int idx) {
   1.134 -      if ((int)first.size() <= data[idx].value) {
   1.135 -	first.resize(data[idx].value + 1, -1);
   1.136 -      }
   1.137 -      data[idx].next = first[data[idx].value];
   1.138 -      if (data[idx].next != -1) {
   1.139 -	data[data[idx].next].prev = idx;
   1.140 -      }
   1.141 -      first[data[idx].value] = idx;
   1.142 -      data[idx].prev = -1;
   1.143 -    }
   1.144 -
   1.145 -  public:
   1.146 -    /// \brief Insert a pair of item and priority into the heap.
   1.147 -    ///
   1.148 -    /// Adds \c p.first to the heap with priority \c p.second.
   1.149 -    /// \param p The pair to insert.
   1.150 -    void push(const Pair& p) {
   1.151 -      push(p.first, p.second);
   1.152 -    }
   1.153 -
   1.154 -    /// \brief Insert an item into the heap with the given priority.
   1.155 -    ///    
   1.156 -    /// Adds \c i to the heap with priority \c p. 
   1.157 -    /// \param i The item to insert.
   1.158 -    /// \param p The priority of the item.
   1.159 -    void push(const Item &i, const Prio &p) { 
   1.160 -      int idx = data.size();
   1.161 -      index[i] = idx;
   1.162 -      data.push_back(LinearItem(i, p));
   1.163 -      lace(idx);
   1.164 -      if (p < minimal) {
   1.165 -	minimal = p;
   1.166 -      }
   1.167 -    }
   1.168 -
   1.169 -    /// \brief Returns the item with minimum priority.
   1.170 -    ///
   1.171 -    /// This method returns the item with minimum priority.
   1.172 -    /// \pre The heap must be nonempty.  
   1.173 -    Item top() const {
   1.174 -      while (first[minimal] == -1) {
   1.175 -	++minimal;
   1.176 -      }
   1.177 -      return data[first[minimal]].item;
   1.178 -    }
   1.179 -
   1.180 -    /// \brief Returns the minimum priority.
   1.181 -    ///
   1.182 -    /// It returns the minimum priority.
   1.183 -    /// \pre The heap must be nonempty.
   1.184 -    Prio prio() const {
   1.185 -      while (first[minimal] == -1) {
   1.186 -	++minimal;
   1.187 -      }
   1.188 -      return minimal;
   1.189 -    }
   1.190 -
   1.191 -    /// \brief Deletes the item with minimum priority.
   1.192 -    ///
   1.193 -    /// This method deletes the item with minimum priority from the heap.  
   1.194 -    /// \pre The heap must be non-empty.  
   1.195 -    void pop() {
   1.196 -      while (first[minimal] == -1) {
   1.197 -	++minimal;
   1.198 -      }
   1.199 -      int idx = first[minimal];
   1.200 -      index[data[idx].item] = -2;
   1.201 -      unlace(idx);
   1.202 -      relocate_last(idx);
   1.203 -    }
   1.204 -
   1.205 -    /// \brief Deletes \c i from the heap.
   1.206 -    ///
   1.207 -    /// This method deletes item \c i from the heap, if \c i was
   1.208 -    /// already stored in the heap.
   1.209 -    /// \param i The item to erase. 
   1.210 -    void erase(const Item &i) {
   1.211 -      int idx = index[i];
   1.212 -      index[data[idx].item] = -2;
   1.213 -      unlace(idx);
   1.214 -      relocate_last(idx);
   1.215 -    }
   1.216 -
   1.217 -    
   1.218 -    /// \brief Returns the priority of \c i.
   1.219 -    ///
   1.220 -    /// This function returns the priority of item \c i.  
   1.221 -    /// \pre \c i must be in the heap.
   1.222 -    /// \param i The item.
   1.223 -    Prio operator[](const Item &i) const {
   1.224 -      int idx = index[i];
   1.225 -      return data[idx].value;
   1.226 -    }
   1.227 -
   1.228 -    /// \brief \c i gets to the heap with priority \c p independently 
   1.229 -    /// if \c i was already there.
   1.230 -    ///
   1.231 -    /// This method calls \ref push(\c i, \c p) if \c i is not stored
   1.232 -    /// in the heap and sets the priority of \c i to \c p otherwise.
   1.233 -    /// \param i The item.
   1.234 -    /// \param p The priority.
   1.235 -    void set(const Item &i, const Prio &p) {
   1.236 -      int idx = index[i];
   1.237 -      if (idx < 0) {
   1.238 -	push(i,p);
   1.239 -      } else if (p > data[idx].value) {
   1.240 -	increase(i, p);
   1.241 -      } else {
   1.242 -	decrease(i, p);
   1.243 -      }
   1.244 -    }
   1.245 -
   1.246 -    /// \brief Decreases the priority of \c i to \c p.
   1.247 -
   1.248 -    /// This method decreases the priority of item \c i to \c p.
   1.249 -    /// \pre \c i must be stored in the heap with priority at least \c
   1.250 -    /// p relative to \c Compare.
   1.251 -    /// \param i The item.
   1.252 -    /// \param p The priority.
   1.253 -    void decrease(const Item &i, const Prio &p) {
   1.254 -      int idx = index[i];
   1.255 -      unlace(idx);
   1.256 -      data[idx].value = p;
   1.257 -      if (p < minimal) {
   1.258 -	minimal = p;
   1.259 -      }
   1.260 -      lace(idx);
   1.261 -    }
   1.262 -    
   1.263 -    /// \brief Increases the priority of \c i to \c p.
   1.264 -    ///
   1.265 -    /// This method sets the priority of item \c i to \c p. 
   1.266 -    /// \pre \c i must be stored in the heap with priority at most \c
   1.267 -    /// p relative to \c Compare.
   1.268 -    /// \param i The item.
   1.269 -    /// \param p The priority.
   1.270 -    void increase(const Item &i, const Prio &p) {
   1.271 -      int idx = index[i];
   1.272 -      unlace(idx);
   1.273 -      data[idx].value = p;
   1.274 -      lace(idx);
   1.275 -    }
   1.276 -
   1.277 -    /// \brief Returns if \c item is in, has already been in, or has 
   1.278 -    /// never been in the heap.
   1.279 -    ///
   1.280 -    /// This method returns PRE_HEAP if \c item has never been in the
   1.281 -    /// heap, IN_HEAP if it is in the heap at the moment, and POST_HEAP
   1.282 -    /// otherwise. In the latter case it is possible that \c item will
   1.283 -    /// get back to the heap again.
   1.284 -    /// \param i The item.
   1.285 -    state_enum state(const Item &i) const {
   1.286 -      int idx = index[i];
   1.287 -      if (idx >= 0) idx = 0;
   1.288 -      return state_enum(idx);
   1.289 -    }
   1.290 -
   1.291 -    /// \brief Sets the state of the \c item in the heap.
   1.292 -    ///
   1.293 -    /// Sets the state of the \c item in the heap. It can be used to
   1.294 -    /// manually clear the heap when it is important to achive the
   1.295 -    /// better time complexity.
   1.296 -    /// \param i The item.
   1.297 -    /// \param st The state. It should not be \c IN_HEAP. 
   1.298 -    void state(const Item& i, state_enum st) {
   1.299 -      switch (st) {
   1.300 -      case POST_HEAP:
   1.301 -      case PRE_HEAP:
   1.302 -        if (state(i) == IN_HEAP) {
   1.303 -          erase(i);
   1.304 -        }
   1.305 -        index[i] = st;
   1.306 -        break;
   1.307 -      case IN_HEAP:
   1.308 -        break;
   1.309 -      }
   1.310 -    }
   1.311 -
   1.312 -  private:
   1.313 -
   1.314 -    struct LinearItem {
   1.315 -      LinearItem(const Item& _item, int _value) 
   1.316 -	: item(_item), value(_value) {}
   1.317 -
   1.318 -      Item item;
   1.319 -      int value;
   1.320 -
   1.321 -      int prev, next;
   1.322 -    };
   1.323 -
   1.324 -    ItemIntMap& index;
   1.325 -    std::vector<int> first;
   1.326 -    std::vector<LinearItem> data;
   1.327 -    mutable int minimal;
   1.328 -
   1.329 -  }; // class LinearHeap
   1.330 -
   1.331 -
   1.332 -  template <typename _Item, typename _ItemIntMap>
   1.333 -  class LinearHeap<_Item, _ItemIntMap, false> {
   1.334 -
   1.335 -  public:
   1.336 -    typedef _Item Item;
   1.337 -    typedef int Prio;
   1.338 -    typedef std::pair<Item, Prio> Pair;
   1.339 -    typedef _ItemIntMap ItemIntMap;
   1.340 -
   1.341 -    enum state_enum {
   1.342 -      IN_HEAP = 0,
   1.343 -      PRE_HEAP = -1,
   1.344 -      POST_HEAP = -2
   1.345 -    };
   1.346 -
   1.347 -  public:
   1.348 -
   1.349 -    explicit LinearHeap(ItemIntMap &_index) : index(_index), maximal(-1) {}
   1.350 -
   1.351 -    int size() const { return data.size(); }
   1.352 -    bool empty() const { return data.empty(); }
   1.353 -
   1.354 -    void clear() { 
   1.355 -      for (int i = 0; i < (int)data.size(); ++i) {
   1.356 -	index[data[i].item] = -2;
   1.357 -      }
   1.358 -      data.clear(); first.clear(); maximal = -1; 
   1.359 -    }
   1.360 -
   1.361 -  private:
   1.362 -
   1.363 -    void relocate_last(int idx) {
   1.364 -      if (idx + 1 != (int)data.size()) {
   1.365 -	data[idx] = data.back();
   1.366 -	if (data[idx].prev != -1) {
   1.367 -	  data[data[idx].prev].next = idx;
   1.368 -	} else {
   1.369 -	  first[data[idx].value] = idx;
   1.370 -	}
   1.371 -	if (data[idx].next != -1) {
   1.372 -	  data[data[idx].next].prev = idx;
   1.373 -	}
   1.374 -	index[data[idx].item] = idx;
   1.375 -      }
   1.376 -      data.pop_back();
   1.377 -    }
   1.378 -
   1.379 -    void unlace(int idx) {
   1.380 -      if (data[idx].prev != -1) {
   1.381 -	data[data[idx].prev].next = data[idx].next;
   1.382 -      } else {
   1.383 -	first[data[idx].value] = data[idx].next;
   1.384 -      }
   1.385 -      if (data[idx].next != -1) {
   1.386 -	data[data[idx].next].prev = data[idx].prev;
   1.387 -      }
   1.388 -    }
   1.389 -
   1.390 -    void lace(int idx) {
   1.391 -      if ((int)first.size() <= data[idx].value) {
   1.392 -	first.resize(data[idx].value + 1, -1);
   1.393 -      }
   1.394 -      data[idx].next = first[data[idx].value];
   1.395 -      if (data[idx].next != -1) {
   1.396 -	data[data[idx].next].prev = idx;
   1.397 -      }
   1.398 -      first[data[idx].value] = idx;
   1.399 -      data[idx].prev = -1;
   1.400 -    }
   1.401 -
   1.402 -  public:
   1.403 -
   1.404 -    void push(const Pair& p) {
   1.405 -      push(p.first, p.second);
   1.406 -    }
   1.407 -
   1.408 -    void push(const Item &i, const Prio &p) { 
   1.409 -      int idx = data.size();
   1.410 -      index[i] = idx;
   1.411 -      data.push_back(LinearItem(i, p));
   1.412 -      lace(idx);
   1.413 -      if (data[idx].value > maximal) {
   1.414 -	maximal = data[idx].value;
   1.415 -      }
   1.416 -    }
   1.417 -
   1.418 -    Item top() const {
   1.419 -      while (first[maximal] == -1) {
   1.420 -	--maximal;
   1.421 -      }
   1.422 -      return data[first[maximal]].item;
   1.423 -    }
   1.424 -
   1.425 -    Prio prio() const {
   1.426 -      while (first[maximal] == -1) {
   1.427 -	--maximal;
   1.428 -      }
   1.429 -      return maximal;
   1.430 -    }
   1.431 -
   1.432 -    void pop() {
   1.433 -      while (first[maximal] == -1) {
   1.434 -	--maximal;
   1.435 -      }
   1.436 -      int idx = first[maximal];
   1.437 -      index[data[idx].item] = -2;
   1.438 -      unlace(idx);
   1.439 -      relocate_last(idx);
   1.440 -    }
   1.441 -
   1.442 -    void erase(const Item &i) {
   1.443 -      int idx = index[i];
   1.444 -      index[data[idx].item] = -2;
   1.445 -      unlace(idx);
   1.446 -      relocate_last(idx);
   1.447 -    }
   1.448 -
   1.449 -    Prio operator[](const Item &i) const {
   1.450 -      int idx = index[i];
   1.451 -      return data[idx].value;
   1.452 -    }
   1.453 -
   1.454 -    void set(const Item &i, const Prio &p) {
   1.455 -      int idx = index[i];
   1.456 -      if (idx < 0) {
   1.457 -	push(i,p);
   1.458 -      } else if (p > data[idx].value) {
   1.459 -	decrease(i, p);
   1.460 -      } else {
   1.461 -	increase(i, p);
   1.462 -      }
   1.463 -    }
   1.464 -
   1.465 -    void decrease(const Item &i, const Prio &p) {
   1.466 -      int idx = index[i];
   1.467 -      unlace(idx);
   1.468 -      data[idx].value = p;
   1.469 -      if (p > maximal) {
   1.470 -	maximal = p;
   1.471 -      }
   1.472 -      lace(idx);
   1.473 -    }
   1.474 -    
   1.475 -    void increase(const Item &i, const Prio &p) {
   1.476 -      int idx = index[i];
   1.477 -      unlace(idx);
   1.478 -      data[idx].value = p;
   1.479 -      lace(idx);
   1.480 -    }
   1.481 -
   1.482 -    state_enum state(const Item &i) const {
   1.483 -      int idx = index[i];
   1.484 -      if (idx >= 0) idx = 0;
   1.485 -      return state_enum(idx);
   1.486 -    }
   1.487 -
   1.488 -    void state(const Item& i, state_enum st) {
   1.489 -      switch (st) {
   1.490 -      case POST_HEAP:
   1.491 -      case PRE_HEAP:
   1.492 -        if (state(i) == IN_HEAP) {
   1.493 -          erase(i);
   1.494 -        }
   1.495 -        index[i] = st;
   1.496 -        break;
   1.497 -      case IN_HEAP:
   1.498 -        break;
   1.499 -      }
   1.500 -    }
   1.501 -
   1.502 -  private:
   1.503 -
   1.504 -    struct LinearItem {
   1.505 -      LinearItem(const Item& _item, int _value) 
   1.506 -	: item(_item), value(_value) {}
   1.507 -
   1.508 -      Item item;
   1.509 -      int value;
   1.510 -
   1.511 -      int prev, next;
   1.512 -    };
   1.513 -
   1.514 -    ItemIntMap& index;
   1.515 -    std::vector<int> first;
   1.516 -    std::vector<LinearItem> data;
   1.517 -    mutable int maximal;
   1.518 -
   1.519 -  }; // class LinearHeap
   1.520 -
   1.521 -}
   1.522 -  
   1.523 -#endif