1 /*==========================================================================
2 SeqAn - The Library for Sequence Analysis
4 ============================================================================
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.
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.
17 ============================================================================
18 $Id: basic_iterator_adaptor.h,v 1.1 2008/08/25 16:20:01 langmead Exp $
19 ==========================================================================*/
21 #ifndef SEQAN_HEADER_BASIC_ITERATOR_ADAPTOR_H
22 #define SEQAN_HEADER_BASIC_ITERATOR_ADAPTOR_H
24 namespace SEQAN_NAMESPACE_MAIN
26 //////////////////////////////////////////////////////////////////////////////
29 //An iterator that adapts a std iterator to a default seqan iterator
30 template <typename TIterator, typename TSpec = Default>
31 struct AdaptorIterator;
33 //////////////////////////////////////////////////////////////////////////////
36 struct Iterator_Default_Imp<T, Rooted>
38 typedef typename Iterator<T, Standard>::Type TStandardIterator;
39 typedef Iter<T, AdaptorIterator<TStandardIterator> > Type;
42 //////////////////////////////////////////////////////////////////////////////
44 //////////////////////////////////////////////////////////////////////////////
47 .Spec.Adaptor Iterator:
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$.
58 template <typename TContainer, typename TIterator, typename TSpec>
59 class Iter<TContainer, AdaptorIterator<TIterator, TSpec> >
62 typename _Pointer<TContainer>::Type data_container;
63 TIterator data_iterator;
64 //____________________________________________________________________________
67 .Memfunc.AdaptorIterator#Iter:
68 ..class:Spec.Adaptor Iterator
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$.
84 data_iterator = TIterator();
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_))
94 Iter(typename _Parameter<TContainer>::Type container_, TIterator it_):
95 data_container(_toPointer(container_)),
100 Iter(Iter const & other_):
101 data_container(other_.data_container),
102 data_iterator(other_.data_iterator)
107 template <typename TSource>
108 Iter(TSource & source)
111 assign(*this, source);
113 template <typename TSource>
114 Iter(TSource const & source)
117 assign(*this, source);
127 operator = (Iter const & other_)
130 data_container = other_.data_container;
131 data_iterator = other_.data_iterator;
135 template <typename TSource>
137 operator = (TSource & source)
140 assign(*this, source);
143 template <typename TSource>
145 operator = (TSource const & source)
148 assign(*this, source);
152 //____________________________________________________________________________
154 friend inline typename _Parameter<TContainer>::Type
158 return _toParameter<TContainer>(me.data_container);
160 friend inline typename _Parameter<TContainer>::Type
161 container(Iter const & me)
164 return _toParameter<TContainer>(me.data_container);
166 //____________________________________________________________________________
169 setContainer(Iter & me, typename _Parameter<TContainer>::Type container_)
172 if (me.data_container && me.data_iterator != TIterator())
174 typename Position<Iter>::Type pos = position(me);
175 me.data_container = _toPointer(container_);
176 setPosition(me, pos);
180 me.data_container = _toPointer(container_);
184 //____________________________________________________________________________
186 friend inline TIterator &
187 hostIterator(Iter & me)
190 return me.data_iterator;
192 friend inline TIterator const &
193 hostIterator(Iter const & me)
196 return me.data_iterator;
199 //____________________________________________________________________________
201 operator TIterator () const
204 return data_iterator;
207 //____________________________________________________________________________
210 //////////////////////////////////////////////////////////////////////////////
212 //////////////////////////////////////////////////////////////////////////////
214 template <typename TContainer, typename TIterator, typename TSpec>
215 struct Reference<Iter<TContainer, AdaptorIterator<TIterator, TSpec> > const>:
216 Reference<TIterator const>
221 //////////////////////////////////////////////////////////////////////////////
223 //////////////////////////////////////////////////////////////////////////////
225 //////////////////////////////////////////////////////////////////////////////
227 //////////////////////////////////////////////////////////////////////////////
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)
234 return hostIterator(me) - begin(container(me), Standard());
237 //____________________________________________________________________________
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,
245 return hostIterator(me) - begin(container(me), Standard());
248 //////////////////////////////////////////////////////////////////////////////
250 //////////////////////////////////////////////////////////////////////////////
252 template <typename TContainer, typename TIterator, typename TSpec, typename TPosition>
254 setPosition(Iter<TContainer, AdaptorIterator<TIterator, TSpec> > & me,
258 hostIterator(me) = begin(container(me), Standard()) + pos_;
261 //////////////////////////////////////////////////////////////////////////////
263 //////////////////////////////////////////////////////////////////////////////
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)
270 return value(hostIterator(me));
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)
277 return value(hostIterator(me));
280 /////////////////////////////////////////////////////////////////////////////
282 //////////////////////////////////////////////////////////////////////////////
284 template <typename TContainer, typename TIterator, typename TSpec, typename TValue>
286 assignValue(Iter<TContainer, AdaptorIterator<TIterator, TSpec> > & me,
287 TValue const & _value)
290 assignValue(hostIterator(me), _value);
292 template <typename TContainer, typename TIterator, typename TSpec, typename TValue>
294 assignValue(Iter<TContainer, AdaptorIterator<TIterator, TSpec> > const & me,
295 TValue const & _value)
298 assignValue(hostIterator(me), _value);
301 /////////////////////////////////////////////////////////////////////////////
303 //////////////////////////////////////////////////////////////////////////////
305 template <typename TContainer, typename TIterator, typename TSpec, typename TValue>
307 moveValue(Iter<TContainer, AdaptorIterator<TIterator, TSpec> > & me,
308 TValue const & _value)
311 moveValue(hostIterator(me), _value);
313 template <typename TContainer, typename TIterator, typename TSpec, typename TValue>
315 moveValue(Iter<TContainer, AdaptorIterator<TIterator, TSpec> > const & me,
316 TValue const & _value)
319 moveValue(hostIterator(me), _value);
322 //////////////////////////////////////////////////////////////////////////////
324 //////////////////////////////////////////////////////////////////////////////
326 template <typename TContainer, typename TIterator, typename TSpec>
328 operator == (Iter<TContainer, AdaptorIterator<TIterator, TSpec> > const & left,
329 Iter<TContainer, AdaptorIterator<TIterator, TSpec> > const & right)
332 return hostIterator(left) == hostIterator(right);
335 //////////////////////////////////////////////////////////////////////////////
337 //////////////////////////////////////////////////////////////////////////////
339 template <typename TContainer, typename TIterator, typename TSpec>
341 operator != (Iter<TContainer, AdaptorIterator<TIterator, TSpec> > const & left,
342 Iter<TContainer, AdaptorIterator<TIterator, TSpec> > const & right)
345 return hostIterator(left) != hostIterator(right);
348 //////////////////////////////////////////////////////////////////////////////
350 //////////////////////////////////////////////////////////////////////////////
352 template <typename TContainer, typename TIterator, typename TSpec>
354 goNext(Iter<TContainer, AdaptorIterator<TIterator, TSpec> > & me)
357 goNext(hostIterator(me));
360 //////////////////////////////////////////////////////////////////////////////
362 //////////////////////////////////////////////////////////////////////////////
364 template <typename TContainer, typename TIterator, typename TSpec>
366 goPrevious(Iter<TContainer, AdaptorIterator<TIterator, TSpec> > & me)
369 goPrevious(hostIterator(me));
372 //////////////////////////////////////////////////////////////////////////////
374 //////////////////////////////////////////////////////////////////////////////
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,
382 return Iter<TContainer, AdaptorIterator<TIterator, TSpec> >(container(left), hostIterator(left) + right);
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,
391 return Iter<TContainer, AdaptorIterator<TIterator, TSpec> >(container(left), hostIterator(left) + right);
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)
400 return Iter<TContainer, AdaptorIterator<TIterator, TSpec> >(container(right), hostIterator(right) + left);
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)
409 return Iter<TContainer, AdaptorIterator<TIterator, TSpec> >(container(right), hostIterator(right) + left);
412 //////////////////////////////////////////////////////////////////////////////
414 //////////////////////////////////////////////////////////////////////////////
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,
422 hostIterator(left) += right;
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,
432 hostIterator(left) += right;
436 //////////////////////////////////////////////////////////////////////////////
438 //////////////////////////////////////////////////////////////////////////////
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,
446 return Iter<TContainer, AdaptorIterator<TIterator, TSpec> >(container(left), hostIterator(left) - right);
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,
455 return Iter<TContainer, AdaptorIterator<TIterator, TSpec> >(container(left), hostIterator(left) - right);
458 //____________________________________________________________________________
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)
466 return hostIterator(left) - hostIterator(right);
469 //////////////////////////////////////////////////////////////////////////////
471 //////////////////////////////////////////////////////////////////////////////
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,
479 hostIterator(left) -= right;
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,
489 hostIterator(left) -= right;
493 //////////////////////////////////////////////////////////////////////////////
494 //////////////////////////////////////////////////////////////////////////////
496 //////////////////////////////////////////////////////////////////////////////
498 template <typename TContainer, typename TIterator, typename TSpec>
500 atEnd(Iter<TContainer, AdaptorIterator<TIterator, TSpec> > & me)
503 return atEnd(me, container(me));
506 //////////////////////////////////////////////////////////////////////////////
507 // assign (Conversion)
508 //////////////////////////////////////////////////////////////////////////////
510 template <typename TTargetContainer, typename TIterator, typename TSpec, typename TSource>
512 assign(Iter<TTargetContainer, AdaptorIterator<TIterator, TSpec> > & target,
513 TSource const & source)
516 target.data_container = container(source);
517 target.data_iterator = begin(container(source)) + position(source);
520 //////////////////////////////////////////////////////////////////////////////
522 } //namespace SEQAN_NAMESPACE_MAIN
524 #endif //#ifndef SEQAN_HEADER_...