File: pipe_base.h

package info (click to toggle)
seqan2 2.5.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 228,748 kB
  • sloc: cpp: 257,602; ansic: 91,967; python: 8,326; sh: 1,056; xml: 570; makefile: 229; awk: 51; javascript: 21
file content (1038 lines) | stat: -rw-r--r-- 33,098 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
// ==========================================================================
//                 SeqAn - The Library for Sequence Analysis
// ==========================================================================
// Copyright (c) 2006-2026, Knut Reinert, FU Berlin
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above copyright
//       notice, this list of conditions and the following disclaimer in the
//       documentation and/or other materials provided with the distribution.
//     * Neither the name of Knut Reinert or the FU Berlin nor the names of
//       its contributors may be used to endorse or promote products derived
//       from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL KNUT REINERT OR THE FU BERLIN BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
// OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
// DAMAGE.
//
// ==========================================================================
// Author: David Weese <david.weese@fu-berlin.de>
// ==========================================================================

#ifndef SEQAN_HEADER_PIPE_BASE_H
#define SEQAN_HEADER_PIPE_BASE_H

namespace seqan2 {

    // shortcuts to ease pipeline construction
    #define TypeOf_(TObject)  typename Value<TObject>::Type
    #define TSizeOf_(TObject) typename Size<TObject>::Type

/*!
 * @class Pipe
 * @headerfile <seqan/pipe.h>
 * @brief Pipes are pop-passive pipeline modules.
 *
 * @signature template <typename TInput, typename TSpec>
 *            class Pipe;
 *
 * @tparam TSpec  The specializing type.
 * @tparam TInput The type of the pipeline module this module reads from.  Use Bundle2, Bundle3, etc. to read
 *                from more than one module.
 *
 * Use @link Pipe#Value @endlink to get the output type of a given Pipe (returns <tt>Value&lt;TInput&gt;::Type</tt> by
 * default).
 *
 * Use Size to get the size type of a given Pipe (returns <tt>Size&lt;TInput&gt;::Type</tt> by default).
 */

/*!
 * @fn Pipe::Pipe
 * @brief Constructor
 *
 * @signature Pipe::Pipe(in);
 *
 * @param[in] in Reference to an input pipe.
 */

    template < typename TInput, typename TSpec >
    struct Pipe {
        TInput &in;
        Pipe(TInput &_in): in(_in) {}
    };

    // base class for multiple sequence algorithms
    // to hold extra information about desired position type (TPair)
    // and the type storing absolute sequence offsets (TLimitsString)
    template <typename TSpec, typename TPair, typename TLimitsString>
    struct Multi;

/*!
 * @class Bundle2
 * @headerfile <seqan/pipe.h>
 * @brief Stores references to two arbitrary objects.
 *
 * @signature template <typename TInput1, typename TInput2>
 *            class Bundle2;
 *
 * @tparam TInput1 The type of the first object.
 * @tparam TInput2 The type of the second object.
 *
 * Primarily used as an adaptor for pipes with two sources.
 *
 * @see bundle2
 *
 * @var TInput1 Bundle2::in1;
 * @brief TInput1 reference
 *
 * @var TInput2 Bundle2::in2;
 * @brief TInput2 reference
 */

    // pipe input adapter 2->1 pipe
    template < typename TInput1, typename TInput2 >
    struct Bundle2 {
        typedef TInput1 Input1;
        typedef TInput2 Input2;
        TInput1 &in1;
        TInput2 &in2;
        Bundle2(TInput1 &_in1, TInput2 &_in2): in1(_in1),in2(_in2) {}
    };

/*!
 * @fn bundle2
 * @headerfile <seqan/pipe.h>
 * @brief Returns a bundle of two objects.
 *
 * @signature TBundle bundle2(in1, in2);
 *
 * @param[in] in1 First object.
 * @param[in] in2 Second object.
 *
 * @return TBundle A Bundle2 with references to <tt>in1</tt> and <tt>in2</tt>.
 *
 * @see Bundle2
 */

    template < typename TInput1, typename TInput2 >
    inline Bundle2< TInput1, TInput2 >
    bundle2(TInput1 &_in1, TInput2 &_in2) {
        return Bundle2< TInput1, TInput2 >(_in1, _in2);
    }
/*!
 * @class Bundle3
 *
 * @brief Stores references to three arbitrary objects.
 *
 * @signature template <typename TInput1, typename TInput2, typename TInput3>
 *            struct Bundle3;
 *
 * @tparam TInput3 The type of the third object.
 * @tparam TInput2 The type of the second object.
 * @tparam TInput1 The type of the first object.
 *
 * Primarily used as an adaptor for pipes with three sources.
 *
 * @see bundle3
 *
 * @var TInput1 Bundle3::in1;
 * @brief TInput1 reference
 *
 * @var TInput2 Bundle3::in2;
 * @brief TInput2 reference
 *
 * @var TInput3 Bundle3::in3;
 * @brief TInput3 reference
 */

    // pipe input adapter 3->1 pipe
    template < typename TInput1, typename TInput2, typename TInput3 >
    struct Bundle3 {
        typedef TInput1 Input1;
        typedef TInput2 Input2;
        typedef TInput3 Input3;
        TInput1 &in1;
        TInput2 &in2;
        TInput3 &in3;
        Bundle3(TInput1 &_in1, TInput2 &_in2, TInput3 &_in3): in1(_in1),in2(_in2),in3(_in3) {}
    };

/*!
 * @fn bundle3
 * @headerfile <seqan/pipe.h>
 * @brief Returns a bundle of three objects.
 *
 * @signature TBundle bundle3(in1, in2, in3);
 *
 * @param[in] in1 First object.
 * @param[in] in2 Second object.
 * @param[in] in3 Third object.
 *
 * @return TBundle A Bundle3 with references to <tt>in1</tt>, <tt>in2</tt>, and <tt>in3</tt>.
 *
 * @see Bundle3
 */

    template < typename TInput1, typename TInput2, typename TInput3 >
    inline Bundle3< TInput1, TInput2, TInput3 >
    bundle3(TInput1 &_in1, TInput2 &_in2, TInput3 &_in3) {
        return Bundle3< TInput1, TInput2, TInput3 >(_in1, _in2, _in3);
    }

/*!
 * @class Bundle5
 * @headerfile <seqan/pipe.h>
 * @brief Stores references to five arbitrary objects.
 *
 * @signature template <typename TInput1, typename TInput2, typename TInput3, typename TInput4, typename TInput5>
 *            class Bundle5;
 *
 * @tparam TInput1 The type of the first object.
 * @tparam TInput2 The type of the second object.
 * @tparam TInput3 The type of the third object.
 * @tparam TInput4 The type of the fourth object.
 * @tparam TInput5 The type of the fifth object.
 *
 * Primarily used as an adaptor for pipes with five sources.
 *
 * @see bundle5
 *
 * @var TInput1 Bundle5::in1;
 * @brief TInput1 reference
 *
 * @var TInput2 Bundle5::in2;
 * @brief TInput2 reference
 *
 * @var TInput3 Bundle5::in3;
 * @brief TInput3 reference
 *
 * @var TInput4 Bundle5::in4;
 * @brief TInput4 reference
 *
 * @var TInput5 Bundle5::in5;
 * @brief TInput5 reference
 */

    // pipe input adapter 5->1 pipe
    template < typename TIn1, typename TIn2, typename TIn3, typename TIn4, typename TIn5 >
    struct Bundle5 {
        TIn1 &in1; TIn2 &in2;
        TIn3 &in3; TIn4 &in4;
        TIn5 &in5;
        Bundle5(TIn1& _in1, TIn2& _in2,
                TIn3& _in3, TIn4& _in4,
                TIn5& _in5):    in1(_in1),in2(_in2),
                                in3(_in3),in4(_in4),
                                in5(_in5) {}
    };

/*!
 * @fn bundle5
 * @headerfile <seqan/pipe.h>
 * @brief Returns a bundle of five objects.
 *
 * @signature TBundle bundle5(in1, in2, in3, in4, in5);
 *
 * @param[in] in1 First object.
 * @param[in] in2 Second object.
 * @param[in] in3 Third object.
 * @param[in] in4 Fourth object.
 * @param[in] in5 Fifth object.
 *
 * @return TBundle A Bundle5 with references to <tt>in1</tt>, <tt>in2</tt>, <tt>in3</tt>, <tt>in4</tt>,
 *                 and <tt>in5</tt>.
 *
 * @see Bundle5
 */

    template < typename TIn1, typename TIn2, typename TIn3, typename TIn4, typename TIn5 >
    inline Bundle5< TIn1, TIn2, TIn3, TIn4, TIn5 >
    bundle5(TIn1 &_in1, TIn2 &_in2, TIn3 &_in3, TIn4 &_in4, TIn5 &_in5) {
        return Bundle5< TIn1, TIn2, TIn3, TIn4, TIn5 >(_in1, _in2, _in3, _in4, _in5);
    }

    template < typename TValue, typename TSize >
    struct AbstractSource {};

/*!
 * @mfn Pipe#Value
 * @brief Return value type of the Pipe specialization.
 *
 * @signature Value<TPipe>::Type;
 *
 * @tparam TPipe The Pipe specialization to query.
 *
 * @return Type The resulting value type.
 */

    template < typename TValue, typename TSize >
    struct Value< Pipe<void, AbstractSource<TValue, TSize> > > {
        typedef TValue Type;
    };

/*!
 * @mfn Pipe#Size
 * @brief Return size type of the Pipe specialization.
 *
 * @signature Size<TPipe>::Type;
 *
 * @tparam TPipe The Pipe specialization to query.
 *
 * @return Type The resulting size type.
 */

    template < typename TValue, typename TSize >
    struct Size< Pipe<void, AbstractSource<TValue, TSize> > > {
        typedef TSize Type;
    };

    template < typename TInput, typename TSpec >
    struct Value< Pipe<TInput, TSpec> > {
        typedef typename Value<TInput>::Type Type;
    };

    template < typename TInput, typename TSpec >
    struct Size< Pipe<TInput, TSpec> > {
        typedef typename Size<TInput>::Type Type;
    };

    template < typename TInput1, typename TInput2 >
    struct Size< Bundle2< TInput1, TInput2 > > {
        typedef typename Size<TInput1>::Type Type;
    };

    template < typename TInput1, typename TInput2, typename TInput3 >
    struct Size< Bundle3< TInput1, TInput2, TInput3 > > {
        typedef typename Size<TInput1>::Type Type;
    };

    template < typename TIn1, typename TIn2, typename TIn3, typename TIn4, typename TIn5 >
    struct Size< Bundle5< TIn1, TIn2, TIn3, TIn4, TIn5 > > {
        typedef typename Size<TIn1>::Type Type;
    };

    template < typename TInput, typename TSpec >
    struct Position< Pipe<TInput, TSpec> > {
        typedef typename Size<Pipe<TInput, TSpec> >::Type Type;
    };

    template < typename TInput, typename TSpec >
    struct Difference< Pipe<TInput, TSpec> > {
        typedef typename MakeSigned_<typename Size<Pipe<TInput, TSpec> >::Type>::Type Type;
    };
/*
    template < typename TInput, typename TSpec >
    struct Iterator< Pipe<TInput, TSpec> >;

    template < typename TInput, typename TSpec >
    struct Iterator< Pipe<TInput, TSpec> const >:
        Iterator< Pipe<TInput, TSpec> > {};
*/

    template <typename T>
    struct Source;

    template <typename TInput, typename TSpec>
    struct Source<Pipe<TInput, TSpec> >
    {
        typedef TInput Type;
    };

    template < typename TInput, typename TSpec >
    inline TInput const &
    source(Pipe<TInput, TSpec> const &me) {
        return me.in;
    }

    template < typename TInput, typename TSpec >
    inline TInput &
    source(Pipe<TInput, TSpec> &me) {
        return me.in;
    }

/*!
 * @fn Pipe#length
 * @headerfile <seqan/pipe.h>
 * @brief Length of the pipe.
 *
 * @signature TSize length(pipe);
 *
 * @param[in] pipe  The Pipe to query for its size.
 *
 * @return    TSize The size of the pipe.
 */

    template < typename TInput, typename TSpec >
    inline typename Size< Pipe<TInput, TSpec> >::Type
    length(Pipe<TInput, TSpec> const &me) {
        return length(me.in);
    }

    template < typename TInput1, typename TInput2 >
    inline typename Size< Bundle2<TInput1, TInput2> >::Type
    length(Bundle2<TInput1, TInput2> const &me) {
        return length(me.in1);
    }

    template < typename TInput1, typename TInput2, typename TInput3 >
    inline typename Size< Bundle3<TInput1, TInput2, TInput3> >::Type
    length(Bundle3<TInput1, TInput2, TInput3> const &me) {
        return length(me.in1);
    }

    template < typename TIn1, typename TIn2, typename TIn3, typename TIn4, typename TIn5 >
    inline typename Size< Bundle5<TIn1, TIn2, TIn3, TIn4, TIn5> >::Type
    length(Bundle5<TIn1, TIn2, TIn3, TIn4, TIn5> const &me) {
        return length(me.in1);
    }

//////////////////////////////////////////////////////////////////////////////


    template < typename TInput, typename TSpec >
    inline typename Size< Pipe<TInput, TSpec> >::Type
    countSequences(Pipe<TInput, TSpec> const &me) {
        return countSequences(me.in);
    }

    template < typename TInput1, typename TInput2 >
    inline typename Size< Bundle2<TInput1, TInput2> >::Type
    countSequences(Bundle2<TInput1, TInput2> const &me) {
        return countSequences(me.in1);
    }

    template < typename TInput1, typename TInput2, typename TInput3 >
    inline typename Size< Bundle3<TInput1, TInput2, TInput3> >::Type
    countSequences(Bundle3<TInput1, TInput2, TInput3> const &me) {
        return countSequences(me.in1);
    }

    template < typename TIn1, typename TIn2, typename TIn3, typename TIn4, typename TIn5 >
    inline typename Size< Bundle5<TIn1, TIn2, TIn3, TIn4, TIn5> >::Type
    countSequences(Bundle5<TIn1, TIn2, TIn3, TIn4, TIn5> const &me) {
        return countSequences(me.in1);
    }

/*!
 * @fn Pipe#front
 * @headerfile <seqan/pipe.h>
 * @brief Gets the first element of the remaining stream.
 *
 * @signature TValue front(object);
 *
 * @param[in] object A pop-passive pipeline module.
 *
 * @return TValue The first element of the remaining input stream.  Return type is <tt>Value&lt;TObject&gt;::Type</tt>
 *                for <tt>object</tt> type <tt>TObject</tt>.
 *
 * Pipe#front or Pipe#pop can only be called within a read process surrounded by beginRead and endRead.
 *
 * @see Pipe#pop
 */

    template < typename TInput, typename TSpec, typename TValue >
    inline typename Value< Pipe<TInput, TSpec> >::Type const &
    front(Pipe<TInput, TSpec> &me) {
        return *me;
    }

/*!
 * @fn Pipe#pop
 * @headerfile <seqan/pipe.h>
 * @brief Pops the first element of the remaining stream.
 *
 * @signature void pop(pipe[, ref]);
 *
 * @param[in,out] pipe A pop-passive pipeline module.
 * @param[out]    ref    Reference to the result.  Result type is <tt>Value&lt;TObject&gt;::Type</tt> for <tt>object</tt>
 *                       type <tt>TObject</tt>.  Returns the first element of the remaining input stream.
 *
 * In contrast to Pipe#front this function also steps one element further.
 *
 * Pipe#front or Pipe#pop can only be called within a read process surrounded by beginRead and endRead.
 */

    template < typename TInput, typename TSpec, typename TValue >
    inline void pop(Pipe<TInput, TSpec> &me, TValue &Ref_) {
        Ref_ = *me;
        ++me;
    }

    template < typename TInput, typename TSpec >
    inline void pop(Pipe<TInput, TSpec> &me) {
        ++me;
    }

/*!
 * @fn Pipe#atEnd
 * @brief Check whether the @link Pipe @endlink object is at end.
 *
 * @signature bool atEnd(pipe);
 *
 * @param[in] pipe The @link Pipe @endlink object to query.
 *
 * @return bool true in case of the pipe being at the end, false otherwise.
 */

    //////////////////////////////////////////////////////////////////////////////
    // pipe flow control

    struct ControlEof_;            // end of stream
    struct ControlEos_;            // end of sequence (for multiple sequences)
    struct ControlClear_;        // clear previous pool
    struct ControlBeginRead_;    // begin read process
    struct ControlEndRead_;        // end read process

    typedef Tag<ControlEof_>        ControlEof;
    typedef Tag<ControlEos_>        ControlEos;
    typedef Tag<ControlClear_>        ControlClear;
    typedef Tag<ControlBeginRead_>    ControlBeginRead;
    typedef Tag<ControlEndRead_>    ControlEndRead;

    template < typename TInput, typename TSpec, typename TCommand >
    inline bool control(Pipe<TInput, TSpec> &me, TCommand const &command) {
        return control(me.in, command);
    }

    template < typename TInput, typename TSpec >
    inline bool eof(Pipe<TInput, TSpec> &me) {
        return control(me, ControlEof());
    }

    template < typename TInput, typename TSpec >
    inline bool eos(Pipe<TInput, TSpec> &me) {
        return control(me, ControlEos());
    }

    template < typename TInput, typename TSpec >
    inline bool clear(Pipe<TInput, TSpec> &me) {
        return control(me, ControlClear());
    }

/*!
 * @fn Pipe#beginRead
 * @headerfile <seqan/pipe.h>
 * @brief Initiates a read process.
 *
 * @signature bool beginRead(object);
 *
 * @param[in,out] object A pop-passive pipeline module.
 *
 * @return bool true on success, false on failure.
 *
 * <tt>beginRead</tt> rewinds the output stream, prepares <tt>object</tt> for succeeding reads, and typically calls
 * <tt>beginRead</tt> of the input pipeline modules.
 *
 * A read process must be terminated with endRead. Nested read processes are not allowed.
 *
 * @see Pipe#endRead
 */

    template < typename TInput, typename TSpec >
    inline bool beginRead(Pipe<TInput, TSpec> &me)
    {
        return control(me, ControlBeginRead());
    }

/*!
 * @fn Pipe#endRead
 * @headerfile <seqan/pipe.h>
 * @brief Terminates a read process.
 *
 * @signature bool endRead(object);
 *
 * @param[in,out] object A pop-passive pipeline module.
 *
 * @return bool true on success, false on failure.
 *
 * <tt>endRead</tt> closes the output stream, frees resources possibly allocated by beginRead, and typically calls
 * <tt>endRead</tt> of the input pipeline modules.
 *
 * @see Pipe#beginRead
 */

    template < typename TInput, typename TSpec >
    inline bool endRead(Pipe<TInput, TSpec> &me) {
        return control(me, ControlEndRead());
    }


    //////////////////////////////////////////////////////////////////////////////
    // 2->1 pipe flow control
    template < typename TInput1, typename TInput2, typename TCommand >
    inline bool control(Bundle2<TInput1, TInput2> &me, TCommand const &command) {
        return    control(me.in1, command) &&
                control(me.in2, command);
    }

    //////////////////////////////////////////////////////////////////////////////
    // 3->1 pipe flow control
    template < typename TInput1, typename TInput2, typename TInput3, typename TCommand >
    inline bool control(Bundle3<TInput1, TInput2, TInput3> &me, TCommand const &command) {
        return    control(me.in1, command) &&
                control(me.in2, command) &&
                control(me.in3, command);
    }

    //////////////////////////////////////////////////////////////////////////////
    // 5->1 pipe flow control
    template < typename TIn1, typename TIn2, typename TIn3, typename TIn4, typename TIn5, typename TCommand >
    inline bool control(Bundle5<TIn1, TIn2, TIn3, TIn4, TIn5 > &me, TCommand const &command) {
        return    control(me.in1, command) &&
                control(me.in2, command) &&
                control(me.in3, command) &&
                control(me.in4, command) &&
                control(me.in5, command);
    }
/*!
 * @fn Pipe#assign
 * @headerfile <seqan/pipe.h>
 * @brief Assigns one object to another object.
 *
 * @signature void assign(target, source);
 *
 * @param[out] target Reference to assign to.
 * @param[in]  source Value to assign.
 *
 * Assign value of source to target.
 */
    //////////////////////////////////////////////////////////////////////////////
    // pipe -> string

    // We cannot use the most-generic TObject as first argument, as operator << specialized somewhere else in
    // the first argument and here we need to specialize it in the second.
    template < typename TValue,
               typename TStringSpec,
               typename TInput,
               typename TSpec >
    inline void assign(String<TValue, TStringSpec> &dest, Pipe<TInput, TSpec> &src)
    {
        typedef typename Iterator<String<TValue, TStringSpec>, Standard >::Type TDestIter;
        resize(dest, length(src));
        beginRead(src);
        for (TDestIter _cur = begin(dest, Standard()), _end = end(dest, Standard()); _cur != _end; ++_cur, ++src)
            *_cur = *src;
        endRead(src);
    }

    template < typename TValue,
               typename TSegmentSpec,
               typename TInput,
               typename TSpec >
    inline void assign(Segment<TValue, TSegmentSpec> &dest, Pipe<TInput, TSpec> &src)
    {
        typedef typename Iterator<Segment<TValue, TSegmentSpec>, Standard >::Type TDestIter;
        resize(dest, length(src));
        beginRead(src);
        for (TDestIter _cur = begin(dest, Standard()), _end = end(dest, Standard()); _cur != _end; ++_cur, ++src)
            *_cur = *src;
        endRead(src);
    }

    template < typename TValue,
               typename TStringSpec,
               typename TInput,
               typename TSpec >
    inline void operator << (String<TValue, TStringSpec> &dest, Pipe<TInput, TSpec> &src)
    {
        assign(dest, src);
    }

    template < typename TValue,
               typename TSegmentSpec,
               typename TInput,
               typename TSpec >
    inline void operator << (Segment<TValue, TSegmentSpec> &dest, Pipe<TInput, TSpec> &src)
    {
        assign(dest, src);
    }

    //////////////////////////////////////////////////////////////////////////////
    // pipe -> out_stream
    template <typename TInput, typename TSpec>
    std::ostream& operator << (std::ostream &out, Pipe<TInput, TSpec> &src)
    {
        beginRead(src);
        while (!eof(src)) {
            out << *src << std::endl;
            ++src;
        }
        endRead(src);
        return out;
    }


    template < typename TObject, typename TSpec >
    struct BufferHandler;

    template <typename TObject, typename TSpec>
    struct Value<BufferHandler<TObject, TSpec> >
    {
        typedef Buffer<typename Value<TObject>::Type> Type;
    };



    template <typename TObject, typename TSpec>
    struct Handler;

    template <typename TObject, typename TSpec>
    struct Value<Handler<TObject, TSpec> >:
        public Value<TObject> {};

    // buffer-based read/write handler metafunctions
    template < typename TInput >
    struct BufReadHandler;

    template < typename TOutput >
    struct BufWriteHandler;



    //////////////////////////////////////////////////////////////////////////////
    // generic adapter for buffered readers/writers

    template <typename TValue, typename TSpec, typename TSize >
    inline void resize(Buffer<TValue, TSpec> &me, TSize size);

    struct AdapterSpec;

    template <typename TBufferHandler>
    struct Handler<TBufferHandler, AdapterSpec>
    {
        typedef typename Value<TBufferHandler>::Type        TBuffer;
        typedef typename Value<TBuffer>::Type               TValue;
        typedef typename Iterator<TBuffer, Standard>::Type  TIterator;

        TBufferHandler  handler;
        TBuffer            buffer;
        TIterator       cur;

        template < typename TObject >
        Handler(TObject &_object) :
            handler(_object), cur() {}

        inline bool begin()
        {
            buffer = handler.first();
            cur = seqan2::begin(buffer, Standard());
            return true;
        }

        inline TValue const & front() const
        {
            return *cur;
        }

        inline void pop()
        {
            if (++cur == seqan2::end(buffer, Standard()))
            {
                buffer = handler.next();
                cur = seqan2::begin(buffer, Standard());
            }
        }

        inline void pop(TValue &Ref_)
        {
            Ref_ = *cur;
            pop();
        }

        inline void push(TValue const & Val_)
        {
            if (cur == seqan2::end(buffer, Standard()))
            {
                buffer = handler.next();
                cur = seqan2::begin(buffer, Standard());
            }
            *cur = Val_;
            ++cur;
        }

        inline bool eof() const
        {
            return length(buffer) == 0;
        }

        inline void end()
        {
            handler.end();
            resize(buffer, 0);
        }

        inline void process()
        {
            handler.process();
        }
    };

    template <typename TBufferHandler>
    struct Value<Handler<TBufferHandler, AdapterSpec> >:
        public Value<typename Value<TBufferHandler>::Type> {};

    // character-based read/write handler metafunctions
    template < typename TInput >
    struct ReadHandler
    {
        typedef Handler< typename BufReadHandler<TInput> ::Type, AdapterSpec > Type;
    };

    template < typename TOutput >
    struct WriteHandler
    {
        typedef Handler< typename BufWriteHandler<TOutput> ::Type, AdapterSpec > Type;
    };


    //////////////////////////////////////////////////////////////////////////////
    // pair incrementer
    //
    // used by pipes processing multiples sequences
    // for generating pairs (seqNo, seqOffs)

    template <typename TPair, typename TLimits>
    struct PairIncrementer_
    {
        typedef typename Iterator<TLimits const, Standard>::Type            TIter;
        typedef typename RemoveConst_<typename Value<TLimits>::Type>::Type  TSize;
        typedef typename Value<TPair, 2>::Type                              TOffset;

        TIter   it, itEnd;
        TSize   old;
        TOffset localEnd;

        TPair pos;
        inline operator TPair () const
        {
            return pos;
        }

        inline TPair const & operator++ ()
        {
            TOffset i2 = getValueI2(pos) + 1;
            if (i2 >= localEnd)
            {
                i2 = 0;
                localEnd = 0;
                while (!localEnd && (it != itEnd))
                {
                    typename Value<TPair,1>::Type i1 = getValueI1(pos);
                    assignValueI1(pos, i1 + 1);
                    localEnd = (*it - old);
                    // test for overflows
                    SEQAN_ASSERT_LT_MSG(i1, getValueI1(pos), "Overflow detected. Use a bigger type for the *first* value in the SAValue pair!");
                    SEQAN_ASSERT_EQ_MSG((TSize)localEnd, (*it - old), "Overflow detected. Use a bigger type for the *second* value in the SAValue pair!");
                    old = *it;
                    ++it;
                }
                if (!localEnd && it == itEnd)
                    assignValueI1(pos, getValueI1(pos) + 1);    // set pos behind the last sequence
            }
            assignValueI2(pos, i2);
            return pos;
        }
    };

    template <typename TPair, typename TLimits, typename TLimits2>
    void setHost(PairIncrementer_<TPair, TLimits> &me, TLimits2 const &limits)
    {
        me.it = begin(limits, Standard());
        me.itEnd = end(limits, Standard());
        me.old = 0;
        me.localEnd = 0;
        me.pos = TPair(0, 0);
        if (length(limits) > 1)
        {
            ++me.it;
            ++me;
            assignValueI1(me.pos, getValueI1(me.pos) - 1);
        }
    }

    template <typename TPair, typename TLimits, typename TLimits2>
    void setHost(PairIncrementer_<TPair, TLimits> &me, TLimits2 &limits)
    {
        setHost(me, const_cast<TLimits2 const &>(limits));
    }
//____________________________________________________________________________

    template <typename TPair, typename TLimits>
    TPair const & value(PairIncrementer_<TPair, TLimits> const &me) {
        return me.pos;
    }

    template <typename TPair, typename TLimits>
    TPair & value(PairIncrementer_<TPair, TLimits> &me) {
        return me.pos;
    }


    //////////////////////////////////////////////////////////////////////////////
    // pair decrementer
    //
    // used by pipes processing multiples sequences
    // for generating pairs (seqNo, seqOffs)

    template <typename TPair, typename TLimits, unsigned m = 0>
    struct PairDecrementer_
    {
        typedef typename Iterator<TLimits const, Standard>::Type            TIter;
        typedef typename RemoveConst_<typename Value<TLimits>::Type>::Type  TSize;

        TIter       it, itEnd;
        TSize       old;
        TPair        pos;
        unsigned    residue;

        PairDecrementer_() : it(), itEnd(), old(), pos(), residue() {}
        PairDecrementer_(TLimits const &_limits) : it(), itEnd(), old(), pos(), residue()
        { setHost(*this, _limits); }

        inline operator TPair () const
        {
            return pos;
        }

        inline TPair const & operator-- ()
        {
            typename Value<TPair,2>::Type i2 = getValueI2(pos);
            if (i2 > 1)
            {
                --i2;
                if (residue == 0) residue = m;
                --residue;
            }
            else
            {
                i2 = 0;
                while (!i2 && (it != itEnd))
                {
                    typename Value<TPair,1>::Type i1 = getValueI1(pos);
                    assignValueI1(pos, i1 + 1);
                    i2 = (*it - old);
                    // test for overflows
                    SEQAN_ASSERT_LT_MSG(i1, getValueI1(pos), "Overflow detected. Use a bigger type for the *first* value in the SAValue pair!");
                    SEQAN_ASSERT_EQ_MSG((TSize)i2, *it - old, "Overflow detected. Use a bigger type for the *second* value in the SAValue pair!");
                    old = *it;
                    ++it;
                }
                residue = i2 % m;
            }
            assignValueI2(pos, i2);
            return pos;
        }
    };

    template <typename TPair, typename TLimits, unsigned m, typename TLimits2>
    void setHost(PairDecrementer_<TPair, TLimits, m> &me, TLimits2 const &limits)
    {
        me.it = begin(limits);
        me.itEnd = end(limits);
        me.old = 0;
        me.pos = TPair(0, 0);
        if (length(limits) > 1)
        {
            ++me.it;
            --me;
            assignValueI1(me.pos, getValueI1(me.pos) - 1);
        } else
            me.residue = 0;
    }

    template <typename TPair, typename TLimits, unsigned m, typename TLimits2>
    void setHost(PairDecrementer_<TPair, TLimits, m> &me, TLimits2 &limits)
    {
        setHost(me, const_cast<TLimits const &>(limits));
    }
//____________________________________________________________________________

    template <typename TPair, typename TLimits>
    struct PairDecrementer_<TPair, TLimits, 0>
    {
        typedef typename Iterator<TLimits const, Standard>::Type            TIter;
        typedef typename RemoveConst_<typename Value<TLimits>::Type>::Type  TSize;

        TIter       it, itEnd;
        TSize       old;
        TPair        pos;

        PairDecrementer_() {}
        PairDecrementer_(TLimits const &_limits) { setHost(*this, _limits); }

        inline operator TPair () const {
            return pos;
        }

        inline TPair const & operator-- ()
        {
            typename Value<TPair,2>::Type i2 = getValueI2(pos);
            if (i2 > 1)
                --i2;
            else
            {
                i2 = 0;
                while (!i2 && (it != itEnd))
                {
                    typename Value<TPair,1>::Type i1 = getValueI1(pos);
                    assignValueI1(pos, i1 + 1);
                    i2 = (*it - old);
                    // test for overflows
                    SEQAN_ASSERT_LT_MSG(i1, getValueI1(pos), "Overflow detected. Use a bigger type for the *first* value in the SAValue pair!");
                    SEQAN_ASSERT_EQ_MSG((TSize)i2, *it - old, "Overflow detected. Use a bigger type for the *second* value in the SAValue pair!");
                    old = *it;
                    ++it;
                }
            }
            assignValueI2(pos, i2);
            return pos;
        }
    };

    template <typename TPair, typename TLimits, typename TLimits2>
    void setHost(PairDecrementer_<TPair, TLimits, 0> &me, TLimits2 const &limits) {
        me.it = begin(limits);
        me.itEnd = end(limits);
        me.old = 0;
        assignValueI1(me.pos, 0);
        assignValueI2(me.pos, 0);
        if (length(limits) > 1) {
            ++me.it;
            --me;
            assignValueI1(me.pos, getValueI1(me.pos) - 1);
        }
    }

    template <typename TPair, typename TLimits, typename TLimits2>
    void setHost(PairDecrementer_<TPair, TLimits, 0> &me, TLimits2 &limits)
    {
        setHost(me, const_cast<TLimits const &>(limits));
    }
//____________________________________________________________________________

    template <typename TPair, typename TLimits, unsigned m>
    TPair const & value(PairDecrementer_<TPair, TLimits, m> const &me) {
        return me.pos;
    }

    template <typename TPair, typename TLimits, unsigned m>
    TPair & value(PairDecrementer_<TPair, TLimits, m> &me) {
        return me.pos;
    }

}

#endif