CppNoddy  0.92
Loading...
Searching...
No Matches
Functors.h
Go to the documentation of this file.
1/// \file Functors.h
2/// Some Function Objects that CppNoddy makes use of
3/// in algorithms applied to STL containers.
4
5#ifndef FUNCTORS_H
6#define FUNCTORS_H
7
8#include <cmath>
9
10namespace CppNoddy {
11
12 /// A function object predicate that compares the absolute value
13 /// of two elements and returns a true of elt1 < elt2.
14 template <typename _Type>
16 public:
17 /// Return true if argument 1 < argument 2.
18 /// \param elt1 First element to compare
19 /// \param elt2 Second element to compare
20 bool operator()(_Type elt1, _Type elt2) {
21 return std::abs(elt2) > std::abs(elt1);
22 }
23 };
24
25 /// A function object predicate that first computes the absolute
26 /// difference between two elements and a specified value, then
27 /// compares these two resulting values. Used for computing which
28 /// of two components is nearest to the specified value.
29 template <typename _Type>
30 class absDiff_predicate : public std::binary_function< _Type, _Type, bool> {
31 public:
32 /// \param value The target value that is to used.
33 absDiff_predicate(_Type value) : std::binary_function< _Type, _Type, bool>() {
34 this -> target_elt = value;
35 }
36
37 /// Returns true of the first argument is closer to the specified
38 /// (in constructor) value than the second argument.
39 /// \param elt1 The first value to be used in the comparison
40 /// \param elt2 The second value to be used in the comparison
41 bool operator()(_Type elt1, _Type elt2) {
42 return std::abs(elt2 - target_elt) > std::abs(elt1 - target_elt);
43 }
44
45 private:
46 _Type target_elt;
47 };
48
49
50 /// A function object used to accumulate the absolute value of a
51 /// container.
52 template <typename _Type>
54 public:
55 double operator()(double x, _Type y) {
56 return x + std::abs(y);
57 }
58 };
59
60 /// A function object used to accumulate the square of the
61 /// absolute value of a container.
62 template <typename _Type>
64 public:
65 double operator()(double x, _Type y) {
66 return x + std::pow(std::abs(y), 2.);
67 }
68 };
69
70 /// A unary pure function object that scales through multiplication.
71 /// The object is type templated for any object that defines
72 /// operator* and the MULTIPLIER is set in the functor constructor.
73 template <class _containerType, typename _valueType>
74 class scale_functor : public std::unary_function< _valueType, _containerType > {
75 public:
76 /// \param value The value to be used in the scale operation
77 scale_functor(_valueType value) : std::unary_function< _valueType, _containerType>() {
78 MULTIPLIER = value;
79 }
80
81 /// \return The operator*( elt, value) operation where value
82 /// is specified in the constructor
83 _containerType operator()(_containerType elt) {
84 return elt * MULTIPLIER;
85 }
86
87 private:
88 _valueType MULTIPLIER;
89 };
90
91
92} // end namespace
93
94#endif // FUNCOBJS_H
A function object used to accumulate the absolute value of a container.
Definition: Functors.h:53
double operator()(double x, _Type y)
Definition: Functors.h:55
A function object predicate that first computes the absolute difference between two elements and a sp...
Definition: Functors.h:30
bool operator()(_Type elt1, _Type elt2)
Returns true of the first argument is closer to the specified (in constructor) value than the second ...
Definition: Functors.h:41
absDiff_predicate(_Type value)
Definition: Functors.h:33
A function object used to accumulate the square of the absolute value of a container.
Definition: Functors.h:63
double operator()(double x, _Type y)
Definition: Functors.h:65
A function object predicate that compares the absolute value of two elements and returns a true of el...
Definition: Functors.h:15
bool operator()(_Type elt1, _Type elt2)
Return true if argument 1 < argument 2.
Definition: Functors.h:20
A unary pure function object that scales through multiplication.
Definition: Functors.h:74
_containerType operator()(_containerType elt)
Definition: Functors.h:83
scale_functor(_valueType value)
Definition: Functors.h:77
A collection of OO numerical routines aimed at simple (typical) applied problems in continuum mechani...

© 2012

R.E. Hewitt