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_prefix.h,v 1.1 2008/08/25 16:20:04 langmead Exp $
19 ==========================================================================*/
21 #ifndef SEQAN_HEADER_SEGMENT_PREFIX_H
22 #define SEQAN_HEADER_SEGMENT_PREFIX_H
25 namespace SEQAN_NAMESPACE_MAIN
28 //////////////////////////////////////////////////////////////////////////////
30 //////////////////////////////////////////////////////////////////////////////
36 ..summary:First part of a sequence.
37 ..general:Class.Segment
38 ..signature:Segment<THost, PrefixSegment>
39 ..param.THost:Type of the whole sequence.
40 ...text:Instances of $Segment<THost, PrefixSegment>$ are prefixes 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.Prefix@ instead of explicitely
44 choose a specialization of @Class.Segment@.
45 ..see:Spec.InfixSegment
46 ..see:Spec.SuffixSegment
47 ..see:Metafunction.Prefix
52 template <typename THost_>
53 class Segment<THost_, PrefixSegment>
56 typedef typename Host<Segment>::Type THost;
58 typename _Pointer<THost>::Type data_host;
59 typename Position<THost>::Type data_end_position;
61 //____________________________________________________________________________
66 .Memfunc.PrefixSegment#Segment:
67 ..class:Spec.PrefixSegment
69 ..signature:Segment<THost, PrefixSegment> ()
70 ..signature:Segment<THost, PrefixSegment> (prefix)
71 ..signature:Segment<THost, PrefixSegment> (host [, end])
72 ..param.prefix:Other prefix object. (copy constructor)
73 ..param.host:The whole sequence.
74 ..param.end:Position in $host$ behind the last item in segment. (optional)
75 ...default:$length(host)$
76 ...type:Metafunction.Position.$Position<THost>::Type$
77 ...type:Metafunction.Iterator.$Iterator<THost>::Type$
79 ...text:A Segment object cannot work without a host. If the object is default constructed,
80 the host must be set by @Function.setHost@ before the segment can be used.
81 ...text:If a segment object is constructed by the copy constructor, the
82 members of the new constructed object are set to the same values as the members in the
83 source object; the host object is not modified.
84 Note that this is a special case, since all other copy operations result in changes
86 ...text:$begin$ must be a valid position/iterator in $host$.
87 If $begin$ is omitted, the prefix segment corresponding to
88 the whole sequence $host$ is constructed.
89 This is the same segment that is returned by @Function.goBegin@.
97 Segment(THost & _host):
99 data_end_position(length(_host))
104 Segment(typename _Parameter<THost>::Type _host, typename Position<THost>::Type _end_index):
105 data_host(_toPointer(_host)),
106 data_end_position(_end_index)
111 Segment(typename _Parameter<THost>::Type _host, typename Iterator<THost, Rooted>::Type _end):
112 data_host(_toPointer(_host)),
113 data_end_position(position(_end))
118 Segment(typename _Parameter<THost>::Type _host, typename Iterator<THost, Standard>::Type _end):
119 data_host(_toPointer(_host)),
120 data_end_position(position(_end, _host))
126 Segment(Segment const & _other):
127 data_host(_other.data_host),
128 data_end_position(_other.data_end_position)
133 template <typename THost2, typename TSpec2>
134 Segment(Segment<THost2, TSpec2> const & _other):
135 data_host(_toPointer(host(_other))),
136 data_end_position(endPosition(_other))
146 template <typename TSource>
148 operator = (TSource const & source)
150 assign(*this, source);
154 operator = (Segment const & source)
156 assign(*this, source);
159 //____________________________________________________________________________
163 friend inline typename _Parameter<THost>::Type
167 return _toParameter<THost>(me.data_host);
170 friend inline typename _Parameter<THost>::Type
171 host(Segment const & me)
174 return _toParameter<THost>(me.data_host);
177 //____________________________________________________________________________
180 setHost(Segment & me, typename _Parameter<THost>::Type _host)
183 me.data_host = _toPointer(_host);
186 //____________________________________________________________________________
188 template <typename TPos>
189 inline typename Reference<Segment>::Type
190 operator [] (TPos pos)
193 return value(*this, pos);
196 template <typename TPos>
197 inline typename Reference<Segment const>::Type
198 operator [] (TPos pos) const
201 return value(*this, pos);
204 //____________________________________________________________________________
206 friend inline typename Iterator<Segment, Standard>::Type
211 return begin(host(me), Standard());
213 friend inline typename Iterator<Segment const, Standard>::Type
214 begin(Segment const & me,
218 return begin(host(me), Standard());
221 //____________________________________________________________________________
223 friend inline typename Position<Segment const>::Type
224 beginPosition(Segment const & /*me*/)
229 friend inline typename Position<Segment>::Type
230 beginPosition(Segment & /*me*/)
236 //____________________________________________________________________________
238 template <typename TIterator>
240 setBegin(Segment &, TIterator)
244 //____________________________________________________________________________
246 friend inline typename Iterator<Segment, Standard>::Type
251 return begin(host(me), Standard()) + me.data_end_position;
253 friend inline typename Iterator<Segment const, Standard>::Type
254 end(Segment const & me,
258 return begin(host(me), Standard()) + me.data_end_position;
261 //____________________________________________________________________________
268 me.data_end_position = length(host(me));
272 setEndPosition(Segment & me, typename Position<Segment>::Type new_end)
275 me.data_end_position = new_end;
279 setEnd(Segment & me, typename Iterator<Segment, Standard>::Type new_end)
282 me.data_end_position = new_end - begin(host(me));//, Standard());
286 setEnd(typename Iterator<Segment, Rooted>::Type new_end)
289 container(new_end).data_end_position = hostIterator(new_end) - begin(host(container(new_end)));//, Standard());
292 //____________________________________________________________________________
297 typename Size<THost>::Type new_length)
300 me.data_end_position = new_length;
303 //____________________________________________________________________________
305 friend inline typename Position<Segment>::Type
306 endPosition(Segment & me)
309 return me.data_end_position;
311 friend inline typename Position<Segment const>::Type
312 endPosition(Segment const & me)
315 return me.data_end_position;
318 //____________________________________________________________________________
321 //////////////////////////////////////////////////////////////////////////////
324 .Metafunction.Prefix:
325 ..summary:Prefix sequence type.
326 ..signature:Prefix<T>::Type
327 ..param.T:A sequence type.
329 ..returns.param.Type:The prefix type.
330 ..see:Spec.PrefixSegment
331 ..see:Metafunction.Infix
335 struct SuffixSegment;
337 template <typename THost>
340 typedef Segment<THost, PrefixSegment> Type;
343 template <typename THost>
344 struct Prefix< Segment<THost, InfixSegment> >
346 typedef Segment<THost, InfixSegment> Type;
348 template <typename THost>
349 struct Prefix< Segment<THost, SuffixSegment> >
351 typedef Segment<THost, InfixSegment> Type;
353 template <typename THost>
354 struct Prefix< Segment<THost, PrefixSegment> >
356 typedef Segment<THost, PrefixSegment> Type;
359 template <typename THost, typename TSpec>
360 struct Prefix< Segment<THost, TSpec> const >:
361 Prefix< Segment<THost, TSpec> > {};
364 //////////////////////////////////////////////////////////////////////////////
366 template <typename THost, typename TPosition>
368 set(Segment<THost, PrefixSegment> & me,
376 //____________________________________________________________________________
378 template <typename THost>
380 set(Segment<THost, PrefixSegment> & me,
385 setEnd(me, end(host_));
388 //____________________________________________________________________________
390 template <typename THost, typename TSpec>
392 set(Segment<THost, PrefixSegment> & me,
393 Segment<THost, TSpec> & source)
396 setHost(me, host(source));
397 setEndPosition(me, endPosition(source));
400 template <typename THost, typename TSpec>
402 set(Segment<THost, PrefixSegment> & me,
403 Segment<THost, TSpec> const & source)
406 setHost(me, host(source));
407 setEndPosition(me, endPosition(source));
410 //////////////////////////////////////////////////////////////////////////////
412 template <typename THost>
414 atBegin(Segment<THost, PrefixSegment> const & segment)
417 return (endPosition(segment) == length(host(segment)));
420 //////////////////////////////////////////////////////////////////////////////
422 template <typename THost>
424 atEnd(Segment<THost, PrefixSegment> const & segment)
427 return (endPosition(segment) == 0);
430 //////////////////////////////////////////////////////////////////////////////
432 template <typename THost>
434 goBegin(Segment<THost, PrefixSegment> & segment,
441 template <typename THost>
443 goBegin(Segment<THost, PrefixSegment> & segment)
448 //////////////////////////////////////////////////////////////////////////////
450 template <typename THost>
452 goEnd(Segment<THost, PrefixSegment> & segment,
459 template <typename THost>
461 goEnd(Segment<THost, PrefixSegment> & segment)
466 //////////////////////////////////////////////////////////////////////////////
468 template <typename THost>
469 inline Segment<THost, PrefixSegment> &
470 operator ++(Segment<THost, PrefixSegment> & segment)
472 setEnd(segment, endPosition(segment) - 1);
476 //////////////////////////////////////////////////////////////////////////////
478 template <typename THost>
479 inline Segment<THost, PrefixSegment> &
480 operator --(Segment<THost, PrefixSegment> & segment)
482 setEnd(segment, endPosition(segment) + 1);
486 //////////////////////////////////////////////////////////////////////////////
491 ..summary:Creates prefix object.
492 ..signature:prefix(host, end)
493 ..param.host:The complete sequence.
495 ...type:Adaption.char array
496 ..param.end:Position or iterator behind the last element of the segment.
497 ...type:Metafunction.Position
498 ...type:Metafunction.Iterator
499 ..returns:The prefix of $host that begins at $begin$.
500 ...remarks:The type of the prefix is given by @Metafunction.Prefix@.
501 ..remarks:Notational sugar.
502 ..see:Spec.PrefixSegment
503 ..see:Function.suffix
507 template <typename T, typename TPosEnd>
508 inline typename Prefix<T>::Type
509 prefix(T & t, TPosEnd pos_end)
512 return typename Prefix<T>::Type(t, pos_end);
515 template <typename T, typename TPosEnd>
516 inline typename Prefix<T *>::Type
517 prefix(T * t, TPosEnd pos_end)
520 return typename Prefix<T *>::Type (t, pos_end);
523 //////////////////////////////////////////////////////////////////////////////
524 // A prefix of a prefix -> is a prefix
525 template <typename T, typename TPosEnd>
526 inline typename Prefix<Segment<T, PrefixSegment> >::Type
527 prefix(Segment<T, PrefixSegment> & t, TPosEnd pos_end)
530 return typename Prefix<Segment<T, PrefixSegment> >::Type (
532 beginPosition(t) + pos_end);
534 template <typename T, typename TPosEnd>
535 inline typename Prefix<Segment<T, PrefixSegment> const>::Type
536 prefix(Segment<T, PrefixSegment> const & t, TPosEnd pos_end)
539 return typename Prefix<Segment<T, PrefixSegment> const>::Type (
541 beginPosition(t) + pos_end);
544 //////////////////////////////////////////////////////////////////////////////
545 // A prefix of an infix -> is an infix
546 template <typename T, typename TPosEnd>
547 inline typename Prefix<Segment<T, InfixSegment> >::Type
548 prefix(Segment<T, InfixSegment> & t, TPosEnd pos_end)
551 return typename Prefix<Segment<T, InfixSegment> >::Type (
554 beginPosition(t) + pos_end);
556 template <typename T, typename TPosEnd>
557 inline typename Prefix<Segment<T, InfixSegment> const>::Type
558 prefix(Segment<T, InfixSegment> const & t, TPosEnd pos_end)
561 return typename Prefix<Segment<T, InfixSegment> const>::Type (
564 beginPosition(t) + pos_end);
568 //////////////////////////////////////////////////////////////////////////////
569 // A prefix of an suffix -> is an infix
570 template <typename T, typename TPosEnd>
571 inline typename Prefix<Segment<T, SuffixSegment> >::Type
572 prefix(Segment<T, SuffixSegment> & t, TPosEnd pos_end)
575 return typename Prefix<Segment<T, SuffixSegment> >::Type (
578 beginPosition(t) + pos_end);
580 template <typename T, typename TPosEnd>
581 inline typename Prefix<Segment<T, SuffixSegment> const>::Type
582 prefix(Segment<T, SuffixSegment> const & t, TPosEnd pos_end)
585 return typename Prefix<Segment<T, SuffixSegment> const>::Type (
588 beginPosition(t) + pos_end);
591 //////////////////////////////////////////////////////////////////////////////
593 } //namespace SEQAN_NAMESPACE_MAIN
595 #endif //#ifndef SEQAN_HEADER_...