Imported Upstream version 0.12.7
[bowtie.git] / SeqAn-1.1 / seqan / sequence / segment_base.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_base.h,v 1.1 2008/08/25 16:20:04 langmead Exp $
19  ==========================================================================*/
20
21 #ifndef SEQAN_HEADER_SEGMENT_BASE_H
22 #define SEQAN_HEADER_SEGMENT_BASE_H
23
24
25 namespace SEQAN_NAMESPACE_MAIN
26 {
27
28 //////////////////////////////////////////////////////////////////////////////
29 // Segment
30 //////////////////////////////////////////////////////////////////////////////
31
32 /**
33 .Class.Segment:
34 ..cat:Sequences
35 ..summary:A contiguous part of a sequence.
36 ..signature:Segment<THost, TSpec>
37 ..param.THost:Type of the whole sequence.
38 ...metafunction:Metafunction.Host
39 ...text:Instances of $Segment<THost, TSpec>$ are subsequences of $THost$ objects.
40 ..param.TSpec:The specializing type.
41 ...metafunction:Metafunction.Spec
42 ...default:@Spec.InfixSegment@.
43 */
44
45 struct InfixSegment;
46
47 template <typename THost, typename TSpec = InfixSegment>
48 class Segment
49 {
50 };
51
52 //////////////////////////////////////////////////////////////////////////////
53
54 ///.Metafunction.Host.param.T.type:Class.Segment
55
56 template <typename THost, typename TSpec>
57 struct Host<Segment<THost, TSpec> >
58 {
59         typedef THost Type;
60 };
61
62 template <typename THost, typename TSpec>
63 struct Host<Segment<THost, TSpec> const >
64 {
65         typedef THost Type;
66 };
67
68 //////////////////////////////////////////////////////////////////////////////
69
70 ///.Metafunction.Spec.param.T.type:Class.Segment
71
72 template <typename THost, typename TSpec>
73 struct Spec<Segment<THost, TSpec> >
74 {
75         typedef TSpec Type;
76 };
77 template <typename THost, typename TSpec>
78 struct Spec<Segment<THost, TSpec> const>
79 {
80         typedef TSpec Type;
81 };
82
83 //////////////////////////////////////////////////////////////////////////////
84
85 ///.Metafunction.Value.param.T.type:Class.Segment
86
87 template <typename THost, typename TSpec>
88 struct Value<Segment<THost, TSpec> >
89 {
90         typedef typename Value<THost>::Type Type;
91 };
92
93 template <typename THost, typename TSpec>
94 struct Value<Segment<THost, TSpec> const >
95 {
96         typedef typename Value<THost const>::Type Type;
97 };
98
99 //////////////////////////////////////////////////////////////////////////////
100
101 ///.Metafunction.GetValue.param.T.type:Class.Segment
102
103 template <typename THost, typename TSpec>
104 struct GetValue<Segment<THost, TSpec> >
105 {
106         typedef typename GetValue<THost>::Type Type;
107 };
108
109 template <typename THost, typename TSpec>
110 struct GetValue<Segment<THost, TSpec> const >
111 {
112         typedef typename GetValue<THost const>::Type Type;
113 };
114
115 //////////////////////////////////////////////////////////////////////////////
116
117 ///.Metafunction.Iterator.param.T.type:Class.Segment
118
119 template <typename THost, typename TSpec>
120 struct Iterator<Segment<THost, TSpec>, Rooted>
121 {
122         typedef Segment<THost, TSpec> TSequence;
123         typedef typename Iterator<THost, Standard>::Type TIterator;
124         typedef Iter<TSequence, AdaptorIterator<TIterator> > Type;
125 };
126 template <typename THost, typename TSpec>
127 struct Iterator<Segment<THost, TSpec> const, Rooted>
128 {
129         typedef Segment<THost, TSpec> const TSequence;
130         typedef typename Iterator<THost const, Standard>::Type TIterator;
131         typedef Iter<TSequence, AdaptorIterator<TIterator> > Type;
132 };
133
134 template <typename THost, typename TSpec>
135 struct Iterator<Segment<THost, TSpec>, Standard>:
136         Iterator<THost, Standard>
137 {
138 };
139 template <typename THost, typename TSpec>
140 struct Iterator<Segment<THost, TSpec> const, Standard>:
141         Iterator<THost, Standard>
142 {
143 };
144
145
146
147 //////////////////////////////////////////////////////////////////////////////
148
149 ///.Metafunction.Size.param.T.type:Class.Segment
150
151 template <typename THost, typename TSpec>
152 struct Size<Segment<THost, TSpec> >
153 {
154         typedef typename Size<THost>::Type Type;
155 };
156
157 template <typename THost, typename TSpec>
158 struct Size<Segment<THost, TSpec> const >
159 {
160         typedef typename Size<THost>::Type Type;
161 };
162
163 template <typename THost, typename TSpec>
164 struct Position<Segment<THost, TSpec> >
165 {
166         typedef typename Position<THost>::Type Type;
167 };
168
169 template <typename THost, typename TSpec>
170 struct Position<Segment<THost, TSpec> const >
171 {
172         typedef typename Position<THost>::Type Type;
173 };
174
175 //////////////////////////////////////////////////////////////////////////////
176
177 ///.Metafunction.DefaultOverflowImplicit.param.T.type:Class.Segment
178
179 template <typename THost, typename TSpec>
180 struct DefaultOverflowImplicit<Segment<THost, TSpec > >:
181         DefaultOverflowImplicit<THost>
182 {
183 };
184
185 template <typename THost, typename TSpec>
186 struct DefaultOverflowImplicit<Segment<THost, TSpec > const >:
187         DefaultOverflowImplicit<THost>
188 {
189 };
190
191 //////////////////////////////////////////////////////////////////////////////
192
193 ///.Metafunction.DefaultOverflowExplicit.param.T.type:Class.Segment
194
195 template <typename THost, typename TSpec>
196 struct DefaultOverflowExplicit<Segment<THost, TSpec > >:
197         DefaultOverflowExplicit<THost>
198 {
199 };
200
201 template <typename THost, typename TSpec>
202 struct DefaultOverflowExplicit<Segment<THost, TSpec > const >:
203         DefaultOverflowExplicit<THost>
204 {
205 };
206
207 //////////////////////////////////////////////////////////////////////////////
208
209 ///.Metafunction.IsContiguous.param.T.type:Class.Segment
210
211 template <typename THost, typename TSpec>
212 struct IsContiguous< Segment<THost, TSpec> >:
213         public IsContiguous<THost> {};
214
215 //////////////////////////////////////////////////////////////////////////////
216
217 ///.Metafunction.IsSequence.param.T.type:Class.Segment
218
219 template <typename THost, typename TSpec>
220 struct IsSequence< Segment<THost, TSpec> > {
221     typedef True Type;
222         enum { VALUE = true };
223 };
224
225 //////////////////////////////////////////////////////////////////////////////
226
227 ///.Function.atBegin.param.iterator.type:Class.Segment
228 ///.Function.atEnd.param.iterator.type:Class.Segment
229 ///.Function.goBegin.param.iterator.type:Class.Segment
230 ///.Function.goEnd.param.iterator.type:Class.Segment
231 ///.Function.goNext.param.iterator.type:Class.Segment
232 ///.Function.goPrevious.param.iterator.type:Class.Segment
233 ///.Function.value.param.container.type:Class.Segment
234
235 ///.Function.shareResources.param.sequence1, sequence2.type:Class.Segment
236
237 //////////////////////////////////////////////////////////////////////////////
238 // functions for all Segment classes
239 //////////////////////////////////////////////////////////////////////////////
240
241 ///.Function.id.param.object.type:Class.Segment
242
243 template <typename THost, typename TSpec>
244 inline void const * 
245 id(Segment<THost, TSpec> const & me)
246 {
247 SEQAN_CHECKPOINT
248         return id(host(me));
249 }
250
251 //////////////////////////////////////////////////////////////////////////////
252
253 ///.Function.length.param.object.type:Class.Segment
254
255 template <typename THost, typename TSpec>
256 inline typename Size<Segment<THost, TSpec> const>::Type 
257 length(Segment<THost, TSpec> const & me)
258 {
259 SEQAN_CHECKPOINT
260         return endPosition(me) - beginPosition(me);
261 }
262
263 //////////////////////////////////////////////////////////////////////////////
264
265 ///.Function.capacity.param.object.type:Class.Segment
266
267 template <typename THost, typename TSpec>
268 inline typename Size< Segment<THost, TSpec> const>::Type 
269 capacity(Segment<THost, TSpec> const & me)
270 {
271 SEQAN_CHECKPOINT
272         return capacity(host(me)) + length(me) - length(host(me));
273 }
274
275 //////////////////////////////////////////////////////////////////////////////
276 // assign
277 //////////////////////////////////////////////////////////////////////////////
278
279 /**
280 .Function.assign:
281 ..remarks:If $target$ is a @Class.Segment@ object, then
282 $limit$ denotes the maximal length of @Function.host.$host(target)$@ after the operation.
283 ..param.target.type:Class.Segment
284 ..param.source.type:Class.Segment
285 */
286
287 //overload of binary version for strings: 
288
289 template<typename THost, typename TSpec, typename TSource>
290 inline void 
291 assign(Segment<THost, TSpec> & target, 
292           TSource & source)
293 {
294 SEQAN_CHECKPOINT
295         typedef Segment<THost, TSpec> TTarget;
296         assign(target, source, typename DefaultOverflowImplicit<TTarget>::Type());
297 }
298 template<typename THost, typename TSpec, typename TSource>
299 inline void 
300 assign(Segment<THost, TSpec> & target, 
301           TSource const & source)
302 {
303 SEQAN_CHECKPOINT
304         typedef Segment<THost, TSpec> TTarget;
305         assign(target, source, typename DefaultOverflowImplicit<TTarget>::Type());
306 }
307
308 //(for temporary targets)
309
310 template<typename THost, typename TSpec, typename TSource>
311 inline void 
312 assign(Segment<THost, TSpec> const & target, 
313           TSource & source)
314 {
315 SEQAN_CHECKPOINT
316         typedef Segment<THost, TSpec> const TTarget;
317         assign(target, source, typename DefaultOverflowImplicit<TTarget>::Type());
318 }
319 template<typename THost, typename TSpec, typename TSource>
320 inline void 
321 assign(Segment<THost, TSpec> const & target, 
322           TSource const & source)
323 {
324 SEQAN_CHECKPOINT
325         typedef Segment<THost, TSpec> const TTarget;
326         assign(target, source, typename DefaultOverflowImplicit<TTarget>::Type());
327 }
328
329 //////////////////////////////////////////////////////////////////////////////
330
331 template <typename TExpand>
332 struct _Assign_Segment
333 {
334         template <typename THost, typename TSpec, typename TSource>
335         static inline void 
336         assign_(
337                 Segment<THost, TSpec> & target, 
338                 TSource & source)
339         {
340 SEQAN_CHECKPOINT
341                 if ((void *) &target == (void *) &source) return;
342
343                 typedef Segment<THost, TSpec> Target;
344
345                 replace(host(target), beginPosition(target), endPosition(target), source, TExpand());
346
347                 typename Iterator<Target, Standard>::Type new_end = begin(target, Standard()) + length(source);
348                 typename Iterator<THost, Standard>::Type host_end = end(host(target), Standard());
349                 if (new_end > host_end) new_end = host_end;
350                 setEnd(target, new_end);
351         }
352
353         template <typename THost, typename TSpec, typename TSource>
354         static inline void 
355         assign_(
356                 Segment<THost, TSpec> & target, 
357                 TSource & source, 
358                 typename Size< Segment<THost, TSpec> >::Type limit)
359         {
360 SEQAN_CHECKPOINT
361                 if ((void *) &target == (void *) &source) return;
362
363                 typedef Segment<THost, TSpec> Target;
364
365                 replace(host(target), beginPosition(target), endPosition(target), source, limit, TExpand());
366
367                 typename Iterator<Target, Standard>::Type new_end = begin(target, Standard()) + length(source);
368                 typename Iterator<THost, Standard>::Type host_end = end(host(target), Standard());
369                 if (begin(target, Standard()) > host_end) setBegin(target, host_end);
370                 if (new_end > host_end) new_end = host_end;
371                 setEnd(target, new_end);
372         }
373
374         template <typename THost, typename TSpec, typename TSource>
375         static inline void 
376         assign_(
377                 Segment<THost, TSpec> const & target, 
378                 TSource & source)
379         {
380 SEQAN_CHECKPOINT
381                 replace(host(target), beginPosition(target), endPosition(target), source, TExpand());
382         }
383
384         template <typename THost, typename TSpec, typename TSource>
385         static inline void 
386         assign_(
387                 Segment<THost, TSpec> const & target, 
388                 TSource & source, 
389                 typename Size< Segment<THost, TSpec> >::Type limit)
390         {
391 SEQAN_CHECKPOINT
392                 replace(host(target), beginPosition(target), endPosition(target), source, limit, TExpand());
393         }
394 };
395
396 //____________________________________________________________________________
397
398 template <typename THost, typename TSpec, typename TSource, typename TExpand>
399 inline void 
400 assign(Segment<THost, TSpec> & target, 
401            TSource & source, 
402            Tag<TExpand> const)
403 {
404 SEQAN_CHECKPOINT
405         _Assign_Segment<Tag<TExpand> const>::assign_(target, source);
406 }
407 template <typename THost, typename TSpec, typename TSource, typename TExpand>
408 inline void 
409 assign(Segment<THost, TSpec> & target, 
410            TSource const & source, 
411            Tag<TExpand> const)
412 {
413 SEQAN_CHECKPOINT
414         _Assign_Segment<Tag<TExpand> const>::assign_(target, source);
415 }
416
417 template <typename THost, typename TSpec, typename TSource, typename TExpand>
418 inline void 
419 assign(Segment<THost, TSpec> & target, 
420            TSource & source, 
421            typename Size< Segment<THost, TSpec> >::Type limit, 
422            Tag<TExpand> const)
423 {
424 SEQAN_CHECKPOINT
425         _Assign_Segment<Tag<TExpand> const>::assign_(target, source, limit);
426 }
427 template <typename THost, typename TSpec, typename TSource, typename TExpand>
428 inline void 
429 assign(Segment<THost, TSpec> & target, 
430            TSource const & source, 
431            typename Size< Segment<THost, TSpec> >::Type limit, 
432            Tag<TExpand> const)
433 {
434 SEQAN_CHECKPOINT
435         _Assign_Segment<Tag<TExpand> const>::assign_(target, source, limit);
436 }
437
438 //(for temporary targets)
439
440 template <typename THost, typename TSpec, typename TSource, typename TExpand>
441 inline void 
442 assign(Segment<THost, TSpec> const & target, 
443            TSource & source, 
444            Tag<TExpand> const)
445 {
446 SEQAN_CHECKPOINT
447         _Assign_Segment<Tag<TExpand> const>::assign_(target, source);
448 }
449 template <typename THost, typename TSpec, typename TSource, typename TExpand>
450 inline void 
451 assign(Segment<THost, TSpec> const & target, 
452            TSource const & source, 
453            Tag<TExpand> const)
454 {
455 SEQAN_CHECKPOINT
456         _Assign_Segment<Tag<TExpand> const>::assign_(target, source);
457 }
458
459 template <typename THost, typename TSpec, typename TSource, typename TExpand>
460 inline void 
461 assign(Segment<THost, TSpec> const & target, 
462            TSource & source, 
463            typename Size< Segment<THost, TSpec> >::Type limit, 
464            Tag<TExpand> const)
465 {
466 SEQAN_CHECKPOINT
467         _Assign_Segment<Tag<TExpand> const>::assign_(target, source, limit);
468 }
469 template <typename THost, typename TSpec, typename TSource, typename TExpand>
470 inline void 
471 assign(Segment<THost, TSpec> const & target, 
472            TSource const & source, 
473            typename Size< Segment<THost, TSpec> >::Type limit, 
474            Tag<TExpand> const)
475 {
476 SEQAN_CHECKPOINT
477         _Assign_Segment<Tag<TExpand> const>::assign_(target, source, limit);
478 }
479
480 //////////////////////////////////////////////////////////////////////////////
481 // move
482 //////////////////////////////////////////////////////////////////////////////
483
484 //overload of binary version: 
485
486 template <typename THost, typename TSpec, typename TSource, typename TExpand>
487 inline void 
488 move(Segment<THost, TSpec> & target, 
489          TSource & source)
490 {
491 SEQAN_CHECKPOINT
492         typedef Segment<THost, TSpec> TTarget;
493         move(target, source, typename DefaultOverflowImplicit<TTarget>::Type());
494 }
495 template <typename THost, typename TSpec, typename TSource, typename TExpand>
496 inline void 
497 move(Segment<THost, TSpec> & target, 
498          TSource const & source)
499 {
500 SEQAN_CHECKPOINT
501         typedef Segment<THost, TSpec> TTarget;
502         move(target, source, typename DefaultOverflowImplicit<TTarget>::Type());
503 }
504
505 //(for temporary targets)
506
507 template <typename THost, typename TSpec, typename TSource, typename TExpand>
508 inline void 
509 move(Segment<THost, TSpec> const & target, 
510          TSource & source)
511 {
512 SEQAN_CHECKPOINT
513         typedef Segment<THost, TSpec> const TTarget;
514         move(target, source, typename DefaultOverflowImplicit<TTarget>::Type());
515 }
516 template <typename THost, typename TSpec, typename TSource, typename TExpand>
517 inline void 
518 move(Segment<THost, TSpec> const & target, 
519          TSource const & source)
520 {
521 SEQAN_CHECKPOINT
522         typedef Segment<THost, TSpec> const TTarget;
523         move(target, source, typename DefaultOverflowImplicit<TTarget>::Type());
524 }
525
526
527
528 //////////////////////////////////////////////////////////////////////////////
529 // append
530 //////////////////////////////////////////////////////////////////////////////
531
532 /**
533 .Function.append:
534 ..remarks:If $target$ is a @Class.Segment@ object, then
535 $limit$ denotes the maximal length of @Function.host.$host(target)$@ after the operation.
536 ..param.target.type:Class.Segment
537 ..param.source.type:Class.Segment
538 */
539
540
541 template <typename TExpand>
542 struct _Append_Sequence_2_Segment
543 {
544         template <typename THost, typename TSpec, typename TSource>
545         static inline void 
546         append_(
547                 Segment<THost, TSpec> & target, 
548                 TSource & source)
549         {
550 SEQAN_CHECKPOINT
551                 typedef Segment<THost, TSpec> Target;
552
553                 replace(host(target), endPosition(target), endPosition(target), source, TExpand());
554
555                 typename Iterator<Target, Standard>::Type new_end = end(target, Standard()) + length(source);
556                 typename Iterator<THost, Standard>::Type host_end = end(host(target), Standard());
557                 if (new_end > host_end) new_end = host_end;
558                 setEnd(target, new_end);
559         }
560
561         template <typename THost, typename TSpec, typename TSource>
562         static inline void 
563         append_(
564                 Segment<THost, TSpec> & target, 
565                 TSource const & source, 
566                 typename Size< Segment<THost, TSpec> >::Type limit)
567         {
568 SEQAN_CHECKPOINT
569                 typedef Segment<THost, TSpec> Target;
570
571                 replace(host(target), endPosition(target), endPosition(target), source, limit, TExpand());
572                 typename Iterator<Target, Standard>::Type new_end = end(target, Standard()) + length(source);
573                 typename Iterator<THost, Standard>::Type host_end = end(host(target), Standard());
574                 if (begin(target) > host_end) setBegin(target, host_end);
575                 if (new_end > host_end) new_end = host_end;
576                 setEnd(target, new_end);
577         }
578
579         template <typename THost, typename TSpec, typename TSource>
580         static inline void 
581         append_(
582                 Segment<THost, TSpec> const & target, 
583                 TSource & source)
584         {
585 SEQAN_CHECKPOINT
586                 replace(host(target), endPosition(target), endPosition(target), source, TExpand());
587         }
588
589         template <typename THost, typename TSpec, typename TSource>
590         static inline void 
591         append_(
592                 Segment<THost, TSpec> const & target, 
593                 TSource const & source, 
594                 typename Size< Segment<THost, TSpec> >::Type limit)
595         {
596 SEQAN_CHECKPOINT
597                 replace(host(target), endPosition(target), endPosition(target), source, limit, TExpand()); //??? INSERT
598         }
599 };
600 //____________________________________________________________________________
601
602
603 template <typename THost, typename TSpec, typename TSource, typename TExpand>
604 inline void 
605 append(
606         Segment<THost, TSpec> & target, 
607         TSource & source, 
608         Tag<TExpand> const)
609 {
610 SEQAN_CHECKPOINT
611         _Append_Sequence_2_Segment<Tag<TExpand> const>::append_(target, source);
612 }
613 template <typename THost, typename TSpec, typename TSource, typename TExpand>
614 inline void 
615 append(
616         Segment<THost, TSpec> & target, 
617         TSource const & source, 
618         Tag<TExpand> const)
619 {
620 SEQAN_CHECKPOINT
621         _Append_Sequence_2_Segment<Tag<TExpand> const>::append_(target, source);
622 }
623
624 template <typename THost, typename TSpec, typename TSource, typename TExpand>
625 inline void 
626 append(
627         Segment<THost, TSpec> & target, 
628         TSource & source, 
629         typename Size< Segment<THost, TSpec> >::Type limit, 
630         Tag<TExpand> const)
631 {
632 SEQAN_CHECKPOINT
633         _Append_Sequence_2_Segment<Tag<TExpand> const>::append_(target, source, limit);
634 }
635 template <typename THost, typename TSpec, typename TSource, typename TExpand>
636 inline void 
637 append(
638         Segment<THost, TSpec> & target, 
639         TSource const & source, 
640         typename Size< Segment<THost, TSpec> >::Type limit, 
641         Tag<TExpand> const)
642 {
643 SEQAN_CHECKPOINT
644         _Append_Sequence_2_Segment<Tag<TExpand> const>::append_(target, source, limit);
645 }
646
647 template <typename THost, typename TSpec, typename TSource, typename TExpand>
648 inline void 
649 append(
650         Segment<THost, TSpec> const & target, 
651         TSource & source, 
652         Tag<TExpand> const)
653 {
654 SEQAN_CHECKPOINT
655         _Append_Sequence_2_Segment<Tag<TExpand> const>::append_(target, source);
656 }
657 template <typename THost, typename TSpec, typename TSource, typename TExpand>
658 inline void 
659 append(
660         Segment<THost, TSpec> const & target, 
661         TSource const & source, 
662         Tag<TExpand> const)
663 {
664 SEQAN_CHECKPOINT
665         _Append_Sequence_2_Segment<Tag<TExpand> const>::append_(target, source);
666 }
667
668 template <typename THost, typename TSpec, typename TSource, typename TExpand>
669 inline void 
670 append(
671         Segment<THost, TSpec> const & target, 
672         TSource & source, 
673         typename Size< Segment<THost, TSpec> >::Type limit, 
674         Tag<TExpand> const)
675 {
676 SEQAN_CHECKPOINT
677         _Append_Sequence_2_Segment<Tag<TExpand> const>::append_(target, source, limit);
678 }
679 template <typename THost, typename TSpec, typename TSource, typename TExpand>
680 inline void 
681 append(
682         Segment<THost, TSpec> const & target, 
683         TSource const & source, 
684         typename Size< Segment<THost, TSpec> >::Type limit, 
685         Tag<TExpand> const)
686 {
687 SEQAN_CHECKPOINT
688         _Append_Sequence_2_Segment<Tag<TExpand> const>::append_(target, source, limit);
689 }
690
691 //////////////////////////////////////////////////////////////////////////////
692 // appendValue
693 //////////////////////////////////////////////////////////////////////////////
694
695 template <typename TExpand>
696 struct _Append_Value_2_Segment
697 {
698         template <typename T, typename TValue>
699         static inline void 
700         appendValue_(T & me,
701                                 TValue & _value)
702         {
703 SEQAN_CHECKPOINT
704                 insertValue(host(me), endPosition(me), TExpand());
705                 if (endPosition(me) < length(host(me)) ) //this could be false for some TExpand
706                 {
707                         setEndPosition(me, endPosition(me) + 1);
708                 }
709         }
710 };
711
712 //____________________________________________________________________________
713
714 template <typename THost, typename TSpec, typename TValue, typename TExpand>
715 inline void
716 appendValue(Segment<THost, TSpec> & me, 
717                         TValue const & _value,
718                         Tag<TExpand> const)
719 {
720 SEQAN_CHECKPOINT
721         _Append_Value_2_Segment<Tag<TExpand> const>::appendValue_(me, _value);
722 }
723 template <typename THost, typename TSpec, typename TValue, typename TExpand>
724 inline void
725 appendValue(Segment<THost, TSpec> const & me, 
726                         TValue const & _value,
727                         Tag<TExpand> const)
728 {
729 SEQAN_CHECKPOINT
730         _Append_Value_2_Segment<Tag<TExpand> const>::appendValue_(me, _value);
731 }
732
733 //////////////////////////////////////////////////////////////////////////////
734 // insertValue
735 //////////////////////////////////////////////////////////////////////////////
736
737 template <typename TExpand>
738 struct _Insert_Value_2_Segment
739 {
740         template <typename T, typename TPosition, typename TValue>
741         static inline void 
742         insertValue_(T & me,
743                                 TPosition pos,
744                                 TValue & _value)
745         {
746 SEQAN_CHECKPOINT
747                 insertValue(host(me), beginPosition(me) + pos, TExpand());
748                 if (endPosition(me) < length(host(me)) ) //this could be false for some TExpand
749                 {
750                         setEndPosition(me, endPosition(me) + 1);
751                 }
752         }
753 };
754
755 //____________________________________________________________________________
756
757 template <typename THost, typename TSpec, typename TPosition, typename TValue, typename TExpand>
758 inline void
759 insertValue(Segment<THost, TSpec> & me, 
760                         TPosition pos,
761                         TValue const & _value,
762                         Tag<TExpand> const)
763 {
764 SEQAN_CHECKPOINT
765         _Insert_Value_2_Segment<Tag<TExpand> const>::insertValue_(me, pos, _value);
766 }
767 template <typename THost, typename TSpec, typename TPosition, typename TValue, typename TExpand>
768 inline void
769 insertValue(Segment<THost, TSpec> const & me, 
770                         TPosition pos,
771                         TValue const & _value,
772                         Tag<TExpand> const)
773 {
774 SEQAN_CHECKPOINT
775         _Insert_Value_2_Segment<Tag<TExpand> const>::insertValue_(me, pos, _value);
776 }
777
778 //////////////////////////////////////////////////////////////////////////////
779 // replace
780 //////////////////////////////////////////////////////////////////////////////
781
782 /**
783 .Function.replace:
784 ..remarks:If $target$ is a @Class.Segment@ object, then
785 $limit$ denotes the maximal length of @Function.host.$host(target)$@ after the operation.
786 ..param.target.type:Class.Segment
787 ..param.source.type:Class.Segment
788 */
789
790 template <typename TExpand>
791 struct _Replace_Sequence_2_Segment
792 {
793         template <typename THost, typename TSpec, typename TSource>
794         static inline void 
795         replace_(
796                 Segment<THost, TSpec> & target, 
797                 typename Position< Segment<THost, TSpec> >::Type pos_begin,
798                 typename Position< Segment<THost, TSpec> >::Type pos_end,
799                 TSource & source)
800         {
801 SEQAN_CHECKPOINT
802                 typedef Segment<THost, TSpec> Target;
803
804                 replace(host(target), beginPosition(target) + pos_begin, beginPosition(target) + pos_end, source, TExpand());
805
806                 typename Iterator<Target, Standard>::Type new_end = begin(target, Standard()) + length(target) - pos_end + pos_begin + length(source);
807                 typename Iterator<THost, Standard>::Type host_end = end(host(target), Standard());
808                 if (new_end > host_end) new_end = host_end;
809                 setEnd(target, new_end);
810         }
811
812         template <typename THost, typename TSpec, typename TSource>
813         static inline void 
814         replace_(
815                 Segment<THost, TSpec> & target, 
816                 typename Position< Segment<THost, TSpec> >::Type pos_begin,
817                 typename Position< Segment<THost, TSpec> >::Type pos_end,
818                 TSource & source, 
819                 typename Size< Segment<THost, TSpec> >::Type limit)
820         {
821 SEQAN_CHECKPOINT
822                 typedef Segment<THost, TSpec> Target;
823
824                 replace(host(target), beginPosition(target) + pos_begin, beginPosition(target) + pos_end, source, limit, TExpand());
825
826                 typename Iterator<Target, Standard>::Type new_end = begin(target, Standard()) + length(target) - pos_end + pos_begin + length(source);
827                 typename Iterator<THost, Standard>::Type host_end = end(host(target), Standard());
828                 if (begin(target, Standard()) > host_end) setBegin(target, host_end);
829                 if (new_end > host_end) new_end = host_end;
830                 setEnd(target, new_end);
831         }
832
833         template <typename THost, typename TSpec, typename TSource>
834         static inline void 
835         replace_(
836                 Segment<THost, TSpec> const & target, 
837                 typename Position< Segment<THost, TSpec> const>::Type pos_begin,
838                 typename Position< Segment<THost, TSpec> const>::Type pos_end,
839                 TSource & source)
840         {
841 SEQAN_CHECKPOINT
842                 replace(host(target), beginPosition(target) + pos_begin, beginPosition(target) + pos_end, source, TExpand());
843         }
844
845         template <typename THost, typename TSpec, typename TSource>
846         static inline void 
847         replace_(
848                 Segment<THost, TSpec> const & target, 
849                 typename Position< Segment<THost, TSpec> const>::Type pos_begin,
850                 typename Position< Segment<THost, TSpec> const>::Type pos_end,
851                 TSource & source, 
852                 typename Size< Segment<THost, TSpec> >::Type limit)
853         {
854 SEQAN_CHECKPOINT
855                 replace(host(target), beginPosition(target) + pos_begin, beginPosition(target) + pos_end, source, limit, TExpand()); //??? INSERT
856         }
857 };
858 //____________________________________________________________________________
859
860
861 template <typename THost, typename TSpec, typename TSource, typename TExpand>
862 inline void 
863 replace(
864         Segment<THost, TSpec> & target, 
865         typename Position< Segment<THost, TSpec> >::Type pos_begin,
866         typename Position< Segment<THost, TSpec> >::Type pos_end,
867         TSource & source, 
868         Tag<TExpand> const)
869 {
870 SEQAN_CHECKPOINT
871         _Replace_Sequence_2_Segment<Tag<TExpand> const>::replace_(target, pos_begin, pos_end, source);
872 }
873 template <typename THost, typename TSpec, typename TSource, typename TExpand>
874 inline void 
875 replace(
876         Segment<THost, TSpec> & target, 
877         typename Position< Segment<THost, TSpec> >::Type pos_begin,
878         typename Position< Segment<THost, TSpec> >::Type pos_end,
879         TSource const & source, 
880         Tag<TExpand> const)
881 {
882 SEQAN_CHECKPOINT
883         _Replace_Sequence_2_Segment<Tag<TExpand> const>::replace_(target, pos_begin, pos_end, source);
884 }
885
886 template <typename THost, typename TSpec, typename TSource, typename TExpand>
887 inline void 
888 replace(
889         Segment<THost, TSpec> & target, 
890         typename Position< Segment<THost, TSpec> >::Type pos_begin,
891         typename Position< Segment<THost, TSpec> >::Type pos_end,
892         TSource & source, 
893         typename Size< Segment<THost, TSpec> >::Type limit, 
894         Tag<TExpand> const)
895 {
896 SEQAN_CHECKPOINT
897         _Replace_Sequence_2_Segment<Tag<TExpand> const>::replace_(target, pos_begin, pos_end, source, limit);
898 }
899 template <typename THost, typename TSpec, typename TSource, typename TExpand>
900 inline void 
901 replace(
902         Segment<THost, TSpec> & target, 
903         typename Position< Segment<THost, TSpec> >::Type pos_begin,
904         typename Position< Segment<THost, TSpec> >::Type pos_end,
905         TSource const & source, 
906         typename Size< Segment<THost, TSpec> >::Type limit, 
907         Tag<TExpand> const)
908 {
909 SEQAN_CHECKPOINT
910         _Replace_Sequence_2_Segment<Tag<TExpand> const>::replace_(target, pos_begin, pos_end, source, limit);
911 }
912
913 template <typename THost, typename TSpec, typename TSource, typename TExpand>
914 inline void 
915 replace(
916         Segment<THost, TSpec> const & target, 
917         typename Position< Segment<THost, TSpec> const>::Type pos_begin,
918         typename Position< Segment<THost, TSpec> const>::Type pos_end,
919         TSource & source, 
920         Tag<TExpand> const)
921 {
922 SEQAN_CHECKPOINT
923         _Replace_Sequence_2_Segment<Tag<TExpand> const>::replace_(target, pos_begin, pos_end, source);
924 }
925 template <typename THost, typename TSpec, typename TSource, typename TExpand>
926 inline void 
927 replace(
928         Segment<THost, TSpec> const & target, 
929         typename Position< Segment<THost, TSpec> const>::Type pos_begin,
930         typename Position< Segment<THost, TSpec> const>::Type pos_end,
931         TSource const & source, 
932         Tag<TExpand> const)
933 {
934 SEQAN_CHECKPOINT
935         _Replace_Sequence_2_Segment<Tag<TExpand> const>::replace_(target, pos_begin, pos_end, source);
936 }
937
938 template <typename THost, typename TSpec, typename TSource, typename TExpand>
939 inline void 
940 replace(
941         Segment<THost, TSpec> const & target, 
942         typename Position< Segment<THost, TSpec> const>::Type pos_begin,
943         typename Position< Segment<THost, TSpec> const>::Type pos_end,
944         TSource & source, 
945         typename Size< Segment<THost, TSpec> >::Type limit, 
946         Tag<TExpand> const )
947 {
948 SEQAN_CHECKPOINT
949         _Replace_Sequence_2_Segment<Tag<TExpand> const>::replace_(target, pos_begin, pos_end, source, limit);
950 }
951 template <typename THost, typename TSpec, typename TSource, typename TExpand>
952 inline void 
953 replace(
954         Segment<THost, TSpec> const & target, 
955         typename Position< Segment<THost, TSpec> const>::Type pos_begin,
956         typename Position< Segment<THost, TSpec> const>::Type pos_end,
957         TSource const & source, 
958         typename Size< Segment<THost, TSpec> >::Type limit, 
959         Tag<TExpand> const)
960 {
961 SEQAN_CHECKPOINT
962         _Replace_Sequence_2_Segment<Tag<TExpand> const>::replace_(target, pos_begin, pos_end, source, limit);
963 }
964
965 //////////////////////////////////////////////////////////////////////////////
966 // handling of iterators as begin and end
967
968 /*
969 template<typename THost, typename TTargetSpec, typename TSource, typename TExpand>
970 inline void 
971 replace(Segment<THost, TTargetSpec> & target,
972                 typename Iterator< Segment<THost, TTargetSpec>, Rooted>::Type pos_begin,
973                 typename Iterator< Segment<THost, TTargetSpec>, Rooted>::Type pos_end,
974                 TSource & source,
975                 Tag<TExpand> const tag)
976 {
977 SEQAN_CHECKPOINT
978         replace(target, position(pos_begin), position(pos_end), source, tag);
979 }
980
981 template<typename THost, typename TTargetSpec, typename TSource, typename TExpand>
982 inline void 
983 replace(Segment<THost, TTargetSpec> & target,
984                 typename Iterator< Segment<THost, TTargetSpec>, Rooted>::Type pos_begin,
985                 typename Iterator< Segment<THost, TTargetSpec>, Rooted>::Type pos_end,
986                 TSource & source,
987                 typename Size< Segment<THost, TTargetSpec> >::Type limit,
988                 Tag<TExpand> const tag)
989 {
990 SEQAN_CHECKPOINT
991         replace(target, position(pos_begin), position(pos_end), source, limit, tag);
992 }
993
994 template<typename THost, typename TTargetSpec, typename TSource, typename TExpand>
995 inline void 
996 replace(Segment<THost, TTargetSpec> const & target,
997                 typename Iterator< Segment<THost, TTargetSpec> const, Rooted>::Type pos_begin,
998                 typename Iterator< Segment<THost, TTargetSpec> const, Rooted>::Type pos_end,
999                 TSource & source,
1000                 Tag<TExpand> const tag)
1001 {
1002 SEQAN_CHECKPOINT
1003         replace(target, position(pos_begin), position(pos_end), source, tag);
1004 }
1005
1006 template<typename THost, typename TTargetSpec, typename TSource, typename TExpand>
1007 inline void 
1008 replace(Segment<THost, TTargetSpec> const & target,
1009                 typename Iterator< Segment<THost, TTargetSpec> const, Rooted>::Type pos_begin,
1010                 typename Iterator< Segment<THost, TTargetSpec> const, Rooted>::Type pos_end,
1011                 TSource & source,
1012                 typename Size< Segment<THost, TTargetSpec> >::Type limit,
1013                 Tag<TExpand> const tag)
1014 {
1015 SEQAN_CHECKPOINT
1016         replace(target, position(pos_begin), position(pos_end), source, limit, tag);
1017 }
1018 */
1019
1020 //////////////////////////////////////////////////////////////////////////////
1021 ///.Function.resize.param.object.type:Class.Segment
1022
1023 template <typename THost, typename TSpec, typename TExpand>
1024 inline typename Size< Segment<THost, TSpec> >::Type 
1025 resize(
1026         Segment<THost, TSpec> & me,
1027         typename Size< Segment<THost, TSpec> >::Type new_length,
1028         Tag<TExpand> const tag)
1029 {
1030 SEQAN_CHECKPOINT
1031
1032         typename Size<Segment<THost, TSpec> >::Type me_length = length(me);
1033         typename Position<THost>::Type me_end_pos = endPosition(me);
1034         if (new_length > me_length)
1035         {
1036                 new_length = me_length + resizeSpace(host(me), new_length - me_length, me_end_pos, me_end_pos, tag);
1037         }
1038         else if (new_length < me_length)
1039         {
1040                 new_length = resizeSpace(host(me), 0, me_end_pos - (me_length - new_length), me_end_pos, tag);
1041         }
1042         _setLength(me, new_length);
1043         return new_length;
1044 }
1045
1046 //////////////////////////////////////////////////////////////////////////////
1047 //??? TODO: fill (kopie von resize anpassen)
1048
1049 //////////////////////////////////////////////////////////////////////////////
1050 ///.Function.clear.param.object.type:Class.Segment
1051
1052 template <typename THost, typename TSpec>
1053 inline void 
1054 clear(Segment<THost, TSpec> & target)
1055 {
1056 SEQAN_CHECKPOINT
1057         assign(target, "");
1058 }
1059
1060 //////////////////////////////////////////////////////////////////////////////
1061
1062 template <typename TLeftValue, typename TLeftSpec, typename TRight>
1063 Segment<TLeftValue, TLeftSpec> const & 
1064 operator += (Segment<TLeftValue, TLeftSpec> & left,
1065                          TRight const & right)
1066 {
1067 SEQAN_CHECKPOINT
1068         append(left, right);
1069         return left;
1070 }
1071
1072 //////////////////////////////////////////////////////////////////////////////
1073 //////////////////////////////////////////////////////////////////////////////
1074
1075 template <typename TLeftHost, typename TLeftSpec, typename TRight >
1076 inline bool
1077 operator == (Segment<TLeftHost, TLeftSpec> const & left, 
1078                         TRight const & right)
1079 {
1080 SEQAN_CHECKPOINT
1081         typename Comparator<Segment<TLeftHost, TLeftSpec> >::Type _lex(left, right);
1082     return isEqual(_lex);
1083 }
1084
1085 //////////////////////////////////////////////////////////////////////////////
1086
1087 template <typename TLeftHost, typename TLeftSpec, typename TRight >
1088 inline bool
1089 operator != (Segment<TLeftHost, TLeftSpec> const & left, 
1090                         TRight const & right)
1091 {
1092 SEQAN_CHECKPOINT
1093         typename Comparator<Segment<TLeftHost, TLeftSpec> >::Type _lex(left, right);
1094     return isNotEqual(_lex);
1095 }
1096
1097 //////////////////////////////////////////////////////////////////////////////
1098
1099 template <typename TLeftHost, typename TLeftSpec, typename TRight>
1100 inline bool
1101 operator < (Segment<TLeftHost, TLeftSpec> const & left, 
1102                         TRight const & right)
1103 {
1104 SEQAN_CHECKPOINT
1105         return isLess(left, right, typename DefaultPrefixOrder<Segment<TLeftHost, TLeftSpec> >::Type());
1106 }
1107
1108 //////////////////////////////////////////////////////////////////////////////
1109
1110 template <typename TLeftHost, typename TLeftSpec, typename TRight>
1111 inline bool
1112 operator <= (Segment<TLeftHost, TLeftSpec> const & left, 
1113                          TRight const & right)
1114 {
1115 SEQAN_CHECKPOINT
1116         return isLessOrEqual(left, right, typename DefaultPrefixOrder<Segment<TLeftHost, TLeftSpec> >::Type());
1117 }
1118 //////////////////////////////////////////////////////////////////////////////
1119
1120 template <typename TLeftHost, typename TLeftSpec, typename TRight>
1121 inline bool
1122 operator > (Segment<TLeftHost, TLeftSpec> const & left, 
1123                         TRight const & right)
1124 {
1125 SEQAN_CHECKPOINT
1126         return isGreater(left, right, typename DefaultPrefixOrder<Segment<TLeftHost, TLeftSpec> >::Type());
1127 }
1128
1129 //////////////////////////////////////////////////////////////////////////////
1130
1131 template <typename TLeftHost, typename TLeftSpec, typename TRight>
1132 inline bool
1133 operator >= (Segment<TLeftHost, TLeftSpec> const & left, 
1134                 TRight const & right)
1135 {
1136 SEQAN_CHECKPOINT
1137         return isGreaterOrEqual(left, right, typename DefaultPrefixOrder<Segment<TLeftHost, TLeftSpec> >::Type());
1138 }
1139
1140
1141 //////////////////////////////////////////////////////////////////////////////
1142 // stream operators
1143 //////////////////////////////////////////////////////////////////////////////
1144
1145 template <typename TStream, typename THost, typename TSpec>
1146 inline TStream &
1147 operator << (TStream & target, 
1148                          Segment<THost, TSpec> const & source)
1149 {
1150 SEQAN_CHECKPOINT
1151         write(target, source);
1152         return target;
1153 }
1154
1155 //////////////////////////////////////////////////////////////////////////////
1156
1157 template <typename TStream, typename THost, typename TSpec>
1158 inline TStream &
1159 operator >> (TStream & source, 
1160                          Segment<THost, TSpec> & target)
1161 {
1162 SEQAN_CHECKPOINT
1163         read(source, target);
1164         return source;
1165 }
1166 template <typename TStream, typename THost, typename TSpec>
1167 inline TStream &
1168 operator >> (TStream & source, 
1169                          Segment<THost, TSpec> const & target)
1170 {
1171 SEQAN_CHECKPOINT
1172         read(source, target);
1173         return source;
1174 }
1175
1176 //////////////////////////////////////////////////////////////////////////////
1177
1178
1179 } //namespace SEQAN_NAMESPACE_MAIN
1180
1181 #endif //#ifndef SEQAN_HEADER_...