spot 2.11.5.dev
graph.hh
1// -*- coding: utf-8 -*-
2// Copyright (C) 2014-2018, 2020-2022 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 <spot/misc/_config.h>
24#include <vector>
25#include <type_traits>
26#include <tuple>
27#include <cassert>
28#include <iterator>
29#include <algorithm>
30#include <map>
31#include <iostream>
32#ifdef SPOT_ENABLE_PTHREAD
33# include <thread>
34#endif // SPOT_ENABLE_PTHREAD
35
36namespace spot
37{
38 template <typename State_Data, typename Edge_Data>
39 class SPOT_API digraph;
40
41 namespace internal
42 {
43#ifndef SWIG
44 template <typename Of, typename ...Args>
46 {
47 static const bool value = false;
48 };
49
50 template <typename Of, typename Arg1, typename ...Args>
51 struct first_is_base_of<Of, Arg1, Args...>
52 {
53 static const bool value =
54 std::is_base_of<Of, typename std::decay<Arg1>::type>::value;
55 };
56#endif
57
58 // The boxed_label class stores Data as an attribute called
59 // "label" if boxed is true. It is an empty class if Data is
60 // void, and it simply inherits from Data if boxed is false.
61 //
62 // The data() method offers an homogeneous access to the Data
63 // instance.
64 template <typename Data, bool boxed = !std::is_class<Data>::value>
65 struct SPOT_API boxed_label
66 {
67 typedef Data data_t;
68 Data label;
69
70#ifndef SWIG
71 template <typename... Args,
72 typename = typename std::enable_if<
73 !first_is_base_of<boxed_label, Args...>::value>::type>
74 boxed_label(Args&&... args)
75 noexcept(std::is_nothrow_constructible<Data, Args...>::value)
76 : label{std::forward<Args>(args)...}
77 {
78 }
79#endif
80
81 // if Data is a POD type, G++ 4.8.2 wants default values for all
82 // label fields unless we define this default constructor here.
83 explicit boxed_label()
84 noexcept(std::is_nothrow_constructible<Data>::value)
85 {
86 }
87
88 Data& data()
89 {
90 return label;
91 }
92
93 const Data& data() const
94 {
95 return label;
96 }
97
98 bool operator<(const boxed_label& other) const
99 {
100 return label < other.label;
101 }
102 };
103
104 template <>
105 struct SPOT_API boxed_label<void, true>: public std::tuple<>
106 {
107 typedef std::tuple<> data_t;
108 std::tuple<>& data()
109 {
110 return *this;
111 }
112
113 const std::tuple<>& data() const
114 {
115 return *this;
116 }
117
118 };
119
120 template <typename Data>
121 struct SPOT_API boxed_label<Data, false>: public Data
122 {
123 typedef Data data_t;
124
125#ifndef SWIG
126 template <typename... Args,
127 typename = typename std::enable_if<
128 !first_is_base_of<boxed_label, Args...>::value>::type>
129 boxed_label(Args&&... args)
130 noexcept(std::is_nothrow_constructible<Data, Args...>::value)
131 : Data{std::forward<Args>(args)...}
132 {
133 }
134#endif
135
136 // if Data is a POD type, G++ 4.8.2 wants default values for all
137 // label fields unless we define this default constructor here.
138 explicit boxed_label()
139 noexcept(std::is_nothrow_constructible<Data>::value)
140 {
141 }
142
143 Data& data()
144 {
145 return *this;
146 }
147
148 const Data& data() const
149 {
150 return *this;
151 }
152 };
153
155 // State storage for digraphs
157
158 // We have two implementations, one with attached State_Data, and
159 // one without.
160
161 template <typename Edge, typename State_Data>
162 struct SPOT_API distate_storage final: public State_Data
163 {
164 Edge succ = 0; // First outgoing edge (used when iterating)
165 Edge succ_tail = 0; // Last outgoing edge (used for
166 // appending new edges)
167#ifndef SWIG
168 template <typename... Args,
169 typename = typename std::enable_if<
170 !first_is_base_of<distate_storage, Args...>::value>::type>
171 distate_storage(Args&&... args)
172 noexcept(std::is_nothrow_constructible<State_Data, Args...>::value)
173 : State_Data{std::forward<Args>(args)...}
174 {
175 }
176#endif
177 };
178
180 // Edge storage
182
183 // Again two implementation: one with label, and one without.
184
185 template <typename StateIn,
186 typename StateOut, typename Edge, typename Edge_Data>
187 struct SPOT_API edge_storage final: public Edge_Data
188 {
189 typedef Edge edge;
190
191 StateOut dst; // destination
192 Edge next_succ; // next outgoing edge with same
193 // source, or 0
194 StateIn src; // source
195
196 explicit edge_storage()
197 noexcept(std::is_nothrow_constructible<Edge_Data>::value)
198 : Edge_Data{}
199 {
200 }
201
202#ifndef SWIG
203 template <typename... Args>
204 edge_storage(StateOut dst, Edge next_succ,
205 StateIn src, Args&&... args)
206 noexcept(std::is_nothrow_constructible<Edge_Data, Args...>::value
207 && std::is_nothrow_constructible<StateOut, StateOut>::value
208 && std::is_nothrow_constructible<Edge, Edge>::value)
209 : Edge_Data{std::forward<Args>(args)...},
210 dst(dst), next_succ(next_succ), src(src)
211 {
212 }
213#endif
214
215 bool operator<(const edge_storage& other) const
216 {
217 if (src < other.src)
218 return true;
219 if (src > other.src)
220 return false;
221 // This might be costly if the destination is a vector
222 if (dst < other.dst)
223 return true;
224 if (dst > other.dst)
225 return false;
226 return this->data() < other.data();
227 }
228
229 bool operator==(const edge_storage& other) const
230 {
231 return src == other.src &&
232 dst == other.dst &&
233 this->data() == other.data();
234 }
235 };
236
238 // Edge iterator
240
241 // This holds a graph and a edge number that is the start of
242 // a list, and it iterates over all the edge_storage_t elements
243 // of that list.
244
245 template <typename Graph>
246 class SPOT_API edge_iterator
247 {
248 public:
249 typedef typename std::conditional<std::is_const<Graph>::value,
250 const typename Graph::edge_storage_t,
251 typename Graph::edge_storage_t>::type
252 value_type;
253 typedef value_type& reference;
254 typedef value_type* pointer;
255 typedef std::ptrdiff_t difference_type;
256 typedef std::forward_iterator_tag iterator_category;
257
258 typedef typename Graph::edge edge;
259
260 edge_iterator() noexcept
261 : g_(nullptr), t_(0)
262 {
263 }
264
265 edge_iterator(Graph* g, edge t) noexcept
266 : g_(g), t_(t)
267 {
268 }
269
270 bool operator==(edge_iterator o) const
271 {
272 return t_ == o.t_;
273 }
274
275 bool operator!=(edge_iterator o) const
276 {
277 return t_ != o.t_;
278 }
279
280 reference operator*() const
281 {
282 return g_->edge_storage(t_);
283 }
284
285 pointer operator->() const
286 {
287 return &g_->edge_storage(t_);
288 }
289
290 edge_iterator operator++()
291 {
292 t_ = operator*().next_succ;
293 return *this;
294 }
295
296 edge_iterator operator++(int)
297 {
298 edge_iterator ti = *this;
299 t_ = operator*().next_succ;
300 return ti;
301 }
302
303 operator bool() const
304 {
305 return t_;
306 }
307
308 edge trans() const
309 {
310 return t_;
311 }
312
313 protected:
314 Graph* g_;
315 edge t_;
316 };
317
318 template <typename Graph>
319 class SPOT_API killer_edge_iterator: public edge_iterator<Graph>
320 {
322 public:
323 typedef typename Graph::state_storage_t state_storage_t;
324 typedef typename Graph::edge edge;
325
326 killer_edge_iterator(Graph* g, edge t, state_storage_t& src) noexcept
327 : super(g, t), src_(src), prev_(0)
328 {
329 }
330
331 killer_edge_iterator operator++()
332 {
333 prev_ = this->t_;
334 this->t_ = this->operator*().next_succ;
335 return *this;
336 }
337
338 killer_edge_iterator operator++(int)
339 {
340 killer_edge_iterator ti = *this;
341 ++*this;
342 return ti;
343 }
344
345 // Erase the current edge and advance the iterator.
346 void erase()
347 {
348 edge next = this->operator*().next_succ;
349
350 // Update source state and previous edges
351 if (prev_)
352 {
353 this->g_->edge_storage(prev_).next_succ = next;
354 }
355 else
356 {
357 if (src_.succ == this->t_)
358 src_.succ = next;
359 }
360 if (src_.succ_tail == this->t_)
361 {
362 src_.succ_tail = prev_;
363 SPOT_ASSERT(next == 0);
364 }
365
366 // Erased edges have themselves as next_succ.
367 this->operator*().next_succ = this->t_;
368
369 // Advance iterator to next edge.
370 this->t_ = next;
371
372 ++this->g_->killed_edge_;
373 }
374
375 protected:
376 state_storage_t& src_;
377 edge prev_;
378 };
379
380
382 // State OUT
384
385 // Fake container listing the outgoing edges of a state.
386
387 template <typename Graph>
388 class SPOT_API state_out
389 {
390 public:
391 typedef typename Graph::edge edge;
392 state_out(Graph* g, edge t) noexcept
393 : g_(g), t_(t)
394 {
395 }
396
397 edge_iterator<Graph> begin() const
398 {
399 return {g_, t_};
400 }
401
402 edge_iterator<Graph> end() const
403 {
404 return {};
405 }
406
407 void recycle(edge t)
408 {
409 t_ = t;
410 }
411
412 protected:
413 Graph* g_;
414 edge t_;
415 };
416
418 // all_trans
420
421 template <typename Graph>
422 class SPOT_API all_edge_iterator
423 {
424 public:
425 typedef typename std::conditional<std::is_const<Graph>::value,
426 const typename Graph::edge_storage_t,
427 typename Graph::edge_storage_t>::type
428 value_type;
429 typedef value_type& reference;
430 typedef value_type* pointer;
431 typedef std::ptrdiff_t difference_type;
432 typedef std::forward_iterator_tag iterator_category;
433
434 protected:
435 typedef typename std::conditional<std::is_const<Graph>::value,
436 const typename Graph::edge_vector_t,
437 typename Graph::edge_vector_t>::type
438 tv_t;
439
440 unsigned t_;
441 tv_t& tv_;
442
443 void skip_()
444 {
445 unsigned s = tv_.size();
446 do
447 ++t_;
448 while (t_ < s && tv_[t_].next_succ == t_);
449 }
450
451 public:
452 all_edge_iterator(unsigned pos, tv_t& tv) noexcept
453 : t_(pos), tv_(tv)
454 {
455 skip_();
456 }
457
458 all_edge_iterator(tv_t& tv) noexcept
459 : t_(tv.size()), tv_(tv)
460 {
461 }
462
463 all_edge_iterator& operator++()
464 {
465 skip_();
466 return *this;
467 }
468
469 all_edge_iterator operator++(int)
470 {
471 all_edge_iterator old = *this;
472 ++*this;
473 return old;
474 }
475
476 bool operator==(all_edge_iterator o) const
477 {
478 return t_ == o.t_;
479 }
480
481 bool operator!=(all_edge_iterator o) const
482 {
483 return t_ != o.t_;
484 }
485
486 reference operator*() const
487 {
488 return tv_[t_];
489 }
490
491 pointer operator->() const
492 {
493 return &tv_[t_];
494 }
495 };
496
497
498 template <typename Graph>
499 class SPOT_API all_trans
500 {
501 public:
502 typedef typename std::conditional<std::is_const<Graph>::value,
503 const typename Graph::edge_vector_t,
504 typename Graph::edge_vector_t>::type
505 tv_t;
507 private:
508 tv_t& tv_;
509 public:
510
511 all_trans(tv_t& tv) noexcept
512 : tv_(tv)
513 {
514 }
515
516 iter_t begin() const
517 {
518 return {0, tv_};
519 }
520
521 iter_t end() const
522 {
523 return {tv_};
524 }
525 };
526
528 {
529 private:
530 const unsigned* begin_;
531 const unsigned* end_;
532 unsigned tmp_;
533 public:
534 const_universal_dests(const unsigned* begin, const unsigned* end) noexcept
535 : begin_(begin), end_(end)
536 {
537 }
538
539 const_universal_dests(unsigned state) noexcept
540 : begin_(&tmp_), end_(&tmp_ + 1), tmp_(state)
541 {
542 }
543
544 const unsigned* begin() const
545 {
546 return begin_;
547 }
548
549 const unsigned* end() const
550 {
551 return end_;
552 }
553 };
554
555 template<class G>
557 {
558 std::map<std::vector<unsigned>, unsigned> uniq_;
559 G& g_;
560 public:
561
562 univ_dest_mapper(G& graph)
563 : g_(graph)
564 {
565 }
566
567 template<class I>
568 unsigned new_univ_dests(I begin, I end)
569 {
570 std::vector<unsigned> tmp(begin, end);
571 std::sort(tmp.begin(), tmp.end());
572 tmp.erase(std::unique(tmp.begin(), tmp.end()), tmp.end());
573 auto p = uniq_.emplace(tmp, 0);
574 if (p.second)
575 p.first->second = g_.new_univ_dests(tmp.begin(), tmp.end());
576 return p.first->second;
577 }
578
579 };
580
581 } // namespace internal
582
583
588 template <typename State_Data, typename Edge_Data>
590 {
591 friend class internal::edge_iterator<digraph>;
592 friend class internal::edge_iterator<const digraph>;
594
595 public:
598
599 // Extra data to store on each state or edge.
600 typedef State_Data state_data_t;
601 typedef Edge_Data edge_data_t;
602
603 // State and edges are identified by their indices in some
604 // vector.
605 typedef unsigned state;
606 typedef unsigned edge;
607
608 typedef internal::distate_storage<edge,
611 typedef internal::edge_storage<state, state, edge,
614 typedef std::vector<state_storage_t> state_vector;
615 typedef std::vector<edge_storage_t> edge_vector_t;
616
617 // A sequence of universal destination groups of the form:
618 // (n state_1 state_2 ... state_n)*
619 typedef std::vector<unsigned> dests_vector_t;
620
621 protected:
622 state_vector states_;
623 edge_vector_t edges_;
624 dests_vector_t dests_; // Only used by alternating automata.
625 // Number of erased edges.
626 unsigned killed_edge_;
627 public:
634 digraph(unsigned max_states = 10, unsigned max_trans = 0)
635 : killed_edge_(0)
636 {
637 states_.reserve(max_states);
638 if (max_trans == 0)
639 max_trans = max_states * 2;
640 edges_.reserve(max_trans + 1);
641 // Edge number 0 is not used, because we use this index
642 // to mark the absence of a edge.
643 edges_.resize(1);
644 // This causes edge 0 to be considered as dead.
645 edges_[0].next_succ = 0;
646 }
647
649 unsigned num_states() const
650 {
651 return states_.size();
652 }
653
657 unsigned num_edges() const
658 {
659 return edges_.size() - killed_edge_ - 1;
660 }
661
663 bool is_existential() const
664 {
665 return dests_.empty();
666 }
667
673 template <typename... Args>
674 state new_state(Args&&... args)
675 {
676 state s = states_.size();
677 states_.emplace_back(std::forward<Args>(args)...);
678 return s;
679 }
680
687 template <typename... Args>
688 state new_states(unsigned n, Args&&... args)
689 {
690 state s = states_.size();
691 states_.reserve(s + n);
692 while (n--)
693 states_.emplace_back(std::forward<Args>(args)...);
694 return s;
695 }
696
702 state_storage_t&
704 {
705 return states_[s];
706 }
707
708 const state_storage_t&
709 state_storage(state s) const
710 {
711 return states_[s];
712 }
714
720 typename state_storage_t::data_t&
721 state_data(state s)
722 {
723 return states_[s].data();
724 }
725
726 const typename state_storage_t::data_t&
727 state_data(state s) const
728 {
729 return states_[s].data();
730 }
732
738 edge_storage_t&
740 {
741 return edges_[s];
742 }
743
744 const edge_storage_t&
745 edge_storage(edge s) const
746 {
747 return edges_[s];
748 }
750
756 typename edge_storage_t::data_t&
757 edge_data(edge s)
758 {
759 return edges_[s].data();
760 }
761
762 const typename edge_storage_t::data_t&
763 edge_data(edge s) const
764 {
765 return edges_[s].data();
766 }
768
774 template <typename... Args>
775 edge
776 new_edge(state src, state dst, Args&&... args)
777 {
778 edge t = edges_.size();
779 edges_.emplace_back(dst, 0, src, std::forward<Args>(args)...);
780
781 edge st = states_[src].succ_tail;
782 SPOT_ASSERT(st < t || !st);
783 if (!st)
784 states_[src].succ = t;
785 else
786 edges_[st].next_succ = t;
787 states_[src].succ_tail = t;
788 return t;
789 }
790
798 template <typename I>
799 state
800 new_univ_dests(I dst_begin, I dst_end)
801 {
802 unsigned sz = std::distance(dst_begin, dst_end);
803 if (sz == 1)
804 return *dst_begin;
805 SPOT_ASSERT(sz > 1);
806 unsigned d = dests_.size();
807 if (!dests_.empty()
808 && &*dst_begin >= &dests_.front()
809 && &*dst_begin <= &dests_.back()
810 && (dests_.capacity() - dests_.size()) < (sz + 1))
811 {
812 // If dst_begin...dst_end points into dests_ and dests_ risk
813 // being reallocated, we have to savea the destination
814 // states before we lose them.
815 std::vector<unsigned> tmp(dst_begin, dst_end);
816 dests_.emplace_back(sz);
817 dests_.insert(dests_.end(), tmp.begin(), tmp.end());
818 }
819 else
820 {
821 dests_.emplace_back(sz);
822 dests_.insert(dests_.end(), dst_begin, dst_end);
823 }
824 return ~d;
825 }
826
833 template <typename I, typename... Args>
834 edge
835 new_univ_edge(state src, I dst_begin, I dst_end, Args&&... args)
836 {
837 return new_edge(src, new_univ_dests(dst_begin, dst_end),
838 std::forward<Args>(args)...);
839 }
840
846 template <typename... Args>
847 edge
848 new_univ_edge(state src, const std::initializer_list<state>& dsts,
849 Args&&... args)
850 {
851 return new_univ_edge(src, dsts.begin(), dsts.end(),
852 std::forward<Args>(args)...);
853 }
854
855 internal::const_universal_dests univ_dests(state src) const
856 {
857 if ((int)src < 0)
858 {
859 unsigned pos = ~src;
860 const unsigned* d = dests_.data();
861 d += pos;
862 unsigned num = *d;
863 return { d + 1, d + num + 1 };
864 }
865 else
866 {
867 return src;
868 }
869 }
870
871 internal::const_universal_dests univ_dests(const edge_storage_t& e) const
872 {
873 return univ_dests(e.dst);
874 }
875
877 state index_of_state(const state_storage_t& ss) const
878 {
879 SPOT_ASSERT(!states_.empty());
880 return &ss - &states_.front();
881 }
882
884 edge index_of_edge(const edge_storage_t& tt) const
885 {
886 SPOT_ASSERT(!edges_.empty());
887 return &tt - &edges_.front();
888 }
889
893 out(state src)
894 {
895 return {this, states_[src].succ};
896 }
897
900 {
901 return out(index_of_state(src));
902 }
903
905 out(state src) const
906 {
907 return {this, states_[src].succ};
908 }
909
912 {
913 return out(index_of_state(src));
914 }
916
923 {
924 return {this, src.succ, src};
925 }
926
928 out_iteraser(state src)
929 {
930 return out_iteraser(state_storage(src));
931 }
933
937 const state_vector& states() const
938 {
939 return states_;
940 }
941
942 state_vector& states()
943 {
944 return states_;
945 }
947
953 {
954 return edges_;
955 }
956
958 {
959 return edges_;
960 }
962
971 const edge_vector_t& edge_vector() const
972 {
973 return edges_;
974 }
975
976 edge_vector_t& edge_vector()
977 {
978 return edges_;
979 }
981
988 bool is_valid_edge(edge t) const
989 {
990 // Erased edges have their next_succ pointing to
991 // themselves.
992 return (t < edges_.size() &&
993 edges_[t].next_succ != t);
994 }
995
1000 bool is_dead_edge(unsigned t) const
1001 {
1002 return edges_[t].next_succ == t;
1003 }
1004
1005 bool is_dead_edge(const edge_storage_t& t) const
1006 {
1007 return t.next_succ == index_of_edge(t);
1008 }
1010
1016 const dests_vector_t& dests_vector() const
1017 {
1018 return dests_;
1019 }
1020
1021 dests_vector_t& dests_vector()
1022 {
1023 return dests_;
1024 }
1026
1028 void dump_storage(std::ostream& o) const
1029 {
1030 unsigned tend = edges_.size();
1031 for (unsigned t = 1; t < tend; ++t)
1032 {
1033 o << 't' << t << ": (s"
1034 << edges_[t].src << ", ";
1035 int d = edges_[t].dst;
1036 if (d < 0)
1037 o << 'd' << ~d;
1038 else
1039 o << 's' << d;
1040 o << ") t" << edges_[t].next_succ << '\n';
1041 }
1042 unsigned send = states_.size();
1043 for (unsigned s = 0; s < send; ++s)
1044 {
1045 o << 's' << s << ": t"
1046 << states_[s].succ << " t"
1047 << states_[s].succ_tail << '\n';
1048 }
1049 unsigned dend = dests_.size();
1050 unsigned size = 0;
1051 for (unsigned s = 0; s < dend; ++s)
1052 {
1053 o << 'd' << s << ": ";
1054 if (size == 0)
1055 {
1056 o << '#';
1057 size = dests_[s];
1058 }
1059 else
1060 {
1061 o << 's';
1062 --size;
1063 }
1064 o << dests_[s] << '\n';
1065 }
1066 }
1067
1068 enum dump_storage_items {
1069 DSI_GraphHeader = 1,
1070 DSI_GraphFooter = 2,
1071 DSI_StatesHeader = 4,
1072 DSI_StatesBody = 8,
1073 DSI_StatesFooter = 16,
1074 DSI_States = DSI_StatesHeader | DSI_StatesBody | DSI_StatesFooter,
1075 DSI_EdgesHeader = 32,
1076 DSI_EdgesBody = 64,
1077 DSI_EdgesFooter = 128,
1078 DSI_Edges = DSI_EdgesHeader | DSI_EdgesBody | DSI_EdgesFooter,
1079 DSI_DestsHeader = 256,
1080 DSI_DestsBody = 512,
1081 DSI_DestsFooter = 1024,
1082 DSI_Dests = DSI_DestsHeader | DSI_DestsBody | DSI_DestsFooter,
1083 DSI_All =
1084 DSI_GraphHeader | DSI_States | DSI_Edges | DSI_Dests | DSI_GraphFooter,
1085 };
1086
1088 void dump_storage_as_dot(std::ostream& o, int dsi = DSI_All) const
1089 {
1090 if (dsi & DSI_GraphHeader)
1091 o << "digraph g { \nnode [shape=plaintext]\n";
1092 unsigned send = states_.size();
1093 if (dsi & DSI_StatesHeader)
1094 {
1095 o << ("states [label=<\n"
1096 "<table border='0' cellborder='1' cellspacing='0'>\n"
1097 "<tr><td sides='b' bgcolor='yellow' port='s'>states</td>\n");
1098 for (unsigned s = 0; s < send; ++s)
1099 o << "<td sides='b' bgcolor='yellow' port='s" << s << "'>"
1100 << s << "</td>\n";
1101 o << "</tr>\n";
1102 }
1103 if (dsi & DSI_StatesBody)
1104 {
1105 o << "<tr><td port='ss'>succ</td>\n";
1106 for (unsigned s = 0; s < send; ++s)
1107 {
1108 o << "<td port='ss" << s;
1109 if (states_[s].succ)
1110 o << "' bgcolor='cyan";
1111 o << "'>" << states_[s].succ << "</td>\n";
1112 }
1113 o << "</tr><tr><td port='st'>succ_tail</td>\n";
1114 for (unsigned s = 0; s < send; ++s)
1115 {
1116 o << "<td port='st" << s;
1117 if (states_[s].succ_tail)
1118 o << "' bgcolor='cyan";
1119 o << "'>" << states_[s].succ_tail << "</td>\n";
1120 }
1121 o << "</tr>\n";
1122 }
1123 if (dsi & DSI_StatesFooter)
1124 o << "</table>>]\n";
1125 unsigned eend = edges_.size();
1126 if (dsi & DSI_EdgesHeader)
1127 {
1128 o << ("edges [label=<\n"
1129 "<table border='0' cellborder='1' cellspacing='0'>\n"
1130 "<tr><td sides='b' bgcolor='cyan' port='e'>edges</td>\n");
1131 for (unsigned e = 1; e < eend; ++e)
1132 {
1133 o << "<td sides='b' bgcolor='"
1134 << (e != edges_[e].next_succ ? "cyan" : "gray")
1135 << "' port='e" << e << "'>" << e << "</td>\n";
1136 }
1137 o << "</tr>";
1138 }
1139 if (dsi & DSI_EdgesBody)
1140 {
1141 o << "<tr><td port='ed'>dst</td>\n";
1142 for (unsigned e = 1; e < eend; ++e)
1143 {
1144 o << "<td port='ed" << e;
1145 int d = edges_[e].dst;
1146 if (d < 0)
1147 o << "' bgcolor='pink'>~" << ~d;
1148 else
1149 o << "' bgcolor='yellow'>" << d;
1150 o << "</td>\n";
1151 }
1152 o << "</tr><tr><td port='en'>next_succ</td>\n";
1153 for (unsigned e = 1; e < eend; ++e)
1154 {
1155 o << "<td port='en" << e;
1156 if (edges_[e].next_succ)
1157 {
1158 if (edges_[e].next_succ != e)
1159 o << "' bgcolor='cyan";
1160 else
1161 o << "' bgcolor='gray";
1162 }
1163 o << "'>" << edges_[e].next_succ << "</td>\n";
1164 }
1165 o << "</tr><tr><td port='es'>src</td>\n";
1166 for (unsigned e = 1; e < eend; ++e)
1167 o << "<td port='es" << e << "' bgcolor='yellow'>"
1168 << edges_[e].src << "</td>\n";
1169 o << "</tr>\n";
1170 }
1171 if (dsi & DSI_EdgesFooter)
1172 o << "</table>>]\n";
1173 if (!dests_.empty())
1174 {
1175 unsigned dend = dests_.size();
1176 if (dsi & DSI_DestsHeader)
1177 {
1178 o << ("dests [label=<\n"
1179 "<table border='0' cellborder='1' cellspacing='0'>\n"
1180 "<tr><td sides='b' bgcolor='pink' port='d'>dests</td>\n");
1181 unsigned d = 0;
1182 while (d < dend)
1183 {
1184 o << "<td sides='b' bgcolor='pink' port='d"
1185 << d << "'>~" << d << "</td>\n";
1186 unsigned cnt = dests_[d];
1187 d += cnt + 1;
1188 while (cnt--)
1189 o << "<td sides='b'></td>\n";
1190 }
1191 o << "</tr>\n";
1192 }
1193 if (dsi & DSI_DestsBody)
1194 {
1195 o << "<tr><td port='dd'>#cnt/dst</td>\n";
1196 unsigned d = 0;
1197 while (d < dend)
1198 {
1199 unsigned cnt = dests_[d];
1200 o << "<td port='d'>#" << cnt << "</td>\n";
1201 ++d;
1202 while (cnt--)
1203 {
1204 o << "<td bgcolor='yellow' port='dd"
1205 << d << "'>" << dests_[d] << "</td>\n";
1206 ++d;
1207 }
1208 }
1209 o << "</tr>\n";
1210 }
1211 if (dsi & DSI_DestsFooter)
1212 o << "</table>>]\n";
1213 }
1214 if (dsi & DSI_GraphFooter)
1215 o << "}\n";
1216 }
1217
1224 {
1225 if (killed_edge_ == 0)
1226 return;
1227 auto i = std::remove_if(edges_.begin() + 1, edges_.end(),
1228 [this](const edge_storage_t& t) {
1229 return this->is_dead_edge(t);
1230 });
1231 edges_.erase(i, edges_.end());
1232 killed_edge_ = 0;
1233 }
1234
1240 template<class Predicate = std::less<edge_storage_t>>
1241 void sort_edges_(Predicate p = Predicate())
1242 {
1243 //std::cerr << "\nbefore\n";
1244 //dump_storage(std::cerr);
1245 std::stable_sort(edges_.begin() + 1, edges_.end(), p);
1246 }
1247
1257 template<class Predicate = std::less<edge_storage_t>>
1258 void sort_edges_srcfirst_(Predicate p = Predicate(),
1259 parallel_policy ppolicy = parallel_policy())
1260 {
1261 SPOT_ASSERT(!edges_.empty());
1262 const unsigned ns = num_states();
1263 std::vector<unsigned> idx_list(ns+1);
1264 edge_vector_t new_edges;
1265 new_edges.reserve(edges_.size());
1266 new_edges.resize(1);
1267 // This causes edge 0 to be considered as dead.
1268 new_edges[0].next_succ = 0;
1269 // Copy all edges so that they are sorted by src
1270 for (unsigned s = 0; s < ns; ++s)
1271 {
1272 idx_list[s] = new_edges.size();
1273 for (const auto& e : out(s))
1274 new_edges.push_back(e);
1275 }
1276 idx_list[ns] = new_edges.size();
1277 // New edge sorted by source
1278 // If we have few edge or only one threads
1279 // Benchmark few?
1280 auto bne = new_edges.begin();
1281#ifndef SPOT_ENABLE_PTHREAD
1282 (void) ppolicy;
1283#else
1284 unsigned nthreads = ppolicy.nthreads();
1285 if (nthreads <= 1)
1286#endif
1287 {
1288 for (unsigned s = 0u; s < ns; ++s)
1289 std::stable_sort(bne + idx_list[s],
1290 bne + idx_list[s+1], p);
1291 }
1292#ifdef SPOT_ENABLE_PTHREAD
1293 else
1294 {
1295 static std::vector<std::thread> tv;
1296 SPOT_ASSERT(tv.empty());
1297 tv.resize(nthreads);
1298 // FIXME: Due to the way these thread advance into the state
1299 // vector, they access very close memory location. It would
1300 // seems more cache friendly to have threads work on blocks
1301 // of continuous states.
1302 for (unsigned id = 0; id < nthreads; ++id)
1303 tv[id] = std::thread(
1304 [bne, id, ns, &idx_list, p, nthreads]()
1305 {
1306 for (unsigned s = id; s < ns; s += nthreads)
1307 std::stable_sort(bne + idx_list[s],
1308 bne + idx_list[s+1], p);
1309 return;
1310 });
1311 for (auto& t : tv)
1312 t.join();
1313 tv.clear();
1314 }
1315#endif
1316 std::swap(edges_, new_edges);
1317 // Like after normal sort_edges, they need to be chained before usage
1318 }
1319
1327 template<bool Stable = false, class Predicate = std::less<edge_storage_t>>
1328 void sort_edges_of_(Predicate p = Predicate(),
1329 const std::vector<bool>* to_sort_ptr = nullptr)
1330 {
1331 SPOT_ASSERT((to_sort_ptr == nullptr)
1332 || (to_sort_ptr->size() == num_states()));
1333 //std::cerr << "\nbefore\n";
1334 //dump_storage(std::cerr);
1335 auto pi = [&](unsigned t1, unsigned t2)
1336 {return p(edges_[t1], edges_[t2]); };
1337
1338 // Sort the outgoing edges of each selected state according
1339 // to predicate p. Do that in place.
1340 std::vector<unsigned> sort_idx_;
1341 unsigned ns = num_states();
1342 for (unsigned i = 0; i < ns; ++i)
1343 {
1344 if (to_sort_ptr && !(*to_sort_ptr)[i])
1345 continue;
1346 unsigned t = states_[i].succ;
1347 if (t == 0)
1348 continue;
1349 sort_idx_.clear();
1350 do
1351 {
1352 sort_idx_.push_back(t);
1353 t = edges_[t].next_succ;
1354 } while (t != 0);
1355 if constexpr (Stable)
1356 std::stable_sort(sort_idx_.begin(), sort_idx_.end(), pi);
1357 else
1358 std::sort(sort_idx_.begin(), sort_idx_.end(), pi);
1359 // Update the graph
1360 states_[i].succ = sort_idx_.front();
1361 states_[i].succ_tail = sort_idx_.back();
1362 const unsigned n_outs_n1 = sort_idx_.size() - 1;
1363 for (unsigned k = 0; k < n_outs_n1; ++k)
1364 edges_[sort_idx_[k]].next_succ = sort_idx_[k+1];
1365 edges_[sort_idx_.back()].next_succ = 0; // terminal
1366 }
1367 // Done
1368 }
1369
1375 {
1376 state last_src = -1U;
1377 edge tend = edges_.size();
1378 for (edge t = 1; t < tend; ++t)
1379 {
1380 state src = edges_[t].src;
1381 if (src != last_src)
1382 {
1383 states_[src].succ = t;
1384 if (last_src != -1U)
1385 {
1386 states_[last_src].succ_tail = t - 1;
1387 edges_[t - 1].next_succ = 0;
1388 }
1389 while (++last_src != src)
1390 {
1391 states_[last_src].succ = 0;
1392 states_[last_src].succ_tail = 0;
1393 }
1394 }
1395 else
1396 {
1397 edges_[t - 1].next_succ = t;
1398 }
1399 }
1400 if (last_src != -1U)
1401 {
1402 states_[last_src].succ_tail = tend - 1;
1403 edges_[tend - 1].next_succ = 0;
1404 }
1405 unsigned send = states_.size();
1406 while (++last_src != send)
1407 {
1408 states_[last_src].succ = 0;
1409 states_[last_src].succ_tail = 0;
1410 }
1411 //std::cerr << "\nafter\n";
1412 //dump_storage(std::cerr);
1413 }
1414
1420 void rename_states_(const std::vector<unsigned>& newst)
1421 {
1422 SPOT_ASSERT(newst.size() == states_.size());
1423 unsigned tend = edges_.size();
1424 for (unsigned t = 1; t < tend; t++)
1425 {
1426 edges_[t].dst = newst[edges_[t].dst];
1427 edges_[t].src = newst[edges_[t].src];
1428 }
1429 }
1430
1448 void defrag_states(const std::vector<unsigned>& newst, unsigned used_states)
1449 {
1450 SPOT_ASSERT(newst.size() >= states_.size());
1451 SPOT_ASSERT(used_states > 0);
1452
1453 //std::cerr << "\nbefore defrag\n";
1454 //dump_storage(std::cerr);
1455
1456 // Shift all states in states_, as indicated by newst.
1457 unsigned send = states_.size();
1458 for (state s = 0; s < send; ++s)
1459 {
1460 state dst = newst[s];
1461 if (dst == s)
1462 continue;
1463 if (dst == -1U)
1464 {
1465 // This is an erased state. Mark all its edges as
1466 // dead (i.e., t.next_succ should point to t for each of
1467 // them).
1468 auto t = states_[s].succ;
1469 while (t)
1470 std::swap(t, edges_[t].next_succ);
1471 continue;
1472 }
1473 states_[dst] = std::move(states_[s]);
1474 }
1475 states_.resize(used_states);
1476
1477 // Shift all edges in edges_. The algorithm is
1478 // similar to remove_if, but it also keeps the correspondence
1479 // between the old and new index as newidx[old] = new.
1480 unsigned tend = edges_.size();
1481 std::vector<edge> newidx(tend);
1482 unsigned dest = 1;
1483 for (edge t = 1; t < tend; ++t)
1484 {
1485 if (is_dead_edge(t))
1486 continue;
1487 if (t != dest)
1488 edges_[dest] = std::move(edges_[t]);
1489 newidx[t] = dest;
1490 ++dest;
1491 }
1492 edges_.resize(dest);
1493 killed_edge_ = 0;
1494
1495 // Adjust next_succ and dst pointers in all edges.
1496 for (edge t = 1; t < dest; ++t)
1497 {
1498 auto& tr = edges_[t];
1499 tr.src = newst[tr.src];
1500 tr.dst = newst[tr.dst];
1501 tr.next_succ = newidx[tr.next_succ];
1502 }
1503
1504 // Adjust succ and succ_tails pointers in all states.
1505 for (auto& s: states_)
1506 {
1507 s.succ = newidx[s.succ];
1508 s.succ_tail = newidx[s.succ_tail];
1509 }
1510
1511 //std::cerr << "\nafter defrag\n";
1512 //dump_storage(std::cerr);
1513 }
1514
1515 // prototype was changed in Spot 2.10
1516 SPOT_DEPRECATED("use reference version of this method")
1517 void defrag_states(std::vector<unsigned>&& newst, unsigned used_states)
1518 {
1519 return defrag_states(newst, used_states);
1520 }
1522 };
1523}
A directed graph.
Definition: graph.hh:590
unsigned num_states() const
The number of states in the automaton.
Definition: graph.hh:649
const dests_vector_t & dests_vector() const
The vector used to store universal destinations.
Definition: graph.hh:1016
void dump_storage_as_dot(std::ostream &o, int dsi=DSI_All) const
Dump the state and edge storage for debugging.
Definition: graph.hh:1088
bool is_dead_edge(const edge_storage_t &t) const
Tests whether an edge has been erased.
Definition: graph.hh:1005
void dump_storage(std::ostream &o) const
Dump the state and edge storage for debugging.
Definition: graph.hh:1028
internal::state_out< digraph > out(state_storage_t &src)
Return a fake container with all edges leaving src.
Definition: graph.hh:899
internal::all_trans< digraph > edges()
Return a fake container with all edges (exluding erased edges)
Definition: graph.hh:957
dests_vector_t & dests_vector()
The vector used to store universal destinations.
Definition: graph.hh:1021
void sort_edges_of_(Predicate p=Predicate(), const std::vector< bool > *to_sort_ptr=nullptr)
Sort edges of the given states.
Definition: graph.hh:1328
state new_states(unsigned n, Args &&... args)
Create n new states.
Definition: graph.hh:688
state new_state(Args &&... args)
Create a new states.
Definition: graph.hh:674
edge index_of_edge(const edge_storage_t &tt) const
Convert a storage reference into an edge number.
Definition: graph.hh:884
const edge_vector_t & edge_vector() const
Return the vector of all edges.
Definition: graph.hh:971
internal::killer_edge_iterator< digraph > out_iteraser(state src)
Return a fake container with all edges leaving src, allowing erasure.
Definition: graph.hh:928
bool is_valid_edge(edge t) const
Test whether the given edge is valid.
Definition: graph.hh:988
internal::state_out< digraph > out(state src)
Return a fake container with all edges leaving src.
Definition: graph.hh:893
internal::state_out< const digraph > out(state_storage_t &src) const
Return a fake container with all edges leaving src.
Definition: graph.hh:911
void sort_edges_srcfirst_(Predicate p=Predicate(), parallel_policy ppolicy=parallel_policy())
Sort all edges by src first, then, within edges of the same source use the predicate.
Definition: graph.hh:1258
edge new_univ_edge(state src, const std::initializer_list< state > &dsts, Args &&... args)
Create a new universal edge.
Definition: graph.hh:848
bool is_existential() const
Whether the automaton uses only existential branching.
Definition: graph.hh:663
edge_vector_t & edge_vector()
Return the vector of all edges.
Definition: graph.hh:976
const state_storage_t::data_t & state_data(state s) const
return the State_Data associated to a state
Definition: graph.hh:727
internal::state_out< const digraph > out(state src) const
Return a fake container with all edges leaving src.
Definition: graph.hh:905
state_storage_t::data_t & state_data(state s)
return the State_Data associated to a state
Definition: graph.hh:721
void remove_dead_edges_()
Remove all dead edges.
Definition: graph.hh:1223
digraph(unsigned max_states=10, unsigned max_trans=0)
Construct an empty graph.
Definition: graph.hh:634
state new_univ_dests(I dst_begin, I dst_end)
Create a new universal destination group.
Definition: graph.hh:800
const edge_storage_t::data_t & edge_data(edge s) const
return the Edgeg_Data of an edge.
Definition: graph.hh:763
edge_storage_t & edge_storage(edge s)
return a reference to the storage of an edge
Definition: graph.hh:739
void rename_states_(const std::vector< unsigned > &newst)
Rename all the states in the edge vector.
Definition: graph.hh:1420
void defrag_states(const std::vector< unsigned > &newst, unsigned used_states)
Rename and remove states.
Definition: graph.hh:1448
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:922
const state_vector & states() const
Return the vector of states.
Definition: graph.hh:937
internal::all_trans< const digraph > edges() const
Return a fake container with all edges (exluding erased edges)
Definition: graph.hh:952
unsigned num_edges() const
The number of edges in the automaton.
Definition: graph.hh:657
edge_storage_t::data_t & edge_data(edge s)
return the Edgeg_Data of an edge.
Definition: graph.hh:757
state_storage_t & state_storage(state s)
return a reference to the storage of a state
Definition: graph.hh:703
state index_of_state(const state_storage_t &ss) const
Convert a storage reference into a state number.
Definition: graph.hh:877
void sort_edges_(Predicate p=Predicate())
Sort all edges according to a predicate.
Definition: graph.hh:1241
bool is_dead_edge(unsigned t) const
Tests whether an edge has been erased.
Definition: graph.hh:1000
edge new_univ_edge(state src, I dst_begin, I dst_end, Args &&... args)
Create a new universal edge.
Definition: graph.hh:835
void chain_edges_()
Reconstruct the chain of outgoing edges.
Definition: graph.hh:1374
const edge_storage_t & edge_storage(edge s) const
return a reference to the storage of an edge
Definition: graph.hh:745
edge new_edge(state src, state dst, Args &&... args)
Create a new edge.
Definition: graph.hh:776
state_vector & states()
Return the vector of states.
Definition: graph.hh:942
const state_storage_t & state_storage(state s) const
return a reference to the storage of a state
Definition: graph.hh:709
Definition: graph.hh:423
Definition: graph.hh:500
Definition: graph.hh:247
Definition: graph.hh:320
Definition: graph.hh:389
Definition: graph.hh:557
This class is used to tell parallel algorithms what resources they may use.
Definition: common.hh:156
Abstract class for states.
Definition: twa.hh:51
@ tt
True.
@ G
Globally.
SPOT_DEPRECATED("use to_parity() instead") twa_graph_ptr iar(const const_twa_graph_ptr &aut
Turn a Rabin-like or Streett-like automaton into a parity automaton based on the index appearence rec...
Definition: automata.hh:27
Definition: graph.hh:66
Definition: graph.hh:163
Definition: graph.hh:188
Definition: graph.hh:46

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.9.4