Commit patch to not break on spaces.
[bowtie.git] / SeqAn-1.1 / seqan / sequence / segment_suffix.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: segment_suffix.h,v 1.1 2008/08/25 16:20:04 langmead Exp $
19  ==========================================================================*/
20
21 #ifndef SEQAN_HEADER_SEGMENT_SUFFIX_H
22 #define SEQAN_HEADER_SEGMENT_SUFFIX_H
23
24
25 namespace SEQAN_NAMESPACE_MAIN
26 {
27
28 //////////////////////////////////////////////////////////////////////////////
29 // SuffixSegment
30 //////////////////////////////////////////////////////////////////////////////
31
32
33 /**
34 .Spec.SuffixSegment:
35 ..cat:Segments
36 ..summary:End part segment of a sequence.
37 ..general:Class.Segment
38 ..signature:Segment<THost, SuffixSegment>
39 ..param.THost:Type of the whole sequence.
40 ...text:Instances of $Segment<THost, SuffixSegment>$ are suffixes of $THost$ objects.
41 ...remarks:Use @Metafunction.Host@ to get the host type for a given class.
42 ..remarks.note:Since the appropriate segment type depends on the host sequence type, 
43         it is recommended to use the metafunction @Metafunction.Suffix@ instead of explicitely 
44         choose a specialization of @Class.Segment@.
45 ..see:Spec.InfixSegment
46 ..see:Metafunction.Suffix
47 */
48
49 struct SuffixSegment;
50
51 template <typename THost_>
52 class Segment<THost_, SuffixSegment>
53 {
54 protected:
55         typedef typename Host<Segment>::Type THost;
56
57         typename _Pointer<THost>::Type data_host;
58         typename Position<THost>::Type data_begin_position;
59
60 //____________________________________________________________________________
61
62 public:
63
64 /**
65 .Memfunc.SuffixSegment#Segment:
66 ..class:Spec.SuffixSegment
67 ..summary:Constructor
68 ..signature:Segment<THost, SuffixSegment> ()
69 ..signature:Segment<THost, SuffixSegment> (suffix)
70 ..signature:Segment<THost, SuffixSegment> (host [, begin])
71 ..param.suffix:Other suffix object. (copy constructor)
72 ..param.host:The whole sequence.
73 ..param.begin:Position in $host$ of the first item in segment. (optional)
74 ...default:$0$
75 ...type:Metafunction.Position.$Position<THost>::Type$
76 ...type:Metafunction.Iterator.$Iterator<THost>::Type$
77 ..remarks:
78 ...text:A Segment object cannot work without a host. If the object is default constructed,
79 the host must be set by @Function.setHost@ before the segment can be used.
80 ...text:If a segment object is constructed by the copy constructor, the
81 members of the new constructed object are set to the same values as the members in the
82 source object; the host object is not modified.
83 Note that this is a special case, since all other copy operations result in changes 
84 of the host object.
85 ...text:$begin$ must be a valid position/iterator in $host$.
86 If $begin$ is omitted, the suffix segment corresponding to
87 the whole sequence $host$ is constructed.
88 This is the same segment that is returned by @Function.goBegin@.
89 */
90         Segment():
91                 data_begin_position(0)
92         {
93 SEQAN_CHECKPOINT
94         }
95
96         Segment(THost & _host):
97                 data_host(& _host),
98                 data_begin_position(0)
99         {
100 SEQAN_CHECKPOINT
101         }
102
103         Segment(typename _Parameter<THost>::Type _host, typename Position<THost>::Type _begin_index):
104                 data_host(_toPointer(_host)),
105                 data_begin_position(_begin_index)
106         {
107 SEQAN_CHECKPOINT
108         }
109 /*
110         Segment(typename _Parameter<THost>::Type _host, typename Iterator<THost, Rooted>::Type _begin):
111                 data_host(_toPointer(_host)),
112                 data_begin_position(position(_begin))
113         {
114 SEQAN_CHECKPOINT
115         }
116 */
117         Segment(typename _Parameter<THost>::Type _host, typename Iterator<THost, Standard>::Type _begin):
118                 data_host(_toPointer(_host)),
119                 data_begin_position(position(_begin, _host))
120         {
121 SEQAN_CHECKPOINT
122         }
123 /*
124         Segment(Segment const & _other):
125                 data_host(_other.data_host),
126                 data_begin_position(_other.data_begin_position)
127         {
128 SEQAN_CHECKPOINT
129         }
130 */
131         template <typename THost2, typename TSpec2>
132         Segment(Segment<THost2, TSpec2> const & _other):
133                 data_host(_toPointer(host(_other))),
134                 data_begin_position(beginPosition(_other))
135         {
136 SEQAN_CHECKPOINT
137         }
138
139         ~ Segment() 
140         {
141 SEQAN_CHECKPOINT
142         }
143
144         template <typename TSource>
145         inline Segment & 
146         operator = (TSource const & source)
147         {
148                 assign(*this, source);
149                 return *this;
150         }
151         inline Segment & 
152         operator = (Segment const & source)
153         {
154                 assign(*this, source);
155                 return *this;
156         }
157 //____________________________________________________________________________
158
159 public:
160
161         friend inline typename _Parameter<THost>::Type 
162         host(Segment & me)
163         {
164 SEQAN_CHECKPOINT
165                 return _toParameter<THost>(me.data_host);
166         }
167
168         friend inline typename _Parameter<THost>::Type 
169         host(Segment const & me)
170         {
171 SEQAN_CHECKPOINT
172                 return _toParameter<THost>(me.data_host);
173         }
174
175 //____________________________________________________________________________
176
177         friend inline void 
178         setHost(Segment & me, typename _Parameter<THost>::Type _host)
179         {
180 SEQAN_CHECKPOINT
181                 me.data_host = _toPointer(_host);
182         }
183
184 //____________________________________________________________________________
185
186         template <typename TPos>
187         inline typename Reference<Segment>::Type
188         operator [] (TPos pos)
189         {
190 SEQAN_CHECKPOINT
191                 return value(*this, pos);
192         }
193
194         template <typename TPos>
195         inline typename Reference<Segment const>::Type 
196         operator [] (TPos pos) const
197         {
198 SEQAN_CHECKPOINT
199                 return value(*this, pos);
200         }
201
202 //____________________________________________________________________________
203
204         friend inline typename Iterator<Segment, Standard>::Type 
205         begin(Segment & me,
206                 Standard)
207         {
208 SEQAN_CHECKPOINT
209                 return begin(host(me), Standard()) + me.data_begin_position;
210         }
211         friend inline typename Iterator<Segment const, Standard>::Type 
212         begin(Segment const & me,
213                 Standard)
214         {
215 SEQAN_CHECKPOINT
216                 return begin(host(me), Standard()) + me.data_begin_position;
217         }
218
219 //____________________________________________________________________________
220
221         friend inline typename Position<Segment const>::Type 
222         beginPosition(Segment const & me)
223         {
224 SEQAN_CHECKPOINT
225                 return me.data_begin_position;
226         }
227         friend inline typename Position<Segment>::Type 
228         beginPosition(Segment & me)
229         {
230 SEQAN_CHECKPOINT
231                 return me.data_begin_position;
232         }
233 //____________________________________________________________________________
234
235         template <typename TIterator>
236         friend inline void 
237         setBegin(Segment & me, TIterator new_begin)
238         {
239 SEQAN_CHECKPOINT
240                 me.data_begin_position = new_begin - begin(host(me));//, Standard());
241         }
242
243         friend inline void 
244         setBegin(typename Iterator<Segment, Rooted>::Type new_begin)
245         {
246 SEQAN_CHECKPOINT
247                 container(new_begin).data_begin_position = hostIterator(new_begin) - begin(host(container(new_begin)));//, Standard());
248         }
249
250 //____________________________________________________________________________
251
252         template <typename TPosition>
253         friend inline void 
254         setBeginPosition(Segment & me, TPosition new_begin)
255         {
256 SEQAN_CHECKPOINT
257                 me.data_begin_position = new_begin;
258         }
259
260 //____________________________________________________________________________
261
262         friend inline typename Iterator<Segment, Standard>::Type 
263         end(Segment & me,
264                 Standard)
265         {
266 SEQAN_CHECKPOINT
267                 return end(host(me), Standard());
268         }
269         friend inline typename Iterator<Segment const, Standard>::Type 
270         end(Segment const & me,
271                 Standard)
272         {
273 SEQAN_CHECKPOINT
274                 return end(host(me), Standard());
275         }
276
277 //____________________________________________________________________________
278
279
280         friend inline typename Position<Segment>::Type 
281         endPosition(Segment & me)
282         {
283 SEQAN_CHECKPOINT
284                 return length(host(me));
285         }
286
287         friend inline typename Position<Segment const>::Type 
288         endPosition(Segment const & me)
289         {
290 SEQAN_CHECKPOINT
291                 return length(host(me));
292         }
293
294 //____________________________________________________________________________
295
296         template <typename TIterator>
297         friend inline void
298         setEnd(Segment &, TIterator)
299         {
300         }
301
302         friend inline void 
303         _setLength(Segment &, typename Size<THost>::Type)
304         {
305         }
306
307 //____________________________________________________________________________
308 };
309
310 //////////////////////////////////////////////////////////////////////////////
311
312 /**
313 .Metafunction.Suffix:
314 ..summary:Suffix sequence type.
315 ..signature:Suffix<T>::Type
316 ..param.T:A sequence type.
317 ...type:Class.String
318 ..returns.param.Type:The suffix type.
319 ..see:Spec.SuffixSegment
320 ..see:Metafunction.Infix
321 ..see:Metafunction.Prefix
322 */
323
324 struct PrefixSegment;
325 struct InfixSegment;
326
327 template <typename THost>
328 struct Suffix
329 {
330         typedef Segment<THost, SuffixSegment> Type;
331 };
332
333 template <typename THost>
334 struct Suffix< Segment<THost, InfixSegment> >
335 {
336         typedef Segment<THost, InfixSegment> Type;
337 };
338 template <typename THost>
339 struct Suffix< Segment<THost, SuffixSegment> >
340 {
341         typedef Segment<THost, SuffixSegment> Type;
342 };
343 template <typename THost>
344 struct Suffix< Segment<THost, PrefixSegment> >
345 {
346         typedef Segment<THost, InfixSegment> Type;
347 };
348
349 template <typename THost, typename TSpec>
350 struct Suffix< Segment<THost, TSpec> const >:
351         Suffix< Segment<THost, TSpec> > {};
352
353 //////////////////////////////////////////////////////////////////////////////
354
355 template <typename THost, typename TPosition>
356 inline void
357 set(Segment<THost, SuffixSegment> & me,
358         THost & host_,
359         TPosition begin_)
360 {
361 SEQAN_CHECKPOINT
362         setHost(me, host_);
363         setBegin(me, begin_);
364 }
365 //____________________________________________________________________________
366
367 template <typename THost>
368 inline void
369 set(Segment<THost, SuffixSegment> & me,
370         THost & host_)
371 {
372 SEQAN_CHECKPOINT
373         setHost(me, host_);
374         setBegin(me, begin(host_, Standard()));
375 }
376
377 //____________________________________________________________________________
378
379 template <typename THost, typename TSpec>
380 inline void
381 set(Segment<THost, SuffixSegment> & me,
382         Segment<THost, TSpec> & source)
383 {
384 SEQAN_CHECKPOINT
385         setHost(me, host(source));
386         setBeginPosition(me, beginPosition(source));
387 }
388
389 template <typename THost, typename TSpec>
390 inline void
391 set(Segment<THost, SuffixSegment> & me,
392         Segment<THost, TSpec> const & source)
393 {
394 SEQAN_CHECKPOINT
395         setHost(me, host(source));
396         setBeginPosition(me, beginPosition(source));
397 }
398
399 //////////////////////////////////////////////////////////////////////////////
400
401 template <typename THost>
402 inline bool
403 atBegin(Segment<THost, SuffixSegment> const & segment)
404 {
405 SEQAN_CHECKPOINT
406         return (beginPosition(segment) == 0);
407 }
408
409 //////////////////////////////////////////////////////////////////////////////
410
411 template <typename THost>
412 inline bool
413 atEnd(Segment<THost, SuffixSegment> const & segment)
414 {
415 SEQAN_CHECKPOINT
416         return (beginPosition(segment) == length(host(segment)));
417 }
418
419 //////////////////////////////////////////////////////////////////////////////
420
421 template <typename THost>
422 inline void
423 goBegin(Segment<THost, SuffixSegment> & segment,
424                 THost &)
425 {
426 SEQAN_CHECKPOINT
427         goBegin(segment);
428 }
429
430 template <typename THost>
431 inline void
432 goBegin(Segment<THost, SuffixSegment> & segment)
433 {
434         setBegin(segment);
435 }
436
437 //////////////////////////////////////////////////////////////////////////////
438
439 template <typename THost>
440 inline void
441 goEnd(Segment<THost, SuffixSegment> & segment,
442           THost &)
443 {
444 SEQAN_CHECKPOINT
445         goEnd(segment);
446 }
447
448 template <typename THost>
449 inline void
450 goEnd(Segment<THost, SuffixSegment> & segment)
451 {
452         setBegin(segment, length(host(segment))-1);
453 }
454
455 //////////////////////////////////////////////////////////////////////////////
456
457 template <typename THost>
458 inline Segment<THost, SuffixSegment> &
459 operator ++(Segment<THost, SuffixSegment> & segment)
460 {
461         setBegin(segment, beginPosition(segment) + 1);
462         return segment;
463 }
464
465 //////////////////////////////////////////////////////////////////////////////
466
467 template <typename THost>
468 inline Segment<THost, SuffixSegment> &
469 operator --(Segment<THost, SuffixSegment> & segment)
470 {
471         setBegin(segment, beginPosition(segment) - 1);
472         return segment;
473 }
474
475 //////////////////////////////////////////////////////////////////////////////
476
477 /**
478 .Function.suffix:
479 ..cat:Containers
480 ..summary:Creates suffix object.
481 ..signature:suffix(host, begin)
482 ..param.host:The complete sequence.
483 ...type:Class.String
484 ...type:Adaption.char array
485 ..param.begin:Position or iterator of the first element of the segment.
486 ...type:Metafunction.Position
487 ...type:Metafunction.Iterator
488 ..returns:The suffix of $host that begins at $begin$.
489 ...remarks:The type of the suffix is given by @Metafunction.Suffix@.
490 ..remarks:Notational sugar.
491 ..see:Spec.SuffixSegment
492 ..see:Function.infix
493 */
494
495 template <typename T, typename TPosBegin>
496 inline typename Suffix<T>::Type
497 suffix(T & t, TPosBegin pos_begin)
498 {
499 SEQAN_CHECKPOINT
500         return typename Suffix<T>::Type(t, pos_begin);
501 }
502
503 template <typename T, typename TPosBegin>
504 inline typename Suffix<T *>::Type
505 suffix(T * t, TPosBegin pos_begin)
506 {
507 SEQAN_CHECKPOINT
508         return typename Suffix<T *>::Type (t, pos_begin);
509 }
510
511 //////////////////////////////////////////////////////////////////////////////
512 // A suffix of a prefix -> is an infix
513 template <typename T, typename TPosBegin>
514 inline typename Suffix<Segment<T, PrefixSegment> >::Type
515 suffix(Segment<T, PrefixSegment> & t, TPosBegin pos_begin)
516 {
517 SEQAN_CHECKPOINT
518         return typename Suffix<Segment<T, PrefixSegment> >::Type (
519                 host(t), 
520                 beginPosition(t) + pos_begin, 
521                 endPosition(t));
522 }
523 template <typename T, typename TPosBegin>
524 inline typename Suffix<Segment<T, PrefixSegment> const>::Type
525 suffix(Segment<T, PrefixSegment> const & t, TPosBegin pos_begin)
526 {
527 SEQAN_CHECKPOINT
528         return typename Suffix<Segment<T, PrefixSegment> const>::Type (
529                 host(t), 
530                 beginPosition(t) + pos_begin, 
531                 endPosition(t));
532 }
533
534 //////////////////////////////////////////////////////////////////////////////
535 // A suffix of a infix -> is an infix
536 template <typename T, typename TPosBegin>
537 inline typename Suffix<Segment<T, InfixSegment> >::Type
538 suffix(Segment<T, InfixSegment> & t, TPosBegin pos_begin)
539 {
540 SEQAN_CHECKPOINT
541         return typename Suffix<Segment<T, InfixSegment> >::Type (
542                 host(t), 
543                 beginPosition(t) + pos_begin, 
544                 endPosition(t));
545 }
546 template <typename T, typename TPosBegin>
547 inline typename Suffix<Segment<T, InfixSegment> const>::Type
548 suffix(Segment<T, InfixSegment> const & t, TPosBegin pos_begin)
549 {
550 SEQAN_CHECKPOINT
551         return typename Suffix<Segment<T, InfixSegment> const>::Type (
552                 host(t), 
553                 beginPosition(t) + pos_begin, 
554                 endPosition(t));
555 }
556
557
558 //////////////////////////////////////////////////////////////////////////////
559 // A suffix of a suffix -> is a suffix
560 template <typename T, typename TPosBegin>
561 inline typename Suffix<Segment<T, SuffixSegment> >::Type
562 suffix(Segment<T, SuffixSegment> & t, TPosBegin pos_begin)
563 {
564 SEQAN_CHECKPOINT
565         return typename Suffix<Segment<T, SuffixSegment> >::Type (
566                 host(t), 
567                 beginPosition(t) + pos_begin);
568 }
569 template <typename T, typename TPosBegin>
570 inline typename Suffix<Segment<T, SuffixSegment> const>::Type
571 suffix(Segment<T, SuffixSegment> const & t, TPosBegin pos_begin)
572 {
573 SEQAN_CHECKPOINT
574         return typename Suffix<Segment<T, SuffixSegment> const>::Type (
575                 host(t), 
576                 beginPosition(t) + pos_begin);
577 }
578
579 //////////////////////////////////////////////////////////////////////////////
580
581 } //namespace SEQAN_NAMESPACE_MAIN
582
583 #endif //#ifndef SEQAN_HEADER_...