Correctly check if item was checked
[TortoiseGit.git] / ext / OGDF / src / energybased / FMEFunctional.h
blobca0a522df584554909570735a647906ab6c7e3fc
1 /*
2 * $Revision: 2559 $
4 * last checkin:
5 * $Author: gutwenger $
6 * $Date: 2012-07-06 15:04:28 +0200 (Fr, 06. Jul 2012) $
7 ***************************************************************/
9 /** \file
10 * \brief Definitions of functors used in FME layout.
12 * \author Martin Gronemann
14 * \par License:
15 * This file is part of the Open Graph Drawing Framework (OGDF).
17 * \par
18 * Copyright (C)<br>
19 * See README.txt in the root directory of the OGDF installation for details.
21 * \par
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
26 * for details.
28 * \par
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.
34 * \par
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
46 namespace ogdf {
48 //! the useless do nothing function
49 struct do_nothing
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
56 template<bool result>
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
71 Func cond_func;
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
90 CondType condFunc;
91 ThenType thenFunc;
92 ElseType elseFunc;
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) { }
98 template<typename A>
99 inline void operator()(A a)
101 if (condFunc(a))
102 thenFunc(a);
103 else
104 elseFunc(a);
107 template<typename A, typename B>
108 inline void operator()(A a, B b)
110 if (condFunc(a, b))
111 thenFunc(a, b);
112 else
113 elseFunc(a, 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
136 F func;
137 A first;
138 pair_call_functor(F f, A a) : func(f), first(a) {};
140 template<typename B>
141 inline void operator()(B second)
143 func(first, 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
160 FuncFirst firstFunc;
161 FuncSecond secondFunc;
163 composition_functor(const FuncFirst& first, const FuncSecond& second) : firstFunc(first), secondFunc(second) {};
164 template<typename A>
165 void operator()(A a)
167 firstFunc(a);
168 secondFunc(a);
171 template<typename A, typename B>
172 void operator()(A a, B b)
174 firstFunc(a, b);
175 secondFunc(a, 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
192 Func func;
194 pair_vice_versa_functor(const Func& f) : func(f) { }
196 template<typename A, typename B>
197 void operator()(A a, B b)
199 func(a, b);
200 func(b, a);
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
214 template<typename T>
215 struct min_max_functor
217 const T* a;
218 T& min_value;
219 T& max_value;
221 min_max_functor(const T* ptr, T& min_var, T& max_var) : a(ptr), min_value(min_var), max_value(max_var)
223 min_value = a[0];
224 max_value = a[0];
227 inline void operator()(__uint32 i)
229 min_value = min<T>(min_value, a[i]);
230 max_value = max<T>(max_value, a[i]);
236 #endif