File: cyclic_shape.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 (623 lines) | stat: -rw-r--r-- 22,340 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
// ========================================================================== 
//                 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: Sascha Meiers <meiers@inf.fu-berlin.de>
// ==========================================================================
// Definition of generic and fixed-at-compile-time CyclicShapes
// ==========================================================================

#ifndef SEQAN_HEADER_CYCLIC_SHAPE_H
#define SEQAN_HEADER_CYCLIC_SHAPE_H

#include <seqan/index/shape_base.h>
#include <seqan/index/shape_gapped.h>

namespace seqan2 {

// ==========================================================================
// Forwards
// ==========================================================================

// needed for GappedShape<HardwiredShape<...> >
template<typename TSpec> struct GappedShape;

// GenericShape defined in shape_gapped.h via typedef
typedef GappedShape<Default> GenericShape;

// ==========================================================================
// Tags, Classes, Enums
// ==========================================================================

// --------------------------------------------------------------------------
// Class CyclicShape
// --------------------------------------------------------------------------

/*!
 * @class CyclicShape
 * @headerfile <seqan/modifier.h>
 *
 * @brief A pattern of zeros and ones to mark "don't care"-positions in a text.
 *
 * @signature template <typename TShapeSpec>
 *            class CyclicShape<TShapeSpec>;
 *
 * @tparam TShapeSpec The specializing type. Default is @link GenericCyclicShape
 *         GenericShape @endlink, another option is @link FixedCyclicShape
 *         FixedShape @endlink.
 * @tparam TSize Size type of the CyclicShape
 *
 * CyclicShapes store a pattern like <tt>11010100110</tt> that mark
 * care (match) positions and don't-care positions.
 * Unlike @link Shape @endlink, the CyclicShape does not perform hashing of q-grams.
 * It is instead useful to modify a text in such a way that zero
 * position are ignored. The pattern is applied repeatedly on the whole
 * text, as the example shows.
 *
 * Note that CyclicShapes can start and end with zero characters, which
 * is not allowed in @link Shape @endlink.
 *
 * @section Examples
 * @include demos/dox/modifier/cyclic_shape.cpp
 *
 * The output is as follows:
 *
 * @include demos/dox/modifier/cyclic_shape.cpp.stdout
 *
 * @see ModCyclicShapeModifiedString
 * @see ModCyclicShapeModifiedIterator
 */
template<typename TCyclicSpec = GenericShape>
class CyclicShape;                           // member: diffs, loffset, span

template<unsigned L, typename THardWiredShape, unsigned R>
struct FixedShape;

// --------------------------------------------------------------------------
// Metafunction Size
// --------------------------------------------------------------------------

/*!
 * @mfn CyclicShape#Size
 * @headerfile <seqan/modifier.h>
 *
 * @brief Size type for parameters used in CyclicShape.
 *
 * @signature Size<CyclicShape<TSpec> >::Type;
 *
 * @tparam TSpec The CyclicShape specialization.
 *
 * @return TReturn Currently the return type <tt>unsigned char</tt>.
 *
 * @section Remarks
 */
template<typename TSpec>
struct Size<CyclicShape<TSpec> >
{
    typedef unsigned char Type;
};

// --------------------------------------------------------------------------
// Class GenericCyclicShape
// --------------------------------------------------------------------------

/*!
 * @class GenericCyclicShape Generic CyclicShape
 * @extends CyclicShape
 * @headerfile <seqan/modifier.h>
 *
 * @brief CyclicShape initiated at runtime.
 *
 * @tparam TSize Size type of the CyclicShape
 *
 * @signature template <>
 *            class CyclicShape<GenericShape>;
 *
 * Generic Shapes can be easily created using <tt>stringToCyclicShape</tt>,
 * but their usage comes with a significant loss in efficiency.
 * For longer sequences, prefer the @link FixedCyclicShape @endlink.
 *
 * @section Examples
 * @include demos/dox/modifier/cyclic_shape.cpp
 *
 * The output is as follows:
 *
 * @include demos/dox/modifier/cyclic_shape.cpp.stdout
 *
 * @see ModCyclicShapeModifiedString
 * @see ModCyclicShapeModifiedIterator
 */
template<>
class CyclicShape<GenericShape>
{
public:
    /*!
     * @var String<TSize> GenericCyclicShape::diffs;
     * @brief Distances between care positions.
     *
     * <tt>diffs</tt> has <i>weight</i> many non-zero entries describing the size
     * of the gap after a care-position. The last entry holds the cyclic distance from
     * the last "1" to the first one.
     */
    String<Size<CyclicShape>::Type> diffs;

    /*!
     * @var TSize CyclicShape::span;
     * @brief span of the CyclicShape.
     * @var TSize CyclicShape::loffset;
     * @brief left offset (number of leading zeros) of the CyclicShape.
     */
    Size<CyclicShape>::Type loffset, span;

    /*!
     * @fn GenericCyclicShape::CyclicShape
     * @brief The constructor
     * @signature CyclicShape();
     * @signature CyclicShape(shape);
     * @param[in] shape A GenericCyclicShape to be copied.
     *
     * The default constructor generates the pattern "1".
     * The copy constructor copies the GenericCyclicShape shape.
     *
     * @snippet demos/dox/modifier/cyclic_shape_snippets.cpp Define GenericCyclicShape
     *
     * @Remarks
     *
     * If you want to generate a GenericCyclicShape from a FixedCyclicShape,
     * use @link CyclicShape#cyclicShapeToString @endlink and @link GenericCyclicShape#stringToCyclicShape @endlink
     */
    CyclicShape() :
        loffset(0), span(1)
    {
        // Pattern "1"
        appendValue(diffs, 1);
    }

    CyclicShape(CyclicShape const & other)  = default;

    CyclicShape & operator=(CyclicShape const &) = default;
};

// --------------------------------------------------------------------------
// Class FixedCyclicShape
// --------------------------------------------------------------------------

/*!
 * @class FixedCyclicShape Fixed CyclicShape
 * @extends CyclicShape
 * @headerfile <seqan/modifier.h>
 *
 * @brief CyclicShape defined at compile time.
 *
 * @signature template <unsigned L, typename THardwiredShape, unsigned R>
 *            class CycledShape<FixedShape<L, GappedShape<THardwiredShape>, R> >;
 *
 * @tparam L Left offset. Number of leading zeros of the Shape.
 * @tparam R Right offset. Number of trailing zeros of the Shape.
 * @tparam THardwiredShape A specialization of @link HardwiredShape @endlink
 * @tparam TSize Size type of the CyclicShape
 *
 * Fixed CylcicShapes contain their information at compile time, so in
 * most cases no object must be created.
 * The notation is similar to the one of @link HardwiredShape @endlink: Template
 * paramters code for the distance between ones. Additionally you have to specify
 * the number of leading and trailing zeros.
 *
 * The notation is chosen in such a way that predefined Shapes like
 * PatternHunter can be plugged into a CyclicShape. Like
 * HardwiredShapes, Fixed CyclicShapes are limited to a weight of 21.
 *
 * See @link CyclicShape @endlink for an example on how to use a CyclicShape.
 *
 * @see ModCyclicShapeModifiedString
 * @see ModCyclicShapeModifiedIterator
 */
template<unsigned LeftOffset, unsigned RightOffset,
         int P00, int P01, int P02, int P03, int P04,
         int P05, int P06, int P07, int P08, int P09,
         int P10, int P11, int P12, int P13, int P14,
         int P15, int P16, int P17, int P18, int P19>
class CyclicShape<FixedShape<LeftOffset, GappedShape<HardwiredShape<
        P00, P01, P02, P03, P04, P05, P06, P07, P08, P09, P10, P11, P12, P13, P14, P15, P16, P17, P18, P19>
        >, RightOffset> >
{
public:
    typedef HardwiredShape<
            P00, P01, P02, P03, P04, P05, P06, P07, P08, P09, P10, P11, P12, P13, P14, P15, P16, P17, P18, P19>
            THardWiredShape;

    enum {loffset = LeftOffset};
    enum {span = LeftOffset + LENGTH<THardWiredShape>::VALUE + RightOffset};

    /*!
     * @var TSize[] FixedCyclicShape::diffs;
     * @brief Distances between care positions.
     *
     * <tt>static const TSize diffs[]</tt> has <i>weight</i> many non-zero entries
     * in the beginning, the remaining ones are zero. An additional entry at position
     * <i>weight1</i> holds the cyclic distance from the last "1" to the first one.
     * During the iteration with a @link ModCyclicShapeModifiedIterator @endlink
     * an index position keeps track of the position inside <tt>diffs</tt>.
     */
    static const typename Size<CyclicShape>::Type diffs[];

    /*!
     * @fn FixedCyclicShape::CyclicShape
     * @brief The constructor
     *
     * @signature CyclicShape();
     * @signature CyclicShape(shape);
     *
     * @param[in] shape A FixedCyclicShape of this type
     *
     * This constructor does not do anything, the FixedCyclicShape is defined by its type alone.
     * See the example on how to create a CyclicShape:
     *
     * @snippet demos/dox/modifier/cyclic_shape_snippets.cpp Define FixedCyclicShape
     */
    CyclicShape()
    {}

    CyclicShape(CyclicShape const &) = default;

    CyclicShape & operator=(CyclicShape const &) = default;
};

// --------------------------------------------------------------------------
// static const int CyclicShape<FixedShape<L, TSpec, R> >::diffs[]
//
// ugly work around to derive the static const array diffs[] from
// the static const array HardwiredShape::DIFFS. In diffs, the first
// zero-entry of DIFFS had to be changed.
// Note: This requires that the non-used entries of
// HardwiredShape::DIFFS are zero.
// --------------------------------------------------------------------------

template<unsigned entry, typename TFixedS>
struct _tmpArrEntry;

// normal positions in diff[]
template<unsigned entry, unsigned L, unsigned R,
         int P00, int P01, int P02, int P03, int P04,
         int P05, int P06, int P07, int P08, int P09,
         int P10, int P11, int P12, int P13, int P14,
         int P15, int P16, int P17, int P18, int P19>
struct _tmpArrEntry<entry, FixedShape<L, GappedShape<HardwiredShape
        <P00, P01, P02, P03, P04, P05, P06, P07, P08, P09, P10, P11, P12, P13, P14, P15, P16, P17, P18, P19> >, R> >
{
    enum {VALUE = entry};
};

// zero positions in diff[]
template<unsigned L, unsigned R,
         int P00, int P01, int P02, int P03, int P04,
         int P05, int P06, int P07, int P08, int P09,
         int P10, int P11, int P12, int P13, int P14,
         int P15, int P16, int P17, int P18, int P19>
struct _tmpArrEntry<0, FixedShape<L, GappedShape<HardwiredShape
        <P00, P01, P02, P03, P04, P05, P06, P07, P08, P09, P10, P11, P12, P13, P14, P15, P16, P17, P18, P19> >, R> >
{
    enum {VALUE = L + R + 1};
};

#define ENTRY(X) \
    _tmpArrEntry<X, FixedShape<L, GappedShape<HardwiredShape \
    <P00, P01, P02, P03, P04, P05, P06, P07, P08, P09, \
    P10, P11, P12, P13, P14, P15, P16, P17, P18, P19> >, R> >::VALUE

template<unsigned L, unsigned R,
        int P00, int P01, int P02, int P03, int P04,
        int P05, int P06, int P07, int P08, int P09,
        int P10, int P11, int P12, int P13, int P14,
        int P15, int P16, int P17, int P18, int P19>
const typename Size<CyclicShape<FixedShape<L, GappedShape<HardwiredShape
        <P00, P01, P02, P03, P04, P05, P06, P07, P08, P09, P10, P11, P12, P13, P14, P15, P16, P17, P18, P19>
        >, R> > >::Type
CyclicShape<FixedShape<L, GappedShape<HardwiredShape
        <P00, P01, P02, P03, P04, P05, P06, P07, P08, P09, P10, P11, P12, P13, P14, P15, P16, P17, P18, P19>
        >, R> >::diffs[] =
{
    ENTRY(P00), ENTRY(P01), ENTRY(P02), ENTRY(P03), ENTRY(P04),
    ENTRY(P05), ENTRY(P06), ENTRY(P07), ENTRY(P08), ENTRY(P09),
    ENTRY(P10), ENTRY(P11), ENTRY(P12), ENTRY(P13), ENTRY(P14),
    ENTRY(P15), ENTRY(P16), ENTRY(P17), ENTRY(P18), ENTRY(P19),
    ENTRY(0)
};

#undef ENTRY



// ==========================================================================
// Metafunctions
// ==========================================================================

// --------------------------------------------------------------------------
// Metafunction WEIGHT
// --------------------------------------------------------------------------

/*!
 * @mfn FixedCyclicShape#WEIGHT
 * @headerfile <seqan/modifier.h>
 *
 * @brief Weight (number of care-positions) of Fixed CyclicShapes
 *
 * @signature WEIGHT<CyclicShape<FixedShape<L, TInnerShape, R> > >::VALUE;
 *
 * @tparam L left offset
 * @tparam R right offset
 * @tparam TInnerShape specialization of a hardwired GappedShape
 *
 * @return Weight of a fixed CyclicShape (enum).
 *
 * @see CyclicShape#weight
 */

template<unsigned L, typename THardwiredShape, unsigned R>
struct WEIGHT<CyclicShape<FixedShape<L, GappedShape<THardwiredShape>, R> > >
{
    enum {VALUE = WEIGHT<THardwiredShape>::VALUE};
};


// ==========================================================================
// Functions
// ==========================================================================

// --------------------------------------------------------------------------
// Function weight
// --------------------------------------------------------------------------

/*!
 * @fn CyclicShape#weight
 * @headerfile <seqan/modifier.h>
 *
 * @brief Return the weight of a CyclicShape
 *
 * @signature TSize weight(const & cyclicShape);
 *
 * @tparam TSpec Specialisation of the CyclicShape.
 * @tparam TSize Size type of CyclicShape
 * @param[in] cyclicShape CyclicShape object
 * @return weight of the CyclicShape (number of care positions)
 */
template<typename TSpec>
inline typename Size<CyclicShape<TSpec> >::Type
weight(CyclicShape<TSpec> const & s)
{
    return static_cast<Size<CyclicShape<> >::Type>(length(s.diffs));
}

template<unsigned L, typename THardwiredShape, unsigned R>
inline typename Size<CyclicShape<FixedShape<L, GappedShape<THardwiredShape>, R> > >::Type
weight(CyclicShape<FixedShape<L, GappedShape<THardwiredShape>, R> > const &)
{
    typedef CyclicShape<FixedShape<L, GappedShape<THardwiredShape>, R> > TCyclicShape;
    return WEIGHT<TCyclicShape>::VALUE;
}

// --------------------------------------------------------------------------
// Function stringToCyclicShape()
// --------------------------------------------------------------------------

/*!
 * @fn GenericCyclicShape#stringToCyclicShape
 * @headerfile <seqan/modifier.h>
 *
 * @brief Converts a 0/1 string to a Generic CyclicShape.
 *
 * @signature void stringToCyclicShape(shape, bitmap);
 *
 * @tparam TString A string type, e.g. CharString.
 * @param[out] shape Generic CyclicShape
 * @param[in] bitmap 0/1 string of type TString. CyclicShapes may start and end with zeros,
 *            but must contain at least one 1.
 *
 */
template<typename TString>
inline bool
stringToCyclicShape(CyclicShape<GenericShape> & shape, TString const & bitmap)
{
    typename Iterator<TString const>::Type it    = begin(bitmap);
    typename Iterator<TString const>::Type itBeg = begin(bitmap);
    typename Iterator<TString const>::Type itEnd = end(bitmap);

    Size<CyclicShape<GenericShape> >::Type countOnes = 0;
    for (; it != itEnd; ++it)
    {
        if (*it == '1')
            ++countOnes;
    }
    SEQAN_ASSERT_GT(countOnes, 0u);

    resize(shape.diffs, countOnes);

    typename Iterator<String<Size<CyclicShape<GenericShape> >::Type> >::Type diffIter = begin(shape.diffs);

    // go to first 1
    for (it = itBeg; *it != '1'; ++it)
        continue;
    shape.loffset = (Size<CyclicShape<GenericShape> >::Type)(it - itBeg);

    countOnes = 1; // now used as the distance between 1s
    for (++it; it != itEnd; ++it)
    {
        if (*it == '1')
        {
            *diffIter = countOnes;
            ++diffIter;
            countOnes = 1;
        }
        else
            ++countOnes;
    }

    *diffIter = countOnes + shape.loffset;
    shape.span = (typename Size<CyclicShape<GenericShape> >::Type)(it - itBeg);
    return true;
}

// --------------------------------------------------------------------------
// Function cyclicShapeToString()
// --------------------------------------------------------------------------

/*!
 * @fn CyclicShape#cyclicShapeToString
 *
 * @brief Print a given cyclic shape as a sequence of '1' (care position) and
 *        '0' (don't-care position).
 *
 * @signature void cyclicShapeToString(bitmap, cyclicShape);
 *
 * @param[in] cyclicShape CyclicShape object. Types: @link CyclicShape @endlink
 * @param[out] bitmap The resulting sequence object. Type: @link String @endlink,
 *             e.g. CharString
 *
 * @see GenericCyclicShape#stringToCyclicShape
 */
template<typename TShapeString, typename TSpec>
inline void
cyclicShapeToString(
    TShapeString & bitmap,
    CyclicShape<TSpec> const & me)
{
    clear(bitmap);
    if (weight(me) == 0)
        return;

    typename Size<CyclicShape<TSpec> >::Type i, j;
    for (i = 0; i < me.loffset; ++i)
        appendValue(bitmap, '0');
    for (i = 0; i < weight(me) - 1; ++i)
    {
        appendValue(bitmap, '1');
        for (j = 1; j < static_cast<typename Size<CyclicShape<TSpec> >::Type>(me.diffs[i]); ++j)
            appendValue(bitmap, '0');
    }
    appendValue(bitmap, '1');
    for (i = length(bitmap); i < me.span; ++i)
        appendValue(bitmap, '0');
}

// --------------------------------------------------------------------------
// Function carePositions()
// --------------------------------------------------------------------------

/*!
 * @fn CyclicShape#carePositions
 *
 * @brief Determine the indices of care positions in the range <i>[0,span)</i>
 *
 * @signature void carePositions(positions, cyclicShape);
 *
 * @param[in] cyclicShape CyclicShape object.
 * @param[out] positions The resulting @link String @endlink to store the care-positions in. Type TString.
 *
 * @tparam TString String or array. Value type should be an integral size type.
 * @tparam TSpec Specialization of CyclicShape.
 *
 * This function can be used to convert <tt>CyclicShape.diffs</tt>, which stores
 * the distances between care positions, to a <tt>positions</tt> string directly
 * containing the care positions. See the example:
 *
 * @snippet demos/dox/modifier/cyclic_shape_snippets.cpp CyclicShape Care Positions
 */

template<typename TString, typename TSpec>
inline void
carePositions(TString & output, CyclicShape<TSpec> const & shape)
{
    typedef typename Size<CyclicShape<TSpec> >::Type TPos;

    resize(output, weight(shape));
    TPos val = shape.loffset;
    output[0] = shape.loffset;

    for (TPos i = 1; i < weight(shape); ++i)
    {
        val += shape.diffs[i - 1];
        output[i] = val;
    }
}

// --------------------------------------------------------------------------
// Function cyclicShapeToSuffixLengths()
// --------------------------------------------------------------------------

/*!
 * @fn CyclicShape#cyclicShapeToSuffixLengths
 *
 * @brief Calculates the number of characters of modified Strings
 *        shorter than the Shape's span.
 *
 * @signature void cyclicShapeToSuffixLengths(TString suffLengths, cyclicShape);
 *
 * @param[in] cyclicShape CyclicShape object.
 * @param[in,out] suffLengths String to be filled. Must be resized beforehands.
 *                Fixed length arrays also work.
 *
 * @tparam TString String type or array with an integral value type, i.e. unsigned or int.
 *
 * Given a CyclicShape, this function calculates a how many characters a String of length
 * <i>x</i> contains after the CyclicShape is applied to it. This is done for all <tt>0 <= x < span</tt>.
 * <tt>suffLengths</tt> therefore must be resized to the shape's span beforehands.
 * The resizing is not done in this function so that it can be applied to arrays, too.
 */

template<typename TString,
         typename TShape>
inline void cyclicShapeToSuffixLengths(TString & suffLengths, TShape const & shape)
{
    typedef typename Size<TString>::Type TSize;

    TSize w = weight(shape);
    TSize s = shape.span;
    TSize o = shape.loffset;

    SEQAN_ASSERT_GEQ(s, 1u);
    //SEQAN_ASSERT_EQ(length(suffLengths), s); // Disabled to support arrays too.

    // build cummulative some of distances first
    String<TSize> sums;
    resize(sums, w);
    sums[0] = o;
    for (unsigned i = 0; i < w - 1; ++i)
        sums[i + 1] = sums[i] + shape.diffs[i];

    // write suffixLengths
    unsigned sumPos = 0;
    for (unsigned i = 0; i < s; ++i)
        suffLengths[i] = (sumPos < w && i == sums[sumPos]) ? sumPos++ : sumPos;
}

} //namespace


#endif