Commit patch to not break on spaces.
[bowtie.git] / SeqAn-1.1 / seqan / basic / basic_iterator.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.h,v 1.1 2008/08/25 16:20:01 langmead Exp $
19  ==========================================================================*/
20
21 #ifndef SEQAN_HEADER_BASIC_ITERATOR_H
22 #define SEQAN_HEADER_BASIC_ITERATOR_H
23
24 namespace SEQAN_NAMESPACE_MAIN
25 {
26 //////////////////////////////////////////////////////////////////////////////
27 // TAGS
28 //////////////////////////////////////////////////////////////////////////////
29
30 /**
31 .Tag.Iterator Spec:
32 ..summary:Specifies the kind of an iterator.
33 ..tag.Rooted:Rooted iterator. 
34 ...remarks
35 ....text:This iterator implements some more advanced functions like
36 @Function.container@ and @Function.position@.
37 ....concept:Concept.Rooted Iterator
38 ..tag.Standard:Standard conform iterator. 
39 ...remarks
40 ....text:Note that standard iterators need not to implement all functions
41 that are available for rooted iterators.
42 ....concept:Concept.Iterator
43 ..remarks.text:The default iterator spec is given by @Metafunction.DefaultIteratorSpec@.
44 ..see:Metafunction.DefaultIteratorSpec
45 ..see:Concept.Iterator
46 */
47
48 struct TagRooted_;
49 typedef Tag<TagRooted_> const Rooted;
50
51 struct TagStandard_;
52 typedef Tag<TagStandard_> const Standard;
53
54
55 //////////////////////////////////////////////////////////////////////////////
56 // METAFUNCTIONS
57 //////////////////////////////////////////////////////////////////////////////
58
59 /**
60 .Metafunction.DefaultIteratorSpec:
61 ..hidefromindex
62 ..summary:Specifies default kind of iterator.
63 ..signature:DefaultIteratorSpec<T>::Type
64 ..param.T:Container type for which the default iterator spec is determined.
65 ...concept:Concept.Container
66 ..returns.param.Type:Iterator spec of $T$.
67 ..see:Metafunction.Iterator
68 */
69
70 template <typename T>
71 struct DefaultIteratorSpec
72 {
73         typedef Standard Type;
74 };
75
76 //////////////////////////////////////////////////////////////////////////////
77
78 /**
79 .Metafunction.DefaultGetIteratorSpec:
80 ..hidefromindex
81 ..summary:Specifies default kind of iterator returned by functions.
82 ..signature:DefaultGetIteratorSpec<T>::Type
83 ..param.T:Container type for which the spec is determined.
84 ...concept:Concept.Container
85 ..returns.param.Type:Iterator spec of $T$.
86 ..remarks:This metafunction returns the iterator spec of iterators that are returned by functions like 
87 @Function.begin@, @Function.end@, or @Function.iter@.
88 ..see:Metafunction.Iterator
89 ..see:Metafunction.DefaultIteratorSpec
90 */
91
92 template <typename T>
93 struct DefaultGetIteratorSpec
94 {
95         typedef Rooted Type;
96 };
97
98 //////////////////////////////////////////////////////////////////////////////
99
100 /**
101 .Metafunction.Iterator:
102 ..summary:Type of iterator objects that are used to traverse the container.
103 ..signature:Iterator<T, TSpec>::Type
104 ..param.T:Type for which the iterator type is determined.
105 ...concept:Concept.Container
106 ...type:Class.Iter
107 ..param.TSpec:Specifies an @Tag.Iterator Spec.iterator spec@.
108 ...default:The default iterator spec is given by @Metafunction.DefaultIteratorSpec@.
109 ..returns.param.Type:Iterator type of $T$.
110 ..remarks.text:Iterators behave like pointers in some respects. 
111  For example, you can use $*it$ to access the value object the iterator $it$ points to.
112  But note that $Iterator<T>::Type$ can differ from $T *$, depending on $T$.
113 ..see:Metafunction.Position
114 */
115
116 //____________________________________________________________________________
117
118 template <typename T, typename TSpec>
119 struct Iterator_Default_Imp;
120
121 //Iterator_Default_Imp<T, Standard> is implemented in basic_iterator_simple.h
122 //Iterator_Default_Imp<T, Rooted> is implemented in basic_iterator_adaptor.h 
123
124 //____________________________________________________________________________
125
126 template <typename T, typename TSpec = typename DefaultIteratorSpec<T>::Type>
127 struct Iterator:
128         Iterator_Default_Imp<T, TSpec>
129 {
130 };
131
132
133 //////////////////////////////////////////////////////////////////////////////
134
135 /**
136 .Metafunction.Container:
137 ..summary:Type of the container given an iterator.
138 ..signature:Container<T>::Type
139 ..param.T:Iterator type.
140 ...type:Class.Iter
141 ...concept:Concept.Iterator
142 ..returns.param.Type:The container type to $T$.
143 */
144
145 template <typename T>
146 struct Container
147 {
148         typedef T Type;
149 };
150
151
152 //////////////////////////////////////////////////////////////////////////////
153 // GENERAL FUNCTIONS
154 //////////////////////////////////////////////////////////////////////////////
155
156 //////////////////////////////////////////////////////////////////////////////
157 // value
158 //////////////////////////////////////////////////////////////////////////////
159
160 /**
161 .Function.value:
162 ..signature:Reference value(object)
163 ..param.object:An object that holds a value or an iterator that points to a value.
164 ...type:Class.Iter
165 ...concept:Concept.Iterator
166 */
167
168 template <typename T>
169 inline typename Reference<T>::Type
170 value(T & me)
171 {
172 SEQAN_CHECKPOINT
173         return *me;
174
175 template <typename T>
176 inline typename Reference<T const>::Type
177 value(T const & me)
178 {
179 SEQAN_CHECKPOINT
180         return *me;
181
182
183
184 template <typename T>
185 inline T &
186 value(T * me)
187 {
188 SEQAN_CHECKPOINT
189         return *me;
190
191
192 //////////////////////////////////////////////////////////////////////////////
193 // getValue
194 //////////////////////////////////////////////////////////////////////////////
195
196 //unary getValue
197 /**
198 .Function.getValue:
199 ..cat:Iteration
200 ..signature:GetValue getValue(object)
201 ..param.object:An object that holds a value or points to a value.
202 ...type:Class.Iter
203 ...concept:Concept.Iterator
204 ..see:Metafunction.GetValue
205 */
206
207 template <typename T>
208 inline typename GetValue<T>::Type
209 getValue(T & me)
210 {
211 SEQAN_CHECKPOINT
212         return value(me);
213
214 template <typename T>
215 inline typename GetValue<T const>::Type
216 getValue(T const & me)
217 {
218 SEQAN_CHECKPOINT
219         return value(me);
220
221
222 template <typename T>
223 inline T &
224 getValue(T * me)
225 {
226 SEQAN_CHECKPOINT
227         return value(me);
228
229
230 //////////////////////////////////////////////////////////////////////////////
231 //toGetValue
232 //////////////////////////////////////////////////////////////////////////////
233 //Nimmt eine Reference und macht daraus einen GetValue
234 //???TODO toGetValue()
235
236 //////////////////////////////////////////////////////////////////////////////
237 // assignValue
238 //////////////////////////////////////////////////////////////////////////////
239 /**
240 .Function.assignValue:
241 ..cat:Iteration
242 ..summary:Assigns value to item.
243 ..signature:assignValue(object, value)
244 ..param.object:An object that holds a value or points to a value.
245 ...type:Class.Iter
246 ...concept:Concept.Iterator
247 ..param.value:A value that is assigned to the item $object$ holds or points to.
248 ..remarks.text:This function is similar to @Function.assign@.
249 The difference is, that $assignValue$ just changes a value stored in $object$ or the value $object$ points to, 
250 while @Function.assign@ changes the whole object.
251 ..see:Function.assign
252 */
253
254 template <typename T, typename TValue>
255 inline void
256 assignValue(T & me,
257                         TValue const & _value)
258 {
259 SEQAN_CHECKPOINT
260         assign(value(me), _value);
261
262
263 //const version for iterators as targets
264 template <typename T, typename TValue>
265 inline void
266 assignValue(T const & me,
267                         TValue const & _value)
268 {
269 SEQAN_CHECKPOINT
270         assign(value(me), _value);
271
272
273 //////////////////////////////////////////////////////////////////////////////
274 // moveValue
275 //////////////////////////////////////////////////////////////////////////////
276
277 /**
278 .Function.moveValue:
279 ..cat:Iteration
280 ..summary:Assigns value to item.
281 ..signature:moveValue(object, value)
282 ..param.object:An object that holds a value or points to a value.
283 ...type:Class.Iter
284 ...concept:Concept.Iterator
285 ..param.value:A value that is handed over to the item $object$ holds or points to.
286 ..remarks.text:This function is similar to @Function.move@.
287 The difference is, that $moveValue$ just changes a value stored in $object$ or the value $object$ points to, 
288 while @Function.move@ changes the whole object.
289 ..see:Function.move
290 ..see:Function.assignValue
291 */
292
293 template <typename T, typename TValue>
294 inline void
295 moveValue(T & me,
296                   TValue const & _value)
297 {
298 SEQAN_CHECKPOINT
299         move(value(me), _value);
300 }
301 //const version for iterators as targets
302 template <typename T, typename TValue>
303 inline void
304 moveValue(T const & me,
305                   TValue const & _value)
306 {
307 SEQAN_CHECKPOINT
308         move(value(me), _value);
309
310
311 //////////////////////////////////////////////////////////////////////////////
312
313 /**
314 .Function.container:
315 ..cat:Iteration
316 ..summary:Container of an iterator.
317 ..signature:Container container(iterator)
318 ..param.iterator:An iterator.
319 ...type:Class.Iter
320 ...concept:Concept.Rooted Iterator
321 ..returns:The container that $iterator$ traverses.
322 ...metafunction:Metafunction.Container
323 */
324
325 template <typename T>
326 inline typename Container<T>::Type 
327 container(T me)
328 {
329 SEQAN_CHECKPOINT
330         return me;
331 }
332
333 //////////////////////////////////////////////////////////////////////////////
334
335 /**
336 .Function.position:
337 ..summary:Position of an iterator.
338 ..cat:Iteration
339 ..signature:Position position(iterator [, container])
340 ..param.iterator:An iterator.
341 ...type:Class.Iter
342 ...concept:Concept.Iterator
343 ..param.container:A container.
344 ...concept:Concept.Container
345 ...remarks:If $iterator$ implements @Concept.Rooted Iterator@, then $container$ is optional.
346 ...remarks:If $container$ is specified, $iterator$ must be a container of $container$.
347 ..returns:The position of the value in the container $iterator$ points to.
348 ...metafunction:Metafunction.Position
349 */
350
351 template <typename T>
352 inline typename Position<T>::Type 
353 position(T * me)
354 {
355 SEQAN_CHECKPOINT
356         return 0;
357 }
358
359 template <typename TContainer, typename TIterator>
360 inline typename Position<TContainer>::Type 
361 position(TIterator const & it,
362                  TContainer const & me)
363 {
364 SEQAN_CHECKPOINT
365         return it - begin(me, Standard());
366 }
367
368 //////////////////////////////////////////////////////////////////////////////
369
370
371 //////////////////////////////////////////////////////////////////////////////
372 // atBegin
373 //////////////////////////////////////////////////////////////////////////////
374
375 /**
376 .Function.atBegin:
377 ..cat:Iteration
378 ..summary:Determines whether an iterator is at the beginning position.
379 ..signature:bool atBegin(iterator [, container])
380 ..param.iterator:An iterator.
381 ...type:Class.Iter
382 ...concept:Concept.Iterator
383 ..param.container:Container of $iterator$. (optional)
384 ...remarks.text:If $iterator$ implements @Concept.Rooted Iterator@ then $container$ is optional otherwise $container$ is required.
385 ..returns:$true$ if $iterator$ points to the fist item of the container, otherwise $false$.
386 ..see:Function.begin
387 */
388
389 //TODO???: Was, wenn der Container leer ist?
390
391 template <typename T, typename TContainer>
392 inline bool
393 atBegin(T const & it, TContainer const & cont)
394 {
395 SEQAN_CHECKPOINT
396         return it == begin(cont, Standard());   
397 }
398
399 //____________________________________________________________________________
400
401 template <typename T>
402 inline bool
403 atBegin(T const & it)
404 {
405 SEQAN_CHECKPOINT
406         return atBegin(it, container(it));      
407 }
408
409
410
411 //////////////////////////////////////////////////////////////////////////////
412 // atEnd
413 //////////////////////////////////////////////////////////////////////////////
414 /**
415 .Function.atEnd:
416 ..cat:Iteration
417 ..summary:Determines whether an iterator is at the end position. 
418 ..signature:bool atEnd(iterator [, container])
419 ..param.iterator:An iterator.
420 ...type:Class.Iter
421 ...concept:Concept.Iterator
422 ..param.container:Container of $iterator$.
423 ...remarks.text:If $iterator$ implements @Concept.Rooted Iterator@ then $container$ is optional.
424 ....text:$container$ is also optional for iterators to @Adaption.char array.char arrays@.
425 ....text:Otherwise, $container$ is required.
426 ..returns:$true$ if $iterator$ points behind the last item of the container, otherwise $false$.
427 ..see:Function.atBegin
428 ..see:Function.end
429 */
430
431 template <typename T, typename TContainer>
432 inline bool
433 atEnd(T & it, 
434           TContainer const & cont)
435 {
436 SEQAN_CHECKPOINT
437         return it == end(cont, Standard());     
438 }
439 template <typename T, typename TContainer>
440 inline bool
441 atEnd(T const & it, 
442           TContainer const & cont)
443 {
444 SEQAN_CHECKPOINT
445         return it == end(cont, Standard());     
446 }
447 //____________________________________________________________________________
448
449 template <typename T>
450 inline bool
451 atEnd(T & it)
452 {
453 SEQAN_CHECKPOINT
454         return atEnd(it, container(it));        
455 }
456 template <typename T>
457 inline bool
458 atEnd(T const & it)
459 {
460 SEQAN_CHECKPOINT
461         return atEnd(it, container(it));        
462 }
463
464 //////////////////////////////////////////////////////////////////////////////
465 // goBegin
466 //////////////////////////////////////////////////////////////////////////////
467 /**
468 .Function.goBegin:
469 ..cat:Iteration
470 ..summary:Iterates to the first position of a container. 
471 ..signature:goBegin(iterator [, container])
472 ..param.iterator:Object that iterates through $container$.
473 ...type:Class.Iter
474 ...concept:Concept.Iterator
475 ...text:$iterator$ is set to the position of the first item in $container$.
476 ..param.container:Container of $iterator$.
477 ...remarks.text:If $iterator$ implements @Concept.Rooted Iterator@ then $container$ is optional,
478 otherwise $container$ is required.
479 ..remarks:This function is equivalent to $iterator = begin(container)$.
480 ..see:Function.begin
481 ..see:Function.atBegin
482 ..see:Function.goEnd
483 */
484 template <typename TIterator, typename TContainer>
485 inline void
486 goBegin(TIterator & it,
487                 TContainer & container)
488 {
489 SEQAN_CHECKPOINT
490         it = begin(container);
491 }
492 /*
493 template <typename TIterator, typename TContainer>
494 inline void
495 goBegin(TIterator & it,
496                 TContainer const & container)
497 {
498 SEQAN_CHECKPOINT
499         it = begin(container);
500 }
501 */
502
503 template <typename TIterator>
504 inline void
505 goBegin(TIterator & it)
506 {
507 SEQAN_CHECKPOINT
508         goBegin(it, container(it));
509 }
510
511 //////////////////////////////////////////////////////////////////////////////
512 // goEnd
513 //////////////////////////////////////////////////////////////////////////////
514 /**
515 .Function.goEnd:
516 ..cat:Iteration
517 ..summary:Iterates to the last position of a container. 
518 ..signature:goEnd(iterator [, container])
519 ..param.iterator:Object that iterates through $container$.
520 ...type:Class.Iter
521 ...concept:Concept.Iterator
522 ...text:$iterator$ is set to the position behin the last item in $container$.
523 ..param.container:Container of $iterator$.
524 ...remarks.text:If $iterator$ implements @Concept.Rooted Iterator@ then $container$ is optional,
525 otherwise $container$ is required.
526 ..remarks:This function is equivalent to $iterator = end(container)$.
527 ..see:Function.end
528 ..see:Function.atEnd
529 ..see:Function.goBegin
530 ..see:Function.goEnd
531 */
532 template <typename TIterator, typename TContainer>
533 inline void
534 goEnd(TIterator & it,
535           TContainer & container)
536 {
537 SEQAN_CHECKPOINT
538         it = end(container);
539 }
540 template <typename TIterator, typename TContainer>
541 inline void
542 goEnd(TIterator & it,
543           TContainer const & container)
544 {
545 SEQAN_CHECKPOINT
546         it = end(container);
547 }
548
549 template <typename TIterator>
550 inline void
551 goEnd(TIterator & it)
552 {
553 SEQAN_CHECKPOINT
554         goEnd(it, container(it));
555 }
556
557 //////////////////////////////////////////////////////////////////////////////
558 // goNext
559 //////////////////////////////////////////////////////////////////////////////
560 /**
561 .Function.goNext:
562 ..cat:Iteration
563 ..summary:Iterates to next position. 
564 ..signature:goNext(iterator)
565 ..param.iterator:An iterator.
566 ...type:Class.Iter
567 ...concept:Concept.Iterator
568 ...text:$iterator$ is set to the next position of an iteration through its container.
569 ..remarks:This function is equivalent to $++iterator$.
570 ..see:Function.goBegin
571 ..see:Function.goEnd
572 */
573 template <typename TIterator>
574 inline void
575 goNext(TIterator & it)
576 {
577 SEQAN_CHECKPOINT
578         ++it;
579 }
580
581 //////////////////////////////////////////////////////////////////////////////
582 // goFurther
583 //////////////////////////////////////////////////////////////////////////////
584 /**
585 .Function.goFurther:
586 ..cat:Iteration
587 ..summary:Iterates some steps further. 
588 ..signature:goFurther(iterator, steps)
589 ..param.iterator:An iterator.
590 ...type:Class.Iter
591 ...concept:Concept.Iterator
592 ...text:$iterator$ is set $steps$ positions further in the iteration through the container.
593 ..param.steps:Number of steps $iterator$ should be moved further.
594 ...remarks:If $iterator$ supports bidirectional iteration, $steps$ could also be negativ.
595 ..remarks:This function is equivalent to $iterator += steps$ for random access iterators.
596 ..see:Function.goNext
597 ..see:Function.goPrevious
598 */
599
600 template <typename TIterator, typename TDiff> inline
601 void goFurther(TIterator & it, TDiff steps)
602 {       // return distance type from arbitrary argument
603     it += steps;
604 }
605
606 //////////////////////////////////////////////////////////////////////////////
607 // goPrevious
608 //////////////////////////////////////////////////////////////////////////////
609 /**
610 .Function.goPrevious:
611 ..cat:Iteration
612 ..summary:Iterates to pevious position. 
613 ..signature:goPrevious(iterator)
614 ..param.iterator:An iterator.
615 ...type:Class.Iter
616 ...concept:Concept.Iterator
617 ...text:$iterator$ is set to the pevious position of an iteration through its container.
618 ..remarks:This function is equivalent to $--iterator$.
619 ..see:Function.goBegin
620 ..see:Function.goEnd
621 ..see:Function.goNext
622 */
623 template <typename TIterator>
624 inline void
625 goPrevious(TIterator & it)
626 {
627 SEQAN_CHECKPOINT
628         --it;
629 }
630
631 //////////////////////////////////////////////////////////////////////////////
632 // difference
633 //////////////////////////////////////////////////////////////////////////////
634 /**
635 .Function.difference:
636 ..cat:Iteration
637 ..summary:The difference between two iterators. 
638 ..signature:difference(begin, end)
639 ..param.begin:Iterator to the first position of a range.
640 ...type:Class.Iter
641 ...Concept.Iterator
642 ..param.end:Iterator behind the last position of a range.
643 ...type:Class.Iter
644 ...Concept.Iterator
645 ..returns:Length of the range between $begin$ and $end$.
646 ..remarks:This function is equivalent to $begin - end$.
647 ...text:Usually, $begin$ and $end$ have the same type.
648 ..see:Function.begin
649 ..see:Function.end
650 ..see:Function.length
651 */
652
653 template <typename TIterator> inline
654 typename Difference<TIterator>::Type difference(
655         TIterator const & begin, 
656         TIterator const & end)
657 {       // return distance type from arbitrary argument
658 SEQAN_CHECKPOINT
659     return end - begin;
660 }
661
662 //////////////////////////////////////////////////////////////////////////////
663 // goNil
664 //////////////////////////////////////////////////////////////////////////////
665
666
667 /**
668 .Function.goNil:
669 ..cat:Iteration
670 ..summary:Moves iterator to nil position.
671 ..signature:goNil(iterator)
672 ..param.iterator:The iterator that will be moved.
673 ...type:Class.String
674 ..remarks:$iterator$ is set to an invalid position, e.g. $NULL$ for pointer types.
675 ..see:Function.clear
676 */
677
678 template <typename TIterator>
679 inline void
680 goNil(TIterator & me)
681 {
682 SEQAN_CHECKPOINT
683         me = TIterator();
684 }
685
686 template <typename TIterator>
687 inline void
688 goNil(TIterator * & me)
689 {
690 SEQAN_CHECKPOINT
691         me = 0;
692 }
693
694 //////////////////////////////////////////////////////////////////////////////
695 // atNil
696 //////////////////////////////////////////////////////////////////////////////
697
698
699 /**
700 .Function.atNil:
701 ..cat:Iteration
702 ..summary:Tests whether iterator is at nil position.
703 ..signature:bool atNil(iterator)
704 ..param.iterator:An iterator.
705 ...type:Class.String
706 ..returns:$true$ if $iterator$ points to an ivalid position, e.g. $iterator$ is a $NULL$ pointer.
707 $false$ otherwise.
708 ..see:Function.goNil
709 */
710
711 template <typename TIterator>
712 inline bool
713 atNil(TIterator & me)
714 {
715 SEQAN_CHECKPOINT
716         return me == TIterator();
717 }
718
719
720 template <typename TIterator>
721 inline bool
722 atNil(TIterator * me)
723 {
724 SEQAN_CHECKPOINT
725         return me == 0;
726 }
727
728 //////////////////////////////////////////////////////////////////////////////
729
730 } //namespace SEQAN_NAMESPACE_MAIN
731
732 #endif //#ifndef SEQAN_HEADER_...