6 * $Date: 2012-07-06 15:04:28 +0200 (Fr, 06. Jul 2012) $
7 ***************************************************************/
10 * \brief Definitions of functors used in FME layout.
12 * \author Martin Gronemann
15 * This file is part of the Open Graph Drawing Framework (OGDF).
19 * See README.txt in the root directory of the OGDF installation for details.
22 * This program is free software; you can redistribute it and/or
23 * modify it under the terms of the GNU General Public License
24 * Version 2 or 3 as published by the Free Software Foundation;
25 * see the file LICENSE.txt included in the packaging of this file
29 * This program is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 * GNU General Public License for more details.
35 * You should have received a copy of the GNU General Public
36 * License along with this program; if not, write to the Free
37 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
38 * Boston, MA 02110-1301, USA.
40 * \see http://www.gnu.org/copyleft/gpl.html
41 ***************************************************************/
43 #ifndef OGDF_FME_FUNCTIONAL_H
44 #define OGDF_FME_FUNCTIONAL_H
48 //! the useless do nothing function
51 template<typename A
> inline void operator()(A a
) { }
52 template<typename A
, typename B
> inline void operator()(A a
, B b
) { }
55 //! condition functor for returning a constant boolean value
57 struct const_condition
59 template<typename A
> inline bool operator()(A a
) { return result
; }
60 template<typename A
, typename B
> inline bool operator()(A a
, B b
) { return result
; }
63 //! the corresponding typedefs
64 typedef const_condition
<true> true_condition
;
65 typedef const_condition
<false> false_condition
;
67 //! functor for negating a condition
68 template<typename Func
>
69 struct not_condition_functor
73 not_condition_functor(const Func
& cond
) : cond_func(cond
) { }
75 template<typename A
> inline bool operator()(A a
) { return !cond_func(a
); }
76 template<typename A
, typename B
> inline void operator()(A a
, B b
) { return !cond_func(a
, b
); }
79 //! creator of the negator
80 template<typename Func
>
81 static inline not_condition_functor
<Func
> not_condition(const Func
& func
)
83 return not_condition_functor
<Func
>(func
);
86 //! Functor for conditional usage of a functor
87 template<typename CondType
, typename ThenType
, typename ElseType
= do_nothing
>
88 struct if_then_else_functor
94 if_then_else_functor(const CondType
& c
, const ThenType
& f1
) : condFunc(c
), thenFunc(f1
) { }
96 if_then_else_functor(const CondType
& c
, const ThenType
& f1
, const ElseType
& f2
) : condFunc(c
), thenFunc(f1
), elseFunc(f2
) { }
99 inline void operator()(A a
)
107 template<typename A
, typename B
>
108 inline void operator()(A a
, B b
)
117 //! creates an if then else functor with a condition and a then and an else functor
118 template<typename CondType
, typename ThenType
, typename ElseType
>
119 static inline if_then_else_functor
<CondType
, ThenType
, ElseType
> if_then_else(const CondType
& cond
, const ThenType
& thenFunc
, const ElseType
& elseFunc
)
121 return if_then_else_functor
<CondType
, ThenType
, ElseType
>(cond
, thenFunc
, elseFunc
);
124 //! creates an if then functor with a condition and a then functor
125 template<typename CondType
, typename ThenType
>
126 static inline if_then_else_functor
<CondType
, ThenType
> if_then(const CondType
& cond
, const ThenType
& thenFunc
)
128 return if_then_else_functor
<CondType
, ThenType
>(cond
, thenFunc
);
132 //! helper functor to generate a pair as parameters
133 template<typename F
, typename A
>
134 struct pair_call_functor
138 pair_call_functor(F f
, A a
) : func(f
), first(a
) {};
141 inline void operator()(B second
)
148 //! creates a pair call resulting in a call f(a, *)
149 template<typename F
, typename A
>
150 static inline pair_call_functor
<F
, A
> pair_call(F f
, A a
)
152 return pair_call_functor
<F
, A
>(f
, a
);
156 //! Functor for composing two other functors
157 template<typename FuncFirst
, typename FuncSecond
>
158 struct composition_functor
161 FuncSecond secondFunc
;
163 composition_functor(const FuncFirst
& first
, const FuncSecond
& second
) : firstFunc(first
), secondFunc(second
) {};
171 template<typename A
, typename B
>
172 void operator()(A a
, B b
)
180 //! create a functor composition of two functors
181 template<typename FuncFirst
, typename FuncSecond
>
182 static inline composition_functor
<FuncFirst
, FuncSecond
> func_comp(const FuncFirst
& first
, const FuncSecond
& second
)
184 return composition_functor
<FuncFirst
, FuncSecond
>(first
, second
);
188 //! functor for invoking a functor for a pair(u,v) and then (v,u)
189 template<typename Func
>
190 struct pair_vice_versa_functor
194 pair_vice_versa_functor(const Func
& f
) : func(f
) { }
196 template<typename A
, typename B
>
197 void operator()(A a
, B b
)
205 //! creates a functor for invoking a functor for a pair(u,v) and then (v,u)
206 template<typename Func
>
207 static inline pair_vice_versa_functor
<Func
> pair_vice_versa(const Func
& f
)
209 return pair_vice_versa_functor
<Func
>(f
);
213 //! generic min max functor for an array
215 struct min_max_functor
221 min_max_functor(const T
* ptr
, T
& min_var
, T
& max_var
) : a(ptr
), min_value(min_var
), max_value(max_var
)
227 inline void operator()(__uint32 i
)
229 min_value
= min
<T
>(min_value
, a
[i
]);
230 max_value
= max
<T
>(max_value
, a
[i
]);