1 // ==========================================================================
2 //                 SeqAn - The Library for Sequence Analysis
3 // ==========================================================================
4 // Copyright (c) 2006-2018, Knut Reinert, FU Berlin
5 // All rights reserved.
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions are met:
9 //
10 //     * Redistributions of source code must retain the above copyright
11 //       notice, this list of conditions and the following disclaimer.
12 //     * Redistributions in binary form must reproduce the above copyright
13 //       notice, this list of conditions and the following disclaimer in the
14 //       documentation and/or other materials provided with the distribution.
15 //     * Neither the name of Knut Reinert or the FU Berlin nor the names of
16 //       its contributors may be used to endorse or promote products derived
17 //       from this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 // ARE DISCLAIMED. IN NO EVENT SHALL KNUT REINERT OR THE FU BERLIN BE LIABLE
23 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
29 // DAMAGE.
30 //
31 // ==========================================================================
32 // Author: David Weese <david.weese@fu-berlin.de>
33 // Author: Enrico Siragusa <enrico.siragusa@fu-berlin.de>
34 // ==========================================================================
35 
36 #ifndef SEQAN_BASIC_FUNCTORS_H_
37 #define SEQAN_BASIC_FUNCTORS_H_
38 
39 namespace seqan {
40 
41 // ============================================================================
42 // Functors
43 // ============================================================================
44 
45 // ----------------------------------------------------------------------------
46 // Functor OrFunctor
47 // ----------------------------------------------------------------------------
48 
49 template <typename TFunctor1, typename TFunctor2>
50 struct OrFunctor
51 {
52     TFunctor1 func1;
53     TFunctor2 func2;
54 
OrFunctorOrFunctor55     OrFunctor()
56     {}
57 
OrFunctorOrFunctor58     OrFunctor(TFunctor1 const &func1, TFunctor2 const &func2):
59         func1(func1), func2(func2)
60     {}
61 
62     template <typename TValue>
operatorOrFunctor63     bool operator() (TValue const & val)
64     {
65         return func1(val) || func2(val);
66     }
67 
68     template <typename TValue>
operatorOrFunctor69     bool operator() (TValue const & val) const
70     {
71         return func1(val) || func2(val);
72     }
73 };
74 
75 // ----------------------------------------------------------------------------
76 // Functor AndFunctor
77 // ----------------------------------------------------------------------------
78 
79 template <typename TFunctor1, typename TFunctor2>
80 struct AndFunctor
81 {
82     TFunctor1 func1;
83     TFunctor2 func2;
84 
AndFunctorAndFunctor85     AndFunctor()
86     {}
87 
AndFunctorAndFunctor88     AndFunctor(TFunctor1 const &func1, TFunctor2 const &func2):
89         func1(func1), func2(func2)
90     {}
91 
92     template <typename TValue>
operatorAndFunctor93     bool operator() (TValue const & val)
94     {
95         return func1(val) && func2(val);
96     }
97 
98     template <typename TValue>
operatorAndFunctor99     bool operator() (TValue const & val) const
100     {
101         return func1(val) && func2(val);
102     }
103 };
104 
105 // ----------------------------------------------------------------------------
106 // Functor NotFunctor
107 // ----------------------------------------------------------------------------
108 
109 template <typename TFunctor>
110 struct NotFunctor
111 {
112     TFunctor func;
113 
NotFunctorNotFunctor114     NotFunctor()
115     {}
116 
NotFunctorNotFunctor117     NotFunctor(TFunctor const &func):
118         func(func)
119     {}
120 
121     template <typename TValue>
operatorNotFunctor122     bool operator() (TValue const & val)
123     {
124         return !func(val);
125     }
126 
127 
128     template <typename TValue>
operatorNotFunctor129     bool operator() (TValue const & val) const
130     {
131         return !func(val);
132     }
133 };
134 
135 // ----------------------------------------------------------------------------
136 // Functor CountDownFunctor
137 // ----------------------------------------------------------------------------
138 
139 template <typename TFunctor = True, uint64_t REMAINING = 0>
140 struct CountDownFunctor
141 {
142     uint64_t remaining;
143     TFunctor func;
144 
145     CountDownFunctor(uint64_t remaining = REMAINING):
remainingCountDownFunctor146         remaining(remaining)
147     {}
148 
CountDownFunctorCountDownFunctor149     CountDownFunctor(uint64_t remaining, TFunctor const &func):
150         remaining(remaining),
151         func(func)
152     {}
153 
154     template <typename TValue>
operatorCountDownFunctor155     bool operator() (TValue const & val)
156     {
157         if (remaining == 0)
158             return true;
159         if (func(val))
160             --remaining;
161         return false;
162     }
163 
164     operator bool()
165     {
166         return remaining == 0;
167     }
168 };
169 
170 // ----------------------------------------------------------------------------
171 // Functor CountFunctor
172 // ----------------------------------------------------------------------------
173 
174 template <typename TFunctor = True>
175 struct CountFunctor
176 {
177     uint64_t count;
178     TFunctor func;
179 
CountFunctorCountFunctor180     CountFunctor() : count(0)
181     {}
182 
CountFunctorCountFunctor183     CountFunctor(TFunctor const & func) : count(0), func(func)
184     {}
185 
186     template <typename TValue>
operatorCountFunctor187     bool operator() (TValue const & val)
188     {
189         if (func(val))
190             ++count;
191         return false;
192     }
193 
uint64_tCountFunctor194     operator uint64_t() const
195     {
196         return count;
197     }
198 };
199 
200 template <typename TFunctor>
clear(CountFunctor<TFunctor> & func)201 inline void clear(CountFunctor<TFunctor> &func)
202 {
203     func.count = 0;
204 }
205 
206 template <typename TFunctor>
value(CountFunctor<TFunctor> & func)207 inline uint64_t & value(CountFunctor<TFunctor> &func)
208 {
209     return func.count;
210 }
211 
212 }   // namespace seqan
213 
214 #endif // SEQAN_BASIC_FUNCTORS_H_
215