DiaDes  0.1
DIAgnosis of Discrete-Event System
Functors.hh
Go to the documentation of this file.
1 #ifndef __DIADES_UTILS_FUNCTORS_HH
2 #define __DIADES_UTILS_FUNCTORS_HH
3 
4 #include<algorithm>
5 
6 
7 
8 namespace Diades
9 {
10 
11  namespace Utils
12  {
13 
14  /**********************************************************************/
15 
19  template<typename _Object>
20  struct UnaryPredicate : public std::unary_function<_Object, bool>
21  {
22  using Object = _Object;
23  };
24 
28  template<typename _Object>
29  struct BinaryPredicate : public std::binary_function<_Object, _Object, bool>
30  {
31  using Object = _Object;
32  };
33 
38  template<typename Object>
39  struct Validity : public UnaryPredicate<Object>
40  {
41 
47  bool operator()(const Object & obj) const
48  {
49  return obj.isValid();
50  }
51  };
52 
53 
54 
55 
56  /**********************************************************************/
57 
62  template<typename Object>
63  struct AlwaysTrue : public UnaryPredicate<Object>
64  {
65 
71  bool operator()(const Object & obj) const
72  {
73  return true;
74  }
75  };
76 
77  /**********************************************************************/
78 
83  template<typename Object>
84  struct AlwaysFalse : public UnaryPredicate<Object>
85  {
86 
92  bool operator()(const Object & obj) const
93  {
94  return false;
95  }
96  };
97 
98  /**********************************************************************/
99 
104  template<typename InputIterator>
105  struct IsIn : public UnaryPredicate<typename InputIterator::value_type>
106  {
107  InputIterator _first;
108  InputIterator _last;
109 
115  IsIn(InputIterator first, InputIterator last) : _first(first), _last(last)
116  {
117  }
118 
124  bool operator()(const typename InputIterator::value_type & obj) const
125  {
126  return std::find(_first, _last, obj) != _last;
127  }
128  };
129 
130  /**********************************************************************/
131 
138  template<typename InputIterator>
139  inline
141  isIn(InputIterator first, InputIterator last)
142  {
143  return IsIn<InputIterator>(first, last);
144  }
145 
146  /**********************************************************************/
147 
153  template<typename _Object>
154  struct Is : public std::unary_function<_Object, _Object>
155  {
156  using Object = _Object;
157 
164  {
165  return obj;
166  }
167 
174  const Object & operator()(const Object & obj) const
175  {
176  return obj;
177  }
178 
179  };
180 
181 
182 
183  /**********************************************************************/
184 
189  template<typename InputIterator>
190  struct IsNotIn : public UnaryPredicate<typename InputIterator::value_type>
191  {
192  InputIterator _first;
193  InputIterator _last;
194 
200  IsNotIn(InputIterator first, InputIterator last) : _first(first), _last(last)
201  {
202  }
203 
209  bool operator()(const typename InputIterator::value_type & obj) const
210  {
211  return !IsIn<InputIterator>(_first, _last)(obj);
212  }
213  };
214 
215  /**********************************************************************/
216 
223  template<typename InputIterator>
224  inline
226  isNotIn(InputIterator first, InputIterator last)
227  {
228  return IsNotIn<InputIterator>(first, last);
229  }
230 
231  /**********************************************************************/
232 
242  template<typename InputIterator, typename OutputIterator1,
243  typename OutputIterator2, typename _UnaryPredicate >
244  void
245  dispatch(InputIterator first,
246  InputIterator last,
247  OutputIterator1 output1,
248  OutputIterator2 output2,
249  _UnaryPredicate predicate)
250  {
251  for(; first != last; ++first)
252  {
253  if(predicate(*first))
254  {
255  *output1 = *first;
256  ++output1;
257  }
258  else
259  {
260  *output2 = *first;
261  ++output2;
262  }
263  }
264  }
265 
266  /**********************************************************************/
267 
274  template <class F, class G, class H>
276  : public std::unary_function<typename G::argument_type,
277  typename F::result_type>
278  {
279  private:
280  F _f;
281  G _g;
282  H _h;
283  public:
284 
291  CompositionFunctorFGxHx(const F& f, const G& g, const H& h)
292  : _f(f), _g(g), _h(h)
293  {
294  }
295 
301  typename F::result_type
302  operator()(const typename G::argument_type& x) const
303  {
304  return _f(_g(x), _h(x));
305  }
306  };
307 
317  template <class F, class G, class H>
319  fgxhx(const F& f, const G& g, const H& h)
320  {
321  return CompositionFunctorFGxHx<F, G, H>(f, g, h);
322  }
323 
330  template <class F, class G>
332  : public std::unary_function<typename G::argument_type,
333  typename F::result_type>
334  {
335  private:
336  F _f;
337  G _g;
338  public:
339 
345  CompositionFunctorFGx(const F & f, const G & g)
346  : _f(f), _g(g)
347  {
348  }
349 
355  typename F::result_type
356  operator()(const typename G::argument_type& x) const
357  {
358  return _f(_g(x));
359  }
360  };
361 
371  template <class F, class G>
373  fgx(const F& f, const G & g)
374  {
375  return CompositionFunctorFGx<F, G>(f, g);
376  }
377 
384  template <class F, class G, class H>
386  : public std::binary_function<typename G::argument_type, typename H::argument_type,
387  typename F::result_type>
388  {
389  private:
390  F _f;
391  G _g;
392  H _h;
393  public:
394 
401  CompositionFunctorFGxHy(const F& f, const G& g, const H& h)
402  : _f(f), _g(g), _h(h)
403  {
404  }
405 
411  typename F::result_type
412  operator()(const typename G::argument_type& x, const typename H::argument_type& y) const
413  {
414  return _f.operator()(_g.operator()(x), _h.operator()(y));
415  }
416  };
417 
427  template <class F, class G, class H>
429  fgxhy(const F& f, const G& g, const H& h)
430  {
431  return CompositionFunctorFGxHy<F, G, H>(f, g, h);
432  }
433 
434 
435 
441  template<typename UnaryPredicate1,typename UnaryPredicate2>
442  struct UnaryAnd : public CompositionFunctorFGxHx<std::logical_and<bool>,UnaryPredicate1, UnaryPredicate2>
443  {
444  UnaryAnd(const UnaryPredicate1 & p1, const UnaryPredicate2 & p2)
445  : CompositionFunctorFGxHx<std::logical_and<bool>,UnaryPredicate1, UnaryPredicate2>(std::logical_and<bool>(),p1,p2){}
446  };
447 
448 
449 
455  template<typename UnaryPredicate1,typename UnaryPredicate2>
456  struct UnaryOr : public CompositionFunctorFGxHx<std::logical_or<bool>,UnaryPredicate1, UnaryPredicate2>
457  {
458  UnaryOr(const UnaryPredicate1 & p1, const UnaryPredicate2 & p2)
459  : CompositionFunctorFGxHx<std::logical_or<bool>,UnaryPredicate1, UnaryPredicate2>(std::logical_or<bool>(),p1,p2){}
460  };
461 
462 
463 
464 
465 
466 
467 
474  template <class Predicate1, class Predicate2>
475  inline
477  unaryAnd(const Predicate1& p1, const Predicate2& p2)
478  {
479  return UnaryAnd<Predicate1, Predicate2>(p1, p2);
480  }
481 
488  template <class Predicate1, class Predicate2>
489  inline
491  unaryOr(const Predicate1& p1, const Predicate2& p2)
492  {
493  return UnaryOr<Predicate1, Predicate2>(p1, p2);
494  }
495 
496 
497 
503  template<typename UnaryPredicate1,typename UnaryPredicate2>
504  struct BinaryAnd : public CompositionFunctorFGxHy<std::logical_and<bool>,UnaryPredicate1, UnaryPredicate2>
505  {
506  BinaryAnd(const UnaryPredicate1 & p1, const UnaryPredicate2 & p2)
507  : CompositionFunctorFGxHy<std::logical_and<bool>,UnaryPredicate1, UnaryPredicate2>(std::logical_and<bool>(),p1,p2){}
508  };
509 
510 
511 
517  template<typename UnaryPredicate1,typename UnaryPredicate2>
518  struct BinaryOr : public CompositionFunctorFGxHx<std::logical_or<bool>,UnaryPredicate1, UnaryPredicate2>
519  {
520  BinaryOr(const UnaryPredicate1 & p1, const UnaryPredicate2 & p2)
521  : CompositionFunctorFGxHx<std::logical_or<bool>,UnaryPredicate1, UnaryPredicate2>(std::logical_or<bool>(),p1,p2){}
522  };
523 
524 
525 
526 
527 
528 
529 
530 
537  template <class Predicate1, class Predicate2>
538  inline
540  binaryAnd(const Predicate1& p1, const Predicate2& p2)
541  {
542  return BinaryAnd<Predicate1, Predicate2>(p1, p2);
543  }
544 
551  template <class Predicate1, class Predicate2>
552  inline
554  binaryOr(const Predicate1& p1, const Predicate2& p2)
555  {
556  return BinaryOr<Predicate1, Predicate2>(p1, p2);
557  }
558 
559 // /**
560 // * Accumulator binary functor
561 // * Accumulates the values returns by the Predicate
562 // * The accumulation is done with the connector
563 // */
564 // template<typename Predicate, typename Connector>
565 // struct PredicateAccumulator : public std::binary_function<bool, typename Predicate::Object, bool>
566 // {
567 //
568 // /**
569 // *
570 // * @param obj an object to check its validity
571 // * @return the result of obj.isValid()
572 // */
573 // bool operator()(bool context, typename Predicate::const Object & obj)
574 // {
575 // return Connector(Is<bool>, Predicate)(context,obj);
576 // }
577 // };
578 
585  template<typename Object>
586  struct AccumulateValidity : public BinaryAnd<Is<bool>,Validity<Object>>
587  {
589  };
590 
591 
592  };
593 
594 };
595 
596 #endif
CompositionFunctorFGxHy(const F &f, const G &g, const H &h)
Definition: Functors.hh:401
InputIterator _last
Definition: Functors.hh:193
bool operator()(const Object &obj) const
Definition: Functors.hh:47
CompositionFunctorFGx< F, G > fgx(const F &f, const G &g)
Definition: Functors.hh:373
CompositionFunctorFGx(const F &f, const G &g)
Definition: Functors.hh:345
BinaryAnd< Predicate1, Predicate2 > binaryAnd(const Predicate1 &p1, const Predicate2 &p2)
Definition: Functors.hh:540
bool operator()(const Object &obj) const
Definition: Functors.hh:92
UnaryOr< Predicate1, Predicate2 > unaryOr(const Predicate1 &p1, const Predicate2 &p2)
Definition: Functors.hh:491
STL namespace.
Object & operator()(Object &obj)
Definition: Functors.hh:163
bool operator()(const typename InputIterator::value_type &obj) const
Definition: Functors.hh:124
F::result_type operator()(const typename G::argument_type &x) const
Definition: Functors.hh:356
CompositionFunctorFGxHx< F, G, H > fgxhx(const F &f, const G &g, const H &h)
Definition: Functors.hh:319
BinaryOr< Predicate1, Predicate2 > binaryOr(const Predicate1 &p1, const Predicate2 &p2)
Definition: Functors.hh:554
IsNotIn< InputIterator > isNotIn(InputIterator first, InputIterator last)
Definition: Functors.hh:226
const Object & operator()(const Object &obj) const
Definition: Functors.hh:174
InputIterator _first
Definition: Functors.hh:107
IsNotIn(InputIterator first, InputIterator last)
Definition: Functors.hh:200
F::result_type operator()(const typename G::argument_type &x) const
Definition: Functors.hh:302
IsIn< InputIterator > isIn(InputIterator first, InputIterator last)
Definition: Functors.hh:141
Namespace of the Diades project.
InputIterator _last
Definition: Functors.hh:108
IsIn(InputIterator first, InputIterator last)
Definition: Functors.hh:115
void dispatch(InputIterator first, InputIterator last, OutputIterator1 output1, OutputIterator2 output2, _UnaryPredicate predicate)
Definition: Functors.hh:245
InputIterator _first
Definition: Functors.hh:192
bool operator()(const Object &obj) const
Definition: Functors.hh:71
CompositionFunctorFGxHy< F, G, H > fgxhy(const F &f, const G &g, const H &h)
Definition: Functors.hh:429
UnaryOr(const UnaryPredicate1 &p1, const UnaryPredicate2 &p2)
Definition: Functors.hh:458
UnaryAnd< Predicate1, Predicate2 > unaryAnd(const Predicate1 &p1, const Predicate2 &p2)
Definition: Functors.hh:477
F::result_type operator()(const typename G::argument_type &x, const typename H::argument_type &y) const
Definition: Functors.hh:412
CompositionFunctorFGxHx(const F &f, const G &g, const H &h)
Definition: Functors.hh:291
UnaryAnd(const UnaryPredicate1 &p1, const UnaryPredicate2 &p2)
Definition: Functors.hh:444
BinaryOr(const UnaryPredicate1 &p1, const UnaryPredicate2 &p2)
Definition: Functors.hh:520
bool operator()(const typename InputIterator::value_type &obj) const
Definition: Functors.hh:209
BinaryAnd(const UnaryPredicate1 &p1, const UnaryPredicate2 &p2)
Definition: Functors.hh:506