File: BFSiterator.h

package info (click to toggle)
polymake 3.0r2-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 19,752 kB
  • ctags: 30,928
  • sloc: cpp: 151,785; perl: 32,510; ansic: 3,597; java: 2,654; python: 278; makefile: 181; xml: 103; sh: 79
file content (210 lines) | stat: -rw-r--r-- 6,113 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/* Copyright (c) 1997-2015
   Ewgenij Gawrilow, Michael Joswig (Technische Universitaet Berlin, Germany)
   http://www.polymake.org

   This program is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by the
   Free Software Foundation; either version 2, or (at your option) any
   later version: http://www.gnu.org/licenses/gpl.txt.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
--------------------------------------------------------------------------------
*/

#ifndef POLYMAKE_GRAPH_BFS_ITERATOR_H
#define POLYMAKE_GRAPH_BFS_ITERATOR_H

#include "polymake/GenericGraph.h"
#include "polymake/Bitset.h"
#include "polymake/list"
#include "polymake/vector"
#include <cassert>

namespace polymake { namespace graph {

template <typename Dist=int>
class NodeVisitor {
protected:
   std::vector<Dist> dist;
public:
   NodeVisitor() {}

   template <typename Graph>
   NodeVisitor(const GenericGraph<Graph>& G, int start_node)
      : dist(G.top().dim(), Dist(-1))
   {
      if (!dist.empty()) dist[start_node]=0;
   }

   template <typename Graph>
   void reset(const GenericGraph<Graph>&, int start_node)
   {
      fill(pm::entire(dist), Dist(-1));
      if (!dist.empty()) dist[start_node]=0;
   }

   bool seen(int n) const { return dist[n]>=0; }
   void add(int n, int n_from) { dist[n]=dist[n_from]+1; }

   static const bool check_edges=false;
   void check(int,int) {}

   const Dist& operator[] (int n) const { return dist[n]; }
};

template <bool _inversed=false>
class BoolNodeVisitor {
protected:
   Bitset visited;
   int n_nodes;
public:
   BoolNodeVisitor() {}

   template <typename Graph>
   BoolNodeVisitor(const GenericGraph<Graph>& G, int start_node)
      : visited(G.top().dim(), _inversed && !G.top().has_gaps()), n_nodes(G.nodes())
   {
      if (_inversed && G.top().has_gaps()) visited=nodes(G);
      if (G.top().dim()) add(start_node);
   }

   template <typename Graph>
   void reset(const GenericGraph<Graph>& G, int start_node)
   {
      if (_inversed) {
         if (G.top().has_gaps())
            visited=nodes(G);
         else
            visited=sequence(0,n_nodes);
         visited-=start_node;
      } else {
         visited.clear();
         visited+=start_node;
      }
   }

   bool seen(int n) const { return _inversed^visited.contains(n); }
   void add(int n, int=0) { if (_inversed) visited-=n; else visited+=n; }

   static const bool check_edges=false;
   void check(int,int) {}

   const Bitset& get_visited_nodes() const { return visited; }
};

template <typename> class Visitor;
template <typename> class Reversed;

template <typename Graph, typename Params=void>
class BFSiterator {
public:
   typedef typename pm::extract_type_param<Params, Visitor, BoolNodeVisitor<> >::type visitor_type;
   static const bool reverse_edges=pm::extract_bool_param<Params,Reversed>::value;
protected:
   const Graph *graph;
   std::list<int> queue;
   visitor_type visitor;
   int unvisited;

   template <typename EdgeList>
   void next_step(int n, const EdgeList& edge_list)
   {
      for (typename Entire<EdgeList>::const_iterator e=entire(edge_list); !e.at_end(); ++e) {
         const int nn= reverse_edges ? e.from_node() : e.to_node();
         if (visitor.seen(nn)) {
            if (visitor.check_edges) visitor.check(nn,n);
         } else {
            visitor.add(nn,n);
            queue.push_back(nn);
            --unvisited;
         }
      }
   }

public:
   typedef std::forward_iterator_tag iterator_category;
   typedef int value_type;
   typedef const int& reference;
   typedef const int* pointer;
   typedef ptrdiff_t difference_type;

   typedef BFSiterator iterator;
   typedef BFSiterator const_iterator;

   BFSiterator() : graph(0) {}

   BFSiterator(const Graph& graph_arg, int start_node)
      : graph(&graph_arg), visitor(graph_arg, start_node), unvisited(graph_arg.nodes()-1)
   {
      if (unvisited>=0) {
         if (POLYMAKE_DEBUG) {
            if (start_node<0 || start_node>=graph->dim())
               throw std::runtime_error("BFSiterator - start node out of range");
         }
         queue.push_back(start_node);
      }
   }

   reference operator* () const { return queue.front(); }
   pointer operator-> () const { return &queue.front(); }

   iterator& operator++ ()
   {
      const int n=queue.front();  queue.pop_front();
      if (visitor.check_edges || unvisited>0) {
         if (reverse_edges)
            next_step(n, graph->in_edges(n));
         else
            next_step(n, graph->out_edges(n));
      }
      return *this;
   }
   const iterator operator++ (int) { iterator copy(*this);  operator++();  return copy; }

   void skip_node() { queue.pop_front(); }

   int unvisited_nodes() const { return unvisited; }
   const visitor_type& node_visitor() const { return visitor; }
   reference last_unvisited() const { return queue.back(); }

   bool at_end() const { return queue.empty(); }

   bool operator== (const iterator& it) const
   {
      return at_end() ? it.at_end() : !it.at_end() && queue.front()==it.queue.front();
   }
   bool operator!= (const iterator& it) const { return !operator==(it); }

   void reset(int start_node)
   {
      const int dim=graph->dim();
      if (dim>0) {
         if (POLYMAKE_DEBUG) {
            if (start_node<0 || start_node>=dim)
               throw std::runtime_error("BFSiterator::reset - start node out of range");
         }
         queue.clear();
         visitor.reset(*graph,start_node);
         queue.push_back(start_node);
         unvisited=graph->nodes()-1;
      }
   }
};

} }

namespace pm {
   template <typename Graph, typename Params>
   struct check_iterator_feature< polymake::graph::BFSiterator<Graph,Params>, end_sensitive > : True {};
}

#endif // POLYMAKE_GRAPH_BFS_ITERATOR_H

// Local Variables:
// mode:C++
// c-basic-offset:3
// indent-tabs-mode:nil
// End: