cfl_transset.h
Go to the documentation of this file.
1 /** @file cfl_transset.h @brief Classes Transition, TTransSet and TaTransSet */
2 
3 /* FAU Discrete Event Systems Library (libfaudes)
4 
5 Copyright (C) 2006 Bernd Opitz
6 Copyright (C) 2007 Thomas Moor
7 
8 Exclusive copyright is granted to Klaus Schmidt
9 
10 This library is free software; you can redistribute it and/or
11 modify it under the terms of the GNU Lesser General Public
12 License as published by the Free Software Foundation; either
13 version 2.1 of the License, or (at your option) any later version.
14 
15 This library is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 Lesser General Public License for more details.
19 
20 You should have received a copy of the GNU Lesser General Public
21 License along with this library; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23 
24 
25 #ifndef FAUDES_TRANSSET_H
26 
27 #include <sstream>
28 #include <map>
29 #include <iostream>
30 #include <typeinfo>
31 
32 #include "cfl_definitions.h"
33 #include "cfl_baseset.h"
34 #include "cfl_abaseset.h"
35 #include "cfl_indexset.h"
36 #include "cfl_nameset.h"
37 #include "cfl_attributes.h"
38 #include "cfl_tokenreader.h"
39 
40 
41 namespace faudes {
42 
43 /** @addtogroup ContainerClasses */
44 /** @{*/
45 
46 /**
47  * Triple (X1,Ev,X2) to represent current state, event and next state.
48  * This class provides the triple in struct like fashion where the components
49  * are of type faudes::Idx. While default order is lexographic, the transition
50  * container TTransSet allows for alternative sorting. Since technically a
51  * Transition is just a triple of indices, it is only the context of a generator
52  * that actually makes it a transition (eg by providing mandatory symbolic event
53  * names).
54  */
55 class Transition {
56 
57  public:
58 
59  /** Construct invalid Transition */
60  Transition(void) : X1(0), Ev(0), X2(0) {};
61 
62  /**
63  * Construct from values.
64  * @param x1
65  * Index of current state
66  * @param ev
67  * Index of Event
68  * @param x2
69  * Index of next state
70  */
71  Transition(Idx x1, Idx ev,Idx x2) :
72  X1(x1), Ev(ev), X2(x2) {}
73 
74  /** Default order for sorting container of Transition (lexographic) */
75  inline bool operator < (const Transition& othertrans) const {
76  if (X1 < othertrans.X1) return true;
77  if (X1 > othertrans.X1) return false;
78  if (Ev < othertrans.Ev) return true;
79  if (Ev > othertrans.Ev) return false;
80  if (X2 < othertrans.X2) return true;
81  return false;
82  }
83 
84  /** Equality operator */
85  inline bool operator == (const Transition& othertrans) const {
86  return ((X1 == othertrans.X1) && (Ev == othertrans.Ev) && (X2 == othertrans.X2));
87  };
88 
89  /** Inequality operator */
90  inline bool operator != (const Transition& othertrans) const {
91  return ((X1 != othertrans.X1) || (Ev != othertrans.Ev) || (X2 != othertrans.X2));
92  };
93 
94  /** Test validity (all indices !=0) */
95  inline bool Valid(void) const {
96  return (X1!=0) && (Ev!=0) && (X2!=0);
97  };
98 
99  /** Current state */
100  Idx X1;
101 
102  /** Event */
103  Idx Ev;
104 
105  /** Next state */
107 
108  /** Pretty print to string */
109  std::string Str(void) const {
110  return ToStringInteger(X1)+"--("+ToStringInteger(Ev) + ")->" + ToStringInteger(X2);}
111 };
112 
113 
114 
115 
116 /**
117  * Alternative ordering of Transitions. This class provides binary predicates
118  * for sorting transition containers by variant lexographic order.
119  */
120 
121 class TransSort {
122 
123  public:
124 
125  /** Binary predicate for sorting transitions in order Ev, X1, X2 */
126  struct EvX1X2 {
127  inline bool operator () (const Transition& left, const Transition& right) const {
128  if (left.Ev < right.Ev) return true;
129  if (left.Ev > right.Ev) return false;
130  if (left.X1 < right.X1) return true;
131  if (left.X1 > right.X1) return false;
132  if (left.X2 < right.X2) return true;
133  return false;
134  }
135  };
136 
137  /** Binary predicate for sorting transitions in order Ev, X2, X1 */
138  struct EvX2X1 {
139  inline bool operator () (const Transition& left, const Transition& right) const {
140  if (left.Ev < right.Ev) return true;
141  if (left.Ev > right.Ev) return false;
142  if (left.X2 < right.X2) return true;
143  if (left.X2 > right.X2) return false;
144  if (left.X1 < right.X1) return true;
145  return false;
146  }
147  };
148 
149  /** Binary predicate for sorting transitions in order X2, Ev, X1 */
150  struct X2EvX1 {
151  inline bool operator () (const Transition& left, const Transition& right) const {
152  if (left.X2 < right.X2) return true;
153  if (left.X2 > right.X2) return false;
154  if (left.Ev < right.Ev) return true;
155  if (left.Ev > right.Ev) return false;
156  if (left.X1 < right.X1) return true;
157  return false;
158  }
159  };
160 
161  /** Binary predicate for sorting transitions in order X2, X1, Ev */
162  struct X2X1Ev {
163  inline bool operator () (const Transition& left, const Transition& right) const {
164  if (left.X2 < right.X2) return true;
165  if (left.X2 > right.X2) return false;
166  if (left.X1 < right.X1) return true;
167  if (left.X1 > right.X1) return false;
168  if (left.Ev < right.Ev) return true;
169  return false;
170  }
171  };
172 
173  /** Binary predicate for sorting transitions in order X1, X2, Ev */
174  struct X1X2Ev {
175  inline bool operator () (const Transition& left, const Transition& right) const {
176  if (left.X1 < right.X1) return true;
177  if (left.X1 > right.X1) return false;
178  if (left.X2 < right.X2) return true;
179  if (left.X2 > right.X2) return false;
180  if (left.Ev < right.Ev) return true;
181  return false;
182  }
183  };
184 
185  /** Binary predicate for sorting transitions in order X1, Ev, X2 */
186  struct X1EvX2 {
187  inline bool operator () (const Transition& left, const Transition& right) const {
188  if (left.X1 < right.X1) return true;
189  if (left.X1 > right.X1) return false;
190  if (left.Ev < right.Ev) return true;
191  if (left.Ev > right.Ev) return false;
192  if (left.X2 < right.X2) return true;
193  return false;
194  }
195  };
196 
197 };
198 
199 
200 
201 /**
202  * Set of Transitions.
203  *
204  * This container class provides similar functionality and interface as BaseSet, but
205  * for Transitions rather than indices. The additional feature is a template parameter
206  * that allows to specify alternative sorting. For algorithms that examine a transition
207  * relation by e.g. the successor state X2, it may be worthwhile to copy a given
208  * TTransRel<TransSort::X1EvX2> to a TTransRel<TransSort::X2EvX1>. Example,
209  * assuming some transition relation is given in default order
210  *
211  * \code
212  * TransSet transrel; // transrel default order X1-Ev-X2
213  *
214  * // ... some operation to set up transrel
215  *
216  * TTransSet<TransSort::X2EvX1> transByX2; // alternative order X2-Ev-X1
217  * transrel.ReSort(transByX2); // copy and re-order transrel
218  * Iterator tit =transByX2.BeginByX2(x2) // begin iterator, X2 specified
219  * Iterator tit_end=transByX2.EndByX2(x2) // end iterator, X2 specified
220  * for(; tit!=tit_end; ++tit) { // loop over all transitions with specified x2
221  *
222  * // ... code to examine tramsitions with specified x2
223  *
224  * }
225  *
226  * \endcode
227  *
228  * Note: it is the context of a Generator that
229  * actually allows to interpret a TTransSet as a set of transitions as opposed to
230  * a set of triples of indices. In particular, file IO of transitions is provided
231  * by the generator class (although TTransSet provides basic output functions for debugging)
232  */
233 
234 template <class Cmp=TransSort::X1EvX2>
235 class TTransSet : public virtual TBaseSet<Transition,Cmp> {
236 
238 
239 
240  public:
241 
242  /** @name Constructors & Destructor */
243  /** @{ doxygen group */
244 
245  /** Construct an empty TTransSet object */
246  TTransSet(void);
247 
248  /**
249  * Copy-constructor
250  */
251  TTransSet(const TBaseSet<Transition,Cmp>& rOtherSet);
252 
253  /**
254  * Re-Sort Copy-constructor
255  */
256  template<class OtherCmp>
257  TTransSet(const TTransSet<OtherCmp>& res);
258 
259  /** Virtual destructor */
260  virtual ~TTransSet() {};
261 
262 
263  /** @} doxygen group */
264 
265 
266  /** Iterator on transition */
268 
269 
270  /** @name Accessing individual transitions */
271  /** @{ doxygen group */
272 
273  /**
274  * Add a Transition.
275  *
276  * @param rTransition
277  * Reference to transition object
278  * @return
279  * True if the transition was new to the set
280  *
281  */
282  bool Insert(const Transition& rTransition);
283 
284  /**
285  * Add a Transition by indices
286  *
287  * @param x1
288  * Predecessor state
289  * @param ev
290  * Event
291  * @param x2
292  * Successor state
293  * @return
294  * True if the transition was new to the set
295  *
296  */
297  bool Insert(Idx x1, Idx ev, Idx x2);
298 
299  /**
300  * Add a Transition.
301  *
302  * @param pos
303  * Insertion hint passed to STL
304  * @param rTransition
305  * Reference to transition object
306  * @return
307  * Insertion position
308  *
309  */
310  Iterator Inject(const Iterator& pos, const Transition& rTransition);
311 
312  /**
313  * Add a Transition.
314  *
315  * @param rTransition
316  * Reference to transition object
317  * @return
318  * Insertion position
319  *
320  */
321  void Inject(const Transition& rTransition);
322 
323  /**
324  * Remove a Transition
325  *
326  * @return
327  * True if transition did exist
328  */
329  bool Erase(const Transition& t);
330 
331  /**
332  * Remove a Transition by x1, ev, x2
333  *
334  * @return
335  * True if transition did exist
336  */
337  bool Erase(Idx x1, Idx ev, Idx x2);
338 
339  /**
340  * Remove a Transition by iterator
341  *
342  * @return
343  * Iterator to next transition
344  */
345  Iterator Erase(const Iterator& it);
346 
347  /**
348  * Remove all transitions containing predecessor state x1.
349  *
350  * @param x1
351  * State index
352  * @exception Exception
353  * - sorting mismatch (id 68)
354  */
355  void EraseByX1(Idx x1);
356 
357  /**
358  * Remove all transitions containing predecessor state x1 and event ev.
359  *
360  * @param x1
361  * State index
362  * @param ev
363  * Event index
364  * @exception Exception
365  * - sorting mismatch (id 68)
366  */
367  void EraseByX1Ev(Idx x1, Idx ev);
368 
369  /**
370  * Remove all transitions containing successor state x2.
371  * This function iterates over all transitions to work with any sorting.
372  *
373  * @param x2
374  * State index
375  */
376  void EraseByX2(Idx x2);
377 
378  /**
379  * Remove all transitions containing event ev.
380  * This function iterates over all transitions to work with any sorting.
381  *
382  * @param ev
383  * Event index
384  */
385  void EraseByEv(Idx ev);
386 
387  /**
388  * Remove all transitions containing state x,
389  * This function iterates over all transitions to work with any sorting.
390  *
391  * @param x
392  * State index
393  */
394  void EraseByX1OrX2(Idx x);
395 
396  /**
397  * Remove all transitions containing a specified state.
398  * This function iterates over all transitions to work with any sorting.
399  *
400  * @param rStates
401  * Set of states to remore
402  */
403  void EraseByX1OrX2(const StateSet& rStates);
404 
405  /**
406  * Restrict to transitions with states as specified.
407  * Erases any transition with X1 or X2 not in the specified state set.
408  *
409  * @param rStateSet
410  * Set of states to keep.
411  */
412  void RestrictStates(const StateSet& rStateSet);
413 
414  /**
415  * Restrict to transitions with events as specified.
416  * Erases any transition with event not in the specified state set.
417  *
418  * @param rEventSet
419  * Set of events to keep.
420  */
421  void RestrictEvents(const EventSet& rEventSet);
422 
423  /**
424  * Find transition given by x1, ev, x2
425  *
426  *
427  * @param x1
428  * Predecessor state
429  * @param ev
430  * Event
431  * @param x2
432  * Successor state
433  *
434  * @return
435  * Iterator to transition or End() if not exists
436  */
437  Iterator Find(Idx x1, Idx ev, Idx x2) const;
438 
439  /**
440  * Find specified transition
441  *
442  *
443  * @param t
444  * Transition
445  *
446  * @return
447  * Iterator to transition or End() if not exists
448  */
449  Iterator Find(const Transition& t) const;
450 
451 
452  /**
453  * Test existence
454  *
455  * @param t
456  * Transition
457  *
458  * @return
459  * bool
460  */
461  bool Exists(const Transition& t) const;
462 
463  /**
464  * Test existence
465  *
466  * @param x1
467  * Predecessor state Idx
468  * @param ev
469  * Event Idx
470  * @param x2
471  * Successor state Idx
472  *
473  * @return
474  * bool
475  */
476  bool Exists(Idx x1, Idx ev, Idx x2) const;
477 
478  /**
479  * Test existence
480  *
481  * @param x1
482  * Predecessor state Idx
483  * @param ev
484  * Event Idx
485  *
486  * @return
487  * bool
488  */
489  bool ExistsByX1Ev(Idx x1, Idx ev) const;
490 
491  /**
492  * Test existence
493  *
494  * @param x1
495  * Predecessor state Idx
496  * @param ev
497  * Event Idx
498  *
499  * @return
500  * bool
501  */
502  bool ExistsByX1(Idx x1) const;
503 
504  /**
505  * Tests if a transition with specified predecessor or successor state
506  * exists.
507  *
508  * @param x
509  * State Idx
510  *
511  * @return
512  * bool
513  */
514  bool ExistsByX1OrX2(Idx x) const;
515 
516 
517 
518  /** @} doxygen group */
519 
520  /** @name Transition iterators
521  *
522  * A variaty of iterators are provided to examine specified
523  * segments of the transition relation, e.g. all transitions starting
524  * from a given state. Note that implemetation of these iterators
525  * requires the transition set to be of sorted accordingly. Eg.
526  * scanning all transitions that are labled with a particular event
527  * requires a sorting TransSOrt::EvX1X2 orT ransSOrt::EvX2X1.
528  *
529  *
530  */
531 
532  /** @{ doxygen group: iterators */
533 
534  /**
535  * Iterator to begin of set
536  *
537  * @return
538  * TTransSet<Cmp>::Iterator
539  */
540  Iterator Begin(void) const;
541 
542  /**
543  * Iterator to end of set
544  *
545  * @return
546  * TTransSet<Cmp>::Iterator
547  */
548  Iterator End(void) const;
549 
550 
551  /**
552  * Iterator to first Transition specified by current state.
553  *
554  * @param x1
555  * Predecessor state index
556  *
557  * @return
558  * TTransSet<Cmp>::Iterator
559  *
560  * @exception Exception
561  * - Sorting mismatch (id 68)
562  */
563  Iterator Begin(Idx x1) const;
564 
565  /**
566  * Iterator to end or Transitions with specified current state.
567  *
568  * @param x1
569  * Predecessor state index
570  *
571  * @return
572  * TTransSet<Cmp>::Iterator
573  *
574  * @exception Exception
575  * - Sorting mismatch (id 68)
576  */
577  Iterator End(Idx x1) const;
578 
579  /**
580  * Iterator to first Transitions specified by current state and event.
581  *
582  * @param x1
583  * Predecessor state index
584  * @param ev
585  * Event index
586  *
587  * @return
588  * TTransSet<Cmp>::Iterator
589  *
590  * @exception Exception
591  * - Sorting mismatch (id 68)
592  */
593  Iterator Begin(Idx x1, Idx ev) const;
594 
595  /**
596  * Iterator to first Transition after spcified current state and event.
597  *
598  * @param x1
599  * Predecessor state index
600  * @param ev
601  * Event index
602  *
603  * @return
604  * TTransSet<Cmp>::Iterator
605  *
606  * @exception Exception
607  * - sorting mismatch (id 68)
608  */
609  Iterator End(Idx x1, Idx ev) const;
610 
611  /**
612  * Iterator to first Transition specified by event.
613  * This function requires sorting TransSort::EvX1X2 or TransSort::EvX2X1.
614  *
615  * @param ev
616  * Event index
617  *
618  * @return
619  * TTransSet<Cmp>::iterator
620  *
621  * @exception Exception
622  * - sorting mismatch (id 68)
623  */
624  Iterator BeginByEv(Idx ev) const;
625 
626  /**
627  * Iterator to first Transition after specified by event.
628  * This function requires sorting TransSort::EvX1X2 or TransSort::EvX2X1
629  *
630  * @param ev
631  * Predecessor state index
632  *
633  * @return
634  * TTransSet<Cmp>::Iterator
635  *
636  * @exception Exception
637  * - sorting mismatch (id 68)
638  */
639  Iterator EndByEv(Idx ev) const;
640 
641  /**
642  * Iterator to first Transition specified by event and current state.
643  * This function requires sorting TransSort::EvX1X2.
644  *
645  * @param ev
646  * Event index
647  * @param x1
648  * Predecessor state index
649  *
650  * @return
651  * TTransSet<Cmp>::iterator
652  *
653  * @exception Exception
654  * - sorting mismatch (id 68)
655  */
656  Iterator BeginByEvX1(Idx ev, Idx x1) const;
657 
658  /**
659  * Iterator to first Transition after specified ev and current state.
660  * This function requires sorting TransSort::EvX1X2.
661  *
662  * @param ev
663  * Event index
664  * @param x1
665  * Predecessor state index
666  *
667  * @return
668  * TTransSet<Cmp>::Iterator
669  *
670  * @exception Exception
671  * - sorting mismatch (id 68)
672  */
673  Iterator EndByEvX1(Idx ev, Idx x1) const;
674 
675  /**
676  * Iterator to first Transition specified by event and next state.
677  * This function requires sorting TransSort::EvX2X1.
678  *
679  * @param ev
680  * Event index
681  * @param x2
682  * Predecessor state index
683  *
684  * @return
685  * TTransSet<Cmp>::Iterator
686  *
687  * @exception Exception
688  * - sorting mismatch (id 68)
689  */
690  Iterator BeginByEvX2(Idx ev, Idx x2) const;
691 
692  /**
693  * Iterator to first Transition after specified event and next state.
694  * This function requires sorting TransSort::EvX2X1.
695  *
696  * @param ev
697  * Event index
698  * @param x2
699  * Predecessor state index
700  *
701  * @return
702  * TTransSet<Cmp>::Iterator
703  *
704  * @exception Exception
705  * - sorting mismatch (id 68)
706  */
707  Iterator EndByEvX2(Idx ev, Idx x2) const;
708 
709  /**
710  * Iterator to first Transition specified by successor state x2.
711  * This function requires sorting TransSort::X2EvX1 or TransSort::X2X1Ev.
712  *
713  * @param x2
714  * Predecessor state index
715  *
716  * @return
717  * TTransSet<Cmp>::iterator
718  *
719  * @exception Exception
720  * - sorting mismatch (id 68)
721  */
722  Iterator BeginByX2(Idx x2) const;
723 
724  /**
725  * Iterator to first Transition after specified successor state x2.
726  * This function requires sorting TransSort::X2EvX1 or TransSort::X2X1Ev
727  *
728  * @param x2
729  * Predecessor state index
730  *
731  * @return
732  * TTransSet<Cmp>::Iterator
733  *
734  * @exception Exception
735  * - sorting mismatch (id 68)
736  */
737  Iterator EndByX2(Idx x2) const;
738 
739  /**
740  * Iterator to first Transition specified by successor x2 and ev.
741  * This function requires sorting TransSort::X2EvX1.
742  *
743  * @param x2
744  * Predecessor state index
745  * @param ev
746  * Event index
747  *
748  * @return
749  * TTransSet<Cmp>::Iterator
750  *
751  * @exception Exception
752  * - sorting mismatch (id 68)
753  */
754  Iterator BeginByX2Ev(Idx x2, Idx ev) const;
755 
756  /**
757  * Iterator to first Transition after specified successor x2 and ev.
758  * This function requires sorting TransSort::X2EvX1.
759  *
760  * @param x2
761  * Predecessor state index
762  * @param ev
763  * Event index
764  *
765  * @return
766  * TTransSet<Cmp>::Iterator
767  *
768  * @exception Exception
769  * - sorting mismatch (id 68)
770  */
771  Iterator EndByX2Ev(Idx x2, Idx ev) const;
772 
773  /** @} doxygen group */
774 
775  /** @name Misc */
776  /** @{ doxygen group */
777 
778  /**
779  * Get copy of trantision relation sorted by other compare
780  * operator, e.g. TSort::X2EvX1
781  *
782  * @return
783  * Transition relation
784  */
785  template<class OtherCmp>
786  void ReSort(TTransSet<OtherCmp>& res) const;
787 
788  /**
789  * Get state set covered by transition set
790  *
791  * @return
792  * Set of state indices used by some transition
793  */
794  StateSet States(void) const;
795 
796  /**
797  * Get set of successor states for specified current state
798  *
799  * @param x1
800  * Current state
801  *
802  * @return
803  * Set of state indices
804  */
805  StateSet SuccessorStates(Idx x1) const;
806 
807  /**
808  * Get set of successor states for specified current states
809  *
810  * @param rX1Set
811  * Current state
812  *
813  * @return
814  * Set of state indices
815  */
816  StateSet SuccessorStates(const StateSet& rX1Set) const;
817 
818  /**
819  * Get set of successor states for specified current state and event
820  *
821  * @param x1
822  * Current state
823  * @param ev
824  * Event
825  *
826  * @return
827  * Set of state indices
828  */
829  StateSet SuccessorStates(Idx x1, Idx ev) const;
830 
831  /**
832  * Get set of successor states for specified current states and events
833  *
834  * @param rX1Set
835  * Current states
836  * @param rEvSet
837  * Events
838  *
839  * @return
840  * Set of state indices
841  */
842  StateSet SuccessorStates(const StateSet& rX1Set, const EventSet& rEvSet) const;
843 
844 
845  /**
846  * Get set of events that are active for a specified current state
847  * Since a transition set does not refer to a SymbolTable, this function
848  * returns a set of plain indices. In order to interpret the set as an EventSet,
849  * the relevant SymbolTable must be supplied as second argument. If obmitting the second
850  * argument, the defult SymbolTable is used.
851  *
852  * @param x1
853  * Current state
854  * @param pSymTab
855  * SymbolTable to refer to
856  *
857  * @return
858  * Set of events.
859  */
860  EventSet ActiveEvents(Idx x1, SymbolTable* pSymTab=NULL) const;
861 
862  /**
863  * Return pretty printable string representation.
864  * Primary meant for debugging messages.
865  *
866  * @param rTrans
867  * Transition to print
868  *
869  * @return
870  * String
871  */
872  std::string Str(const Transition& rTrans) const { return rTrans.Str();} ;
873 
874 
875  /** @} doxygen group */
876 
877  protected:
878 
879 
880  /**
881  * Assign my members.
882  *
883  * @param rSource
884  * Source to copy from
885  * @return
886  * Ref to this set
887  */
888  virtual void DoAssign(const TTransSet& rSource);
889 
890  /**
891  * Write to TokenWriter, see Type::Write for public wrappers.
892  *
893  * @param rTw
894  * Reference to TokenWriter
895  * @param rLabel
896  * Label of section to write, defaults to name of set
897  * @param pContext
898  * Write context eg symboltables
899  *
900  * @exception Exception
901  * - IO errors (id 2)
902  */
903 
904  virtual void DoWrite(TokenWriter& rTw, const std::string& rLabel="", const Type* pContext=0) const;
905 
906 
907 };
908 
909 /** Type definition for default sorted TTransSet */
911 
912 /** Type definition for default sorted TTransSet */
914 
915 /** Type definition for ev, x1, x2 sorted TTransSet */
917 
918 /** Type definition for ev, x2, x1 sorted TTransSet */
920 
921 /** Type definition for x2, ev, x1 sorted TTransSet */
923 
924 /** Type definition for x2, x1, ev sorted TTransSet */
926 
927 /** Type definition for x1, x2, ev sorted TTransSet */
929 
930 
931 /**
932  * Set of Transitions with attributes.
933  *
934  * This container class is derived from TTransSet to provide attributes as an
935  * additional feature. As with TaBaseSet, the template parameter specifies the attribute class,
936  * which in turn must provide some basic funtionality. In contrast to the TTransSet, the TaTransSet
937  * is restricted to standard ordering.
938  *
939  * Note that it is the context of a Generator that
940  * actually allows to interpret a TaTransSet as a set of transitions as opposed to
941  * a set of triples of indices with attributes. In particular, file IO of transitions is provided
942  * by the generator class (although TaTransSet provides output functions for debugging)
943  */
944 
945 
946 template <class Attr>
947 class TaTransSet : public TransSet, public TaBaseSet<Transition,Attr,TransSort::X1EvX2> {
948 
950 
951 public:
952 
953 
954  /** @name Constructors & Destructor */
955  /** @{ doxygen group */
956 
957  /** Construct an empty TaTransSet object */
958  TaTransSet(void);
959 
960  /**
961  * Copy-constructor (incl attributes)
962  */
963  TaTransSet(const TaTransSet& rOtherSet);
964 
965  /**
966  * Copy-Constructor (set attributes to default)
967  */
968  TaTransSet(const TTransSet<TransSort::X1EvX2>& rOtherSet);
969 
970  /** Virtual destructor */
971  virtual ~TaTransSet() {}
972 
973  /** Relaxed assignment method. Maintains attributes provided they can be casted.
974  *
975  * @param rSrc
976  * Source from which to assign
977  * @return
978  * Ref to this set
979  */
980  virtual TaTransSet& Assign(const TransSet& rSrc);
981 
982  /** Relaxed assignment operator. Maintains attributes provided they can be casted.
983  *
984  * @param rSrc
985  * Source from which to assign
986  * @return
987  * Ref to this set
988  */
989  virtual TaTransSet& operator=(const TransSet& rSrc);
990 
991  /** @} doxygen group */
992 
993 
994 
995  /** @name Accessing individual transitions */
996  /** @{ doxygen group */
997 
998  /** Clear all transitions incl attributes */
999  void Clear(void);
1000 
1001  /** Iterator on transition */
1003 
1004  /**
1005  * Add a Transition by indices
1006  *
1007  * @param x1
1008  * Predecessor state
1009  * @param ev
1010  * Event
1011  * @param x2
1012  * Successor state
1013  *
1014  * @return
1015  * True if the transition was new to the set
1016  */
1017  bool Insert(Idx x1, Idx ev, Idx x2);
1018 
1019  /**
1020  * Add a Transition directly. If the transition already
1021  * exists, the attribute is maintained. Otherwise, the transition
1022  * is inserted with default attribute.
1023  *
1024  * @param rTransition
1025  * Reference to transition object
1026  *
1027  * @return
1028  * True if the transition was new to the set
1029  */
1030  bool Insert(const Transition& rTransition);
1031 
1032  /**
1033  * Add a Transition with attribute.
1034  *
1035  * @param rTransition
1036  * Reference to transition object
1037  * @param rAttr
1038  * Reference to attribute
1039  *
1040  * @return
1041  * True if the transition was new to the set
1042  */
1043  bool Insert(const Transition& rTransition, const Attr& rAttr);
1044 
1045  /**
1046  * Inserts transitions of rOtherSet.
1047  * Attributes of this set are maintained, newly inserted transitions have default attribute.
1048  *
1049  *
1050  * @param rOtherSet
1051  * Other IndexSet
1052  */
1053  virtual void InsertSet(const TransSet& rOtherSet);
1054 
1055  /**
1056  * Inserts transitions of rOtherSet.
1057  * Attributes of this set are maintained, new transitions are inserted with attribute.
1058  *
1059  * @param rOtherSet
1060  * Other IndexSet
1061  */
1062  virtual void InsertSet(const TaTransSet& rOtherSet);
1063 
1064  /**
1065  * Remove a Transition
1066  *
1067  * @return
1068  * True if transition did exist
1069  */
1070  bool Erase(const Transition& t);
1071 
1072  /**
1073  * Remove a Transition
1074  *
1075  * @return
1076  * True if transition did exist
1077  */
1078  bool Erase(Idx x1, Idx ev, Idx x2);
1079 
1080  /**
1081  * Remove a Transition by iterator
1082  *
1083  * @return
1084  * Iterator to next transition
1085  */
1086  Iterator Erase(const Iterator& it);
1087 
1088  /**
1089  * Remove all transitions containing predecessor state x1.
1090  *
1091  * @param x1
1092  * State index
1093  */
1094  void EraseByX1(Idx x1);
1095 
1096  /**
1097  * Remove all transitions containing successor state x2.
1098  *
1099  * @param x2
1100  * State index
1101  */
1102  void EraseByX2(Idx x2);
1103 
1104  /**
1105  * Remove all transitions containing event ev.
1106  *
1107  * @param ev
1108  * Event index
1109  */
1110  void EraseByEv(Idx ev);
1111 
1112  /**
1113  * Remove all transitions containing state x.
1114  *
1115  * @param x
1116  * State index
1117  */
1118  void EraseByX1OrX2(Idx x);
1119 
1120  /**
1121  * Remove all transitions containing a specified state.
1122  * This function iterates over all transitions to work with any sorting.
1123  *
1124  * @param rStateSet
1125  * Set of states to remore
1126  */
1127  void EraseByX1OrX2(const StateSet& rStateSet);
1128 
1129 
1130  /**
1131  * Erase elements given by other set. This function
1132  * ignores the attributes of the other set and maintains the attributes
1133  * of the remaining elements in this set.
1134  *
1135  * @param rOtherSet
1136  * Elements to erase
1137  */
1138  void EraseSet(const TransSet& rOtherSet);
1139 
1140  /**
1141  * Restrict to specified subset. Erases any elements no in
1142  * the specified set. This function
1143  * ignores the attributes of the other set and maintains the attributes
1144  * of the remaining elements in this set.
1145  *
1146  * @param rOtherSet
1147  * Elements to erase
1148  */
1149  void RestrictSet(const TransSet& rOtherSet);
1150 
1151 
1152  /**
1153  * Restrict to transitions with states as specified.
1154  * Erases any transition with X1 or X2 not in the specified state set.
1155  *
1156  * @param rStateSet
1157  * Set of states to keep.
1158  */
1159  void RestrictStates(const StateSet& rStateSet);
1160 
1161  /**
1162  * Restrict to transitions with events as specified.
1163  * Erases any transition with event not in the specified state set.
1164  *
1165  * @param rEventSet
1166  * Set of events to keep.
1167  */
1168  void RestrictEvents(const EventSet& rEventSet);
1169 
1170 
1171  /**
1172  * Set attributes. Provided that rOtherSet has attributes that can be
1173  * casted to the appropriate type, attributes are copied per element from rOtherSet.
1174  * Elements of this set which are not in rOtherSet maintain their attribute.
1175  *
1176  * @param rOtherSet
1177  * Other IndexSet
1178  * @exception Exception
1179  * - Element does not exist (63)
1180  * - Cannot cast attribute type (63)
1181  */
1182  virtual void Attributes(const TransSet& rOtherSet);
1183 
1184  /**
1185  * Set attributes. Attributes are copied per element from rOtherSet.
1186  * Elements of this set which are not in rOtherSet maintain their attribute.
1187  *
1188  * @param rOtherSet
1189  * Other IndexSet
1190  */
1191  virtual void Attributes(const TaTransSet& rOtherSet);
1192 
1193 
1194  /** @} doxygen group */
1195 
1196 
1197  protected:
1198 
1199  /**
1200  * Assign my members. Maintain attributes.
1201  *
1202  * @param rSource
1203  * Source to copy from
1204  * @return
1205  * Ref to this set
1206  */
1207  virtual void DoAssign(const TaTransSet& rSource);
1208 
1209 };
1210 
1211 /** @} doxygen group*/
1212 
1213 
1214 /*
1215 *************************************************************************************************
1216 *************************************************************************************************
1217 * Implementation of transset without attributes
1218 *************************************************************************************************
1219 *************************************************************************************************
1220 */
1221 
1222 
1223 /* convenience access to relevant scopes */
1224 #define THIS TTransSet<Cmp>
1225 #define TEMP template<class Cmp>
1226 #define BASE TBaseSet<Transition,Cmp>
1227 
1228 // std faudes type
1230 
1231 // TTransSet(void)
1233 {
1234  FD_DC("TTransSet(" << this << ")::TTransSet()");
1235 }
1236 
1237 // TTransSet(othertransrel)
1238  TEMP THIS::TTransSet(const TBaseSet<Transition,Cmp>& rOtherSet) :
1239  BASE()
1240 {
1241  FD_DC("TTransSet(" << this << ")::TTransSet(rOtherSet "<< &rOtherSet <<")");
1242  Assign(rOtherSet);
1243 }
1244 
1245 // TTransSet(othertransrel othersort)
1246 TEMP template<class OtherCmp>
1247 THIS::TTransSet(const TTransSet<OtherCmp>& rOtherSet) :
1248  BASE()
1249 {
1250  FD_DC("TTransSet(" << this << ")::TTransSet(rOtherSet/ReSort "<< &rOtherSet <<")");
1251  rOtherSet.ReSort(*this);
1252 }
1253 
1254 // assignment (maintain/cast attributes)
1255 TEMP void THIS::DoAssign(const TTransSet& rSourceSet) {
1256  FD_DC("TTransSet(" << this << ")::DoAssign(..)");
1257  // call base; incl virtual clear incl attributes
1258  BASE::DoAssign(rSourceSet);
1259 }
1260 
1261 // iterator Begin() const
1262 TEMP typename THIS::Iterator THIS::Begin(void) const {
1263  return BASE::Begin();
1264 }
1265 
1266 // iterator End() const
1267 TEMP typename THIS::Iterator THIS::End(void) const {
1268  return BASE::End();
1269 }
1270 
1271 
1272 // Convenience macro for order typecheck
1273 #define SORT_EXCEPTION { std::stringstream errstr; \
1274  errstr << "Transition set order mismatch " << std::endl; \
1275  throw Exception("TransSet::Iterator()", errstr.str(), 68); }
1276 
1277 
1278 // iterator Begin(x1) const
1279 TEMP typename THIS::Iterator THIS::Begin(Idx x1) const {
1280 #ifdef FAUDES_CHECKED
1281  if(typeid(Cmp)!=typeid(TransSort::X1EvX2))
1282  if(typeid(Cmp)!=typeid(TransSort::X1X2Ev))
1284 #endif
1285  Transition tlx(x1,0,0);
1286  return THIS::ThisIterator(BASE::pSet->lower_bound(tlx));
1287 }
1288 
1289 // iterator End(x1) const
1290 TEMP typename THIS::Iterator THIS::End(Idx x1) const {
1291 #ifdef FAUDES_CHECKED
1292  if(typeid(Cmp)!=typeid(TransSort::X1EvX2))
1293  if(typeid(Cmp)!=typeid(TransSort::X1X2Ev))
1295 #endif
1296  Transition tlx(x1+1,0,0);
1297  return THIS::ThisIterator(BASE::pSet->lower_bound(tlx));
1298 }
1299 
1300 // iterator Begin(x1,ev) const
1301 TEMP typename THIS::Iterator THIS::Begin(Idx x1, Idx ev) const {
1302 #ifdef FAUDES_CHECKED
1303  if(typeid(Cmp)!=typeid(TransSort::X1EvX2))
1305 #endif
1306  Transition tlx(x1,ev,0);
1307  return THIS::ThisIterator(BASE::pSet->lower_bound(tlx));
1308 }
1309 
1310 // iterator End(x1,ev) const
1311 TEMP typename THIS::Iterator THIS::End(Idx x1, Idx ev) const {
1312 #ifdef FAUDES_CHECKED
1313  if(typeid(Cmp)!=typeid(TransSort::X1EvX2))
1315 #endif
1316  Transition tlx(x1,ev+1, 0);
1317  return THIS::ThisIterator(BASE::pSet->lower_bound(tlx));
1318 }
1319 
1320 // iterator BeginByEv(ev) const
1321 TEMP typename THIS::Iterator THIS::BeginByEv(Idx ev) const {
1322 #ifdef FAUDES_CHECKED
1323  if(typeid(Cmp)!=typeid(TransSort::EvX1X2))
1324  if(typeid(Cmp)!=typeid(TransSort::EvX2X1))
1326 #endif
1327  Transition tlx(0,ev,0);
1328  return THIS::ThisIterator(BASE::pSet->lower_bound(tlx));
1329 }
1330 
1331 // iterator EndByEv(ev) const
1332 TEMP typename THIS::Iterator THIS::EndByEv(Idx ev) const {
1333 #ifdef FAUDES_CHECKED
1334  if(typeid(Cmp)!=typeid(TransSort::EvX1X2))
1335  if(typeid(Cmp)!=typeid(TransSort::EvX2X1))
1337 #endif
1338  Transition tlx(0,ev+1,0);
1339  return THIS::ThisIterator(BASE::pSet->lower_bound(tlx));
1340 }
1341 
1342 // iterator BeginByEvX1(ev,x1) const
1343 TEMP typename THIS::Iterator THIS::BeginByEvX1(Idx ev, Idx x1) const {
1344 #ifdef FAUDES_CHECKED
1345  if(typeid(Cmp)!=typeid(TransSort::EvX1X2))
1347 #endif
1348  Transition tlx(x1,ev,0);
1349  return THIS::ThisIterator(BASE::pSet->lower_bound(tlx));
1350 }
1351 
1352 // iterator EndByEvX1(ev,x1) const
1353 TEMP typename THIS::Iterator THIS::EndByEvX1(Idx ev, Idx x1) const {
1354 #ifdef FAUDES_CHECKED
1355  if(typeid(Cmp)!=typeid(TransSort::EvX1X2))
1357 #endif
1358  Transition tlx(x1+1,ev,0);
1359  return THIS::ThisIterator(BASE::pSet->lower_bound(tlx));
1360 }
1361 
1362 // iterator BeginByEvX2(ev,x2) const
1363 TEMP typename THIS::Iterator THIS::BeginByEvX2(Idx ev, Idx x2) const {
1364 #ifdef FAUDES_CHECKED
1365  if(typeid(Cmp)!=typeid(TransSort::EvX2X1))
1367 #endif
1368  Transition tlx(0,ev,x2);
1369  return THIS::ThisIterator(BASE::pSet->lower_bound(tlx));
1370 }
1371 
1372 // iterator EndByEvX2(ev,x2) const
1373 TEMP typename THIS::Iterator THIS::EndByEvX2(Idx ev, Idx x2) const {
1374 #ifdef FAUDES_CHECKED
1375  if(typeid(Cmp)!=typeid(TransSort::EvX2X1))
1377 #endif
1378  Transition tlx(0,ev,x2+1);
1379  return THIS::ThisIterator(BASE::pSet->lower_bound(tlx));
1380 }
1381 
1382 // iterator BeginByX2(x2) const
1383 TEMP typename THIS::Iterator THIS::BeginByX2(Idx x2) const {
1384 #ifdef FAUDES_CHECKED
1385  if(typeid(Cmp)!=typeid(TransSort::X2EvX1))
1386  if(typeid(Cmp)!=typeid(TransSort::X2X1Ev))
1388 #endif
1389  Transition tlx(0,0,x2);
1390  return THIS::ThisIterator(BASE::pSet->lower_bound(tlx));
1391 }
1392 
1393 // iterator EndByX2(x2) const
1394 TEMP typename THIS::Iterator THIS::EndByX2(Idx x2) const {
1395 #ifdef FAUDES_CHECKED
1396  if(typeid(Cmp)!=typeid(TransSort::X2EvX1))
1397  if(typeid(Cmp)!=typeid(TransSort::X2X1Ev))
1399 #endif
1400  Transition tlx(0,0,x2+1);
1401  return THIS::ThisIterator(BASE::pSet->lower_bound(tlx));
1402 }
1403 
1404 // iterator BeginByX2Ev(x2,ev) const
1405 TEMP typename THIS::Iterator THIS::BeginByX2Ev(Idx x2, Idx ev) const {
1406 #ifdef FAUDES_CHECKED
1407  if(typeid(Cmp)!=typeid(TransSort::X2EvX1))
1409 #endif
1410  Transition tlx(0,ev,x2);
1411  return THIS::ThisIterator(BASE::pSet->lower_bound(tlx));
1412 }
1413 
1414 // iterator EndByX2Ev(x2,ev) const
1415 TEMP typename THIS::Iterator THIS::EndByX2Ev(Idx x2, Idx ev) const {
1416 #ifdef FAUDES_CHECKED
1417  if(typeid(Cmp)!=typeid(TransSort::X2EvX1))
1419 #endif
1420  Transition tlx(0,ev+1,x2);
1421  return THIS::ThisIterator(BASE::pSet->lower_bound(tlx));
1422 }
1423 
1424 
1425 
1426 // DoWrite(rw,label)
1427 TEMP void THIS::DoWrite(TokenWriter& rTw, const std::string& rLabel, const Type* pContext) const
1428 {
1429  (void) pContext;
1430  std::string label=rLabel;
1431  if(label=="") label=BASE::Name();
1432  rTw.WriteBegin(label);
1433  int oldcolumns = rTw.Columns();
1434  rTw.Columns(3);
1435 
1436  Iterator tit;
1437  for (tit = Begin(); tit != End(); ++tit) {
1438  rTw << tit->X1; rTw << tit->Ev; rTw << tit->X2;
1439  }
1440 
1441  rTw.WriteEnd(label);
1442  rTw.Columns(oldcolumns);
1443 }
1444 
1445 
1446 // Insert(transition)
1447 TEMP bool THIS::Insert(const Transition& t) {
1448  return BASE::Insert(t);
1449 }
1450 
1451 // Insert(x1,ev,x2)
1452 TEMP bool THIS::Insert(Idx x1, Idx ev, Idx x2) {
1453  FD_DC("TTransSet(" << this << ")::Insert(" << x1 << "-" << ev << "-" << x2 << ")");
1454  return BASE::Insert(Transition(x1, ev, x2));
1455 }
1456 
1457 // Inject(transition)
1458 TEMP typename THIS::Iterator THIS::Inject(const Iterator& pos, const Transition& t) {
1459  return BASE::Inject(pos,t);
1460 }
1461 
1462 // Inject(transition)
1463 TEMP void THIS::Inject(const Transition& t) {
1464  BASE::Inject(t);
1465 }
1466 
1467 
1468 // Erase(transition)
1469 TEMP bool THIS::Erase(const Transition& t) {
1470  return BASE::Erase(t);
1471 }
1472 
1473 // Erase(x1,ev,x2)
1474 TEMP bool THIS::Erase(Idx x1, Idx ev, Idx x2) {
1475  FD_DC("TTransSet(" << this << ")::Erase(" << x1 << "-" << ev << "-" << x2 << ")");
1476  return BASE::Erase(Transition(x1, ev, x2));
1477 }
1478 
1479 // Erase(it)
1480 TEMP typename THIS::Iterator THIS::Erase(const Iterator& it) {
1481  FD_DC("TTransSet(" << this << ")::Erase(" << it->X1 << "-" << it->Ev
1482  << "-" << it->X2 << ")");
1483  return BASE::Erase(it);
1484 }
1485 
1486 // EraseByX1(x)
1487 TEMP void THIS::EraseByX1(Idx x1) {
1488  FD_DC("TTransSet(" << this << ")::EraseByX1(" << x1 << ")");
1489 #ifdef FAUDES_CHECKED
1490  if(typeid(Cmp)!=typeid(TransSort::X1EvX2))
1491  if(typeid(Cmp)!=typeid(TransSort::X1X2Ev))
1493 #endif
1494  this->Detach();
1495  typename BASE::iterator lower, upper, it;
1496  Transition tl(x1,0,0);
1497  Transition tu(x1+1,0,0);
1498  lower = BASE::pSet->lower_bound(tl);
1499  upper = BASE::pSet->upper_bound(tu);
1500  BASE::pSet->erase(lower, upper);
1501 }
1502 
1503 // EraseByX1Ev(x,e)
1504 TEMP void THIS::EraseByX1Ev(Idx x1, Idx ev) {
1505  FD_DC("TTransSet(" << this << ")::EraseByX1Ev(" << x1 << "," << ev << ")");
1506 #ifdef FAUDES_CHECKED
1507  if(typeid(Cmp)!=typeid(TransSort::X1EvX2))
1509 #endif
1510  this->Detach();
1511  typename BASE::iterator lower, upper, it;
1512  Transition tl(x1,ev,0);
1513  Transition tu(x1,ev+1,0);
1514  lower = BASE::pSet->lower_bound(tl);
1515  upper = BASE::pSet->upper_bound(tu);
1516  BASE::pSet->erase(lower, upper);
1517 }
1518 
1519 // EraseByX2(x)
1520 TEMP void THIS::EraseByX2(Idx x2) {
1521  FD_DC("TTransSet(" << this << ")::EraseByX2(" << x2 << ")");
1522  this->Detach();
1523  typename BASE::iterator it, tmpit;
1524  for(it = BASE::pSet->begin(); it != BASE::pSet->end();) {
1525  if (it->X2 == x2) {
1526  tmpit = it;
1527  it++;
1528  BASE::pSet->erase(tmpit);
1529  continue;
1530  }
1531  it++;
1532  }
1533 }
1534 
1535 // EraseByEv(ev)
1536 TEMP void THIS::EraseByEv(Idx ev) {
1537  FD_DC("TTransSet(" << this << ")::EraseByEv(" << ev << ")");
1538  this->Detach();
1539  typename BASE::iterator it, tmpit;
1540  for(it = BASE::pSet->begin(); it != BASE::pSet->end();) {
1541  if (it->Ev == ev) {
1542  tmpit = it;
1543  it++;
1544  BASE::pSet->erase(tmpit);
1545  continue;
1546  }
1547  it++;
1548  }
1549 }
1550 
1551 
1552 
1553 // EraseByX1OrX2(x)
1554 TEMP void THIS::EraseByX1OrX2(Idx x) {
1555  FD_DC("TTransSet(" << this << ")::EraseByX1OrX2(" << x << ")");
1556  this->Detach();
1557  typename BASE::iterator it, tmpit;
1558  for(it = BASE::pSet->begin(); it != BASE::pSet->end();) {
1559  if ((it->X1 == x) || (it->X2 == x)) {
1560  tmpit = it;
1561  it++;
1562  BASE::pSet->erase(tmpit);
1563  continue;
1564  }
1565  it++;
1566  }
1567  FD_DC("TTransSet(" << this << ")::EraseByX1OrX2(" << x << "): done");
1568 }
1569 
1570 
1571 // EraseByX1OrX2(xset)
1572 TEMP void THIS::EraseByX1OrX2(const StateSet& rStates) {
1573  FD_DC("TTransSet(" << this << ")::EraseByX1OrX2(#" << rStates.Size() <<")");
1574  this->Detach();
1575  typename BASE::iterator it=BASE::pSet->begin();
1576  while(it != BASE::pSet->end()) {
1577  if(!rStates.Exists(it->X1) && !rStates.Exists(it->X2)) { ++it; continue;}
1578  BASE::pSet->erase(it++);
1579  }
1580  FD_DC("TTransSet(" << this << ")::EraseByX1OrX2(): done");
1581 }
1582 
1583 
1584 // RestrictStates(xset)
1585 TEMP void THIS::RestrictStates(const StateSet& rStates) {
1586  FD_DC("TTransSet(" << this << ")::RestrictByX1AndX2(#" << rStates.Size() <<")");
1587  this->Detach();
1588  typename BASE::iterator it;
1589  it = BASE::pSet->begin();
1590  while(it != BASE::pSet->end()) {
1591  if(rStates.Exists(it->X1) && rStates.Exists(it->X2)) { ++it; continue;}
1592  BASE::pSet->erase(it++);
1593  }
1594  FD_DC("TTransSet(" << this << ")::EraseByX1OrX2(): done");
1595 }
1596 
1597 
1598 // RestrictEvents(eset)
1599 TEMP void THIS::RestrictEvents(const EventSet& rEvents) {
1600  FD_DC("TTransSet(" << this << ")::RestrictEvents(#" << rEvents.Size() <<")");
1601  this->Detach();
1602  typename BASE::iterator it;
1603  it = BASE::pSet->begin();
1604  while(it != BASE::pSet->end()) {
1605  if(rEvents.Exists(it->Ev)) { ++it; continue;}
1606  BASE::pSet->erase(it++);
1607  }
1608  FD_DC("TTransSet(" << this << ")::RestrictEvents(): done");
1609 }
1610 
1611 
1612 // iterator Find(x1,ev,x2)
1613 TEMP typename THIS::Iterator THIS::Find(Idx x1, Idx ev, Idx x2) const {
1614  return BASE::Find(Transition(x1,ev,x2));
1615 }
1616 
1617 
1618 // iterator Find(t)
1619 TEMP typename THIS::Iterator THIS::Find(const Transition& t) const{
1620  return BASE::Find(t);
1621 }
1622 
1623 // Exists(t)
1624 TEMP bool THIS::Exists(const Transition& t) const {
1625  return BASE::Exists(t);
1626 }
1627 
1628 // Exists(x1, ev, x2)
1629 TEMP bool THIS::Exists(Idx x1, Idx ev, Idx x2) const {
1630  return BASE::Exists(Transition(x1,ev,x2));
1631 }
1632 
1633 // Exists(x)
1634 TEMP bool THIS::ExistsByX1OrX2(Idx x) const {
1635  typename BASE::iterator it;
1636  for(it = BASE::pSet->begin(); it != BASE::pSet->end(); ++it) {
1637  if ((it->X1 == x) || (it->X2 == x)) {
1638  return true;
1639  }
1640  }
1641  return false;
1642 }
1643 
1644 // ExistsByX1Ev(x,e)
1645 TEMP bool THIS::ExistsByX1Ev(Idx x1, Idx ev) const {
1646  FD_DC("TTransSet(" << this << ")::ExistsByX1Ev(" << x1 << "," << ev << ")");
1647 #ifdef FAUDES_CHECKED
1648  if(typeid(Cmp)!=typeid(TransSort::X1EvX2))
1650 #endif
1651  this->Detach();
1652  typename BASE::iterator lower, upper, it;
1653  Transition tl(x1,ev,0);
1654  Transition tu(x1,ev+1,0);
1655  lower = BASE::pSet->lower_bound(tl);
1656  upper = BASE::pSet->upper_bound(tu);
1657  return lower != upper;
1658 }
1659 
1660 // ExistsByX1(x)
1661 TEMP bool THIS::ExistsByX1(Idx x1) const {
1662  FD_DC("TTransSet(" << this << ")::ExistsByX1(" << x1 << ")");
1663 #ifdef FAUDES_CHECKED
1664  if(typeid(Cmp)!=typeid(TransSort::X1EvX2))
1665  if(typeid(Cmp)!=typeid(TransSort::X1X2Ev))
1667 #endif
1668  this->Detach();
1669  typename BASE::iterator lower, upper, it;
1670  Transition tl(x1,0,0);
1671  Transition tu(x1+1,0,0);
1672  lower = BASE::pSet->lower_bound(tl);
1673  upper = BASE::pSet->upper_bound(tu);
1674  return lower != upper;
1675 }
1676 
1677 
1678 // ReSort(res)
1679 TEMP template<class OtherCmp>
1680 void THIS::ReSort(TTransSet<OtherCmp>& res) const {
1681  Iterator it;
1682  res.Clear();
1683  for (it = Begin(); it != End(); ++it) {
1684  res.Insert(*it);
1685  }
1686 }
1687 
1688 // States()
1689 TEMP StateSet THIS::States(void) const {
1690  StateSet states;
1691  Iterator it;
1692  for (it=Begin(); it!=End(); ++it) {
1693  states.Insert(it->X1);
1694  states.Insert(it->X2);
1695  }
1696  return states;
1697 }
1698 
1699 // SuccessorStates(x1)
1700 TEMP StateSet THIS::SuccessorStates(Idx x1) const {
1701 #ifdef FAUDES_CHECKED
1702  if(typeid(Cmp)!=typeid(TransSort::X1EvX2))
1703  if(typeid(Cmp)!=typeid(TransSort::X1X2Ev))
1705 #endif
1706  StateSet states;
1707  Iterator it = Begin(x1);
1708  Iterator it_end = End(x1);
1709  while (it != it_end) {
1710  states.Insert(it->X2);
1711  ++it;
1712  }
1713  return states;
1714 }
1715 
1716 // SuccessorStates(x1set)
1717 TEMP StateSet THIS::SuccessorStates(const StateSet& rX1Set) const {
1718 #ifdef FAUDES_CHECKED
1719  if(typeid(Cmp)!=typeid(TransSort::X1EvX2))
1720  if(typeid(Cmp)!=typeid(TransSort::X1X2Ev))
1722 #endif
1723  StateSet states;
1724  StateSet::Iterator sit= rX1Set.Begin();
1725  StateSet::Iterator sit_end= rX1Set.End();
1726  for(;sit!=sit_end; ++sit) {
1727  Iterator tit = Begin(*sit);
1728  Iterator tit_end = End(*sit);
1729  while(tit!=tit_end) {
1730  states.Insert(tit->X2);
1731  ++tit;
1732  }
1733  }
1734  return states;
1735 }
1736 
1737 // SuccessorStates(x1, ev)
1738 TEMP StateSet THIS::SuccessorStates(Idx x1, Idx ev) const {
1739 #ifdef FAUDES_CHECKED
1740  if(typeid(Cmp)!=typeid(TransSort::X1EvX2))
1742 #endif
1743  StateSet states;
1744  Iterator it = Begin(x1, ev);
1745  Iterator it_end = End(x1, ev);
1746  while (it != it_end) {
1747  states.Insert(it->X2);
1748  ++it;
1749  }
1750  return states;
1751 }
1752 
1753 // SuccessorStates(x1set, evset)
1754 TEMP StateSet THIS::SuccessorStates(const StateSet& rX1Set, const EventSet& rEvSet) const {
1755 #ifdef FAUDES_CHECKED
1756  if(typeid(Cmp)!=typeid(TransSort::X1EvX2))
1758 #endif
1759  StateSet states;
1760  if(rEvSet.Empty()) return states;
1761  StateSet::Iterator sit= rX1Set.Begin();
1762  StateSet::Iterator sit_end= rX1Set.End();
1763  for(;sit!=sit_end; ++sit) {
1764  EventSet::Iterator eit= rEvSet.Begin();
1765  EventSet::Iterator eit_end= rEvSet.End();
1766  Iterator tit = Begin(*sit,*eit);
1767  Iterator tit_end = End(*sit);
1768  while(tit!=tit_end) {
1769  // match
1770  if(tit->Ev == *eit) {
1771  states.Insert(tit->X2);
1772  ++tit;
1773  continue;
1774  }
1775  // tit behind
1776  if(tit->Ev < *eit) {
1777  ++tit;
1778  continue;
1779  }
1780  // tit upfront
1781  ++eit;
1782  if(eit==eit_end) break;
1783  }
1784  }
1785  return states;
1786 }
1787 
1788 // ActiveEvents(x1,pSymTab)
1789 TEMP EventSet THIS::ActiveEvents(Idx x1, SymbolTable* pSymTab) const {
1790  Iterator it = Begin(x1);
1791  Iterator it_end = End(x1);
1792  EventSet result;
1793  if(pSymTab!=NULL) result.SymbolTablep(pSymTab);
1794  for (; it != it_end; ++it) {
1795  result.Insert(it->Ev);
1796  }
1797  return result;
1798 }
1799 
1800 
1801 #undef THIS
1802 #undef TEMP
1803 #undef BASE
1804 
1805 /*
1806 *************************************************************************************************
1807 *************************************************************************************************
1808 * Implementation of transset with attributes
1809 *************************************************************************************************
1810 *************************************************************************************************
1811 */
1812 
1813 
1814 /* convenience access to relevant scopes */
1815 #define THIS TaTransSet<Attr>
1816 #define TEMP template <class Attr>
1817 #define BASE TTransSet<TransSort::X1EvX2>
1818 #define ABASE TaBaseSet<Transition,Attr,TransSort::X1EvX2>
1819 #define VBASE TBaseSet<Transition,TransSort::X1EvX2>
1820 
1821 // std faudes type
1823 
1824 // TaTransSet(void)
1826  VBASE(),
1827  BASE(),
1828  ABASE()
1829 {
1830  FD_DC("TaTransSet(" << this << ")::TaTransSet()");
1831 }
1832 
1833 // TaTransSet(othertransrel)
1834 TEMP THIS::TaTransSet(const TaTransSet& rOtherSet) :
1835  VBASE(),
1836  BASE(),
1837  ABASE()
1838 {
1839  FD_DC("TaTransSet(" << this << ")::TaTransSet(rOtherSet "<< &rOtherSet <<")");
1840  DoAssign(rOtherSet);
1841 }
1842 
1843 
1844 // TaTransSet(othertransrel)
1845 TEMP THIS::TaTransSet(const BASE& rOtherSet) :
1846  VBASE(),
1847  BASE(),
1848  ABASE()
1849 {
1850  FD_DC("TaTransSet(" << this << ")::TaTransSet(rOtherSet "<< &rOtherSet <<")");
1851  Assign(rOtherSet);
1852 }
1853 
1854 
1855 // copy to known same attributes
1856 TEMP void THIS::DoAssign(const THIS& rSourceSet) {
1857  // call base incl attributes
1858  ABASE::DoAssign(rSourceSet);
1859 }
1860 
1861 // Relaxed Assign()
1862 TEMP THIS& THIS::Assign(const TransSet& rSourceSet) {
1863  FD_DC("TaTransSet(" << this << ")::Assign([v] " << &rSourceSet<<")");
1864  FD_DC("TaTransSet(" << this << ")::Assign(): src type " << typeid(rSourceSet).name());
1865  FD_DC("TaTransSet(" << this << ")::Assign(): dst type " << typeid(*this).name());
1866  // call attribute smart base
1867  ABASE::Assign(rSourceSet);
1868  // done
1869  return *this;
1870 }
1871 
1872 // Relaxed Assignment Operator(rSet)
1873 TEMP THIS& THIS::operator=(const TransSet& rSourceSet) {
1874  return Assign(rSourceSet);
1875 }
1876 
1877 // Clear()
1878 TEMP void THIS::Clear(void) {
1879  FD_DC("TaTransSet(" << this << ")::Clear()");
1880  ABASE::Clear();
1881 }
1882 
1883 // Insert(transition)
1884 TEMP bool THIS::Insert(const Transition& t) {
1885  FD_DC("TaTransSet(" << this << ")::Insert(" << t.Str() << ")");
1886  return ABASE::Insert(t);
1887 }
1888 
1889 // Insert(x1,ev,x2)
1890 TEMP bool THIS::Insert(Idx x1, Idx ev, Idx x2) {
1891  FD_DC("TaTransSet(" << this << ")::Insert(" << x1 << "-" << ev << "-" << x2 << ")");
1892  Transition t(x1, ev, x2);
1893  return ABASE::Insert(t);
1894 }
1895 
1896 // Insert(transition,attr)
1897 TEMP bool THIS::Insert(const Transition& t, const Attr& attr) {
1898  return ABASE::Insert(t,attr);
1899 }
1900 
1901 // InsertSet(set)
1902 TEMP void THIS::InsertSet(const TaTransSet& rOtherSet) {
1903  FD_DC("TaIndexSet(" << this << ")::InsertSet( [v] " << &rOtherSet << ")");
1904  ABASE::InsertSet(rOtherSet);
1905 }
1906 
1907 // InsertSet(set)
1908 TEMP void THIS::InsertSet(const TransSet& rOtherSet) {
1909  FD_DC("TaIndexSet(" << this << ")::InsertSet( [a] " << &rOtherSet << ")");
1910  ABASE::InsertSet(rOtherSet);
1911 }
1912 
1913 
1914 // Erase(transition)
1915 TEMP bool THIS::Erase(const Transition& t) {
1916  return ABASE::Erase(t);
1917 }
1918 
1919 // Erase(x1,ev,x2)
1920 TEMP bool THIS::Erase(Idx x1, Idx ev, Idx x2) {
1921  FD_DC("TaTransSet(" << this << ")::Erase(" << x1 << "-" << ev << "-" << x2 << ")");
1922  Transition t(x1, ev, x2);
1923  return ABASE::Erase(t);
1924 }
1925 
1926 // Erase(it)
1927 TEMP typename THIS::Iterator THIS::Erase(const Iterator& it) {
1928 #ifdef FAUDES_CHECKED
1929  if (it == End()) {
1930  std::stringstream errstr;
1931  errstr << "iterator out of range " << std::endl;
1932  throw Exception("TTransSet::Erase", errstr.str(), 69);
1933  }
1934 #endif
1935  return ABASE::Erase(it);
1936 }
1937 
1938 // EraseByX1(x)
1939 TEMP void THIS::EraseByX1(Idx x1) {
1940  FD_DC("TaTransSet(" << this << ")::EraseByX1(" << x1 << ")");
1941  this->Detach();
1942  Transition tl(x1,0,0);
1943  Transition tu(x1+1,0,0);
1944  BASE::iterator lower = BASE::pSet->lower_bound(tl);
1945  BASE::iterator upper = BASE::pSet->upper_bound(tu);
1946  BASE::iterator it;
1947  if(this->AttributeSize()!=0)
1948  for(it=lower; it!=upper; ++it)
1949  ABASE::ClrAttribute(*it);
1950  BASE::pSet->erase(lower, upper);
1951 }
1952 
1953 // EraseByX2(x)
1954 TEMP void THIS::EraseByX2(Idx x2) {
1955  FD_DC("TaTransSet(" << this << ")::EraseByX2(" << x2 << ")");
1956  this->Detach();
1957  BASE::iterator it, tmpit;
1958  for(it = BASE::pSet->begin(); it !=BASE::pSet->end();) {
1959  if (it->X2 == x2) {
1960  tmpit = it;
1961  it++;
1962  ABASE::ClrAttribute(*tmpit);
1963  BASE::pSet->erase(tmpit);
1964  continue;
1965  }
1966  it++;
1967  }
1968 }
1969 
1970 // EraseByEv(ev)
1971 TEMP void THIS::EraseByEv(Idx ev) {
1972  FD_DC("TaTransSet(" << this << ")::EraseByEv(" << ev << ")");
1973  this->Detach();
1974  BASE::iterator it, tmpit;
1975  for(it = BASE::pSet->begin(); it !=BASE::pSet->end();) {
1976  if (it->Ev == ev) {
1977  tmpit = it;
1978  it++;
1979  ABASE::ClrAttribute(*tmpit);
1980  BASE::pSet->erase(tmpit);
1981  continue;
1982  }
1983  it++;
1984  }
1985 }
1986 
1987 
1988 // EraseByX1OrX2(x)
1989 TEMP void THIS::EraseByX1OrX2(Idx x) {
1990  FD_DC("TaTransSet(" << this << ")::EraseByX1OrX2(" << x << ")");
1991  this->Detach();
1992  BASE::iterator it, tmpit;
1993  for(it = BASE::pSet->begin(); it !=BASE::pSet->end();) {
1994  if ((it->X1 == x) || (it->X2 == x)) {
1995  tmpit = it;
1996  it++;
1997  ABASE::ClrAttribute(*tmpit);
1998  BASE::pSet->erase(tmpit);
1999  continue;
2000  }
2001  it++;
2002  }
2003 }
2004 
2005 
2006 // EraseByX1OrX2(xset)
2007 TEMP void THIS::EraseByX1OrX2(const StateSet& rStates) {
2008  FD_DC("TaTransSet(" << this << ")::EraseByX1OrX2(#" << rStates.Size() <<")");
2009  this->Detach();
2010  typename BASE::iterator it = BASE::pSet->begin();
2011  while(it != BASE::pSet->end()) {
2012  if(!rStates.Exists(it->X1) && !rStates.Exists(it->X2)) { ++it; continue;}
2013  ABASE::ClrAttribute(*it);
2014  BASE::pSet->erase(it++);
2015  }
2016  FD_DC("TaTransSet(" << this << ")::EraseByX1OrX2(): done");
2017 }
2018 
2019 // RestrictStates(xset)
2020 TEMP void THIS::RestrictStates(const StateSet& rStates) {
2021  FD_DC("TaTransSet(" << this << ")::RestrictByX1AndX2(#" << rStates.Size() <<")");
2022  this->Detach();
2023  typename BASE::iterator it=BASE::pSet->begin();
2024  while(it != BASE::pSet->end()) {
2025  if(rStates.Exists(it->X1) && rStates.Exists(it->X2)) { ++it; continue;}
2026  ABASE::ClrAttribute(*it);
2027  BASE::pSet->erase(it++);
2028  }
2029  FD_DC("TaTransSet(" << this << ")::EraseByX1OrX2(): done");
2030 }
2031 
2032 // RestrictEvents(eset)
2033 TEMP void THIS::RestrictEvents(const EventSet& rEvents) {
2034  FD_DC("TaTransSet(" << this << ")::RestrictEvents(#" << rEvents.Size() <<")");
2035  this->Detach();
2036  typename BASE::iterator it;
2037  it = BASE::pSet->begin();
2038  while(it != BASE::pSet->end()) {
2039  if(rEvents.Exists(it->Ev)) { ++it; continue;}
2040  ABASE::ClrAttribute(*it);
2041  BASE::pSet->erase(it++);
2042  }
2043  FD_DC("TaTransSet(" << this << ")::RestrictEvents(): done");
2044 }
2045 
2046 
2047 
2048 
2049 
2050 // Attributes(set)
2051 TEMP void THIS::Attributes(const TaTransSet& rOtherSet) {
2052  FD_DC("TaTransSet(" << this << ")::Attributes(set) with type " << typeid(rOtherSet.Attribute()).name());
2053  ABASE::Attributes(rOtherSet);
2054 }
2055 
2056 // Attributes(set)
2057 TEMP void THIS::Attributes(const TransSet& rOtherSet) {
2058  FD_DC("TaTransSet(" << this << ")::Attributes(set) with type " << typeid(rOtherSet.Attribute()).name());
2059  ABASE::Attributes(rOtherSet);
2060 }
2061 
2062 
2063 #undef THIS
2064 #undef TEMP
2065 #undef BASE
2066 #undef ABASE
2067 
2068 #undef SORT_EXECPTION
2069 
2070 } // namespace faudes
2071 
2072 
2073 
2074 
2075 #define FAUDES_TRANSSET_H
2076 #endif
2077 

libFAUDES 2.26g --- 2015.08.17 --- c++ api documentaion by doxygen