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 // ==========================================================================
34 
35 #ifndef SEQAN_HEADER_PIPE_ITERATOR_H
36 #define SEQAN_HEADER_PIPE_ITERATOR_H
37 
38 namespace seqan
39 {
40 
41 //namespace SEQAN_NAMESPACE_PIPELINING
42 //{
43 
44     //////////////////////////////////////////////////////////////////////////////
45     // input pipe iterator
46     // interface between pipe modules and algorithms working with iterators
47 
48     template <typename TInput>
49     struct IPipeIterator
50     {
51         //////////////////////////////////////////////////////////////////////////////
52         // public iterator interface
53 
54         typedef IPipeIterator                    iterator;
55         typedef std::forward_iterator_tag        iterator_category;
56         typedef typename Value<TInput>::Type    value_type;
57         typedef const value_type                const_value_type;
58         typedef typename Size<TInput>::Type        difference_type;
59         typedef value_type*                        pointer;
60         typedef value_type&                        reference;
61         typedef const value_type&                const_reference;
62 
63         TInput*            in;
64         difference_type    rest;
65 
IPipeIteratorIPipeIterator66         IPipeIterator():
67             in(NULL),
68             rest(0) {}
69 
IPipeIteratorIPipeIterator70         IPipeIterator(TInput &_in):
71             in(&_in),
72             rest(length(_in))
73         {
74             beginRead(*in);
75         }
76 
IPipeIteratorIPipeIterator77         IPipeIterator(const iterator &I):
78             in(I.in),
79             rest(I.rest) {}
80 
81         const_value_type operator* () const {
82             return **in;
83         }
84 
85         //const_reference operator* () const {
86         //    return **in;
87         //}
88 
89         iterator& operator++ () {
90             ++(*in);
91             if (!--rest) endRead(*in);
92             return *this;
93         }
94 
95         iterator operator++ (int) {
96             iterator before = *this;
97             ++*this;
98             return before;
99         }
100 
101         iterator operator+ (difference_type delta) {
102             for(; delta != 0; --delta)
103                 ++*this;
104             return *this;
105         }
106 
107         difference_type operator- (const iterator &I) const {
108             return I.rest - rest;
109         }
110 
Equal_IPipeIterator111         bool Equal_(const iterator &I) const {
112             return rest == I.rest;
113         }
114     };
115 
116     template <typename TInput>
117     bool operator==(const IPipeIterator<TInput>& _Left, const IPipeIterator<TInput>& Right_)
118     {
119         return _Left.Equal_(Right_);
120     }
121 
122     template <typename TInput>
123     bool operator!=(const IPipeIterator<TInput>& _Left, const IPipeIterator<TInput>& Right_)
124     {
125         return !(_Left == Right_);
126     }
127 
128 
129 
130     //////////////////////////////////////////////////////////////////////////////
131     // output pipe iterator
132     // interface between algorithms working with iterators and pipe modules
133 
134     template <typename TOutput>
135     struct OPipeIterator
136     {
137         //////////////////////////////////////////////////////////////////////////////
138         // public iterator interface
139 
140         typedef OPipeIterator                    iterator;
141         typedef std::forward_iterator_tag        iterator_category;
142         typedef typename Value<TOutput>::Type    value_type;
143         typedef typename Size<TOutput>::Type    difference_type;
144         typedef iterator*                        pointer;
145         typedef iterator&                        reference;
146         typedef value_type const &                const_reference;
147 
148         TOutput*        out;
149         difference_type    rest;
150 
OPipeIteratorOPipeIterator151         OPipeIterator():
152             out(NULL),
153             rest(0) {}
154 
OPipeIteratorOPipeIterator155         OPipeIterator(TOutput &_out):
156             out(&_out),
157             rest(length(_out))
158         {
159             beginWrite(*out);
160         }
161 
OPipeIteratorOPipeIterator162         OPipeIterator(const iterator &I):
163             out(I.out),
164             rest(I.rest) {}
165 
166         reference operator* () {
167             return *this;
168         }
169 
170         iterator operator= (const_reference _v) {
171             out->push(_v);
172             return *this;
173         }
174 
175         iterator& operator++ () {
176             ++(*out);
177             if (!--rest) endWrite(*out);
178             return *this;
179         }
180 
181         iterator operator++ (int) {
182             iterator before = *this;
183             ++*this;
184             return before;
185         }
186 
187         difference_type operator- (const iterator &I) const {
188             return I.rest - rest;
189         }
190 
Equal_OPipeIterator191         bool Equal_(const iterator &I) const {
192             return rest == I.rest;
193         }
194     };
195 
196     template <typename TOutput>
197     bool operator==(const OPipeIterator<TOutput>& _Left, const OPipeIterator<TOutput>& Right_)
198     {
199         return _Left.Equal_(Right_);
200     }
201 
202     template <typename TOutput>
203     bool operator!=(const OPipeIterator<TOutput>& _Left, const OPipeIterator<TOutput>& Right_)
204     {
205         return !(_Left == Right_);
206     }
207 
208 
209     template < typename TInput, typename TSpec, typename TIteratorSpec >
210     struct Iterator< Pipe< TInput, TSpec >, TIteratorSpec> {
211         typedef IPipeIterator< Pipe< TInput, TSpec > > Type;
212     };
213 
214     template < typename TInput >
215     struct Value< IPipeIterator< TInput > > {
216         typedef typename Value<TInput>::Type Type;
217     };
218 
219     template < typename TInput >
220     struct Size< IPipeIterator< TInput > > {
221         typedef typename Size<TInput>::Type Type;
222     };
223 
224     template < typename TOutput >
225     struct Value< OPipeIterator< TOutput > > {
226         typedef typename Value<TOutput>::Type Type;
227     };
228 
229     template < typename TOutput >
230     struct Size< OPipeIterator< TOutput > > {
231         typedef typename Size<TOutput>::Type Type;
232     };
233 
234 
235     template < typename TInput, typename TSpec >
236     IPipeIterator< Pipe< TInput, TSpec > >
237     begin(Pipe< TInput, TSpec > &pipe) {
238         return IPipeIterator< Pipe< TInput, TSpec > >(pipe);
239     }
240 
241     template < typename TInput, typename TSpec >
242     IPipeIterator< Pipe< TInput, TSpec > >
243     end(Pipe< TInput, TSpec > &/*pipe*/) {
244         return IPipeIterator< Pipe< TInput, TSpec > >();
245     }
246 
247 
248     template < typename TInput >
249     inline typename Difference<TInput>::Type
250     difference(IPipeIterator<TInput> first, IPipeIterator<TInput> last) {
251         return last - first;
252     }
253 
254     template < typename TOutput >
255     inline typename Difference<TOutput>::Type
256     difference(OPipeIterator<TOutput> first, OPipeIterator<TOutput> last) {
257         return last - first;
258     }
259 
260 //}
261 
262 }
263 
264 #endif
265