[Lemon-commits] Alpar Juttner: Merge
Lemon HG
hg at lemon.cs.elte.hu
Fri Nov 21 11:53:57 CET 2008
details: http://lemon.cs.elte.hu/hg/lemon/rev/fc6954b4fce4
changeset: 399:fc6954b4fce4
user: Alpar Juttner <alpar [at] cs.elte.hu>
date: Fri Nov 21 10:49:39 2008 +0000
description:
Merge
diffstat:
2 files changed, 982 insertions(+)
lemon/Makefile.am | 1
lemon/elevator.h | 981 +++++++++++++++++++++++++++++++++++++++++++++++++++++
diffs (truncated from 996 to 300 lines):
diff -r 80ec623f529f -r fc6954b4fce4 lemon/Makefile.am
--- a/lemon/Makefile.am Tue Nov 11 10:25:57 2008 +0000
+++ b/lemon/Makefile.am Fri Nov 21 10:49:39 2008 +0000
@@ -27,6 +27,7 @@
lemon/dfs.h \
lemon/dijkstra.h \
lemon/dim2.h \
+ lemon/elevator.h \
lemon/error.h \
lemon/full_graph.h \
lemon/graph_to_eps.h \
diff -r 80ec623f529f -r fc6954b4fce4 lemon/elevator.h
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/lemon/elevator.h Fri Nov 21 10:49:39 2008 +0000
@@ -0,0 +1,981 @@
+/* -*- mode: C++; indent-tabs-mode: nil; -*-
+ *
+ * This file is a part of LEMON, a generic C++ optimization library.
+ *
+ * Copyright (C) 2003-2008
+ * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
+ * (Egervary Research Group on Combinatorial Optimization, EGRES).
+ *
+ * Permission to use, modify and distribute this software is granted
+ * provided that this copyright notice appears in all copies. For
+ * precise terms see the accompanying LICENSE file.
+ *
+ * This software is provided "AS IS" with no warranty of any kind,
+ * express or implied, and with no claim as to its suitability for any
+ * purpose.
+ *
+ */
+
+#ifndef LEMON_ELEVATOR_H
+#define LEMON_ELEVATOR_H
+
+///\ingroup auxdat
+///\file
+///\brief Elevator class
+///
+///Elevator class implements an efficient data structure
+///for labeling items in push-relabel type algorithms.
+///
+
+#include <lemon/bits/traits.h>
+
+namespace lemon {
+
+ ///Class for handling "labels" in push-relabel type algorithms.
+
+ ///A class for handling "labels" in push-relabel type algorithms.
+ ///
+ ///\ingroup auxdat
+ ///Using this class you can assign "labels" (nonnegative integer numbers)
+ ///to the edges or nodes of a graph, manipulate and query them through
+ ///operations typically arising in "push-relabel" type algorithms.
+ ///
+ ///Each item is either \em active or not, and you can also choose a
+ ///highest level active item.
+ ///
+ ///\sa LinkedElevator
+ ///
+ ///\param Graph Type of the underlying graph.
+ ///\param Item Type of the items the data is assigned to (Graph::Node,
+ ///Graph::Arc, Graph::Edge).
+ template<class Graph, class Item>
+ class Elevator
+ {
+ public:
+
+ typedef Item Key;
+ typedef int Value;
+
+ private:
+
+ typedef Item *Vit;
+ typedef typename ItemSetTraits<Graph,Item>::template Map<Vit>::Type VitMap;
+ typedef typename ItemSetTraits<Graph,Item>::template Map<int>::Type IntMap;
+
+ const Graph &_g;
+ int _max_level;
+ int _item_num;
+ VitMap _where;
+ IntMap _level;
+ std::vector<Item> _items;
+ std::vector<Vit> _first;
+ std::vector<Vit> _last_active;
+
+ int _highest_active;
+
+ void copy(Item i, Vit p)
+ {
+ _where.set(*p=i,p);
+ }
+ void copy(Vit s, Vit p)
+ {
+ if(s!=p)
+ {
+ Item i=*s;
+ *p=i;
+ _where.set(i,p);
+ }
+ }
+ void swap(Vit i, Vit j)
+ {
+ Item ti=*i;
+ Vit ct = _where[ti];
+ _where.set(ti,_where[*i=*j]);
+ _where.set(*j,ct);
+ *j=ti;
+ }
+
+ public:
+
+ ///Constructor with given maximum level.
+
+ ///Constructor with given maximum level.
+ ///
+ ///\param graph The underlying graph.
+ ///\param max_level The maximum allowed level.
+ ///Set the range of the possible labels to <tt>[0..max_level]</tt>.
+ Elevator(const Graph &graph,int max_level) :
+ _g(graph),
+ _max_level(max_level),
+ _item_num(_max_level),
+ _where(graph),
+ _level(graph,0),
+ _items(_max_level),
+ _first(_max_level+2),
+ _last_active(_max_level+2),
+ _highest_active(-1) {}
+ ///Constructor.
+
+ ///Constructor.
+ ///
+ ///\param graph The underlying graph.
+ ///Set the range of the possible labels to <tt>[0..max_level]</tt>,
+ ///where \c max_level is equal to the number of labeled items in the graph.
+ Elevator(const Graph &graph) :
+ _g(graph),
+ _max_level(countItems<Graph, Item>(graph)),
+ _item_num(_max_level),
+ _where(graph),
+ _level(graph,0),
+ _items(_max_level),
+ _first(_max_level+2),
+ _last_active(_max_level+2),
+ _highest_active(-1)
+ {
+ }
+
+ ///Activate item \c i.
+
+ ///Activate item \c i.
+ ///\pre Item \c i shouldn't be active before.
+ void activate(Item i)
+ {
+ const int l=_level[i];
+ swap(_where[i],++_last_active[l]);
+ if(l>_highest_active) _highest_active=l;
+ }
+
+ ///Deactivate item \c i.
+
+ ///Deactivate item \c i.
+ ///\pre Item \c i must be active before.
+ void deactivate(Item i)
+ {
+ swap(_where[i],_last_active[_level[i]]--);
+ while(_highest_active>=0 &&
+ _last_active[_highest_active]<_first[_highest_active])
+ _highest_active--;
+ }
+
+ ///Query whether item \c i is active
+ bool active(Item i) const { return _where[i]<=_last_active[_level[i]]; }
+
+ ///Return the level of item \c i.
+ int operator[](Item i) const { return _level[i]; }
+
+ ///Return the number of items on level \c l.
+ int onLevel(int l) const
+ {
+ return _first[l+1]-_first[l];
+ }
+ ///Return true if level \c l is empty.
+ bool emptyLevel(int l) const
+ {
+ return _first[l+1]-_first[l]==0;
+ }
+ ///Return the number of items above level \c l.
+ int aboveLevel(int l) const
+ {
+ return _first[_max_level+1]-_first[l+1];
+ }
+ ///Return the number of active items on level \c l.
+ int activesOnLevel(int l) const
+ {
+ return _last_active[l]-_first[l]+1;
+ }
+ ///Return true if there is no active item on level \c l.
+ bool activeFree(int l) const
+ {
+ return _last_active[l]<_first[l];
+ }
+ ///Return the maximum allowed level.
+ int maxLevel() const
+ {
+ return _max_level;
+ }
+
+ ///\name Highest Active Item
+ ///Functions for working with the highest level
+ ///active item.
+
+ ///@{
+
+ ///Return a highest level active item.
+
+ ///Return a highest level active item or INVALID if there is no active
+ ///item.
+ Item highestActive() const
+ {
+ return _highest_active>=0?*_last_active[_highest_active]:INVALID;
+ }
+
+ ///Return the highest active level.
+
+ ///Return the level of the highest active item or -1 if there is no active
+ ///item.
+ int highestActiveLevel() const
+ {
+ return _highest_active;
+ }
+
+ ///Lift the highest active item by one.
+
+ ///Lift the item returned by highestActive() by one.
+ ///
+ void liftHighestActive()
+ {
+ Item it = *_last_active[_highest_active];
+ _level.set(it,_level[it]+1);
+ swap(_last_active[_highest_active]--,_last_active[_highest_active+1]);
+ --_first[++_highest_active];
+ }
+
+ ///Lift the highest active item to the given level.
+
+ ///Lift the item returned by highestActive() to level \c new_level.
+ ///
+ ///\warning \c new_level must be strictly higher
+ ///than the current level.
+ ///
+ void liftHighestActive(int new_level)
+ {
+ const Item li = *_last_active[_highest_active];
+
+ copy(--_first[_highest_active+1],_last_active[_highest_active]--);
+ for(int l=_highest_active+1;l<new_level;l++)
+ {
+ copy(--_first[l+1],_first[l]);
+ --_last_active[l];
+ }
+ copy(li,_first[new_level]);
+ _level.set(li,new_level);
+ _highest_active=new_level;
+ }
+
+ ///Lift the highest active item to the top level.
+
+ ///Lift the item returned by highestActive() to the top level and
+ ///deactivate it.
+ void liftHighestActiveToTop()
+ {
+ const Item li = *_last_active[_highest_active];
+
+ copy(--_first[_highest_active+1],_last_active[_highest_active]--);
+ for(int l=_highest_active+1;l<_max_level;l++)
+ {
+ copy(--_first[l+1],_first[l]);
+ --_last_active[l];
+ }
+ copy(li,_first[_max_level]);
+ --_last_active[_max_level];
+ _level.set(li,_max_level);
+
+ while(_highest_active>=0 &&
+ _last_active[_highest_active]<_first[_highest_active])
+ _highest_active--;
+ }
+
+ ///@}
+
+ ///\name Active Item on Certain Level
+ ///Functions for working with the active items.
+
+ ///@{
+
+ ///Return an active item on level \c l.
More information about the Lemon-commits
mailing list