graph.hh 10.4 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
// -*- coding: utf-8 -*-
// Copyright (C) 2014 Laboratoire de Recherche et Développement de
// l'Epita.
//
// This file is part of Spot, a model checking library.
//
// Spot 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 3 of the License, or
// (at your option) any later version.
//
// Spot 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.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

#ifndef SPOT_GRAPH_GRAPH_HH
# define SPOT_GRAPH_GRAPH_HH

23
#include "misc/common.hh"
24
25
26
#include <vector>
#include <type_traits>
#include <tuple>
27
#include <cassert>
28
29
30
31

namespace spot
{
  template <typename State_Data, typename Trans_Data, bool Alternating = false>
32
  class SPOT_API digraph;
33
34
35
36
37
38
39
40
41
42
43

  namespace internal
  {
    // The boxed_label class stores Data as an attribute called
    // "label" if boxed is true.  It is an empty class if Data is
    // void, and it simply inherits from Data if boxed is false.
    //
    // The data() method offers an homogeneous access to the Data
    // instance.

    template <typename Data, bool boxed = !std::is_class<Data>::value>
44
    struct SPOT_API boxed_label
45
    {
46
      typedef Data data_t;
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
      Data label;

      template <typename... Args>
      boxed_label(Args&&... args):
	label{std::forward<Args>(args)...}
      {
      }

      // if Data is a POD type, G++ 4.8.2 wants default values for all
      // label fields unless we define this default constructor here.
      explicit boxed_label()
      {
      }

      Data& data()
      {
	return label;
      }

      const Data& data() const
      {
	return label;
      }
    };

    template <>
73
    struct SPOT_API boxed_label<void, true>: public std::tuple<>
74
    {
75
76
77
78
79
80
81
82
83
84
85
      typedef std::tuple<> data_t;
      std::tuple<>& data()
      {
	return *this;
      }

      const std::tuple<>& data() const
      {
	return *this;
      }

86
87
88
    };

    template <typename Data>
89
    struct SPOT_API boxed_label<Data, false>: public Data
90
    {
91
92
      typedef Data data_t;

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
      template <typename... Args>
      boxed_label(Args&&... args):
	Data{std::forward<Args>(args)...}
      {
      }

      // if Data is a POS type, G++ 4.8.2 wants default values for all
      // label fields unless we define this default constructor here.
      explicit boxed_label()
      {
      }

      Data& data()
      {
	return *this;
      }

      const Data& data() const
      {
	return *this;
      }
    };

    //////////////////////////////////////////////////
    // State storage for digraphs
    //////////////////////////////////////////////////

    // We have two implementations, one with attached State_Data, and
    // one without.

    template <typename Transition, typename State_Data>
124
    struct SPOT_API distate_storage: public State_Data
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
    {
      Transition succ; // First outgoing transition (used when iterating)
      Transition succ_tail;	// Last outgoing transition (used for
				// appending new transitions)

      template <typename... Args>
      distate_storage(Args&&... args):
	State_Data{std::forward<Args>(args)...},
	succ(0),
	succ_tail(0)
      {
      }
    };

    //////////////////////////////////////////////////
    // Transition storage
    //////////////////////////////////////////////////

    // Again two implementation: one with label, and one without.

    template <typename State, typename Transition, typename Trans_Data>
146
    struct SPOT_API trans_storage: public Trans_Data
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
    {
      typedef Transition transition;

      State dst;		// destination
      Transition next_succ;	// next outgoing transition with same
				// source, or 0
      explicit trans_storage()
	: Trans_Data{}
      {
      }

      template <typename... Args>
      trans_storage(State dst, Transition next_succ, Args&&... args)
	: Trans_Data{std::forward<Args>(args)...},
	  dst(dst), next_succ(next_succ)
      {
      }
    };

    //////////////////////////////////////////////////
    // Transition iterator
    //////////////////////////////////////////////////

    // This holds a graph and a transition number that is the start of
    // a list, and it iterates over all the trans_storage_t elements
    // of that list.

    template <typename Graph>
175
    class SPOT_API trans_iterator
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
    {
    public:
      typedef typename Graph::transition transition;
      typedef typename Graph::trans_storage_t trans_storage_t;

      trans_iterator(Graph* g, transition t): t_(t), g_(g)
      {
      }

      bool operator==(trans_iterator o)
      {
	return t_ == o.t_;
      }

      bool operator!=(trans_iterator o)
      {
	return t_ != o.t_;
      }

      typename std::conditional<std::is_const<Graph>::value,
				const trans_storage_t&,
				trans_storage_t&>::type
      operator*()
      {
200
	return g_->trans_storage(t_);
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
      }

      trans_iterator operator++()
      {
	t_ = operator*().next_succ;
	return *this;
      }

      trans_iterator operator++(int)
      {
	trans_iterator ti = *this;
	t_ = operator*().next_succ;
	return ti;
      }

    protected:
      transition t_;
      Graph* g_;
    };

    //////////////////////////////////////////////////
    // State OUT
    //////////////////////////////////////////////////

    // Fake container listing the outgoing transitions of a state.

    template <typename Graph>
228
    class SPOT_API state_out
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
    {
    public:
      typedef typename Graph::transition transition;
      state_out(Graph* g, transition t):
	t_(t), g_(g)
      {
      }

      trans_iterator<Graph> begin()
      {
	return {g_, t_};
      }

      trans_iterator<Graph> end()
      {
	return {nullptr, 0};
      }

247
248
249
250
251
      void recycle(transition t)
      {
	t_ = t;
      }

252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
    protected:
      transition t_;
      Graph* g_;
    };

  }

  // The actual graph implementation

  template <typename State_Data, typename Trans_Data, bool Alternating>
  class digraph
  {
    friend class internal::trans_iterator<digraph>;
    friend class internal::trans_iterator<const digraph>;
  public:
    static constexpr bool alternating()
    {
      return Alternating;
    }

    // Extra data to store on each state or transition.
    typedef State_Data state_data_t;
    typedef Trans_Data trans_data_t;

    // State and transitions are identified by their indices in some
    // vector.
    typedef unsigned state;
    typedef unsigned transition;

    // The type of an output state (when seen from a transition)
    // depends on the kind of graph we build
    typedef typename std::conditional<Alternating,
				      std::vector<state>,
				      state>::type out_state;

    typedef internal::distate_storage<transition,
				      internal::boxed_label<State_Data>>
      state_storage_t;
    typedef internal::trans_storage<out_state, transition,
				    internal::boxed_label<Trans_Data>>
      trans_storage_t;
    typedef std::vector<state_storage_t> state_vector;
    typedef std::vector<trans_storage_t> trans_vector;
  protected:
    state_vector states_;
    trans_vector transitions_;

  public:
    /// \brief construct an empty graph
    ///
    /// Construct an empty graph, and reserve space for \a max_states
    /// states and \a max_trans transitions.  These are not hard
    /// limits, but just hints to pre-allocate a data structure that
    /// may hold that much items.
    digraph(unsigned max_states = 10, unsigned max_trans = 0)
    {
      states_.reserve(max_states);
      if (max_trans == 0)
	max_trans = max_states * 2;
      transitions_.reserve(max_trans + 1);
      // Transition number 0 is not used, because we use this index
      // to mark the absence of a transition.
      transitions_.resize(1);
    }

317
318
319
320
321
322
323
324
325
326
    unsigned num_states() const
    {
      return states_.size();
    }

    unsigned num_transitions() const
    {
      return transitions_.size();
    }

327
328
329
330
331
332
333
334
    template <typename... Args>
    state new_state(Args&&... args)
    {
      state s = states_.size();
      states_.emplace_back(std::forward<Args>(args)...);
      return s;
    }

335
336
337
338
339
340
341
342
343
344
345
346
347
348
    state_storage_t&
    state_storage(state s)
    {
      assert(s < states_.size());
      return states_[s];
    }

    const state_storage_t&
    state_storage(state s) const
    {
      assert(s < states_.size());
      return states_[s];
    }

349
350
351
    // Do not use State_Data& as return type, because State_Data might
    // be void.
    typename state_storage_t::data_t&
352
353
    state_data(state s)
    {
354
      assert(s < states_.size());
355
356
357
358
      return states_[s].data();
    }

    // May not be called on states with no data.
359
    const typename state_storage_t::data_t&
360
361
    state_data(state s) const
    {
362
      assert(s < states_.size());
363
364
365
      return states_[s].data();
    }

366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
    trans_storage_t&
    trans_storage(transition s)
    {
      assert(s < transitions_.size());
      return transitions_[s];
    }

    const trans_storage_t&
    trans_storage(transition s) const
    {
      assert(s < transitions_.size());
      return transitions_[s];
    }

    typename trans_storage_t::data_t&
    trans_data(transition s)
    {
      assert(s < transitions_.size());
      return transitions_[s].data();
    }

    const typename trans_storage_t::data_t&
    trans_data(transition s) const
    {
      assert(s < transitions_.size());
      return transitions_[s].data();
    }

394
395
396
397
    template <typename... Args>
    transition
    new_transition(state src, out_state dst, Args&&... args)
    {
398
399
      assert(src < states_.size());

400
401
402
403
      transition t = transitions_.size();
      transitions_.emplace_back(dst, 0, std::forward<Args>(args)...);

      transition st = states_[src].succ_tail;
404
      assert(st < t || !st);
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
      if (!st)
	states_[src].succ = t;
      else
	transitions_[st].next_succ = t;
      states_[src].succ_tail = t;
      return t;
    }

    internal::state_out<digraph>
    out(state src)
    {
      return {this, states_[src].succ};
    }

    internal::state_out<const digraph>
    out(state src) const
    {
      return {this, states_[src].succ};
    }

    unsigned nb_states() const
    {
      return states_.size();
    }

    unsigned nb_trans() const
    {
      return transitions_.size();
    }

    const state_vector& states() const
    {
      return states_;
    }

    state_vector& states()
    {
      return states_;
    }

  };

}

#endif // SPOT_GRAPH_GRAPH_HH