tp_timeinterval.h
Go to the documentation of this file.
1 /** @file tp_timeinterval.h Class TimeInterval */
2 
3 
4 /*
5  Time plug-in for FAU Discrete Event Systems Library (libfaudes)
6 
7  Copyright (C) 2007 Ruediger Berndt
8  Copyright (C) 2007 Thomas Moor
9  Exclusive copyright is granted to Klaus Schmidt
10 
11 */
12 
13 #ifndef FAUDES_TP_TIMEINTERVAL_H
14 #define FAUDES_TP_TIMEINTERVAL_H
15 
16 #include "corefaudes.h"
17 #include <cstdlib>
18 #include <cstring>
19 #include <cfloat>
20 #include <climits>
21 #include <iostream>
22 
23 
24 namespace faudes {
25 
26 /**
27  * Type to represent time.
28  *
29  * The time plugin will compile with integer or float time type. However,
30  * we insist in discrete time in the sense that there is a smallest
31  * time step. So float time would be cosmetic only.
32  *
33  * Note that timed generators do not explicitely refer to a physical time unit like e.g.
34  * hours or seconds. However, it is assumed, that a fixed conversion factor
35  * from values of type tpTime to some physical unit exists. Thus, we say tpTime
36  * variables hold time in faudes-time units. We feel that this is consistent with
37  * Alur's timed automata.
38  *
39  * @ingroup TimedPlugin
40  */
41 class tpTime {
42  public:
43  /** Datatype for point on time axis */
44  typedef Int Type;
45  /** Maximum time, associated with infinitiy */
46  const static Type Max=FAUDES_INT_MAX;
47  /** Minimum time, associated with minus infinitiy */
48  const static Type Min=FAUDES_INT_MIN+1;
49  /** Undefined time value */
50  const static Type UnDef=FAUDES_INT_MIN;
51  /** Smallest representable time step */
52  const static Type Step=1;
53  /** convert to string */
54  std::string static Str(Type time) {
55  if(time == UnDef) return "undef";
56  if(time < Min) return "err";
57  if(time == Min) return "-inf";
58  if(time > Max) return "err";
59  if(time == Max) return "inf";
60  return ToStringInteger(time);
61  };
62 };
63 
64 /*
65 Alternative time representation: cosmetic float
66 class tpTime {
67  public:
68  typedef double Type;
69  const static Type Max=FLT_MAX;
70  const static Type Min=FLT_MIN*0.5;
71  const static Type UnDef=FLT_MIN;
72  const static Type Step=0.0001;
73 };
74 */
75 
76 
77 
78 /**
79  * Model of a time interval. An interval consisits of a lower and upper bound,
80  * plus flags to indicate whether the bounds are inclusive or not.
81  * The maximum and minimum values of the time data tyle are interpreted as infinity.
82  *
83  *
84  * @ingroup TimedPlugin
85  */
86  class TimeInterval {
87 
88  public:
89 
90  /**
91  * Default constructor - sets time interval to (-inf; +inf)
92  */
93  TimeInterval(void) {SetFull();};
94 
95  /**
96  * Default destructor
97  */
98  ~TimeInterval(void) {};
99 
100  /**
101  * Set the lower bound to a given value.
102  * If the value is minus infinity, it is set exclusive. Otherwise,
103  * the exclusive/inclusive flag is kept.
104  *
105  * @param time
106  * The new value of lower bound
107  */
108  void LB(tpTime::Type time) {
109  if(time <= tpTime::Min) time=tpTime::Min;
110  if(time >= tpTime::Max) time=tpTime::Max;
111  mstLB=time;
112  if(mstLB==tpTime::Min) mbLBincl=false;
113  }
114 
115 
116  /**
117  * Set the upper bound to a given value.
118  * If the value is infinity, it is set exclusive. Otherwise,
119  * the exclusive/inclusive flag is kept.
120  *
121  * @param time
122  * The new value of upper bound
123  */
124  void UB(tpTime::Type time) {
125  if(time <= tpTime::Min) time=tpTime::Min;
126  if(time >= tpTime::Max) time=tpTime::Max;
127  mstUB=time;
128  if(mstUB==tpTime::Max) mbUBincl=false;
129  }
130 
131 
132  /**
133  * Configures the upper bound to be inclusive.
134  * If the upper bound is infinity, it stays exclusive.
135  *
136  * @param incl
137  */
138  void UBincl(bool incl) {
139  mbUBincl=incl;
140  if(mstUB>=tpTime::Max) mbUBincl=false;
141  }
142 
143 
144  /**
145  * Configures the lower bound to be inclusive.
146  * If the lower bound is minus infinity, it stays exclusive.
147  *
148  * @param incl
149  */
150  void LBincl(bool incl) {
151  mbLBincl=incl;
152  if(mstLB<=tpTime::Min) mbLBincl=false;
153  }
154 
155 
156  /**
157  * Set upper bound to infinity
158  */
159  void setUBinf(void) {
160  mstUB=tpTime::Max; mbUBincl=false; }
161 
162  /**
163  * Set lower bound to infinity
164  */
165  void setLBinf(void) {
166  mstLB=tpTime::Min; mbLBincl=false; }
167 
168  /**
169  * Return upper bound.
170  *
171  * @return upper bound
172  */
173  tpTime::Type UB(void) const {return mstUB;}
174 
175  /**
176  * Return lower bound.
177  *
178  * @return lower bound
179  */
180  tpTime::Type LB(void) const {return mstLB;}
181 
182  /**
183  * Test for lower bound inclusive
184  *
185  * @return true for inclusive
186  */
187  bool LBincl(void) const {return mbLBincl;}
188 
189  /**
190  * Test for upper bound inclusive
191  *
192  * @return true for inclusive
193  */
194  bool UBincl(void) const {return mbUBincl;}
195 
196  /**
197  * Test for lower bound infinity
198  *
199  * @return true for infinity
200  */
201  bool LBinf(void) const {return mstLB == tpTime::Min; }
202 
203  /**
204  * Test for upper bound infinity
205  *
206  * @return true for infinity
207  */
208  bool UBinf(void) const {return mstUB == tpTime::Max;}
209 
210 
211  /**
212  * Convert to canonical representation.
213  *
214  * Uses tpTime::Step to convert to aleft closed / right opened
215  * representation
216  */
217  void Canonical(void);
218 
219  /**
220  * Set to full (-inf, +inf)
221  */
222  void SetFull(void) {
224 
225  /**
226  * Set to empty (1, -1)
227  */
228  void SetEmpty(void) {
229  mstUB=-1; mstLB=1; mbUBincl=false; mbLBincl=false; };
230 
231  /**
232  * Set to positive [0, inf)
233  */
234  void SetPositive(void) {
235  mstUB=tpTime::Max; mstLB=0; mbUBincl=false; mbLBincl=true; };
236 
237 
238  /**
239  * Set to negative (-inf, 0]
240  */
241  void SetNegative(void) {
242  mstLB=tpTime::Min; mstUB=0; mbUBincl=false; mbLBincl=true; };
243 
244  /**
245  * Test whether interval is full
246  *
247  * @return
248  * True if interval is (-inf,+inf)
249  */
250  bool Full(void) const { return (mstUB==tpTime::Max) && (mstLB==tpTime::Min) ;};
251 
252  /**
253  * Test interval for empty set
254  *
255  * @return
256  * True if interval is empty
257  */
258  bool Empty(void) const;
259 
260  /**
261  * Test whether interval includes [0,inf)
262  *
263  * @return
264  * True if interval includes [0,inf)
265  */
266  bool IncludesPositive(void) const {
267  return ( (mstLB<0) || ((mstLB ==0) && mbLBincl) ) && (mstUB==tpTime::Max);};
268 
269  /**
270  * Test whether interval includes (-inf,0]
271  *
272  * @return
273  * True if interval includes (-inf,0]
274  */
275  bool IncludesNegative(void) const {
276  return ( (mstUB>0) || ((mstUB ==0) && mbUBincl) ) && (mstLB==tpTime::Min);};
277 
278  /**
279  * Test whether a point satisfies interval
280  * @return
281  * True if interval includes the point
282  */
283  bool In(tpTime::Type time) const;
284 
285 
286  /**
287  * Test whether two intervals are equal.
288  *
289  * Note that the test is strictly based on the
290  * internal representation, ie [0,10] is not equal to [0,11). This
291  * may change in future implementations to consider the tpTime::TypeStep
292  *
293  * @return
294  * True on equality
295  */
296  bool operator == (const TimeInterval& rOtherInterval) const {
297  if(mstUB!=rOtherInterval.mstUB) return false;
298  if(mstLB!=rOtherInterval.mstLB) return false;
299  if(mbUBincl!=rOtherInterval.mbUBincl) return false;
300  if(mbLBincl!=rOtherInterval.mbLBincl) return false;
301  return true;
302  }
303 
304  /**
305  * Test whether two intervals not equal.
306  *
307  * Note that the test is strictly based on the
308  * internal representation, ie [0,10] is not equal to [0,11). This
309  * may change in future implementations to consider the tpTime::TypeStep
310  *
311  * @return
312  * False on equality
313  */
314  bool operator != (const TimeInterval& rOtherInterval) const {
315  return ! (*this == rOtherInterval) ;
316  }
317 
318  /**
319  * Transform by left shift and intersection with [0, inf)
320  * @param time
321  * Amount to shift left
322  *
323  */
324  void PositiveLeftShift(tpTime::Type time);
325 
326  /**
327  * Intersect this interval with other interval
328  *
329  * @param rOtherInterval
330  *
331  */
332  void Intersect(const TimeInterval& rOtherInterval);
333 
334  /**
335  * Intersection of two time intervals.
336  *
337  * @param rInterval1
338  * Reference of first time interval
339  * @param rInterval2
340  * Reference of second time interval
341  *
342  * @return Intersection of both intervals.
343  */
344  static TimeInterval Intersect(const TimeInterval& rInterval1, const TimeInterval& rInterval2);
345 
346 
347  /**
348  * Merge this interval with other interval.
349  * I.e. find smallest superset of the union of the two intervals.
350  *
351  * @param rOtherInterval
352  * Reference to other time interval
353  *
354  */
355  void Merge(const TimeInterval& rOtherInterval);
356 
357  /**
358  * Merge this interval with other interval.
359  * I.e. find smallest superset of the union of the two intervals.
360  *
361  * @param rInterval1
362  * Reference of first time interval
363  * @param rInterval2
364  * Reference of second time interval
365  *
366  * @return Superset of both intervals.
367  */
368  static TimeInterval Merge(const TimeInterval& rInterval1, const TimeInterval& rInterval2);
369 
370  /**
371  * Pretty printable string.
372  *
373  * @return printable string.
374  */
375  std::string Str(void) const;
376 
377 
378  private:
379 
380  /** Upper bound */
382 
383  /** Lower bound */
385 
386  /** Flag to indicate that the upper bound is part of the interval */
387  bool mbUBincl;
388 
389  /** Flag to indicate that lower boundary is part of the interval. */
390  bool mbLBincl;
391 
392 };
393 
394 
395 } // end namespace faudes
396 
397 
398 #endif

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