spot  2.8.4
graph.hh
1 // -*- coding: utf-8 -*-
2 // Copyright (C) 2014-2018 Laboratoire de Recherche et
3 // Développement de l'Epita.
4 //
5 // This file is part of Spot, a model checking library.
6 //
7 // Spot is free software; you can redistribute it and/or modify it
8 // under the terms of the GNU General Public License as published by
9 // the Free Software Foundation; either version 3 of the License, or
10 // (at your option) any later version.
11 //
12 // Spot is distributed in the hope that it will be useful, but WITHOUT
13 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
15 // License for more details.
16 //
17 // You should have received a copy of the GNU General Public License
18 // along with this program. If not, see <http://www.gnu.org/licenses/>.
19 
20 #pragma once
21 
22 #include <spot/misc/common.hh>
23 #include <vector>
24 #include <type_traits>
25 #include <tuple>
26 #include <cassert>
27 #include <iterator>
28 #include <algorithm>
29 #include <map>
30 #include <iostream>
31 
32 namespace spot
33 {
34  template <typename State_Data, typename Edge_Data>
35  class SPOT_API digraph;
36 
37  namespace internal
38  {
39 #ifndef SWIG
40  template <typename Of, typename ...Args>
42  {
43  static const bool value = false;
44  };
45 
46  template <typename Of, typename Arg1, typename ...Args>
47  struct first_is_base_of<Of, Arg1, Args...>
48  {
49  static const bool value =
50  std::is_base_of<Of, typename std::decay<Arg1>::type>::value;
51  };
52 #endif
53 
54  // The boxed_label class stores Data as an attribute called
55  // "label" if boxed is true. It is an empty class if Data is
56  // void, and it simply inherits from Data if boxed is false.
57  //
58  // The data() method offers an homogeneous access to the Data
59  // instance.
60  template <typename Data, bool boxed = !std::is_class<Data>::value>
61  struct SPOT_API boxed_label
62  {
63  typedef Data data_t;
64  Data label;
65 
66 #ifndef SWIG
67  template <typename... Args,
68  typename = typename std::enable_if<
69  !first_is_base_of<boxed_label, Args...>::value>::type>
70  boxed_label(Args&&... args)
71  noexcept(std::is_nothrow_constructible<Data, Args...>::value)
72  : label{std::forward<Args>(args)...}
73  {
74  }
75 #endif
76 
77  // if Data is a POD type, G++ 4.8.2 wants default values for all
78  // label fields unless we define this default constructor here.
79  explicit boxed_label()
80  noexcept(std::is_nothrow_constructible<Data>::value)
81  {
82  }
83 
84  Data& data()
85  {
86  return label;
87  }
88 
89  const Data& data() const
90  {
91  return label;
92  }
93 
94  bool operator<(const boxed_label& other) const
95  {
96  return label < other.label;
97  }
98  };
99 
100  template <>
101  struct SPOT_API boxed_label<void, true>: public std::tuple<>
102  {
103  typedef std::tuple<> data_t;
104  std::tuple<>& data()
105  {
106  return *this;
107  }
108 
109  const std::tuple<>& data() const
110  {
111  return *this;
112  }
113 
114  };
115 
116  template <typename Data>
117  struct SPOT_API boxed_label<Data, false>: public Data
118  {
119  typedef Data data_t;
120 
121 #ifndef SWIG
122  template <typename... Args,
123  typename = typename std::enable_if<
124  !first_is_base_of<boxed_label, Args...>::value>::type>
125  boxed_label(Args&&... args)
126  noexcept(std::is_nothrow_constructible<Data, Args...>::value)
127  : Data{std::forward<Args>(args)...}
128  {
129  }
130 #endif
131 
132  // if Data is a POD type, G++ 4.8.2 wants default values for all
133  // label fields unless we define this default constructor here.
134  explicit boxed_label()
135  noexcept(std::is_nothrow_constructible<Data>::value)
136  {
137  }
138 
139  Data& data()
140  {
141  return *this;
142  }
143 
144  const Data& data() const
145  {
146  return *this;
147  }
148  };
149 
151  // State storage for digraphs
153 
154  // We have two implementations, one with attached State_Data, and
155  // one without.
156 
157  template <typename Edge, typename State_Data>
158  struct SPOT_API distate_storage final: public State_Data
159  {
160  Edge succ = 0; // First outgoing edge (used when iterating)
161  Edge succ_tail = 0; // Last outgoing edge (used for
162  // appending new edges)
163 #ifndef SWIG
164  template <typename... Args,
165  typename = typename std::enable_if<
166  !first_is_base_of<distate_storage, Args...>::value>::type>
167  distate_storage(Args&&... args)
168  noexcept(std::is_nothrow_constructible<State_Data, Args...>::value)
169  : State_Data{std::forward<Args>(args)...}
170  {
171  }
172 #endif
173  };
174 
176  // Edge storage
178 
179  // Again two implementation: one with label, and one without.
180 
181  template <typename StateIn,
182  typename StateOut, typename Edge, typename Edge_Data>
183  struct SPOT_API edge_storage final: public Edge_Data
184  {
185  typedef Edge edge;
186 
187  StateOut dst; // destination
188  Edge next_succ; // next outgoing edge with same
189  // source, or 0
190  StateIn src; // source
191 
192  explicit edge_storage()
193  noexcept(std::is_nothrow_constructible<Edge_Data>::value)
194  : Edge_Data{}
195  {
196  }
197 
198 #ifndef SWIG
199  template <typename... Args>
200  edge_storage(StateOut dst, Edge next_succ,
201  StateIn src, Args&&... args)
202  noexcept(std::is_nothrow_constructible<Edge_Data, Args...>::value
203  && std::is_nothrow_constructible<StateOut, StateOut>::value
204  && std::is_nothrow_constructible<Edge, Edge>::value)
205  : Edge_Data{std::forward<Args>(args)...},
206  dst(dst), next_succ(next_succ), src(src)
207  {
208  }
209 #endif
210 
211  bool operator<(const edge_storage& other) const
212  {
213  if (src < other.src)
214  return true;
215  if (src > other.src)
216  return false;
217  // This might be costly if the destination is a vector
218  if (dst < other.dst)
219  return true;
220  if (dst > other.dst)
221  return false;
222  return this->data() < other.data();
223  }
224 
225  bool operator==(const edge_storage& other) const
226  {
227  return src == other.src &&
228  dst == other.dst &&
229  this->data() == other.data();
230  }
231  };
232 
234  // Edge iterator
236 
237  // This holds a graph and a edge number that is the start of
238  // a list, and it iterates over all the edge_storage_t elements
239  // of that list.
240 
241  template <typename Graph>
242  class SPOT_API edge_iterator
243  {
244  public:
245  typedef typename std::conditional<std::is_const<Graph>::value,
246  const typename Graph::edge_storage_t,
247  typename Graph::edge_storage_t>::type
248  value_type;
249  typedef value_type& reference;
250  typedef value_type* pointer;
251  typedef std::ptrdiff_t difference_type;
252  typedef std::forward_iterator_tag iterator_category;
253 
254  typedef typename Graph::edge edge;
255 
256  edge_iterator() noexcept
257  : g_(nullptr), t_(0)
258  {
259  }
260 
261  edge_iterator(Graph* g, edge t) noexcept
262  : g_(g), t_(t)
263  {
264  }
265 
266  bool operator==(edge_iterator o) const
267  {
268  return t_ == o.t_;
269  }
270 
271  bool operator!=(edge_iterator o) const
272  {
273  return t_ != o.t_;
274  }
275 
276  reference operator*() const
277  {
278  return g_->edge_storage(t_);
279  }
280 
281  pointer operator->() const
282  {
283  return &g_->edge_storage(t_);
284  }
285 
286  edge_iterator operator++()
287  {
288  t_ = operator*().next_succ;
289  return *this;
290  }
291 
292  edge_iterator operator++(int)
293  {
294  edge_iterator ti = *this;
295  t_ = operator*().next_succ;
296  return ti;
297  }
298 
299  operator bool() const
300  {
301  return t_;
302  }
303 
304  edge trans() const
305  {
306  return t_;
307  }
308 
309  protected:
310  Graph* g_;
311  edge t_;
312  };
313 
314  template <typename Graph>
315  class SPOT_API killer_edge_iterator: public edge_iterator<Graph>
316  {
317  typedef edge_iterator<Graph> super;
318  public:
319  typedef typename Graph::state_storage_t state_storage_t;
320  typedef typename Graph::edge edge;
321 
322  killer_edge_iterator(Graph* g, edge t, state_storage_t& src) noexcept
323  : super(g, t), src_(src), prev_(0)
324  {
325  }
326 
327  killer_edge_iterator operator++()
328  {
329  prev_ = this->t_;
330  this->t_ = this->operator*().next_succ;
331  return *this;
332  }
333 
334  killer_edge_iterator operator++(int)
335  {
336  killer_edge_iterator ti = *this;
337  ++*this;
338  return ti;
339  }
340 
341  // Erase the current edge and advance the iterator.
342  void erase()
343  {
344  edge next = this->operator*().next_succ;
345 
346  // Update source state and previous edges
347  if (prev_)
348  {
349  this->g_->edge_storage(prev_).next_succ = next;
350  }
351  else
352  {
353  if (src_.succ == this->t_)
354  src_.succ = next;
355  }
356  if (src_.succ_tail == this->t_)
357  {
358  src_.succ_tail = prev_;
359  SPOT_ASSERT(next == 0);
360  }
361 
362  // Erased edges have themselves as next_succ.
363  this->operator*().next_succ = this->t_;
364 
365  // Advance iterator to next edge.
366  this->t_ = next;
367 
368  ++this->g_->killed_edge_;
369  }
370 
371  protected:
372  state_storage_t& src_;
373  edge prev_;
374  };
375 
376 
378  // State OUT
380 
381  // Fake container listing the outgoing edges of a state.
382 
383  template <typename Graph>
384  class SPOT_API state_out
385  {
386  public:
387  typedef typename Graph::edge edge;
388  state_out(Graph* g, edge t) noexcept
389  : g_(g), t_(t)
390  {
391  }
392 
393  edge_iterator<Graph> begin() const
394  {
395  return {g_, t_};
396  }
397 
398  edge_iterator<Graph> end() const
399  {
400  return {};
401  }
402 
403  void recycle(edge t)
404  {
405  t_ = t;
406  }
407 
408  protected:
409  Graph* g_;
410  edge t_;
411  };
412 
414  // all_trans
416 
417  template <typename Graph>
418  class SPOT_API all_edge_iterator
419  {
420  public:
421  typedef typename std::conditional<std::is_const<Graph>::value,
422  const typename Graph::edge_storage_t,
423  typename Graph::edge_storage_t>::type
424  value_type;
425  typedef value_type& reference;
426  typedef value_type* pointer;
427  typedef std::ptrdiff_t difference_type;
428  typedef std::forward_iterator_tag iterator_category;
429 
430  protected:
431  typedef typename std::conditional<std::is_const<Graph>::value,
432  const typename Graph::edge_vector_t,
433  typename Graph::edge_vector_t>::type
434  tv_t;
435 
436  unsigned t_;
437  tv_t& tv_;
438 
439  void skip_()
440  {
441  unsigned s = tv_.size();
442  do
443  ++t_;
444  while (t_ < s && tv_[t_].next_succ == t_);
445  }
446 
447  public:
448  all_edge_iterator(unsigned pos, tv_t& tv) noexcept
449  : t_(pos), tv_(tv)
450  {
451  skip_();
452  }
453 
454  all_edge_iterator(tv_t& tv) noexcept
455  : t_(tv.size()), tv_(tv)
456  {
457  }
458 
459  all_edge_iterator& operator++()
460  {
461  skip_();
462  return *this;
463  }
464 
465  all_edge_iterator operator++(int)
466  {
467  all_edge_iterator old = *this;
468  ++*this;
469  return old;
470  }
471 
472  bool operator==(all_edge_iterator o) const
473  {
474  return t_ == o.t_;
475  }
476 
477  bool operator!=(all_edge_iterator o) const
478  {
479  return t_ != o.t_;
480  }
481 
482  reference operator*() const
483  {
484  return tv_[t_];
485  }
486 
487  pointer operator->() const
488  {
489  return &tv_[t_];
490  }
491  };
492 
493 
494  template <typename Graph>
495  class SPOT_API all_trans
496  {
497  public:
498  typedef typename std::conditional<std::is_const<Graph>::value,
499  const typename Graph::edge_vector_t,
500  typename Graph::edge_vector_t>::type
501  tv_t;
503  private:
504  tv_t& tv_;
505  public:
506 
507  all_trans(tv_t& tv) noexcept
508  : tv_(tv)
509  {
510  }
511 
512  iter_t begin() const
513  {
514  return {0, tv_};
515  }
516 
517  iter_t end() const
518  {
519  return {tv_};
520  }
521  };
522 
523  class SPOT_API const_universal_dests
524  {
525  private:
526  const unsigned* begin_;
527  const unsigned* end_;
528  unsigned tmp_;
529  public:
530  const_universal_dests(const unsigned* begin, const unsigned* end) noexcept
531  : begin_(begin), end_(end)
532  {
533  }
534 
535  const_universal_dests(unsigned state) noexcept
536  : begin_(&tmp_), end_(&tmp_ + 1), tmp_(state)
537  {
538  }
539 
540  const unsigned* begin() const
541  {
542  return begin_;
543  }
544 
545  const unsigned* end() const
546  {
547  return end_;
548  }
549  };
550 
551  template<class G>
553  {
554  std::map<std::vector<unsigned>, unsigned> uniq_;
555  G& g_;
556  public:
557 
558  univ_dest_mapper(G& graph)
559  : g_(graph)
560  {
561  }
562 
563  template<class I>
564  unsigned new_univ_dests(I begin, I end)
565  {
566  std::vector<unsigned> tmp(begin, end);
567  std::sort(tmp.begin(), tmp.end());
568  tmp.erase(std::unique(tmp.begin(), tmp.end()), tmp.end());
569  auto p = uniq_.emplace(tmp, 0);
570  if (p.second)
571  p.first->second = g_.new_univ_dests(tmp.begin(), tmp.end());
572  return p.first->second;
573  }
574 
575  };
576 
577  } // namespace internal
578 
579 
584  template <typename State_Data, typename Edge_Data>
585  class digraph
586  {
587  friend class internal::edge_iterator<digraph>;
588  friend class internal::edge_iterator<const digraph>;
589  friend class internal::killer_edge_iterator<digraph>;
590 
591  public:
592  typedef internal::edge_iterator<digraph> iterator;
593  typedef internal::edge_iterator<const digraph> const_iterator;
594 
595  // Extra data to store on each state or edge.
596  typedef State_Data state_data_t;
597  typedef Edge_Data edge_data_t;
598 
599  // State and edges are identified by their indices in some
600  // vector.
601  typedef unsigned state;
602  typedef unsigned edge;
603 
604  typedef internal::distate_storage<edge,
606  state_storage_t;
607  typedef internal::edge_storage<state, state, edge,
609  edge_storage_t;
610  typedef std::vector<state_storage_t> state_vector;
611  typedef std::vector<edge_storage_t> edge_vector_t;
612 
613  // A sequence of universal destination groups of the form:
614  // (n state_1 state_2 ... state_n)*
615  typedef std::vector<unsigned> dests_vector_t;
616 
617  protected:
618  state_vector states_;
619  edge_vector_t edges_;
620  dests_vector_t dests_; // Only used by alternating automata.
621  // Number of erased edges.
622  unsigned killed_edge_;
623  public:
630  digraph(unsigned max_states = 10, unsigned max_trans = 0)
631  : killed_edge_(0)
632  {
633  states_.reserve(max_states);
634  if (max_trans == 0)
635  max_trans = max_states * 2;
636  edges_.reserve(max_trans + 1);
637  // Edge number 0 is not used, because we use this index
638  // to mark the absence of a edge.
639  edges_.resize(1);
640  // This causes edge 0 to be considered as dead.
641  edges_[0].next_succ = 0;
642  }
643 
645  unsigned num_states() const
646  {
647  return states_.size();
648  }
649 
653  unsigned num_edges() const
654  {
655  return edges_.size() - killed_edge_ - 1;
656  }
657 
659  bool is_existential() const
660  {
661  return dests_.empty();
662  }
663 
664 #ifndef SWIG
665  SPOT_DEPRECATED("use !is_existential() instead")
670  bool is_alternating() const
671  {
672  return !is_existential();
673  }
674 #endif
675 
681  template <typename... Args>
682  state new_state(Args&&... args)
683  {
684  state s = states_.size();
685  states_.emplace_back(std::forward<Args>(args)...);
686  return s;
687  }
688 
695  template <typename... Args>
696  state new_states(unsigned n, Args&&... args)
697  {
698  state s = states_.size();
699  states_.reserve(s + n);
700  while (n--)
701  states_.emplace_back(std::forward<Args>(args)...);
702  return s;
703  }
704 
711  state_storage(state s)
712  {
713  return states_[s];
714  }
715 
716  const state_storage_t&
717  state_storage(state s) const
718  {
719  return states_[s];
720  }
722 
728  typename state_storage_t::data_t&
729  state_data(state s)
730  {
731  return states_[s].data();
732  }
733 
734  const typename state_storage_t::data_t&
735  state_data(state s) const
736  {
737  return states_[s].data();
738  }
740 
747  edge_storage(edge s)
748  {
749  return edges_[s];
750  }
751 
752  const edge_storage_t&
753  edge_storage(edge s) const
754  {
755  return edges_[s];
756  }
758 
764  typename edge_storage_t::data_t&
765  edge_data(edge s)
766  {
767  return edges_[s].data();
768  }
769 
770  const typename edge_storage_t::data_t&
771  edge_data(edge s) const
772  {
773  return edges_[s].data();
774  }
776 
782  template <typename... Args>
783  edge
784  new_edge(state src, state dst, Args&&... args)
785  {
786  edge t = edges_.size();
787  edges_.emplace_back(dst, 0, src, std::forward<Args>(args)...);
788 
789  edge st = states_[src].succ_tail;
790  SPOT_ASSERT(st < t || !st);
791  if (!st)
792  states_[src].succ = t;
793  else
794  edges_[st].next_succ = t;
795  states_[src].succ_tail = t;
796  return t;
797  }
798 
806  template <typename I>
807  state
808  new_univ_dests(I dst_begin, I dst_end)
809  {
810  unsigned sz = std::distance(dst_begin, dst_end);
811  if (sz == 1)
812  return *dst_begin;
813  SPOT_ASSERT(sz > 1);
814  unsigned d = dests_.size();
815  dests_.emplace_back(sz);
816  dests_.insert(dests_.end(), dst_begin, dst_end);
817  return ~d;
818  }
819 
826  template <typename I, typename... Args>
827  edge
828  new_univ_edge(state src, I dst_begin, I dst_end, Args&&... args)
829  {
830  return new_edge(src, new_univ_dests(dst_begin, dst_end),
831  std::forward<Args>(args)...);
832  }
833 
839  template <typename... Args>
840  edge
841  new_univ_edge(state src, const std::initializer_list<state>& dsts,
842  Args&&... args)
843  {
844  return new_univ_edge(src, dsts.begin(), dsts.end(),
845  std::forward<Args>(args)...);
846  }
847 
848  internal::const_universal_dests univ_dests(state src) const
849  {
850  if ((int)src < 0)
851  {
852  unsigned pos = ~src;
853  const unsigned* d = dests_.data();
854  d += pos;
855  unsigned num = *d;
856  return { d + 1, d + num + 1 };
857  }
858  else
859  {
860  return src;
861  }
862  }
863 
864  internal::const_universal_dests univ_dests(const edge_storage_t& e) const
865  {
866  return univ_dests(e.dst);
867  }
868 
870  state index_of_state(const state_storage_t& ss) const
871  {
872  SPOT_ASSERT(!states_.empty());
873  return &ss - &states_.front();
874  }
875 
877  edge index_of_edge(const edge_storage_t& tt) const
878  {
879  SPOT_ASSERT(!edges_.empty());
880  return &tt - &edges_.front();
881  }
882 
886  out(state src)
887  {
888  return {this, states_[src].succ};
889  }
890 
893  {
894  return out(index_of_state(src));
895  }
896 
898  out(state src) const
899  {
900  return {this, states_[src].succ};
901  }
902 
904  out(state_storage_t& src) const
905  {
906  return out(index_of_state(src));
907  }
909 
916  {
917  return {this, src.succ, src};
918  }
919 
921  out_iteraser(state src)
922  {
923  return out_iteraser(state_storage(src));
924  }
926 
930  const state_vector& states() const
931  {
932  return states_;
933  }
934 
935  state_vector& states()
936  {
937  return states_;
938  }
940 
946  {
947  return edges_;
948  }
949 
951  {
952  return edges_;
953  }
955 
964  const edge_vector_t& edge_vector() const
965  {
966  return edges_;
967  }
968 
969  edge_vector_t& edge_vector()
970  {
971  return edges_;
972  }
974 
981  bool is_valid_edge(edge t) const
982  {
983  // Erased edges have their next_succ pointing to
984  // themselves.
985  return (t < edges_.size() &&
986  edges_[t].next_succ != t);
987  }
988 
993  bool is_dead_edge(unsigned t) const
994  {
995  return edges_[t].next_succ == t;
996  }
997 
998  bool is_dead_edge(const edge_storage_t& t) const
999  {
1000  return t.next_succ == index_of_edge(t);
1001  }
1003 
1009  const dests_vector_t& dests_vector() const
1010  {
1011  return dests_;
1012  }
1013 
1014  dests_vector_t& dests_vector()
1015  {
1016  return dests_;
1017  }
1019 
1021  void dump_storage(std::ostream& o) const
1022  {
1023  unsigned tend = edges_.size();
1024  for (unsigned t = 1; t < tend; ++t)
1025  {
1026  o << 't' << t << ": (s"
1027  << edges_[t].src << ", ";
1028  int d = edges_[t].dst;
1029  if (d < 0)
1030  o << 'd' << ~d;
1031  else
1032  o << 's' << d;
1033  o << ") t" << edges_[t].next_succ << '\n';
1034  }
1035  unsigned send = states_.size();
1036  for (unsigned s = 0; s < send; ++s)
1037  {
1038  o << 's' << s << ": t"
1039  << states_[s].succ << " t"
1040  << states_[s].succ_tail << '\n';
1041  }
1042  unsigned dend = dests_.size();
1043  unsigned size = 0;
1044  for (unsigned s = 0; s < dend; ++s)
1045  {
1046  o << 'd' << s << ": ";
1047  if (size == 0)
1048  {
1049  o << '#';
1050  size = dests_[s];
1051  }
1052  else
1053  {
1054  o << 's';
1055  --size;
1056  }
1057  o << dests_[s] << '\n';
1058  }
1059  }
1060 
1061  enum dump_storage_items {
1062  DSI_GraphHeader = 1,
1063  DSI_GraphFooter = 2,
1064  DSI_StatesHeader = 4,
1065  DSI_StatesBody = 8,
1066  DSI_StatesFooter = 16,
1067  DSI_States = DSI_StatesHeader | DSI_StatesBody | DSI_StatesFooter,
1068  DSI_EdgesHeader = 32,
1069  DSI_EdgesBody = 64,
1070  DSI_EdgesFooter = 128,
1071  DSI_Edges = DSI_EdgesHeader | DSI_EdgesBody | DSI_EdgesFooter,
1072  DSI_DestsHeader = 256,
1073  DSI_DestsBody = 512,
1074  DSI_DestsFooter = 1024,
1075  DSI_Dests = DSI_DestsHeader | DSI_DestsBody | DSI_DestsFooter,
1076  DSI_All =
1077  DSI_GraphHeader | DSI_States | DSI_Edges | DSI_Dests | DSI_GraphFooter,
1078  };
1079 
1081  void dump_storage_as_dot(std::ostream& o, int dsi = DSI_All) const
1082  {
1083  if (dsi & DSI_GraphHeader)
1084  o << "digraph g { \nnode [shape=plaintext]\n";
1085  unsigned send = states_.size();
1086  if (dsi & DSI_StatesHeader)
1087  {
1088  o << ("states [label=<\n"
1089  "<table border='0' cellborder='1' cellspacing='0'>\n"
1090  "<tr><td sides='b' bgcolor='yellow' port='s'>states</td>\n");
1091  for (unsigned s = 0; s < send; ++s)
1092  o << "<td sides='b' bgcolor='yellow' port='s" << s << "'>"
1093  << s << "</td>\n";
1094  o << "</tr>\n";
1095  }
1096  if (dsi & DSI_StatesBody)
1097  {
1098  o << "<tr><td port='ss'>succ</td>\n";
1099  for (unsigned s = 0; s < send; ++s)
1100  {
1101  o << "<td port='ss" << s;
1102  if (states_[s].succ)
1103  o << "' bgcolor='cyan";
1104  o << "'>" << states_[s].succ << "</td>\n";
1105  }
1106  o << "</tr><tr><td port='st'>succ_tail</td>\n";
1107  for (unsigned s = 0; s < send; ++s)
1108  {
1109  o << "<td port='st" << s;
1110  if (states_[s].succ_tail)
1111  o << "' bgcolor='cyan";
1112  o << "'>" << states_[s].succ_tail << "</td>\n";
1113  }
1114  o << "</tr>\n";
1115  }
1116  if (dsi & DSI_StatesFooter)
1117  o << "</table>>]\n";
1118  unsigned eend = edges_.size();
1119  if (dsi & DSI_EdgesHeader)
1120  {
1121  o << ("edges [label=<\n"
1122  "<table border='0' cellborder='1' cellspacing='0'>\n"
1123  "<tr><td sides='b' bgcolor='cyan' port='e'>edges</td>\n");
1124  for (unsigned e = 1; e < eend; ++e)
1125  {
1126  o << "<td sides='b' bgcolor='"
1127  << (e != edges_[e].next_succ ? "cyan" : "gray")
1128  << "' port='e" << e << "'>" << e << "</td>\n";
1129  }
1130  o << "</tr>";
1131  }
1132  if (dsi & DSI_EdgesBody)
1133  {
1134  o << "<tr><td port='ed'>dst</td>\n";
1135  for (unsigned e = 1; e < eend; ++e)
1136  {
1137  o << "<td port='ed" << e;
1138  int d = edges_[e].dst;
1139  if (d < 0)
1140  o << "' bgcolor='pink'>~" << ~d;
1141  else
1142  o << "' bgcolor='yellow'>" << d;
1143  o << "</td>\n";
1144  }
1145  o << "</tr><tr><td port='en'>next_succ</td>\n";
1146  for (unsigned e = 1; e < eend; ++e)
1147  {
1148  o << "<td port='en" << e;
1149  if (edges_[e].next_succ)
1150  {
1151  if (edges_[e].next_succ != e)
1152  o << "' bgcolor='cyan";
1153  else
1154  o << "' bgcolor='gray";
1155  }
1156  o << "'>" << edges_[e].next_succ << "</td>\n";
1157  }
1158  o << "</tr><tr><td port='es'>src</td>\n";
1159  for (unsigned e = 1; e < eend; ++e)
1160  o << "<td port='es" << e << "' bgcolor='yellow'>"
1161  << edges_[e].src << "</td>\n";
1162  o << "</tr>\n";
1163  }
1164  if (dsi & DSI_EdgesFooter)
1165  o << "</table>>]\n";
1166  if (!dests_.empty())
1167  {
1168  unsigned dend = dests_.size();
1169  if (dsi & DSI_DestsHeader)
1170  {
1171  o << ("dests [label=<\n"
1172  "<table border='0' cellborder='1' cellspacing='0'>\n"
1173  "<tr><td sides='b' bgcolor='pink' port='d'>dests</td>\n");
1174  unsigned d = 0;
1175  while (d < dend)
1176  {
1177  o << "<td sides='b' bgcolor='pink' port='d"
1178  << d << "'>~" << d << "</td>\n";
1179  unsigned cnt = dests_[d];
1180  d += cnt + 1;
1181  while (cnt--)
1182  o << "<td sides='b'></td>\n";
1183  }
1184  o << "</tr>\n";
1185  }
1186  if (dsi & DSI_DestsBody)
1187  {
1188  o << "<tr><td port='dd'>#cnt/dst</td>\n";
1189  unsigned d = 0;
1190  while (d < dend)
1191  {
1192  unsigned cnt = dests_[d];
1193  o << "<td port='d'>#" << cnt << "</td>\n";
1194  ++d;
1195  while (cnt--)
1196  {
1197  o << "<td bgcolor='yellow' port='dd"
1198  << d << "'>" << dests_[d] << "</td>\n";
1199  ++d;
1200  }
1201  }
1202  o << "</tr>\n";
1203  }
1204  if (dsi & DSI_DestsFooter)
1205  o << "</table>>]\n";
1206  }
1207  if (dsi & DSI_GraphFooter)
1208  o << "}\n";
1209  }
1210 
1217  {
1218  if (killed_edge_ == 0)
1219  return;
1220  auto i = std::remove_if(edges_.begin() + 1, edges_.end(),
1221  [this](const edge_storage_t& t) {
1222  return this->is_dead_edge(t);
1223  });
1224  edges_.erase(i, edges_.end());
1225  killed_edge_ = 0;
1226  }
1227 
1233  template<class Predicate = std::less<edge_storage_t>>
1234  void sort_edges_(Predicate p = Predicate())
1235  {
1236  //std::cerr << "\nbefore\n";
1237  //dump_storage(std::cerr);
1238  std::stable_sort(edges_.begin() + 1, edges_.end(), p);
1239  }
1240 
1246  {
1247  state last_src = -1U;
1248  edge tend = edges_.size();
1249  for (edge t = 1; t < tend; ++t)
1250  {
1251  state src = edges_[t].src;
1252  if (src != last_src)
1253  {
1254  states_[src].succ = t;
1255  if (last_src != -1U)
1256  {
1257  states_[last_src].succ_tail = t - 1;
1258  edges_[t - 1].next_succ = 0;
1259  }
1260  while (++last_src != src)
1261  {
1262  states_[last_src].succ = 0;
1263  states_[last_src].succ_tail = 0;
1264  }
1265  }
1266  else
1267  {
1268  edges_[t - 1].next_succ = t;
1269  }
1270  }
1271  if (last_src != -1U)
1272  {
1273  states_[last_src].succ_tail = tend - 1;
1274  edges_[tend - 1].next_succ = 0;
1275  }
1276  unsigned send = states_.size();
1277  while (++last_src != send)
1278  {
1279  states_[last_src].succ = 0;
1280  states_[last_src].succ_tail = 0;
1281  }
1282  //std::cerr << "\nafter\n";
1283  //dump_storage(std::cerr);
1284  }
1285 
1291  void rename_states_(const std::vector<unsigned>& newst)
1292  {
1293  SPOT_ASSERT(newst.size() == states_.size());
1294  unsigned tend = edges_.size();
1295  for (unsigned t = 1; t < tend; t++)
1296  {
1297  edges_[t].dst = newst[edges_[t].dst];
1298  edges_[t].src = newst[edges_[t].src];
1299  }
1300  }
1301 
1307  void defrag_states(std::vector<unsigned>&& newst, unsigned used_states)
1308  {
1309  SPOT_ASSERT(newst.size() >= states_.size());
1310  SPOT_ASSERT(used_states > 0);
1311 
1312  //std::cerr << "\nbefore defrag\n";
1313  //dump_storage(std::cerr);
1314 
1315  // Shift all states in states_, as indicated by newst.
1316  unsigned send = states_.size();
1317  for (state s = 0; s < send; ++s)
1318  {
1319  state dst = newst[s];
1320  if (dst == s)
1321  continue;
1322  if (dst == -1U)
1323  {
1324  // This is an erased state. Mark all its edges as
1325  // dead (i.e., t.next_succ should point to t for each of
1326  // them).
1327  auto t = states_[s].succ;
1328  while (t)
1329  std::swap(t, edges_[t].next_succ);
1330  continue;
1331  }
1332  states_[dst] = std::move(states_[s]);
1333  }
1334  states_.resize(used_states);
1335 
1336  // Shift all edges in edges_. The algorithm is
1337  // similar to remove_if, but it also keeps the correspondence
1338  // between the old and new index as newidx[old] = new.
1339  unsigned tend = edges_.size();
1340  std::vector<edge> newidx(tend);
1341  unsigned dest = 1;
1342  for (edge t = 1; t < tend; ++t)
1343  {
1344  if (is_dead_edge(t))
1345  continue;
1346  if (t != dest)
1347  edges_[dest] = std::move(edges_[t]);
1348  newidx[t] = dest;
1349  ++dest;
1350  }
1351  edges_.resize(dest);
1352  killed_edge_ = 0;
1353 
1354  // Adjust next_succ and dst pointers in all edges.
1355  for (edge t = 1; t < dest; ++t)
1356  {
1357  auto& tr = edges_[t];
1358  tr.src = newst[tr.src];
1359  tr.dst = newst[tr.dst];
1360  tr.next_succ = newidx[tr.next_succ];
1361  }
1362 
1363  // Adjust succ and succ_tails pointers in all states.
1364  for (auto& s: states_)
1365  {
1366  s.succ = newidx[s.succ];
1367  s.succ_tail = newidx[s.succ_tail];
1368  }
1369 
1370  //std::cerr << "\nafter defrag\n";
1371  //dump_storage(std::cerr);
1372  }
1373  };
1374 }
state new_states(unsigned n, Args &&... args)
Create n new states.
Definition: graph.hh:696
Definition: automata.hh:26
state index_of_state(const state_storage_t &ss) const
Convert a storage reference into a state number.
Definition: graph.hh:870
Definition: graph.hh:61
const edge_storage_t::data_t & edge_data(edge s) const
return the Edgeg_Data of an edge.
Definition: graph.hh:771
internal::killer_edge_iterator< digraph > out_iteraser(state src)
Return a fake container with all edges leaving src, allowing erasure.
Definition: graph.hh:921
state_vector & states()
Return the vector of states.
Definition: graph.hh:935
const dests_vector_t & dests_vector() const
The vector used to store universal destinations.
Definition: graph.hh:1009
internal::state_out< const digraph > out(state src) const
Return a fake container with all edges leaving src.
Definition: graph.hh:898
state new_state(Args &&... args)
Create a new states.
Definition: graph.hh:682
A directed graph.
Definition: graph.hh:35
Definition: graph.hh:242
void dump_storage(std::ostream &o) const
Dump the state and edge storage for debugging.
Definition: graph.hh:1021
void sort_edges_(Predicate p=Predicate())
Sort all edges according to a predicate.
Definition: graph.hh:1234
Abstract class for states.
Definition: twa.hh:50
bool is_existential() const
Whether the automaton uses only existential branching.
Definition: graph.hh:659
Definition: graph.hh:183
Globally.
const state_storage_t::data_t & state_data(state s) const
return the State_Data associated to a state
Definition: graph.hh:735
internal::killer_edge_iterator< digraph > out_iteraser(state_storage_t &src)
Return a fake container with all edges leaving src, allowing erasure.
Definition: graph.hh:915
edge_storage_t & edge_storage(edge s)
return a reference to the storage of an edge
Definition: graph.hh:747
edge new_edge(state src, state dst, Args &&... args)
Create a new edge.
Definition: graph.hh:784
Definition: graph.hh:552
const state_vector & states() const
Return the vector of states.
Definition: graph.hh:930
state_storage_t::data_t & state_data(state s)
return the State_Data associated to a state
Definition: graph.hh:729
void dump_storage_as_dot(std::ostream &o, int dsi=DSI_All) const
Dump the state and edge storage for debugging.
Definition: graph.hh:1081
internal::state_out< const digraph > out(state_storage_t &src) const
Return a fake container with all edges leaving src.
Definition: graph.hh:904
Definition: graph.hh:158
void remove_dead_edges_()
Remove all dead edges.
Definition: graph.hh:1216
state_storage_t & state_storage(state s)
return a reference to the storage of a state
Definition: graph.hh:711
const state_storage_t & state_storage(state s) const
return a reference to the storage of a state
Definition: graph.hh:717
internal::all_trans< const digraph > edges() const
Return a fake container with all edges (exluding erased edges)
Definition: graph.hh:945
bool is_dead_edge(unsigned t) const
Tests whether an edge has been erased.
Definition: graph.hh:993
internal::all_trans< digraph > edges()
Return a fake container with all edges (exluding erased edges)
Definition: graph.hh:950
edge_storage_t::data_t & edge_data(edge s)
return the Edgeg_Data of an edge.
Definition: graph.hh:765
Definition: graph.hh:495
const edge_storage_t & edge_storage(edge s) const
return a reference to the storage of an edge
Definition: graph.hh:753
edge_vector_t & edge_vector()
Return the vector of all edges.
Definition: graph.hh:969
unsigned num_states() const
The number of states in the automaton.
Definition: graph.hh:645
const edge_vector_t & edge_vector() const
Return the vector of all edges.
Definition: graph.hh:964
Definition: graph.hh:41
dests_vector_t & dests_vector()
The vector used to store universal destinations.
Definition: graph.hh:1014
edge new_univ_edge(state src, const std::initializer_list< state > &dsts, Args &&... args)
Create a new universal edge.
Definition: graph.hh:841
unsigned num_edges() const
The number of edges in the automaton.
Definition: graph.hh:653
bool is_valid_edge(edge t) const
Test whether the given edge is valid.
Definition: graph.hh:981
edge index_of_edge(const edge_storage_t &tt) const
Conveart a storage reference into an edge number.
Definition: graph.hh:877
void rename_states_(const std::vector< unsigned > &newst)
Rename all the states in the edge vector.
Definition: graph.hh:1291
Definition: graph.hh:418
Definition: twa.hh:1721
internal::state_out< digraph > out(state src)
Return a fake container with all edges leaving src.
Definition: graph.hh:886
void defrag_states(std::vector< unsigned > &&newst, unsigned used_states)
Rename and remove states.
Definition: graph.hh:1307
internal::state_out< digraph > out(state_storage_t &src)
Return a fake container with all edges leaving src.
Definition: graph.hh:892
state new_univ_dests(I dst_begin, I dst_end)
Create a new universal destination group.
Definition: graph.hh:808
digraph(unsigned max_states=10, unsigned max_trans=0)
Construct an empty graph.
Definition: graph.hh:630
Definition: graph.hh:384
edge new_univ_edge(state src, I dst_begin, I dst_end, Args &&... args)
Create a new universal edge.
Definition: graph.hh:828
Definition: graph.hh:315
void chain_edges_()
Reconstruct the chain of outgoing edges.
Definition: graph.hh:1245
bool is_dead_edge(const edge_storage_t &t) const
Tests whether an edge has been erased.
Definition: graph.hh:998

Please direct any question, comment, or bug report to the Spot mailing list at spot@lrde.epita.fr.
Generated on Fri Feb 27 2015 10:00:07 for spot by doxygen 1.8.13