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: segment_suffix.h,v 1.1 2008/08/25 16:20:04 langmead Exp $
19 ==========================================================================*/
21 #ifndef SEQAN_HEADER_SEGMENT_SUFFIX_H
22 #define SEQAN_HEADER_SEGMENT_SUFFIX_H
25 namespace SEQAN_NAMESPACE_MAIN
28 //////////////////////////////////////////////////////////////////////////////
30 //////////////////////////////////////////////////////////////////////////////
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
51 template <typename THost_>
52 class Segment<THost_, SuffixSegment>
55 typedef typename Host<Segment>::Type THost;
57 typename _Pointer<THost>::Type data_host;
58 typename Position<THost>::Type data_begin_position;
60 //____________________________________________________________________________
65 .Memfunc.SuffixSegment#Segment:
66 ..class:Spec.SuffixSegment
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)
75 ...type:Metafunction.Position.$Position<THost>::Type$
76 ...type:Metafunction.Iterator.$Iterator<THost>::Type$
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
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@.
91 data_begin_position(0)
96 Segment(THost & _host):
98 data_begin_position(0)
103 Segment(typename _Parameter<THost>::Type _host, typename Position<THost>::Type _begin_index):
104 data_host(_toPointer(_host)),
105 data_begin_position(_begin_index)
110 Segment(typename _Parameter<THost>::Type _host, typename Iterator<THost, Rooted>::Type _begin):
111 data_host(_toPointer(_host)),
112 data_begin_position(position(_begin))
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))
124 Segment(Segment const & _other):
125 data_host(_other.data_host),
126 data_begin_position(_other.data_begin_position)
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))
144 template <typename TSource>
146 operator = (TSource const & source)
148 assign(*this, source);
152 operator = (Segment const & source)
154 assign(*this, source);
157 //____________________________________________________________________________
161 friend inline typename _Parameter<THost>::Type
165 return _toParameter<THost>(me.data_host);
168 friend inline typename _Parameter<THost>::Type
169 host(Segment const & me)
172 return _toParameter<THost>(me.data_host);
175 //____________________________________________________________________________
178 setHost(Segment & me, typename _Parameter<THost>::Type _host)
181 me.data_host = _toPointer(_host);
184 //____________________________________________________________________________
186 template <typename TPos>
187 inline typename Reference<Segment>::Type
188 operator [] (TPos pos)
191 return value(*this, pos);
194 template <typename TPos>
195 inline typename Reference<Segment const>::Type
196 operator [] (TPos pos) const
199 return value(*this, pos);
202 //____________________________________________________________________________
204 friend inline typename Iterator<Segment, Standard>::Type
209 return begin(host(me), Standard()) + me.data_begin_position;
211 friend inline typename Iterator<Segment const, Standard>::Type
212 begin(Segment const & me,
216 return begin(host(me), Standard()) + me.data_begin_position;
219 //____________________________________________________________________________
221 friend inline typename Position<Segment const>::Type
222 beginPosition(Segment const & me)
225 return me.data_begin_position;
227 friend inline typename Position<Segment>::Type
228 beginPosition(Segment & me)
231 return me.data_begin_position;
233 //____________________________________________________________________________
235 template <typename TIterator>
237 setBegin(Segment & me, TIterator new_begin)
240 me.data_begin_position = new_begin - begin(host(me));//, Standard());
244 setBegin(typename Iterator<Segment, Rooted>::Type new_begin)
247 container(new_begin).data_begin_position = hostIterator(new_begin) - begin(host(container(new_begin)));//, Standard());
250 //____________________________________________________________________________
252 template <typename TPosition>
254 setBeginPosition(Segment & me, TPosition new_begin)
257 me.data_begin_position = new_begin;
260 //____________________________________________________________________________
262 friend inline typename Iterator<Segment, Standard>::Type
267 return end(host(me), Standard());
269 friend inline typename Iterator<Segment const, Standard>::Type
270 end(Segment const & me,
274 return end(host(me), Standard());
277 //____________________________________________________________________________
280 friend inline typename Position<Segment>::Type
281 endPosition(Segment & me)
284 return length(host(me));
287 friend inline typename Position<Segment const>::Type
288 endPosition(Segment const & me)
291 return length(host(me));
294 //____________________________________________________________________________
296 template <typename TIterator>
298 setEnd(Segment &, TIterator)
303 _setLength(Segment &, typename Size<THost>::Type)
307 //____________________________________________________________________________
310 //////////////////////////////////////////////////////////////////////////////
313 .Metafunction.Suffix:
314 ..summary:Suffix sequence type.
315 ..signature:Suffix<T>::Type
316 ..param.T:A sequence type.
318 ..returns.param.Type:The suffix type.
319 ..see:Spec.SuffixSegment
320 ..see:Metafunction.Infix
321 ..see:Metafunction.Prefix
324 struct PrefixSegment;
327 template <typename THost>
330 typedef Segment<THost, SuffixSegment> Type;
333 template <typename THost>
334 struct Suffix< Segment<THost, InfixSegment> >
336 typedef Segment<THost, InfixSegment> Type;
338 template <typename THost>
339 struct Suffix< Segment<THost, SuffixSegment> >
341 typedef Segment<THost, SuffixSegment> Type;
343 template <typename THost>
344 struct Suffix< Segment<THost, PrefixSegment> >
346 typedef Segment<THost, InfixSegment> Type;
349 template <typename THost, typename TSpec>
350 struct Suffix< Segment<THost, TSpec> const >:
351 Suffix< Segment<THost, TSpec> > {};
353 //////////////////////////////////////////////////////////////////////////////
355 template <typename THost, typename TPosition>
357 set(Segment<THost, SuffixSegment> & me,
363 setBegin(me, begin_);
365 //____________________________________________________________________________
367 template <typename THost>
369 set(Segment<THost, SuffixSegment> & me,
374 setBegin(me, begin(host_, Standard()));
377 //____________________________________________________________________________
379 template <typename THost, typename TSpec>
381 set(Segment<THost, SuffixSegment> & me,
382 Segment<THost, TSpec> & source)
385 setHost(me, host(source));
386 setBeginPosition(me, beginPosition(source));
389 template <typename THost, typename TSpec>
391 set(Segment<THost, SuffixSegment> & me,
392 Segment<THost, TSpec> const & source)
395 setHost(me, host(source));
396 setBeginPosition(me, beginPosition(source));
399 //////////////////////////////////////////////////////////////////////////////
401 template <typename THost>
403 atBegin(Segment<THost, SuffixSegment> const & segment)
406 return (beginPosition(segment) == 0);
409 //////////////////////////////////////////////////////////////////////////////
411 template <typename THost>
413 atEnd(Segment<THost, SuffixSegment> const & segment)
416 return (beginPosition(segment) == length(host(segment)));
419 //////////////////////////////////////////////////////////////////////////////
421 template <typename THost>
423 goBegin(Segment<THost, SuffixSegment> & segment,
430 template <typename THost>
432 goBegin(Segment<THost, SuffixSegment> & segment)
437 //////////////////////////////////////////////////////////////////////////////
439 template <typename THost>
441 goEnd(Segment<THost, SuffixSegment> & segment,
448 template <typename THost>
450 goEnd(Segment<THost, SuffixSegment> & segment)
452 setBegin(segment, length(host(segment))-1);
455 //////////////////////////////////////////////////////////////////////////////
457 template <typename THost>
458 inline Segment<THost, SuffixSegment> &
459 operator ++(Segment<THost, SuffixSegment> & segment)
461 setBegin(segment, beginPosition(segment) + 1);
465 //////////////////////////////////////////////////////////////////////////////
467 template <typename THost>
468 inline Segment<THost, SuffixSegment> &
469 operator --(Segment<THost, SuffixSegment> & segment)
471 setBegin(segment, beginPosition(segment) - 1);
475 //////////////////////////////////////////////////////////////////////////////
480 ..summary:Creates suffix object.
481 ..signature:suffix(host, begin)
482 ..param.host:The complete sequence.
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
495 template <typename T, typename TPosBegin>
496 inline typename Suffix<T>::Type
497 suffix(T & t, TPosBegin pos_begin)
500 return typename Suffix<T>::Type(t, pos_begin);
503 template <typename T, typename TPosBegin>
504 inline typename Suffix<T *>::Type
505 suffix(T * t, TPosBegin pos_begin)
508 return typename Suffix<T *>::Type (t, pos_begin);
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)
518 return typename Suffix<Segment<T, PrefixSegment> >::Type (
520 beginPosition(t) + pos_begin,
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)
528 return typename Suffix<Segment<T, PrefixSegment> const>::Type (
530 beginPosition(t) + pos_begin,
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)
541 return typename Suffix<Segment<T, InfixSegment> >::Type (
543 beginPosition(t) + pos_begin,
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)
551 return typename Suffix<Segment<T, InfixSegment> const>::Type (
553 beginPosition(t) + pos_begin,
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)
565 return typename Suffix<Segment<T, SuffixSegment> >::Type (
567 beginPosition(t) + pos_begin);
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)
574 return typename Suffix<Segment<T, SuffixSegment> const>::Type (
576 beginPosition(t) + pos_begin);
579 //////////////////////////////////////////////////////////////////////////////
581 } //namespace SEQAN_NAMESPACE_MAIN
583 #endif //#ifndef SEQAN_HEADER_...