Imported Upstream version 0.12.7
[bowtie.git] / SeqAn-1.1 / seqan / basic / basic_iterator_adaptor.h
1  /*==========================================================================
2                 SeqAn - The Library for Sequence Analysis
3                           http://www.seqan.de 
4  ============================================================================
5   Copyright (C) 2007
6
7   This library is free software; you can redistribute it and/or
8   modify it under the terms of the GNU Lesser General Public
9   License as published by the Free Software Foundation; either
10   version 3 of the License, or (at your option) any later version.
11
12   This library is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   Lesser General Public License for more details.
16
17  ============================================================================
18   $Id: basic_iterator_adaptor.h,v 1.1 2008/08/25 16:20:01 langmead Exp $
19  ==========================================================================*/
20
21 #ifndef SEQAN_HEADER_BASIC_ITERATOR_ADAPTOR_H
22 #define SEQAN_HEADER_BASIC_ITERATOR_ADAPTOR_H
23
24 namespace SEQAN_NAMESPACE_MAIN
25 {
26 //////////////////////////////////////////////////////////////////////////////
27 // Tag
28
29 //An iterator that adapts a std iterator to a default seqan iterator
30 template <typename TIterator, typename TSpec = Default>
31 struct AdaptorIterator;
32
33 //////////////////////////////////////////////////////////////////////////////
34
35 template <typename T>
36 struct Iterator_Default_Imp<T, Rooted>
37 {
38         typedef typename Iterator<T, Standard>::Type TStandardIterator;
39         typedef Iter<T, AdaptorIterator<TStandardIterator> > Type;
40 };
41
42 //////////////////////////////////////////////////////////////////////////////
43 // Adaptor Iterator
44 //////////////////////////////////////////////////////////////////////////////
45
46 /**
47 .Spec.Adaptor Iterator:
48 ..cat:Iterators
49 ..general:Class.Iter
50 ..summary:Adapts iterators to @Concept.Rooted Iterator@.
51 ..signature:Iter<TContainer, AdaptorIterator<TIterator [, TSpec]> >
52 ..param.TContainer:Type of the container that can be iterated by $TIterator$.
53 ...remarks:Use @Metafunction.Container@ to get the container type for a given iterator.
54 ..param.TIterator:Type of the iterator that is adapted to @Concept.Rooted Iterator@.
55 ..remarks.text:Adaptor iterators can implicitly converted to $TIterator$.
56 */
57
58 template <typename TContainer, typename TIterator, typename TSpec>
59 class Iter<TContainer, AdaptorIterator<TIterator, TSpec> >
60 {
61 private:
62         typename _Pointer<TContainer>::Type data_container;
63         TIterator data_iterator;
64 //____________________________________________________________________________
65
66 /**
67 .Memfunc.AdaptorIterator#Iter:
68 ..class:Spec.Adaptor Iterator
69 ..summary:Constructor
70 ..signature:Iter()
71 ..signature:Iter(iter)
72 ..signature:Iter(container [, iterator])
73 ..param.iter:Another adaptor iterator object.
74 ..param.container:The corresponding container object.
75 ..param.iterator:A iterator of $container$. (optional)
76 ...remarks.text:If this argument is omitted, the adaptor iterator is initialized to the @Function.begin.begin iterator@ of $container$.
77 */
78
79 public:
80         Iter():
81                 data_container(0)
82         {
83 SEQAN_CHECKPOINT
84                 data_iterator = TIterator();
85         }
86 /*//TODO: welches "begin" zur initialisierung von "data_iterator" aufrufen?
87         Iter(typename _Parameter<TContainer>::Type container_):
88                 data_container(_toPointer(container_)),
89                 data_iterator(begin(container_))
90         {
91 SEQAN_CHECKPOINT
92         }
93 */
94         Iter(typename _Parameter<TContainer>::Type container_, TIterator it_):
95                 data_container(_toPointer(container_)),
96                 data_iterator(it_)
97         {
98 SEQAN_CHECKPOINT
99         }
100         Iter(Iter const & other_):
101                 data_container(other_.data_container),
102                 data_iterator(other_.data_iterator)
103         {
104 SEQAN_CHECKPOINT
105         }
106 /*
107         template <typename TSource>
108         Iter(TSource & source)
109         {
110 SEQAN_CHECKPOINT
111                 assign(*this, source);
112         }
113         template <typename TSource>
114         Iter(TSource const & source)
115         {
116 SEQAN_CHECKPOINT
117                 assign(*this, source);
118         }
119 */
120
121         ~Iter()
122         {
123 SEQAN_CHECKPOINT
124         }
125
126         Iter const & 
127         operator = (Iter const & other_)
128         {
129 SEQAN_CHECKPOINT
130                 data_container = other_.data_container;
131                 data_iterator = other_.data_iterator;
132                 return *this;
133         }
134 /*
135         template <typename TSource>
136         Iter const & 
137         operator = (TSource & source)
138         {
139 SEQAN_CHECKPOINT
140                 assign(*this, source);
141                 return *this;
142         }
143         template <typename TSource>
144         Iter const & 
145         operator = (TSource const & source)
146         {
147 SEQAN_CHECKPOINT
148                 assign(*this, source);
149                 return *this;
150         }
151 */
152 //____________________________________________________________________________
153
154         friend inline typename _Parameter<TContainer>::Type 
155         container(Iter & me)
156         {
157 SEQAN_CHECKPOINT
158                 return _toParameter<TContainer>(me.data_container);
159         }
160         friend inline typename _Parameter<TContainer>::Type 
161         container(Iter const & me)
162         {
163 SEQAN_CHECKPOINT
164                 return _toParameter<TContainer>(me.data_container);
165         }
166 //____________________________________________________________________________
167
168         friend inline void
169         setContainer(Iter & me, typename _Parameter<TContainer>::Type container_)
170         {
171 SEQAN_CHECKPOINT
172                 if (me.data_container && me.data_iterator != TIterator())
173                 {
174                         typename Position<Iter>::Type pos = position(me);
175                         me.data_container = _toPointer(container_);
176                         setPosition(me, pos);
177                 }
178                 else
179                 {       
180                         me.data_container = _toPointer(container_);
181                 }
182         }
183
184 //____________________________________________________________________________
185
186         friend inline TIterator &
187         hostIterator(Iter & me)
188         {
189 SEQAN_CHECKPOINT
190                 return me.data_iterator;
191         }
192         friend inline TIterator const &
193         hostIterator(Iter const & me)
194         {
195 SEQAN_CHECKPOINT
196                 return me.data_iterator;
197         }
198
199 //____________________________________________________________________________
200
201         operator TIterator () const
202         {
203 SEQAN_CHECKPOINT
204                 return data_iterator;
205         }
206
207 //____________________________________________________________________________
208 };
209
210 //////////////////////////////////////////////////////////////////////////////
211 // METAFUNCTIONS
212 //////////////////////////////////////////////////////////////////////////////
213 /*
214 template <typename TContainer, typename TIterator, typename TSpec>
215 struct Reference<Iter<TContainer, AdaptorIterator<TIterator, TSpec> > const>:
216         Reference<TIterator const>
217 {
218 };
219 */
220
221 //////////////////////////////////////////////////////////////////////////////
222 // FUNCTIONS
223 //////////////////////////////////////////////////////////////////////////////
224
225 //////////////////////////////////////////////////////////////////////////////
226 // position
227 //////////////////////////////////////////////////////////////////////////////
228
229 template <typename TContainer, typename TIterator, typename TSpec>
230 inline typename Position<Iter<TContainer, AdaptorIterator<TIterator, TSpec> > const>::Type 
231 position(Iter<TContainer, AdaptorIterator<TIterator, TSpec> > const & me)
232 {
233 SEQAN_CHECKPOINT
234         return hostIterator(me) - begin(container(me), Standard());
235 }
236
237 //____________________________________________________________________________
238
239 template <typename TContainer, typename TIterator, typename TSpec, typename TContainer2>
240 inline typename Position<Iter<TContainer, AdaptorIterator<TIterator, TSpec> > const>::Type 
241 position(Iter<TContainer, AdaptorIterator<TIterator, TSpec> > const & me,
242                  TContainer2 const &)
243 {
244 SEQAN_CHECKPOINT
245         return hostIterator(me) - begin(container(me), Standard());
246 }
247
248 //////////////////////////////////////////////////////////////////////////////
249 // setPosition
250 //////////////////////////////////////////////////////////////////////////////
251
252 template <typename TContainer, typename TIterator, typename TSpec, typename TPosition>
253 inline void 
254 setPosition(Iter<TContainer, AdaptorIterator<TIterator, TSpec> > & me,
255                         TPosition pos_)
256 {
257 SEQAN_CHECKPOINT
258         hostIterator(me) = begin(container(me), Standard()) + pos_;
259 }
260
261 //////////////////////////////////////////////////////////////////////////////
262 // value
263 //////////////////////////////////////////////////////////////////////////////
264
265 template <typename TContainer, typename TIterator, typename TSpec>
266 inline typename Reference<Iter<TContainer, AdaptorIterator<TIterator, TSpec> > >::Type 
267 value(Iter<TContainer, AdaptorIterator<TIterator, TSpec> > & me)
268 {
269 SEQAN_CHECKPOINT
270         return value(hostIterator(me));
271 }
272 template <typename TContainer, typename TIterator, typename TSpec>
273 inline typename Reference<Iter<TContainer, AdaptorIterator<TIterator, TSpec> > const>::Type 
274 value(Iter<TContainer, AdaptorIterator<TIterator, TSpec> > const & me)
275 {
276 SEQAN_CHECKPOINT
277         return value(hostIterator(me));
278 }
279
280 /////////////////////////////////////////////////////////////////////////////
281 // assignValue
282 //////////////////////////////////////////////////////////////////////////////
283
284 template <typename TContainer, typename TIterator, typename TSpec, typename TValue>
285 inline void
286 assignValue(Iter<TContainer, AdaptorIterator<TIterator, TSpec> > & me,
287                         TValue const & _value)
288 {
289 SEQAN_CHECKPOINT
290         assignValue(hostIterator(me), _value);
291 }
292 template <typename TContainer, typename TIterator, typename TSpec, typename TValue>
293 inline void
294 assignValue(Iter<TContainer, AdaptorIterator<TIterator, TSpec> > const & me,
295                         TValue const & _value)
296 {
297 SEQAN_CHECKPOINT
298         assignValue(hostIterator(me), _value);
299 }
300
301 /////////////////////////////////////////////////////////////////////////////
302 // moveValue
303 //////////////////////////////////////////////////////////////////////////////
304
305 template <typename TContainer, typename TIterator, typename TSpec, typename TValue>
306 inline void
307 moveValue(Iter<TContainer, AdaptorIterator<TIterator, TSpec> > & me,
308                   TValue const & _value)
309 {
310 SEQAN_CHECKPOINT
311         moveValue(hostIterator(me), _value);
312 }
313 template <typename TContainer, typename TIterator, typename TSpec, typename TValue>
314 inline void
315 moveValue(Iter<TContainer, AdaptorIterator<TIterator, TSpec> > const & me,
316                   TValue const & _value)
317 {
318 SEQAN_CHECKPOINT
319         moveValue(hostIterator(me), _value);
320 }
321
322 //////////////////////////////////////////////////////////////////////////////
323 // operator ==
324 //////////////////////////////////////////////////////////////////////////////
325
326 template <typename TContainer, typename TIterator, typename TSpec>
327 inline bool 
328 operator == (Iter<TContainer, AdaptorIterator<TIterator, TSpec> > const & left,
329                          Iter<TContainer, AdaptorIterator<TIterator, TSpec> > const & right)
330 {
331 SEQAN_CHECKPOINT
332         return hostIterator(left) == hostIterator(right);
333 }
334
335 //////////////////////////////////////////////////////////////////////////////
336 // operator !=
337 //////////////////////////////////////////////////////////////////////////////
338
339 template <typename TContainer, typename TIterator, typename TSpec>
340 inline bool 
341 operator != (Iter<TContainer, AdaptorIterator<TIterator, TSpec> > const & left,
342                          Iter<TContainer, AdaptorIterator<TIterator, TSpec> > const & right)
343 {
344 SEQAN_CHECKPOINT
345         return hostIterator(left) != hostIterator(right);
346 }
347
348 //////////////////////////////////////////////////////////////////////////////
349 // goNext
350 //////////////////////////////////////////////////////////////////////////////
351
352 template <typename TContainer, typename TIterator, typename TSpec>
353 inline void
354 goNext(Iter<TContainer, AdaptorIterator<TIterator, TSpec> > & me)
355 {
356 SEQAN_CHECKPOINT
357         goNext(hostIterator(me));
358 }
359
360 //////////////////////////////////////////////////////////////////////////////
361 // goPrevious
362 //////////////////////////////////////////////////////////////////////////////
363
364 template <typename TContainer, typename TIterator, typename TSpec>
365 inline void
366 goPrevious(Iter<TContainer, AdaptorIterator<TIterator, TSpec> > & me)
367 {
368 SEQAN_CHECKPOINT
369         goPrevious(hostIterator(me));
370 }
371
372 //////////////////////////////////////////////////////////////////////////////
373 // operator +
374 //////////////////////////////////////////////////////////////////////////////
375
376 template <typename TContainer, typename TIterator, typename TSpec, typename TIntegral>
377 inline Iter<TContainer, AdaptorIterator<TIterator, TSpec> >  
378 operator + (Iter<TContainer, AdaptorIterator<TIterator, TSpec> > const & left,
379                         TIntegral right)
380 {
381 SEQAN_CHECKPOINT
382         return Iter<TContainer, AdaptorIterator<TIterator, TSpec> >(container(left), hostIterator(left) + right);
383 }
384 // for <anonymous enum> types
385 template <typename TContainer, typename TIterator, typename TSpec>
386 inline Iter<TContainer, AdaptorIterator<TIterator, TSpec> >  
387 operator + (Iter<TContainer, AdaptorIterator<TIterator, TSpec> > const & left,
388                         int right)
389 {
390 SEQAN_CHECKPOINT
391         return Iter<TContainer, AdaptorIterator<TIterator, TSpec> >(container(left), hostIterator(left) + right);
392 }
393
394 template <typename TContainer, typename TIterator, typename TSpec, typename TIntegral>
395 inline Iter<TContainer, AdaptorIterator<TIterator, TSpec> >  
396 operator + (TIntegral left,
397                         Iter<TContainer, AdaptorIterator<TIterator, TSpec> > const & right)
398 {
399 SEQAN_CHECKPOINT
400         return Iter<TContainer, AdaptorIterator<TIterator, TSpec> >(container(right), hostIterator(right) + left);
401 }
402 // for <anonymous enum> types
403 template <typename TContainer, typename TIterator, typename TSpec>
404 inline Iter<TContainer, AdaptorIterator<TIterator, TSpec> >  
405 operator + (int left,
406                         Iter<TContainer, AdaptorIterator<TIterator, TSpec> > const & right)
407 {
408 SEQAN_CHECKPOINT
409         return Iter<TContainer, AdaptorIterator<TIterator, TSpec> >(container(right), hostIterator(right) + left);
410 }
411
412 //////////////////////////////////////////////////////////////////////////////
413 // operator +=
414 //////////////////////////////////////////////////////////////////////////////
415
416 template <typename TContainer, typename TIterator, typename TSpec, typename TIntegral>
417 inline Iter<TContainer, AdaptorIterator<TIterator, TSpec> > &
418 operator += (Iter<TContainer, AdaptorIterator<TIterator, TSpec> > & left,
419                          TIntegral right)
420 {
421 SEQAN_CHECKPOINT
422         hostIterator(left) += right;
423         return left;
424 }
425 // for <anonymous enum> types
426 template <typename TContainer, typename TIterator, typename TSpec>
427 inline Iter<TContainer, AdaptorIterator<TIterator, TSpec> > &
428 operator += (Iter<TContainer, AdaptorIterator<TIterator, TSpec> > & left,
429                          int right)
430 {
431 SEQAN_CHECKPOINT
432         hostIterator(left) += right;
433         return left;
434 }
435
436 //////////////////////////////////////////////////////////////////////////////
437 // operator -
438 //////////////////////////////////////////////////////////////////////////////
439
440 template <typename TContainer, typename TIterator, typename TSpec, typename TIntegral>
441 inline Iter<TContainer, AdaptorIterator<TIterator, TSpec> >  
442 operator - (Iter<TContainer, AdaptorIterator<TIterator, TSpec> > const & left,
443                         TIntegral right)
444 {
445 SEQAN_CHECKPOINT
446         return Iter<TContainer, AdaptorIterator<TIterator, TSpec> >(container(left), hostIterator(left) - right);
447 }
448 // for <anonymous enum> types
449 template <typename TContainer, typename TIterator, typename TSpec>
450 inline Iter<TContainer, AdaptorIterator<TIterator, TSpec> >  
451 operator - (Iter<TContainer, AdaptorIterator<TIterator, TSpec> > const & left,
452                         int right)
453 {
454 SEQAN_CHECKPOINT
455         return Iter<TContainer, AdaptorIterator<TIterator, TSpec> >(container(left), hostIterator(left) - right);
456 }
457
458 //____________________________________________________________________________
459
460 template <typename TContainer, typename TIterator, typename TSpec>
461 inline typename Difference<Iter<TContainer, AdaptorIterator<TIterator, TSpec> > >::Type  
462 operator - (Iter<TContainer, AdaptorIterator<TIterator, TSpec> > const & left,
463                         Iter<TContainer, AdaptorIterator<TIterator, TSpec> > const & right)
464 {
465 SEQAN_CHECKPOINT
466         return hostIterator(left) - hostIterator(right);
467 }
468
469 //////////////////////////////////////////////////////////////////////////////
470 // operator -=
471 //////////////////////////////////////////////////////////////////////////////
472
473 template <typename TContainer, typename TIterator, typename TSpec, typename TIntegral>
474 inline Iter<TContainer, AdaptorIterator<TIterator, TSpec> > &
475 operator -= (Iter<TContainer, AdaptorIterator<TIterator, TSpec> > & left,
476                          TIntegral right)
477 {
478 SEQAN_CHECKPOINT
479         hostIterator(left) -= right;
480         return left;
481 }
482 // for <anonymous enum> types
483 template <typename TContainer, typename TIterator, typename TSpec>
484 inline Iter<TContainer, AdaptorIterator<TIterator, TSpec> > &
485 operator -= (Iter<TContainer, AdaptorIterator<TIterator, TSpec> > & left,
486                          int right)
487 {
488 SEQAN_CHECKPOINT
489         hostIterator(left) -= right;
490         return left;
491 }
492
493 //////////////////////////////////////////////////////////////////////////////
494 //////////////////////////////////////////////////////////////////////////////
495 // atEnd
496 //////////////////////////////////////////////////////////////////////////////
497
498 template <typename TContainer, typename TIterator, typename TSpec>
499 inline bool
500 atEnd(Iter<TContainer, AdaptorIterator<TIterator, TSpec> > & me)
501 {
502 SEQAN_CHECKPOINT
503         return atEnd(me, container(me));
504 }
505
506 //////////////////////////////////////////////////////////////////////////////
507 // assign (Conversion)
508 //////////////////////////////////////////////////////////////////////////////
509
510 template <typename TTargetContainer, typename TIterator, typename TSpec, typename TSource>
511 inline void
512 assign(Iter<TTargetContainer, AdaptorIterator<TIterator, TSpec> > & target,
513            TSource const & source)
514 {
515 SEQAN_CHECKPOINT
516         target.data_container = container(source);
517         target.data_iterator = begin(container(source)) + position(source);
518 }
519
520 //////////////////////////////////////////////////////////////////////////////
521
522 } //namespace SEQAN_NAMESPACE_MAIN
523
524 #endif //#ifndef SEQAN_HEADER_...