[Lemon-commits] Alpar Juttner: Merge
Lemon HG
hg at lemon.cs.elte.hu
Thu Nov 6 15:49:43 CET 2008
details: http://lemon.cs.elte.hu/hg/lemon/rev/efbd0ab50a77
changeset: 378:efbd0ab50a77
user: Alpar Juttner <alpar [at] cs.elte.hu>
date: Thu Nov 06 14:40:32 2008 +0000
description:
Merge
diffstat:
5 files changed, 504 insertions(+), 10 deletions(-)
lemon/Makefile.am | 1
lemon/hypercube_graph.h | 439 +++++++++++++++++++++++++++++++++++++++++++++
test/digraph_test.cc | 5
test/graph_test.cc | 68 ++++++
tools/lemon-0.x-to-1.x.sh | 1
diffs (truncated from 614 to 300 lines):
diff -r a637fb9d457b -r efbd0ab50a77 lemon/Makefile.am
--- a/lemon/Makefile.am Wed Nov 05 14:44:37 2008 +0000
+++ b/lemon/Makefile.am Thu Nov 06 14:40:32 2008 +0000
@@ -31,6 +31,7 @@
lemon/full_graph.h \
lemon/graph_to_eps.h \
lemon/grid_graph.h \
+ lemon/hypercube_graph.h \
lemon/kruskal.h \
lemon/lgf_reader.h \
lemon/lgf_writer.h \
diff -r a637fb9d457b -r efbd0ab50a77 lemon/hypercube_graph.h
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/lemon/hypercube_graph.h Thu Nov 06 14:40:32 2008 +0000
@@ -0,0 +1,439 @@
+/* -*- 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 HYPERCUBE_GRAPH_H
+#define HYPERCUBE_GRAPH_H
+
+#include <vector>
+#include <lemon/core.h>
+#include <lemon/assert.h>
+#include <lemon/bits/graph_extender.h>
+
+///\ingroup graphs
+///\file
+///\brief HypercubeGraph class.
+
+namespace lemon {
+
+ class HypercubeGraphBase {
+
+ public:
+
+ typedef HypercubeGraphBase Graph;
+
+ class Node;
+ class Edge;
+ class Arc;
+
+ public:
+
+ HypercubeGraphBase() {}
+
+ protected:
+
+ void construct(int dim) {
+ LEMON_ASSERT(dim >= 1, "The number of dimensions must be at least 1.");
+ _dim = dim;
+ _node_num = 1 << dim;
+ _edge_num = dim * (1 << dim-1);
+ }
+
+ public:
+
+ typedef True NodeNumTag;
+ typedef True EdgeNumTag;
+ typedef True ArcNumTag;
+
+ int nodeNum() const { return _node_num; }
+ int edgeNum() const { return _edge_num; }
+ int arcNum() const { return 2 * _edge_num; }
+
+ int maxNodeId() const { return _node_num - 1; }
+ int maxEdgeId() const { return _edge_num - 1; }
+ int maxArcId() const { return 2 * _edge_num - 1; }
+
+ static Node nodeFromId(int id) { return Node(id); }
+ static Edge edgeFromId(int id) { return Edge(id); }
+ static Arc arcFromId(int id) { return Arc(id); }
+
+ static int id(Node node) { return node._id; }
+ static int id(Edge edge) { return edge._id; }
+ static int id(Arc arc) { return arc._id; }
+
+ Node u(Edge edge) const {
+ int base = edge._id & ((1 << _dim-1) - 1);
+ int k = edge._id >> _dim-1;
+ return ((base >> k) << k+1) | (base & ((1 << k) - 1));
+ }
+
+ Node v(Edge edge) const {
+ int base = edge._id & ((1 << _dim-1) - 1);
+ int k = edge._id >> _dim-1;
+ return ((base >> k) << k+1) | (base & ((1 << k) - 1)) | (1 << k);
+ }
+
+ Node source(Arc arc) const {
+ return (arc._id & 1) == 1 ? u(arc) : v(arc);
+ }
+
+ Node target(Arc arc) const {
+ return (arc._id & 1) == 1 ? v(arc) : u(arc);
+ }
+
+ typedef True FindEdgeTag;
+ typedef True FindArcTag;
+
+ Edge findEdge(Node u, Node v, Edge prev = INVALID) const {
+ if (prev != INVALID) return INVALID;
+ int d = u._id ^ v._id;
+ int k = 0;
+ if (d == 0) return INVALID;
+ for ( ; (d & 1) == 0; d >>= 1) ++k;
+ if (d >> 1 != 0) return INVALID;
+ return (k << _dim-1) | ((u._id >> k+1) << k) | (u._id & ((1 << k) - 1));
+ }
+
+ Arc findArc(Node u, Node v, Arc prev = INVALID) const {
+ Edge edge = findEdge(u, v, prev);
+ if (edge == INVALID) return INVALID;
+ int k = edge._id >> _dim-1;
+ return ((u._id >> k) & 1) == 1 ? edge._id << 1 : (edge._id << 1) | 1;
+ }
+
+ class Node {
+ friend class HypercubeGraphBase;
+
+ protected:
+ int _id;
+ Node(int id) : _id(id) {}
+ public:
+ Node() {}
+ Node (Invalid) : _id(-1) {}
+ bool operator==(const Node node) const {return _id == node._id;}
+ bool operator!=(const Node node) const {return _id != node._id;}
+ bool operator<(const Node node) const {return _id < node._id;}
+ };
+
+ class Edge {
+ friend class HypercubeGraphBase;
+ friend class Arc;
+
+ protected:
+ int _id;
+
+ Edge(int id) : _id(id) {}
+
+ public:
+ Edge() {}
+ Edge (Invalid) : _id(-1) {}
+ bool operator==(const Edge edge) const {return _id == edge._id;}
+ bool operator!=(const Edge edge) const {return _id != edge._id;}
+ bool operator<(const Edge edge) const {return _id < edge._id;}
+ };
+
+ class Arc {
+ friend class HypercubeGraphBase;
+
+ protected:
+ int _id;
+
+ Arc(int id) : _id(id) {}
+
+ public:
+ Arc() {}
+ Arc (Invalid) : _id(-1) {}
+ operator Edge() const { return _id != -1 ? Edge(_id >> 1) : INVALID; }
+ bool operator==(const Arc arc) const {return _id == arc._id;}
+ bool operator!=(const Arc arc) const {return _id != arc._id;}
+ bool operator<(const Arc arc) const {return _id < arc._id;}
+ };
+
+ void first(Node& node) const {
+ node._id = _node_num - 1;
+ }
+
+ static void next(Node& node) {
+ --node._id;
+ }
+
+ void first(Edge& edge) const {
+ edge._id = _edge_num - 1;
+ }
+
+ static void next(Edge& edge) {
+ --edge._id;
+ }
+
+ void first(Arc& arc) const {
+ arc._id = 2 * _edge_num - 1;
+ }
+
+ static void next(Arc& arc) {
+ --arc._id;
+ }
+
+ void firstInc(Edge& edge, bool& dir, const Node& node) const {
+ edge._id = node._id >> 1;
+ dir = (node._id & 1) == 0;
+ }
+
+ void nextInc(Edge& edge, bool& dir) const {
+ Node n = dir ? u(edge) : v(edge);
+ int k = (edge._id >> _dim-1) + 1;
+ if (k < _dim) {
+ edge._id = (k << _dim-1) |
+ ((n._id >> k+1) << k) | (n._id & ((1 << k) - 1));
+ dir = ((n._id >> k) & 1) == 0;
+ } else {
+ edge._id = -1;
+ dir = true;
+ }
+ }
+
+ void firstOut(Arc& arc, const Node& node) const {
+ arc._id = ((node._id >> 1) << 1) | (~node._id & 1);
+ }
+
+ void nextOut(Arc& arc) const {
+ Node n = (arc._id & 1) == 1 ? u(arc) : v(arc);
+ int k = (arc._id >> _dim) + 1;
+ if (k < _dim) {
+ arc._id = (k << _dim-1) |
+ ((n._id >> k+1) << k) | (n._id & ((1 << k) - 1));
+ arc._id = (arc._id << 1) | (~(n._id >> k) & 1);
+ } else {
+ arc._id = -1;
+ }
+ }
+
+ void firstIn(Arc& arc, const Node& node) const {
+ arc._id = ((node._id >> 1) << 1) | (node._id & 1);
+ }
+
+ void nextIn(Arc& arc) const {
+ Node n = (arc._id & 1) == 1 ? v(arc) : u(arc);
+ int k = (arc._id >> _dim) + 1;
+ if (k < _dim) {
+ arc._id = (k << _dim-1) |
+ ((n._id >> k+1) << k) | (n._id & ((1 << k) - 1));
+ arc._id = (arc._id << 1) | ((n._id >> k) & 1);
+ } else {
+ arc._id = -1;
+ }
+ }
+
+ static bool direction(Arc arc) {
+ return (arc._id & 1) == 1;
+ }
+
+ static Arc direct(Edge edge, bool dir) {
+ return Arc((edge._id << 1) | (dir ? 1 : 0));
+ }
+
+ int dimension() const {
+ return _dim;
+ }
+
+ bool projection(Node node, int n) const {
+ return static_cast<bool>(node._id & (1 << n));
+ }
+
+ int dimension(Edge edge) const {
+ return edge._id >> _dim-1;
+ }
+
+ int dimension(Arc arc) const {
+ return arc._id >> _dim;
+ }
+
+ int index(Node node) const {
+ return node._id;
+ }
+
+ Node operator()(int ix) const {
+ return Node(ix);
+ }
+
+ private:
+ int _dim;
+ int _node_num, _edge_num;
+ };
+
+
+ typedef GraphExtender<HypercubeGraphBase> ExtendedHypercubeGraphBase;
+
+ /// \ingroup graphs
+ ///
+ /// \brief Hypercube graph class
+ ///
+ /// This class implements a special graph type. The nodes of the graph
+ /// are indiced with integers with at most \c dim binary digits.
More information about the Lemon-commits
mailing list