vmap.h: Enables one to create maps with a virtual base class.
3 * This file is a part of LEMON, a generic C++ optimization library
5 * Copyright (C) 2003-2006
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
9 * Permission to use, modify and distribute this software is granted
10 * provided that this copyright notice appears in all copies. For
11 * precise terms see the accompanying LICENSE file.
13 * This software is provided "AS IS" with no warranty of any kind,
14 * express or implied, and with no claim as to its suitability for any
19 #include <lemon/bits/graph_extender.h>
20 #include <lemon/concept/ugraph.h>
21 #include <lemon/list_graph.h>
22 #include <lemon/smart_graph.h>
23 #include <lemon/full_graph.h>
24 #include <lemon/grid_graph.h>
26 #include <lemon/graph_utils.h>
28 #include "test_tools.h"
31 using namespace lemon;
32 using namespace lemon::concept;
34 void check_concepts() {
35 typedef UGraphExtender<ListGraphBase> ListUGraphBase;
37 typedef IterableUGraphExtender<
38 AlterableUGraphExtender<ListUGraphBase> > IterableListUGraph;
40 typedef MappableUGraphExtender<IterableListUGraph>
43 typedef ErasableUGraphExtender<
44 ClearableUGraphExtender<
45 ExtendableUGraphExtender<MappableListUGraph> > > Graph;
47 checkConcept<BaseIterableUGraphConcept, Graph>();
48 checkConcept<IterableUGraphConcept, Graph>();
49 checkConcept<MappableUGraphConcept, Graph>();
51 checkConcept<UGraph, Graph>();
52 checkConcept<ErasableUGraph, Graph>();
54 checkConcept<UGraph, ListUGraph>();
55 checkConcept<ErasableUGraph, ListUGraph>();
57 checkConcept<UGraph, SmartUGraph>();
58 checkConcept<ExtendableUGraph, SmartUGraph>();
60 checkConcept<UGraph, FullUGraph>();
62 checkConcept<UGraph, UGraph>();
64 checkConcept<UGraph, GridGraph>();
67 template <typename Graph>
68 void check_item_counts(Graph &g, int n, int e) {
70 for (typename Graph::NodeIt it(g); it != INVALID; ++it) {
74 check(nn == n, "Wrong node number.");
75 check(countNodes(g) == n, "Wrong node number.");
78 for (typename Graph::EdgeIt it(g); it != INVALID; ++it) {
82 check(ee == 2*e, "Wrong edge number.");
83 check(countEdges(g) == 2*e, "Wrong edge number.");
86 for (typename Graph::UEdgeIt it(g); it != INVALID; ++it) {
90 check(uee == e, "Wrong uedge number.");
91 check(countUEdges(g) == e, "Wrong uedge number.");
94 template <typename Graph>
95 void print_items(Graph &g) {
97 typedef typename Graph::NodeIt NodeIt;
98 typedef typename Graph::UEdgeIt UEdgeIt;
99 typedef typename Graph::EdgeIt EdgeIt;
101 std::cout << "Nodes" << std::endl;
103 for(NodeIt it(g); it!=INVALID; ++it, ++i) {
104 std::cout << " " << i << ": " << g.id(it) << std::endl;
107 std::cout << "UEdge" << std::endl;
109 for(UEdgeIt it(g); it!=INVALID; ++it, ++i) {
110 std::cout << " " << i << ": " << g.id(it)
111 << " (" << g.id(g.source(it)) << ", " << g.id(g.target(it))
115 std::cout << "Edge" << std::endl;
117 for(EdgeIt it(g); it!=INVALID; ++it, ++i) {
118 std::cout << " " << i << ": " << g.id(it)
119 << " (" << g.id(g.source(it)) << ", " << g.id(g.target(it))
125 template <typename Graph>
128 typedef typename Graph::Node Node;
129 typedef typename Graph::UEdge UEdge;
130 typedef typename Graph::Edge Edge;
131 typedef typename Graph::NodeIt NodeIt;
132 typedef typename Graph::UEdgeIt UEdgeIt;
133 typedef typename Graph::EdgeIt EdgeIt;
137 check_item_counts(g,0,0);
145 e1 = g.addEdge(n1, n2),
146 e2 = g.addEdge(n2, n3);
150 check_item_counts(g,3,2);
153 void checkGridGraph(const GridGraph& g, int w, int h) {
154 check(g.width() == w, "Wrong width");
155 check(g.height() == h, "Wrong height");
157 for (int i = 0; i < w; ++i) {
158 for (int j = 0; j < h; ++j) {
159 check(g.col(g(i, j)) == i, "Wrong col");
160 check(g.row(g(i, j)) == j, "Wrong row");
164 for (int i = 0; i < w; ++i) {
165 for (int j = 0; j < h - 1; ++j) {
166 check(g.source(g.down(g(i, j))) == g(i, j), "Wrong down");
167 check(g.target(g.down(g(i, j))) == g(i, j + 1), "Wrong down");
169 check(g.down(g(i, h - 1)) == INVALID, "Wrong down");
172 for (int i = 0; i < w; ++i) {
173 for (int j = 1; j < h; ++j) {
174 check(g.source(g.up(g(i, j))) == g(i, j), "Wrong up");
175 check(g.target(g.up(g(i, j))) == g(i, j - 1), "Wrong up");
177 check(g.up(g(i, 0)) == INVALID, "Wrong up");
180 for (int j = 0; j < h; ++j) {
181 for (int i = 0; i < w - 1; ++i) {
182 check(g.source(g.right(g(i, j))) == g(i, j), "Wrong right");
183 check(g.target(g.right(g(i, j))) == g(i + 1, j), "Wrong right");
185 check(g.right(g(w - 1, j)) == INVALID, "Wrong right");
188 for (int j = 0; j < h; ++j) {
189 for (int i = 1; i < w; ++i) {
190 check(g.source(g.left(g(i, j))) == g(i, j), "Wrong left");
191 check(g.target(g.left(g(i, j))) == g(i - 1, j), "Wrong left");
193 check(g.left(g(0, j)) == INVALID, "Wrong left");
200 check_graph<ListUGraph>();
201 check_graph<SmartUGraph>();
205 check_item_counts(g, 5, 10);
210 check_item_counts(g, 30, 49);
211 checkGridGraph(g, 5, 6);
214 std::cout << __FILE__ ": All tests passed.\n";