1 /*==========================================================================
2 SeqAn - The Library for Sequence Analysis
4 ============================================================================
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 3 of the License, or (at your option) any later version.
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 ============================================================================
18 $Id: sequence_multiple.h,v 1.2 2009/02/19 01:51:23 langmead Exp $
19 ==========================================================================*/
21 #ifndef SEQAN_HEADER_STRING_SET_H
22 #define SEQAN_HEADER_STRING_SET_H
24 namespace SEQAN_NAMESPACE_MAIN
27 //////////////////////////////////////////////////////////////////////////////
29 //////////////////////////////////////////////////////////////////////////////
31 //struct Generous; // large string-string, large id-string
32 //struct Tight; // small string-string, small id-string
33 //struct Optimal?? // small string-string, large id-string
37 ..summary:A string set storing references of the strings.
39 ..general:Class.StringSet
40 ..signature:StringSet<TString, Dependent<TSpec> >
41 ..param.TString:The string type.
43 ..param.TSpec:The specializing type for the dependent string set.
44 ...remarks:Possible values are $Tight$ or $Generous$
45 ...remarks:$Tight$ is very space efficient whereas $Generous$ provides fast access to the strings in the container via ids.
48 // Default id holder string set
49 template<typename TSpec = Generous >
50 struct Dependent; // holds references of its elements
55 ..summary:A string set storing the concatenation of all strings within one string.
58 ..signature:StringSet<TString, Owner<ConcatDirect<> > >
59 ..param.TString:The string type.
61 ..remarks:The strings are internally stored in a $TString$ object and the character position type is a
62 a single integer value between 0 and the sum of string lengths minus 1.
63 ..remarks:The position type can be returned or modified by the meta-function @Metafunction.SAValue@ called with the @Class.StringSet@ type.
66 template < typename TDelimiter = void >
67 struct ConcatDirect; // contains 1 string (the concatenation of n strings)
68 //struct Default; // contains n strings in a string
72 ..summary:A string set storing the strings as members.
74 ..general:Class.StringSet
75 ..signature:StringSet<TString, Owner<> >
76 ..signature:StringSet<TString, Owner<Default> >
77 ..param.TString:The string type.
79 ..remarks:The strings are internally stored in a $String<TString>$ object and the character position type is a
80 @Class.Pair@ $(seqNo,seqOfs)$ where seqNo identifies the string within the stringset and seqOfs identifies the position within this string.
81 ..remarks:The position type can be returned or modified by the meta-function @Metafunction.SAValue@ called with the @Class.StringSet@ type.
83 template < typename TSpec = Default >
84 struct Owner; // owns its elements
87 //////////////////////////////////////////////////////////////////////////////
89 //////////////////////////////////////////////////////////////////////////////
91 template < typename TString, typename TSpec = Owner<> >
94 template <typename TObject>
99 template <typename TObject>
100 struct Concatenator<TObject const> {
101 typedef typename Concatenator<TObject>::Type const Type;
105 //////////////////////////////////////////////////////////////////////////////
107 //////////////////////////////////////////////////////////////////////////////
109 template <typename TString>
110 struct StringSetLimits {
111 typedef Nothing Type;
114 template <typename TString>
115 struct StringSetLimits<TString const> {
116 typedef typename StringSetLimits<TString>::Type const Type;
119 template <typename TString>
120 struct StringSetPosition {
121 typedef typename Size<TString>::Type Type;
124 template <typename TString, typename TSpec>
125 struct StringSetLimits< StringSet<TString, TSpec> > {
126 typedef typename Size<TString>::Type TSize;
127 typedef String<TSize> Type;
130 template <typename TString, typename TSpec>
131 struct StringSetPosition< StringSet<TString, TSpec> > {
132 typedef typename Size<TString>::Type TSize;
133 typedef Pair<TSize> Type;
138 //////////////////////////////////////////////////////////////////////////////
139 // get StringSet limits
140 //////////////////////////////////////////////////////////////////////////////
143 .Function.stringSetLimits:
145 ..summary:Retrieves a string of delimiter positions of a @Class.StringSet@ which is needed for local<->global position conversions.
146 ..signature:stringSetLimits(me)
147 ..param.me:A string or string set.
149 ...type:Class.StringSet
150 ..returns:A reference to a string.
151 ...remarks:If $me$ is a @Class.StringSet@ then the returned string is of size $length(me)+1$ and contains the ascending (virtual) delimiter positions of the concatenation of all strings in the string set.
152 ...remarks:If $me$ is a @Class.String@, @Tag.Nothing@ is returned.
155 template <typename TStringSet>
156 inline typename StringSetLimits<TStringSet>::Type
157 stringSetLimits(TStringSet &) {
158 return typename StringSetLimits<TStringSet>::Type();
161 template <typename TString, typename TSpec>
162 inline typename StringSetLimits< StringSet<TString, TSpec> >::Type &
163 stringSetLimits(StringSet<TString, TSpec> &stringSet) {
164 if (!_validStringSetLimits(stringSet))
165 _refreshStringSetLimits(stringSet);
166 return stringSet.limits;
169 template <typename TString, typename TSpec>
170 inline typename StringSetLimits< StringSet<TString, TSpec> const>::Type &
171 stringSetLimits(StringSet<TString, TSpec> const &stringSet) {
172 if (!_validStringSetLimits(stringSet))
173 _refreshStringSetLimits(const_cast< StringSet<TString, TSpec>& >(stringSet));
174 return stringSet.limits;
177 //////////////////////////////////////////////////////////////////////////////
178 // accessing local positions
179 //////////////////////////////////////////////////////////////////////////////
184 ..summary:Returns the sequence number of a position.
185 ..signature:getSeqNo(pos[, limits])
186 ..param.pos:A position.
188 ..param.limits:The limits string returned by @Function.stringSetLimits@.
189 ..returns:A single integer value that identifies the string within the stringset $pos$ points at.
190 ...remarks:If $limits$ is omitted or @Tag.Nothing@ $getSeqNo$ returns 0.
191 ...remarks:If $pos$ is a local position (of class @Class.Pair@) then $i1$ is returned.
192 ...remarks:If $pos$ is a global position (integer type and $limits$ is a @Class.String@) then $pos$ is converted to a local position and $i1$ is returned.
196 .Function.getSeqOffset:
198 ..summary:Returns the local sequence offset of a position.
199 ..signature:getSeqOffset(pos[, limits])
200 ..param.pos:A position.
202 ..param.limits:The limits string returned by @Function.stringSetLimits@.
203 ..returns:A single integer value that identifies the position within the string $pos$ points at.
204 ...remarks:If $limits$ is omitted or @Tag.Nothing@ $getSeqNo$ returns $pos$.
205 ...remarks:If $pos$ is a local position (of class @Class.Pair@) then $i2$ is returned.
206 ...remarks:If $pos$ is a global position (integer type and $limits$ is a @Class.String@) then $pos$ is converted to a local position and $i2$ is returned.
209 //////////////////////////////////////////////////////////////////////////////
211 //////////////////////////////////////////////////////////////////////////////
213 template <typename TPosition>
214 inline TPosition getSeqNo(TPosition const &, Nothing const &) {
218 template <typename TPosition>
219 inline TPosition getSeqOffset(TPosition const &pos, Nothing const &) {
222 //____________________________________________________________________________
224 template <typename TPosition>
225 inline TPosition getSeqNo(TPosition const &) {
229 template <typename TPosition>
230 inline TPosition getSeqOffset(TPosition const &pos) {
234 //////////////////////////////////////////////////////////////////////////////
235 // n sequences (position type is Pair)
236 //////////////////////////////////////////////////////////////////////////////
238 template <typename T1, typename T2, typename TCompression, typename TLimitsString>
239 inline T1 getSeqNo(Pair<T1, T2, TCompression> const &pos, TLimitsString const &) {
240 return getValueI1(pos);
243 template <typename T1, typename T2, typename TCompression>
244 inline T1 getSeqNo(Pair<T1, T2, TCompression> const &pos) {
245 return getValueI1(pos);
247 //____________________________________________________________________________
249 template <typename T1, typename T2, typename TCompression, typename TLimitsString>
250 inline T2 getSeqOffset(Pair<T1, T2, TCompression> const &pos, TLimitsString const &) {
251 return getValueI2(pos);
254 template <typename T1, typename T2, typename TCompression>
255 inline T1 getSeqOffset(Pair<T1, T2, TCompression> const &pos) {
256 return getValueI2(pos);
259 //////////////////////////////////////////////////////////////////////////////
260 // n sequences (position type is an integral type)
261 //////////////////////////////////////////////////////////////////////////////
263 template <typename TPos, typename TLimitsString>
264 inline TPos getSeqNo(TPos const &pos, TLimitsString const &limits) {
265 typedef typename Iterator<TLimitsString const, Standard>::Type TIter;
266 typedef typename Value<TLimitsString>::Type TSize;
267 TIter _begin = begin(limits, Standard());
268 TIter _upper = ::std::upper_bound(_begin, end(limits, Standard()), (TSize)pos) - 1;
269 return difference(_begin, _upper);
272 template <typename TPos, typename TLimitsString>
273 inline TPos getSeqOffset(TPos const &pos, TLimitsString const &limits) {
274 typedef typename Iterator<TLimitsString const, Standard>::Type TIter;
275 typedef typename Value<TLimitsString>::Type TSize;
276 TIter _begin = begin(limits, Standard());
277 TIter _upper = ::std::upper_bound(_begin, end(limits, Standard()), (TSize)pos) - 1;
278 return pos - *_upper;
281 //////////////////////////////////////////////////////////////////////////////
282 // local -> global conversions
283 //////////////////////////////////////////////////////////////////////////////
286 .Function.posGlobalize:
288 ..summary:Converts a local/global to a global position.
289 ..signature:posGlobalize(pos, limits)
290 ..param.pos:A local or global position (pair or integer value).
292 ..param.limits:The limits string returned by @Function.stringSetLimits@.
293 ..returns:The corresponding global position of $pos$.
294 ...remarks:If $pos$ is an integral type $pos$ is returned.
295 ...remarks:If not, $limits[getSeqNo(pos, limits)] + getSeqOffset(pos, limits)$ is returned.
298 // any_position and no limits_string -> any_position
299 template <typename TPosition>
300 inline TPosition posGlobalize(TPosition const &pos, Nothing const &) {
304 // local_position (0,x) and no limits_string -> global_position x
305 template <typename T1, typename T2, typename TCompression>
306 inline T2 posGlobalize(Pair<T1, T2, TCompression> const &pos, Nothing const &) {
307 return getSeqOffset(pos);
310 // any_position and no limits_string -> any_position
311 template <typename TLimitsString, typename TPosition>
312 inline TPosition posGlobalize(TPosition const &pos, TLimitsString const &) {
316 // local_position and limits_string -> global_position
317 template <typename TLimitsString, typename T1, typename T2, typename TCompression>
318 inline typename Value<TLimitsString>::Type
319 posGlobalize(Pair<T1, T2, TCompression> const &pos, TLimitsString const &limits) {
320 return limits[getSeqNo(pos, limits)] + getSeqOffset(pos, limits);
323 //////////////////////////////////////////////////////////////////////////////
324 // global -> local position
325 //////////////////////////////////////////////////////////////////////////////
328 .Function.posLocalize:
330 ..summary:Converts a local/global to a local position.
331 ..signature:posLocalize(result, pos, limits)
332 ..param.pos:A local or global position (pair or integer value).
334 ..param.limits:The limits string returned by @Function.stringSetLimits@.
335 ..param.result:Reference to the resulting corresponding local position of $pos$.
336 ...remarks:If $pos$ is an integral type and $limits$ is omitted or @Tag.Nothing@, $pos$ is returned.
337 ...remarks:If $pos$ is a local position (of class @Class.Pair@) then $pos$ is returned.
338 ...remarks:If $pos$ is a global position (integer type and $limits$ is a @Class.String@) then $pos$ is converted to a local position.
341 // any_position and no limits_string -> any_position
342 template <typename TResult, typename TPosition>
343 inline void posLocalize(TResult &result, TPosition const &pos, Nothing const &) {
347 template <typename T1, typename T2, typename TCompression, typename TPosition>
348 inline void posLocalize(Pair<T1, T2, TCompression> &result, TPosition const &pos, Nothing const &) {
353 // global_position and limits_string -> local_position
354 template <typename TResult, typename TSize, typename TSpec, typename TPosition>
355 inline void posLocalize(TResult &result, TPosition const &pos, String<TSize, TSpec> const &limits) {
356 typedef typename Iterator<String<TSize> const, Standard>::Type TIter;
357 TIter _begin = begin(limits, Standard());
358 TIter _upper = ::std::upper_bound(_begin, end(limits, Standard()), (TSize)pos) - 1;
359 result.i1 = difference(_begin, _upper);
360 result.i2 = pos - *_upper;
363 // local_position -> local_position
364 template <typename TResult, typename TSize, typename TSpec, typename T1, typename T2, typename TCompression>
365 inline void posLocalize(TResult &result, Pair<T1, T2, TCompression> const &pos, String<TSize, TSpec> const &/*limits*/) {
369 //////////////////////////////////////////////////////////////////////////////
371 //////////////////////////////////////////////////////////////////////////////
373 template < typename TString, typename TSpec, typename TPosition >
374 inline typename Prefix<TString>::Type
375 prefix(StringSet< TString, TSpec > &me, TPosition pos)
377 typedef StringSet<TString, TSpec> TStringSet;
378 typedef typename Size<TStringSet>::Type TSetSize;
379 typedef typename Size<TString>::Type TStringSize;
380 typedef Pair<TSetSize, TStringSize, Compressed> TPair;
383 posLocalize(lPos, pos, stringSetLimits(me));
384 return prefix(me[getSeqNo(lPos)], getSeqOffset(lPos));
387 template < typename TString, typename TSpec, typename TPosition >
388 inline typename Prefix<TString const>::Type
389 prefix(StringSet< TString, TSpec > const &me, TPosition pos)
391 typedef StringSet<TString, TSpec> TStringSet;
392 typedef typename Size<TStringSet>::Type TSetSize;
393 typedef typename Size<TString>::Type TStringSize;
394 typedef Pair<TSetSize, TStringSize, Compressed> TPair;
397 posLocalize(lPos, pos, stringSetLimits(me));
398 return prefix(me[getSeqNo(lPos)], getSeqOffset(lPos));
401 template < typename TString, typename TDelimiter, typename TPosition >
402 inline typename Infix<TString>::Type
403 prefix(StringSet< TString, Owner<ConcatDirect<TDelimiter> > > &me, TPosition pos)
405 return infix(me.concat, stringSetLimits(me)[getSeqNo(pos, stringSetLimits(me))], posGlobalize(pos, stringSetLimits(me)));
408 template < typename TString, typename TDelimiter, typename TPosition >
409 inline typename Infix<TString const>::Type
410 prefix(StringSet< TString, Owner<ConcatDirect<TDelimiter> > > const &me, TPosition pos)
412 return infix(me.concat, stringSetLimits(me)[getSeqNo(pos, stringSetLimits(me))], posGlobalize(pos, stringSetLimits(me)));
415 //////////////////////////////////////////////////////////////////////////////
417 //////////////////////////////////////////////////////////////////////////////
419 template < typename TString, typename TSpec, typename TPosition >
420 inline typename Suffix<TString>::Type
421 suffix(StringSet< TString, TSpec > &me, TPosition pos)
423 typedef StringSet<TString, TSpec> TStringSet;
424 typedef typename Size<TStringSet>::Type TSetSize;
425 typedef typename Size<TString>::Type TStringSize;
426 typedef Pair<TSetSize, TStringSize, Compressed> TPair;
429 posLocalize(lPos, pos, stringSetLimits(me));
430 return suffix(me[getSeqNo(lPos)], getSeqOffset(lPos));
433 template < typename TString, typename TSpec, typename TPosition >
434 inline typename Suffix<TString const>::Type
435 suffix(StringSet< TString, TSpec > const &me, TPosition pos)
437 typedef StringSet<TString, TSpec> TStringSet;
438 typedef typename Size<TStringSet>::Type TSetSize;
439 typedef typename Size<TString>::Type TStringSize;
440 typedef Pair<TSetSize, TStringSize, Compressed> TPair;
443 posLocalize(lPos, pos, stringSetLimits(me));
444 return suffix(me[getSeqNo(lPos)], getSeqOffset(lPos));
447 template < typename TString, typename TDelimiter, typename TPosition >
448 inline typename Infix<TString>::Type
449 suffix(StringSet< TString, Owner<ConcatDirect<TDelimiter> > > &me, TPosition pos)
451 return infix(me.concat, posGlobalize(pos, stringSetLimits(me)), stringSetLimits(me)[getSeqNo(pos, stringSetLimits(me)) + 1]);
454 template < typename TString, typename TDelimiter, typename TPosition >
455 inline typename Infix<TString const>::Type
456 suffix(StringSet< TString, Owner<ConcatDirect<TDelimiter> > > const &me, TPosition pos)
458 return infix(me.concat, posGlobalize(pos, stringSetLimits(me)), stringSetLimits(me)[getSeqNo(pos, stringSetLimits(me)) + 1]);
461 //////////////////////////////////////////////////////////////////////////////
463 //////////////////////////////////////////////////////////////////////////////
465 template < typename TString, typename TSpec, typename TPosition, typename TSize >
466 inline typename Infix<TString>::Type
467 infixWithLength(StringSet< TString, TSpec > &me, TPosition pos, TSize length)
469 typedef StringSet<TString, TSpec> TStringSet;
470 typedef typename Size<TStringSet>::Type TSetSize;
471 typedef typename Size<TString>::Type TStringSize;
472 typedef Pair<TSetSize, TStringSize, Compressed> TPair;
475 posLocalize(lPos, pos, stringSetLimits(me));
476 return infixWithLength(me[getSeqNo(lPos)], getSeqOffset(lPos), length);
479 template < typename TString, typename TSpec, typename TPosition, typename TSize >
480 inline typename Infix<TString const>::Type
481 infixWithLength(StringSet< TString, TSpec > const &me, TPosition pos, TSize length)
483 typedef StringSet<TString, TSpec> TStringSet;
484 typedef typename Size<TStringSet>::Type TSetSize;
485 typedef typename Size<TString>::Type TStringSize;
486 typedef Pair<TSetSize, TStringSize, Compressed> TPair;
489 posLocalize(lPos, pos, stringSetLimits(me));
490 return infixWithLength(me[getSeqNo(lPos)], getSeqOffset(lPos), length);
493 template < typename TString, typename TDelimiter, typename TPosition, typename TSize >
494 inline typename Infix<TString>::Type
495 infixWithLength(StringSet< TString, Owner<ConcatDirect<TDelimiter> > > &me, TPosition pos, TSize length)
497 return infixWithLength(me.concat, posGlobalize(pos, stringSetLimits(me)), length);
500 template < typename TString, typename TDelimiter, typename TPosition, typename TSize >
501 inline typename Infix<TString const>::Type
502 infixWithLength(StringSet< TString, Owner<ConcatDirect<TDelimiter> > > const &me, TPosition pos, TSize length)
504 return infixWithLength(me.concat, posGlobalize(pos, stringSetLimits(me)), length);
507 //////////////////////////////////////////////////////////////////////////////
508 // position arithmetics
509 //////////////////////////////////////////////////////////////////////////////
512 template <typename TPos, typename TLimitsString>
513 inline bool posAtFirstLocal(TPos pos, TLimitsString const &limits) {
514 return getSeqOffset(pos, limits) == 0;
516 template <typename TPos>
517 inline bool posAtFirstLocal(TPos pos) {
518 return getSeqOffset(pos) == 0;
523 template <typename TPos>
524 inline TPos posPrev(TPos pos) {
528 template <typename T1, typename T2, typename TCompression>
529 inline Pair<T1, T2, TCompression> posPrev(Pair<T1, T2, TCompression> const &pos) {
530 return Pair<T1, T2, TCompression>(getValueI1(pos), getValueI2(pos) - 1);
534 template <typename TPos>
535 inline TPos posNext(TPos pos) {
539 template <typename T1, typename T2, typename TCompression>
540 inline Pair<T1, T2, TCompression>
541 posNext(Pair<T1, T2, TCompression> const &pos) {
542 return Pair<T1, T2, TCompression>(getValueI1(pos), getValueI2(pos) + 1);
546 template <typename TPos, typename TDelta>
547 inline TPos posAdd(TPos pos, TDelta delta) {
551 template <typename T1, typename T2, typename TCompression, typename TDelta>
552 inline Pair<T1, T2, TCompression>
553 posAdd(Pair<T1, T2, TCompression> const &pos, TDelta delta) {
554 return Pair<T1, T2, TCompression>(getValueI1(pos), getValueI2(pos) + delta);
558 template <typename TA, typename TB>
559 inline TA posSub(TA a, TB b) {
564 typename TA1, typename TA2, typename TACompression,
565 typename TB1, typename TB2, typename TBCompression
568 posSub(Pair<TA1, TA2, TACompression> const &a, Pair<TB1, TB2, TBCompression> const &b) {
569 return getValueI2(a) - getValueI2(b);
572 //////////////////////////////////////////////////////////////////////////////
573 // position relations
574 //////////////////////////////////////////////////////////////////////////////
576 template <typename TPos>
577 inline bool posLess(TPos const &a, TPos const &b) {
581 template <typename T1, typename T2, typename TCompression>
582 inline bool posLess(Pair<T1, T2, TCompression> const &a, Pair<T1, T2, TCompression> const &b) {
584 (getValueI1(a) < getValueI1(b)) ||
585 ((getValueI1(a) == getValueI1(b)) && (getValueI2(a) < getValueI2(b)));
588 template <typename TPos>
589 inline int posCompare(TPos const &a, TPos const &b) {
590 if (a < b) return -1;
595 template <typename T1, typename T2, typename TCompression>
596 inline int posCompare(Pair<T1, T2, TCompression> const &a, Pair<T1, T2, TCompression> const &b) {
597 if (getValueI1(a) < getValueI1(b)) return -1;
598 if (getValueI1(a) > getValueI1(b)) return 1;
599 return posCompare(getValueI2(a), getValueI2(b));
602 //////////////////////////////////////////////////////////////////////////////
604 template <typename TPos, typename TString>
605 inline typename Size<TString>::Type
606 suffixLength(TPos pos, TString const &string) {
607 return length(string) - pos;
610 template <typename TPos, typename TString, typename TSpec>
611 inline typename Size<TString>::Type
612 suffixLength(TPos pos, StringSet<TString, TSpec> const &stringSet) {
613 return length(stringSet[getSeqNo(pos, stringSetLimits(stringSet))]) - getSeqOffset(pos, stringSetLimits(stringSet));
616 //////////////////////////////////////////////////////////////////////////////
618 template <typename TString>
620 countSequences(TString const &) {
624 template <typename TString, typename TSpec>
625 inline typename Size<StringSet<TString, TSpec> >::Type
626 countSequences(StringSet<TString, TSpec> const &stringSet) {
627 return length(stringSet);
630 //////////////////////////////////////////////////////////////////////////////
632 template <typename TSeqNo, typename TString>
633 inline typename Size<TString>::Type
634 sequenceLength(TSeqNo /*seqNo*/, TString const &string) {
635 return length(string);
638 template <typename TSeqNo, typename TString, typename TSpec>
639 inline typename Size<StringSet<TString, TSpec> >::Type
640 sequenceLength(TSeqNo seqNo, StringSet<TString, TSpec> const &stringSet) {
641 return length(stringSet[seqNo]);
644 //////////////////////////////////////////////////////////////////////////////
645 // StringSet Container
646 //////////////////////////////////////////////////////////////////////////////
651 ..summary:A container class for a set of strings.
652 ..signature:StringSet<TString, TSpec>
653 ..param.TString:The string type.
655 ..param.TSpec:The specializing type for the StringSet.
656 ...metafunction:Metafunction.Spec
657 ...default:$Generous$.
661 //////////////////////////////////////////////////////////////////////////////
662 // StringSet with individual sequences in a tight string of string pointers and corr. IDs
663 template <typename TString>
664 class StringSet<TString, Dependent<Tight> >
667 typedef String<TString*> TStrings;
668 typedef typename Id<StringSet>::Type TIdType;
669 typedef String<TIdType> TIds;
670 typedef typename StringSetLimits<StringSet>::Type TLimits;
671 typedef typename Concatenator<StringSet>::Type TConcatenator;
672 //____________________________________________________________________________
677 bool limitsValid; // is true if limits contains the cumulative sum of the sequence lengths
678 TConcatenator concat;
679 //____________________________________________________________________________
685 appendValue(limits, 0);
689 template <typename TDefault>
690 StringSet(StringSet<TString, Owner<TDefault> > const& _other) :
694 appendValue(limits, 0);
696 for(unsigned int i = 0; i<length(_other); ++i) appendValue(*this, _other[i]);
699 template <typename TPos>
700 inline typename Reference<StringSet>::Type
701 operator [] (TPos pos)
704 return value(*this, pos);
707 template <typename TPos>
708 inline typename Reference<StringSet const>::Type
709 operator [] (TPos pos) const
711 return value(*this, pos);
715 //////////////////////////////////////////////////////////////////////////////
716 // StringSet with individual sequences in a string of string pointers
717 template <typename TString>
718 class StringSet<TString, Dependent<Generous> >
721 typedef String<TString*> TStrings;
722 typedef typename Size<StringSet>::Type TSize;
723 typedef typename StringSetLimits<StringSet>::Type TLimits;
724 typedef typename Concatenator<StringSet>::Type TConcatenator;
725 //____________________________________________________________________________
729 bool limitsValid; // is true if limits contains the cumulative sum of the sequence lengths
730 TConcatenator concat;
731 //____________________________________________________________________________
737 appendValue(limits, 0);
741 template <typename TDefault>
742 StringSet(StringSet<TString, Owner<TDefault> > const& _other) :
746 appendValue(limits, 0);
748 for(unsigned int i = 0; i<length(_other); ++i) appendValue(*this, _other[i]);
751 template <typename TPos>
752 inline typename Reference<StringSet>::Type
753 operator [] (TPos pos)
755 return value(*this, pos);
758 template <typename TPos>
759 inline typename Reference<StringSet const>::Type
760 operator [] (TPos pos) const
762 return value(*this, pos);
766 //////////////////////////////////////////////////////////////////////////////
767 // StringSet with individual sequences in a string of strings
768 template < typename TString >
769 class StringSet< TString, Owner<Default> >
773 typedef String<TString> TStrings;
774 typedef typename StringSetLimits<StringSet>::Type TLimits;
775 typedef typename Concatenator<StringSet>::Type TConcatenator;
776 //____________________________________________________________________________
780 bool limitsValid; // is true if limits contains the cumulative sum of the sequence lengths
781 TConcatenator concat;
782 //____________________________________________________________________________
787 appendValue(limits, 0);
790 //____________________________________________________________________________
792 template <typename TPos>
793 inline typename Reference<StringSet>::Type
794 operator [] (TPos pos)
796 return value(*this, pos);
799 template <typename TPos>
800 inline typename Reference<StringSet const>::Type
801 operator [] (TPos pos) const
803 return value(*this, pos);
808 //////////////////////////////////////////////////////////////////////////////
809 // StringSet with directly concatenated sequences
810 template < typename TString, typename TDelimiter >
811 class StringSet< TString, Owner<ConcatDirect<TDelimiter> > >
814 typedef typename StringSetLimits<StringSet>::Type TLimits;
815 typedef typename Concatenator<StringSet>::Type TConcatenator;
816 //____________________________________________________________________________
819 TConcatenator concat;
820 //____________________________________________________________________________
823 appendValue(limits, 0);
825 //____________________________________________________________________________
827 template <typename TPos>
828 inline typename Reference<StringSet>::Type
829 operator [] (TPos pos)
832 return value(*this, pos);
835 template <typename TPos>
836 inline typename Reference<StringSet const>::Type
837 operator [] (TPos pos) const
840 return value(*this, pos);
845 //////////////////////////////////////////////////////////////////////////////
848 template < typename TString, typename TSpec >
849 struct Value< StringSet< TString, TSpec > > {
850 typedef TString Type;
853 template < typename TString, typename TSpec >
854 struct Value< StringSet< TString, TSpec > const> {
855 typedef TString Type;
858 template < typename TString, typename TSpec >
859 struct Size< StringSet< TString, TSpec > >:
860 Size< typename StringSetLimits< StringSet<TString, TSpec> >::Type > {};
862 template < typename TString, typename TSpec >
863 struct Prefix< StringSet< TString, TSpec > >:
864 Prefix< TString > {};
866 template < typename TString, typename TSpec >
867 struct Prefix< StringSet< TString, TSpec > const >:
868 Prefix< TString const > {};
870 template < typename TString, typename TSpec >
871 struct Suffix< StringSet< TString, TSpec > >:
872 Suffix< TString > {};
874 template < typename TString, typename TSpec >
875 struct Suffix< StringSet< TString, TSpec > const >:
876 Suffix< TString const > {};
878 template < typename TString, typename TSpec >
879 struct Infix< StringSet< TString, TSpec > >:
882 template < typename TString, typename TSpec >
883 struct Infix< StringSet< TString, TSpec > const >:
884 Infix< TString const > {};
887 // direct concatenation
888 template < typename TString, typename TSpec >
889 struct Value< StringSet< TString, Owner<ConcatDirect<TSpec> > > >:
892 template < typename TString, typename TSpec >
893 struct GetValue< StringSet< TString, Owner<ConcatDirect<TSpec> > > >:
896 template < typename TString, typename TSpec >
897 struct GetValue< StringSet< TString, Owner<ConcatDirect<TSpec> > > const >:
898 Infix<TString const> {};
900 template < typename TString, typename TSpec >
901 struct Reference< StringSet< TString, Owner<ConcatDirect<TSpec> > > >:
904 template < typename TString, typename TSpec >
905 struct Reference< StringSet< TString, Owner<ConcatDirect<TSpec> > > const >:
906 Infix<TString const> {};
908 template <typename TString, typename TSpec>
909 struct AllowsFastRandomAccess< StringSet< TString, TSpec > >:
910 AllowsFastRandomAccess<TString> {};
912 template < typename TString, typename TSpec >
913 struct Prefix< StringSet< TString, Owner<ConcatDirect<TSpec> > > >:
916 template < typename TString, typename TSpec >
917 struct Prefix< StringSet< TString, Owner<ConcatDirect<TSpec> > > const >:
918 Infix< TString const > {};
920 template < typename TString, typename TSpec >
921 struct Suffix< StringSet< TString, Owner<ConcatDirect<TSpec> > > >:
924 template < typename TString, typename TSpec >
925 struct Suffix< StringSet< TString, Owner<ConcatDirect<TSpec> > > const >:
926 Infix< TString const > {};
928 template < typename TString, typename TSpec >
929 struct Infix< StringSet< TString, Owner<ConcatDirect<TSpec> > > >:
932 template < typename TString, typename TSpec >
933 struct Infix< StringSet< TString, Owner<ConcatDirect<TSpec> > > const >:
934 Infix< TString const > {};
937 //////////////////////////////////////////////////////////////////////////////
938 // validStringSetLimits
940 template < typename T >
941 inline bool _validStringSetLimits(T const &) {
945 template < typename TString, typename TSpec >
946 inline bool _validStringSetLimits(StringSet< TString, TSpec > const &me) {
947 return me.limitsValid;
950 template < typename TString, typename TSpec >
951 inline bool _validStringSetLimits(StringSet< TString, Owner<ConcatDirect<TSpec> > > const &) {
955 //////////////////////////////////////////////////////////////////////////////
956 // _refreshStringSetLimits
958 template < typename T >
959 inline void _refreshStringSetLimits(T &) {}
961 template < typename TString, typename TSpec >
962 inline void _refreshStringSetLimits(StringSet< TString, Owner<ConcatDirect<TSpec> > > &) {}
964 template < typename TString, typename TSpec >
965 inline void _refreshStringSetLimits(StringSet< TString, TSpec > &me)
967 typedef StringSet< TString, TSpec > TStringSet;
968 typedef typename StringSetLimits<TStringSet>::Type TLimits;
970 typename Value<TLimits>::Type sum = 0;
971 typename Size<TStringSet>::Type len = length(me);
972 typename Size<TStringSet>::Type i = 0;
974 // SEQAN_ASSERT(length(me.limits) == len + 1);
975 // resize(me.limits, len + 1);
976 for(; i < len; ++i) {
978 sum += length(me[i]);
981 me.limitsValid = true;
984 //////////////////////////////////////////////////////////////////////////////
985 // find the i-th non-zero value of a string me
987 template < typename TValue, typename TSpec, typename TPos >
988 inline typename Size< String<TValue, TSpec> >::Type
989 _findIthNonZeroValue(String<TValue, TSpec> const &me, TPos i)
991 typename Iterator< String<TValue, TSpec> const, Standard >::Type it = begin(me, Standard());
992 typename Iterator< String<TValue, TSpec> const, Standard >::Type itEnd = end(me, Standard());
994 for(; it != itEnd; ++it)
1000 return position(it, me);
1006 //////////////////////////////////////////////////////////////////////////////
1007 // count non-zero values before position i
1009 template < typename TValue, typename TSpec, typename TPos >
1010 inline typename Size< String<TValue, TSpec> >::Type
1011 _countNonZeroValues(String<TValue, TSpec> const &me, TPos i)
1013 typename Iterator< String<TValue, TSpec> const, Standard >::Type it = begin(me, Standard());
1014 typename Iterator< String<TValue, TSpec> const, Standard >::Type itEnd = begin(me, Standard()) + i;
1015 typename Size< String<TValue, TSpec> >::Type counter = 0;
1017 for(; it != itEnd; ++it)
1022 //////////////////////////////////////////////////////////////////////////////
1025 template < typename TString >
1026 inline typename Size<TString>::Type lengthSum(TString const &me) {
1030 template < typename TString, typename TSpec >
1031 inline typename Size<TString>::Type lengthSum(StringSet< TString, TSpec > const &me) {
1032 return back(stringSetLimits(me));
1035 ///.Function.appendValue.param.target.type:Class.StringSet
1036 //////////////////////////////////////////////////////////////////////////////
1040 template < typename TString, typename TString2, typename TExpand >
1041 inline void appendValue(
1042 StringSet< TString, Owner<Default> > &me,
1043 TString2 const &obj,
1046 appendValue(me.strings, obj);
1047 appendValue(me.limits, lengthSum(me) + length(obj));
1051 template < typename TString, typename TString2, typename TExpand >
1052 inline void appendValue(
1053 StringSet< TString, Owner<ConcatDirect<void> > > &me,
1054 TString2 const &obj,
1057 append(me.concat, obj);
1058 appendValue(me.limits, lengthSum(me) + length(obj));
1061 template < typename TString, typename TDelimiter, typename TString2, typename TExpand >
1062 inline void appendValue(
1063 StringSet< TString, Owner<ConcatDirect<TDelimiter> > > &me,
1064 TString2 const &obj,
1067 append(me.concat, obj);
1068 appendValue(me.concat, TDelimiter());
1069 appendValue(me.limits, lengthSum(me) + length(obj) + 1);
1073 template < typename TString, typename TExpand >
1074 inline void appendValue(
1075 StringSet<TString, Dependent<Generous> > &me,
1080 appendValue(me.strings, const_cast<TString*>(&obj));
1081 appendValue(me.limits, lengthSum(me) + length(obj));
1085 template < typename TString, typename TExpand >
1086 inline void appendValue(
1087 StringSet<TString, Dependent<Tight> > &me,
1092 appendValue(me.strings, const_cast<TString*>(&obj));
1093 appendValue(me.ids, length(me.strings) - 1);
1094 appendValue(me.limits, lengthSum(me) + length(obj));
1098 inline void append(TString *_objs[], unsigned count) {
1099 for(unsigned i = 0; i < count; ++i)
1104 ///.Function.clear.param.object.type:Class.StringSet
1105 //////////////////////////////////////////////////////////////////////////////
1108 template < typename TString >
1109 inline void clear(StringSet< TString, Owner<Default> > &me)
1113 resize(me.limits, 1);
1114 me.limitsValid = true;
1117 template < typename TString, typename TDelimiter >
1118 inline void clear(StringSet< TString, Owner<ConcatDirect<TDelimiter> > > &me)
1122 resize(me.limits, 1);
1125 template < typename TString >
1126 inline void clear(StringSet< TString, Dependent<Generous> > & me)
1130 resize(me.limits, 1);
1131 me.limitsValid = true;
1134 template < typename TString >
1135 inline void clear(StringSet<TString, Dependent<Tight> >& me)
1139 resize(me.limits, 1);
1140 me.limitsValid = true;
1145 ///.Function.length.param.object.type:Class.StringSet
1146 //////////////////////////////////////////////////////////////////////////////
1149 template < typename TString, typename TSpec >
1150 inline typename Size< StringSet< TString, TSpec > >::Type
1151 length(StringSet< TString, TSpec > const &me) {
1152 return length(me.limits) - 1;
1155 template <typename TString>
1156 inline typename Size<StringSet<TString, Dependent<Tight> > >::Type
1157 length(StringSet<TString, Dependent<Tight> > const &me)
1159 return length(me.strings);
1162 ///.Function.resize.param.object.type:Class.StringSet
1163 //////////////////////////////////////////////////////////////////////////////
1166 template < typename TString, typename TSpec, typename TSize >
1167 inline typename Size< StringSet< TString, TSpec > >::Type
1168 resize(StringSet< TString, TSpec > &me, TSize new_size) {
1169 resize(me.limits, new_size + 1);
1170 me.limitsValid = (new_size == 0);
1171 return resize(me.strings, new_size);
1174 template < typename TString, typename TSpec, typename TSize >
1175 inline typename Size< StringSet< TString, Owner<ConcatDirect<TSpec> > > >::Type
1176 resize(StringSet< TString, Owner<ConcatDirect<TSpec> > > &me, TSize new_size) {
1177 return resize(me.limits, new_size + 1) - 1;
1180 ///.Function.value.param.object.type:Class.StringSet
1181 //////////////////////////////////////////////////////////////////////////////
1185 template < typename TString, typename TPos >
1186 inline typename Reference< StringSet< TString, Owner<Default> > >::Type
1187 value(StringSet< TString, Owner<Default> > & me, TPos pos)
1189 return me.strings[pos];
1192 template < typename TString, typename TPos >
1193 inline typename Reference< StringSet< TString, Owner<Default> > const >::Type
1194 value(StringSet< TString, Owner<Default> > const & me, TPos pos)
1196 return me.strings[pos];
1200 template < typename TString, typename TSpec, typename TPos >
1201 inline typename Infix<TString>::Type
1202 value(StringSet< TString, Owner<ConcatDirect<TSpec> > > & me, TPos pos)
1204 return infix(me.concat, me.limits[pos], me.limits[pos + 1]);
1207 template < typename TString, typename TSpec, typename TPos >
1208 inline typename Infix<TString const>::Type
1209 value(StringSet< TString, Owner<ConcatDirect<TSpec> > > const & me, TPos pos)
1211 return infix(me.concat, me.limits[pos], me.limits[pos + 1]);
1215 template < typename TString, typename TPos >
1216 inline typename Reference<StringSet< TString, Dependent<Tight> > >::Type
1217 value(StringSet< TString, Dependent<Tight> >& me, TPos pos)
1220 if (me.strings[pos])
1221 return *me.strings[pos];
1222 static TString tmp = "";
1226 template < typename TString, typename TPos >
1227 inline typename Reference<StringSet< TString, Dependent<Tight> > const >::Type
1228 value(StringSet< TString, Dependent<Tight> >const & me, TPos pos)
1230 if (me.strings[pos])
1231 return *me.strings[pos];
1232 static TString tmp = "";
1237 template < typename TString, typename TPos >
1238 inline typename Reference<StringSet< TString, Dependent<Generous> > >::Type
1239 value(StringSet< TString, Dependent<Generous> >& me, TPos pos)
1242 unsigned i = _findIthNonZeroValue(me.strings, pos);
1243 if (i < length(me.strings))
1244 return *me.strings[i];
1245 static TString tmp = "";
1249 template < typename TString, typename TPos >
1250 inline typename Reference< StringSet< TString, Dependent<Generous> > const >::Type
1251 value(StringSet< TString, Dependent<Generous> > const & me, TPos pos)
1254 unsigned i = _findIthNonZeroValue(me.strings, pos);
1255 if (i < length(me.strings))
1256 return *me.strings[i];
1257 static TString tmp = "";
1261 //////////////////////////////////////////////////////////////////////////////
1265 .Function.getValueById:
1267 ..summary:Retrieves a string from the StringSet given an id.
1268 ..signature:getValueById(me, id)
1269 ..param.me:A StringSet.
1270 ...type:Class.StringSet
1272 ...type:Metafunction.Id
1273 ..returns:A reference to a string.
1274 ..see:Function.assignValueById
1275 ..see:Function.valueById
1278 template <typename TString, typename TSpec, typename TId>
1279 inline typename Reference<StringSet<TString, Owner<TSpec> > >::Type
1280 getValueById(StringSet<TString, Owner<TSpec> >& me,
1284 if (id < (TId) length(me)) return value(me, id);
1285 static TString tmp = "";
1289 template <typename TString, typename TId>
1290 inline typename Reference<StringSet<TString, Dependent<Generous> > >::Type
1291 getValueById(StringSet<TString, Dependent<Generous> >& me,
1296 return *me.strings[id];
1297 static TString tmp = "";
1301 template <typename TString, typename TId>
1302 inline typename Reference<StringSet<TString, Dependent<Tight> > >::Type
1303 getValueById(StringSet<TString, Dependent<Tight> >&me,
1307 for(unsigned i = 0; i < length(me.strings); ++i)
1308 if ((TId) me.ids[i] == id)
1309 return value(me, i);
1310 static TString tmp = "";
1315 //////////////////////////////////////////////////////////////////////////////
1319 .Function.valueById:
1321 ..summary:Retrieves a string from the StringSet given an id.
1322 ..signature:valueById(me, id)
1323 ..param.me:A StringSet.
1324 ...type:Class.StringSet
1326 ...type:Metafunction.Id
1327 ..returns:A reference to a string.
1328 ..see:Function.assignValueById
1329 ..see:Function.getValueById
1332 template<typename TString, typename TSpec, typename TId>
1333 inline typename Reference<StringSet<TString, TSpec> >::Type
1334 valueById(StringSet<TString, TSpec>& me,
1338 return getValueById(me, id);
1342 //////////////////////////////////////////////////////////////////////////////
1346 .Function.assignValueById:
1348 ..summary:Adds a new string to the StringSet and returns an id.
1349 ..signature:assignValueById(dest, str, [id])
1350 ..signature:assignValueById(dest, source, id)
1351 ..param.dest:A StringSet.
1352 ...type:Class.StringSet
1353 ..param.source:A StringSet.
1354 ...type:Class.StringSet
1355 ..param.str:A new string.
1356 ...type:Metafunction.Value
1357 ..param.id:An associated id.
1358 ...type:Metafunction.Id
1360 ...type:Metafunction.Id
1361 ..see:Function.getValueById
1362 ..see:Function.valueById
1365 template<typename TString, typename TSpec, typename TString2>
1366 inline typename Id<StringSet<TString, TSpec> >::Type
1367 assignValueById(StringSet<TString, TSpec>& me,
1371 appendValue(me, obj);
1372 SEQAN_ASSERT(length(me.limits) == length(me) + 1);
1373 return length(me.strings) - 1;
1376 template <typename TString, typename TSpec, typename TId>
1377 inline typename Id<StringSet<TString, Owner<TSpec> > >::Type
1378 assignValueById(StringSet<TString, Owner<TSpec> >& me,
1383 if (id >= (TId) length(me.strings)) {
1384 fill(me.strings, id+1, TString());
1385 resize(me.limits, length(me.limits) + 1);
1387 assignValue(me, id, obj);
1388 me.limitsValid = false;
1392 template<typename TString, typename TId>
1393 inline typename Id<StringSet<TString, Dependent<Generous> > >::Type
1394 assignValueById(StringSet<TString, Dependent<Generous> >& me,
1399 SEQAN_ASSERT(length(me.limits) == length(me) + 1);
1400 if (id >= (TId) length(me.strings)) fill(me.strings, id+1, (TString*) 0);
1401 if ((TString*) me.strings[id] == (TString*) 0)
1402 resize(me.limits, length(me.limits) + 1);
1403 me.strings[id] = &obj;
1404 me.limitsValid = false;
1405 SEQAN_ASSERT(length(me.limits) == length(me) + 1);
1409 //////////////////////////////////////////////////////////////////////////////
1411 template<typename TString, typename TId>
1412 inline typename Id<StringSet<TString, Dependent<Tight> > >::Type
1413 assignValueById(StringSet<TString, Dependent<Tight> >& me,
1418 typedef StringSet<TString, Dependent<Tight> > TStringSet;
1419 typedef typename Size<TStringSet>::Type TSize;
1421 for(TSize i = 0; i < length(me.ids); ++i)
1422 if ((TId) me.ids[i] == id) {
1423 me.strings[i] = &obj;
1424 me.limitsValid = false;
1427 appendValue(me.strings, &obj);
1428 appendValue(me.ids, id);
1432 template<typename TString, typename TSpec1, typename TSpec2, typename TId>
1433 inline typename Id<StringSet<TString, TSpec1> >::Type
1434 assignValueById(StringSet<TString, TSpec1>& dest,
1435 StringSet<TString, TSpec2>& source,
1439 return assignValueById(dest, getValueById(source, id), id);
1442 //////////////////////////////////////////////////////////////////////////////
1446 .Function.removeValueById:
1448 ..summary:Removes a string from the StringSet given an id.
1449 ..signature:removeValueById(me, id)
1450 ..param.me:A StringSet.
1451 ...type:Class.StringSet
1453 ...type:Metafunction.Id
1455 ..see:Function.assignValueById
1458 template<typename TString, typename TSpec, typename TId>
1460 removeValueById(StringSet<TString, Owner<TSpec> >& me, TId const id)
1463 erase(me.strings, id);
1464 resize(me.limits, length(me.limits) - 1);
1465 me.limitsValid = empty(me);
1468 template<typename TString, typename TId>
1470 removeValueById(StringSet<TString, Dependent<Generous> >& me, TId const id)
1473 if (me.strings[id] != (TString*) 0) {
1474 resize(me.limits, length(me.limits) - 1);
1475 me.limitsValid = empty(me);
1478 while (!empty(me.strings) && !me.strings[length(me.strings) - 1])
1479 resize(me.strings, length(me.strings) - 1);
1482 template<typename TString, typename TId>
1484 removeValueById(StringSet<TString, Dependent<Tight> >& me, TId const id)
1487 typedef StringSet<TString, Dependent<Tight> > TStringSet;
1488 typedef typename Size<TStringSet>::Type TSize;
1490 SEQAN_ASSERT(length(me.limits) == length(me) + 1);
1491 for(TSize i = 0; i < length(me.strings); ++i)
1492 if (me.ids[i] == id) {
1493 erase(me.strings, i);
1495 resize(me.limits, length(me.limits) - 1);
1496 me.limitsValid = empty(me);
1498 SEQAN_ASSERT(length(me.limits) == length(me) + 1);
1501 //////////////////////////////////////////////////////////////////////////////
1505 .Function.positionToId:
1507 ..summary:Retrieves the id of a string in the StringSet given a position.
1508 ..signature:positionToId(string_set, pos)
1509 ..param.string_set:A StringSet.
1510 ...type:Class.StringSet
1511 ..param.pos:A position that is transfored into an id.
1512 ..returns:An id that corresponds to $pos$ within $string_set$
1513 ..see:Function.assignValueById
1514 ..see:Function.valueById
1517 template <typename TString, typename TSpec, typename TPos>
1518 inline typename Id<StringSet<TString, Owner<TSpec> > >::Type
1519 positionToId(StringSet<TString, Owner<TSpec> >&,
1526 template <typename TString, typename TPos>
1527 inline typename Id<StringSet<TString, Dependent<Generous> > >::Type
1528 positionToId(StringSet<TString, Dependent<Generous> >& me,
1532 return _findIthNonZeroValue(me.strings,pos);
1535 template <typename TString, typename TPos>
1536 inline typename Id<StringSet<TString, Dependent<Generous> > >::Type
1537 positionToId(StringSet<TString, Dependent<Generous> > const& me,
1541 return _findIthNonZeroValue(me.strings,pos);
1544 template <typename TString, typename TPos>
1545 inline typename Id<StringSet<TString, Dependent<Tight> > >::Type
1546 positionToId(StringSet<TString, Dependent<Tight> >&me,
1553 template <typename TString, typename TPos>
1554 inline typename Id<StringSet<TString, Dependent<Tight> > >::Type
1555 positionToId(StringSet<TString, Dependent<Tight> > const&me,
1564 .Function.idToPosition:
1566 ..summary:Retrieves the position of a string in the StringSet given an id.
1567 ..signature:idToPosition(me, id)
1568 ..param.me:A StringSet.
1569 ...type:Class.StringSet
1571 ...type:Metafunction.Id
1572 ..returns:A reference to a string.
1573 ..see:Function.assignValueById
1574 ..see:Function.valueById
1577 template <typename TString, typename TSpec, typename TId>
1578 inline typename Id<StringSet<TString, Owner<TSpec> > >::Type
1579 idToPosition(StringSet<TString, Owner<TSpec> >&,
1586 template <typename TString, typename TId>
1587 inline typename Id<StringSet<TString, Dependent<Generous> > >::Type
1588 idToPosition(StringSet<TString, Dependent<Generous> >& me,
1592 return _countNonZeroValues(me.strings,id);
1595 template <typename TString, typename TId>
1596 inline typename Id<StringSet<TString, Dependent<Tight> > >::Type
1597 idToPosition(StringSet<TString, Dependent<Tight> >&me,
1601 for(unsigned i = 0; i < length(me.ids); ++i)
1602 if ((TId) me.ids[i] == id)
1610 //////////////////////////////////////////////////////////////////////////////
1616 ..summary:Creates a subset of a given StringSet.
1617 ..signature:subset(source, dest, id_array [, len])
1618 ..param.source:In-parameter:The source StringSet.
1619 ...type:Class.StringSet
1620 ..param.dest:Out-parameter:The destination StringSet (the subset).
1621 ...type:Class.StringSet
1622 ..param.id_array:In-parameter:An array of ids. Each id corresponds to a sequence that is supposed to be in the subset.
1623 ..param.len:In-parameter:Optional length of the id array.
1624 ...remarks:If len is not defined the length function must be valid for this array or string type.
1628 template <typename TString, typename TSpec, typename TDestSpec, typename TIds, typename TLength>
1630 subset(StringSet<TString, Owner<TSpec> >& source,
1631 StringSet<TString, TDestSpec>& dest,
1638 template <typename TString, typename TIds, typename TLength>
1640 subset(StringSet<TString, Dependent<Generous> >& source,
1641 StringSet<TString, Dependent<Generous> >& dest,
1646 typedef StringSet<TString, Dependent<Generous> > TStringSet;
1647 typedef typename Id<TStringSet>::Type TId;
1648 typedef typename Size<TStringSet>::Type TSize;
1651 resize(dest.limits, len + 1);
1652 dest.limitsValid = (len == 0);
1653 fill(dest.strings, length(source.strings), (TString*) 0);
1654 for(TSize i = 0; i < len; ++i)
1655 dest.strings[ids[i]] = source.strings[ids[i]];
1658 template <typename TString, typename TIds, typename TLength>
1660 subset(StringSet<TString, Dependent<Tight> >& source,
1661 StringSet<TString, Dependent<Tight> >& dest,
1666 typedef StringSet<TString, Dependent<Tight> > TStringSet;
1667 typedef typename Id<TStringSet>::Type TId;
1668 typedef typename Size<TStringSet>::Type TSize;
1671 resize(dest.limits, len + 1);
1672 dest.limitsValid = (len == 0);
1673 TLength upperBound = length(source.ids);
1674 for(TSize i=0;i<len;++i) {
1676 if ((upperBound > id) &&
1677 (source.ids[id] == id)) {
1678 appendValue(dest.strings, source.strings[id]);
1679 appendValue(dest.ids, id);
1681 typedef String<TId> TIdString;
1682 typedef typename Iterator<TIdString, Rooted>::Type TIter;
1683 TIter it = begin(source.ids);
1684 for(;!atEnd(it);goNext(it)) {
1686 appendValue(dest.strings, source.strings[position(it)]);
1687 appendValue(dest.ids, id);
1694 template <typename TString, typename TSpec, typename TIds>
1696 subset(StringSet<TString, TSpec>& source,
1697 StringSet<TString, TSpec>& dest,
1701 subset(source, dest, ids, length(ids));
1704 //////////////////////////////////////////////////////////////////////////////
1709 //////////////////////////////////////////////////////////////////////////////
1710 // ConcatenatorNto1 - a StringSet to String converter
1711 //////////////////////////////////////////////////////////////////////////////
1713 template <typename TStringSet>
1714 struct ConcatenatorNto1 {
1716 ConcatenatorNto1 () {}
1717 ConcatenatorNto1 (TStringSet &_set): set(&_set) {}
1719 //____________________________________________________________________________
1721 // operator[] conducts a binary search and should be avoided
1722 // you better use StringSet<.., Owner<ConcatDirect<..> > > for random access
1723 // or ConcatenatorNto1's iterators for sequential access
1725 template <typename TPos>
1726 inline typename Reference<ConcatenatorNto1>::Type
1727 operator [] (TPos pos)
1730 return value(*this, pos);
1733 template <typename TPos>
1734 inline typename Reference<ConcatenatorNto1 const>::Type
1735 operator [] (TPos pos) const
1738 return value(*this, pos);
1741 //____________________________________________________________________________
1743 template <typename TStringSet>
1744 struct Value< ConcatenatorNto1<TStringSet> > {
1745 typedef typename Value< typename Value<TStringSet>::Type >::Type Type;
1748 template <typename TStringSet>
1749 struct Value< ConcatenatorNto1<TStringSet> const >:
1750 Value< ConcatenatorNto1<TStringSet> > {};
1751 //____________________________________________________________________________
1753 template <typename TStringSet>
1754 struct Size< ConcatenatorNto1<TStringSet> > {
1755 typedef typename Size< typename Value<TStringSet>::Type >::Type Type;
1757 //____________________________________________________________________________
1759 template <typename TStringSet>
1760 struct AllowsFastRandomAccess< ConcatenatorNto1<TStringSet> >
1763 enum { VALUE = false };
1766 //////////////////////////////////////////////////////////////////////////////
1769 template < typename TStringSet, typename TPos >
1770 inline typename Reference< ConcatenatorNto1<TStringSet> >::Type
1771 value(ConcatenatorNto1<TStringSet> &me, TPos globalPos)
1773 Pair<unsigned, typename Size< typename Value<TStringSet>::Type >::Type> localPos;
1774 posLocalize(localPos, globalPos, stringSetLimits(*me.set));
1775 return value(value(*me.set, getValueI1(localPos)), getValueI2(localPos));
1778 template < typename TStringSet, typename TPos >
1779 inline typename Reference< ConcatenatorNto1<TStringSet> const >::Type
1780 value(ConcatenatorNto1<TStringSet> const &me, TPos globalPos)
1782 typedef typename Value<TStringSet>::Type TString;
1783 Pair<unsigned, typename Size<TString>::Type> localPos;
1784 posLocalize(localPos, globalPos, stringSetLimits(*me.set));
1785 return value(value(*(TStringSet const*)me.set, getValueI1(localPos)), getValueI2(localPos));
1788 //////////////////////////////////////////////////////////////////////////////
1791 template < typename TStringSet >
1792 inline typename Size< ConcatenatorNto1<TStringSet> >::Type
1793 length(ConcatenatorNto1<TStringSet> const &me) {
1794 return lengthSum(*me.set);
1797 //////////////////////////////////////////////////////////////////////////////
1800 template < typename TStringSet, typename TSpec >
1801 inline typename Iterator< ConcatenatorNto1<TStringSet const>, Tag<TSpec> const >::Type
1802 begin(ConcatenatorNto1<TStringSet const> concat, Tag<TSpec> const)
1804 return typename Iterator< ConcatenatorNto1<TStringSet const>, Tag<TSpec> const >::Type (*concat.set);
1807 template < typename TStringSet, typename TSpec >
1808 inline typename Iterator< ConcatenatorNto1<TStringSet>, Tag<TSpec> const >::Type
1809 begin(ConcatenatorNto1<TStringSet> concat, Tag<TSpec> const)
1811 return typename Iterator< ConcatenatorNto1<TStringSet>, Tag<TSpec> const >::Type (*concat.set);
1814 //////////////////////////////////////////////////////////////////////////////
1817 template < typename TStringSet, typename TSpec >
1818 inline typename Iterator< ConcatenatorNto1<TStringSet const>, Tag<TSpec> const >::Type
1819 end(ConcatenatorNto1<TStringSet const> concat, Tag<TSpec> const)
1821 return typename Iterator< ConcatenatorNto1<TStringSet>, Tag<TSpec> const >::Type
1822 (*concat.set, length(*concat.set), 0);
1825 template < typename TStringSet, typename TSpec >
1826 inline typename Iterator< ConcatenatorNto1<TStringSet>, Tag<TSpec> const >::Type
1827 end(ConcatenatorNto1<TStringSet> concat, Tag<TSpec> const)
1829 return typename Iterator< ConcatenatorNto1<TStringSet>, Tag<TSpec> const >::Type
1830 (*concat.set, length(*concat.set), 0);
1833 //////////////////////////////////////////////////////////////////////////////
1834 // Concatenator metafunction
1836 template < typename TString, typename TSpec >
1837 struct Concatenator< StringSet<TString, TSpec> > {
1838 typedef ConcatenatorNto1< StringSet<TString, TSpec> > Type;
1841 template < typename TString, typename TSpec >
1842 struct Concatenator< StringSet<TString, Owner<ConcatDirect<TSpec> > > > {
1843 typedef TString Type;
1846 //////////////////////////////////////////////////////////////////////////////
1849 template <typename TString>
1850 inline typename Concatenator<TString>::Type &
1851 concat(TString &string) {
1855 template <typename TString, typename TSpec>
1856 inline typename Concatenator< StringSet<TString, TSpec> >::Type &
1857 concat(StringSet<TString, TSpec> &set) {
1861 template <typename TString, typename TSpec>
1862 inline typename Concatenator< StringSet<TString, TSpec> const>::Type &
1863 concat(StringSet<TString, TSpec> const &set) {
1868 //////////////////////////////////////////////////////////////////////////////
1869 // This iterator sequentially iterates through the elements of TStringSet
1870 // as if they were directly concatenated (compare StringSet<.., Owner<ConcatDirect<> > >
1871 //////////////////////////////////////////////////////////////////////////////
1873 template < typename TDelimiter = void >
1874 struct ConcatVirtual;
1876 template < typename TStringSet, typename TSpec >
1877 class Iter< TStringSet, ConcatVirtual<TSpec> >
1880 typedef typename Value<TStringSet>::Type TString;
1881 typedef typename Value<TString>::Type TValue;
1882 typedef typename Size<TString>::Type TSize;
1883 //____________________________________________________________________________
1886 typedef typename Iterator<TString, Standard>::Type obj_iterator;
1887 typedef typename Iterator<TString const, Standard>::Type const_obj_iterator;
1889 //////////////////////////////////////////////////////////////////////////////
1890 // STL compatible public iterator interface
1892 typedef Iter iterator;
1893 typedef ::std::bidirectional_iterator_tag iterator_category;
1894 typedef TValue value_type;
1895 typedef TValue & reference;
1896 typedef TValue const & const_reference;
1897 typedef TValue* pointer;
1898 typedef TSize size_type;
1899 typedef typename Difference<TString>::Type difference_type;
1900 //____________________________________________________________________________
1904 obj_iterator _begin, _cur, _end;
1905 //____________________________________________________________________________
1909 inline Iter(TStringSet &_host):
1913 _begin = _cur = begin(_host[objNo]);
1914 _end = end(_host[objNo]);
1918 inline Iter(TStringSet &_host, unsigned _objNo, difference_type _offset):
1921 if (_objNo < length(_host)) {
1923 _begin = _cur = begin(_host[objNo]);
1924 _end = end(_host[objNo]);
1925 goFurther(_cur, _offset);
1928 objNo = length(_host) - 1;
1929 _begin = _cur = _end = end(_host[objNo]);
1933 inline operator obj_iterator() {
1936 //____________________________________________________________________________
1938 inline bool _atEndOfSequence() {
1939 if (_cur == _begin && objNo > 0) return true;
1940 if (_cur == _end) return true;
1944 inline void _testBegin() {
1945 while (_cur == _begin && objNo > 0) {
1947 _begin = host->_begin(objNo);
1948 _end = _cur = host->_end(objNo);
1952 inline void _testEnd() {
1953 while (_cur == _end && objNo < (length(*host) - 1)) {
1955 _begin = _cur = begin((*host)[objNo]);
1956 _end = end((*host)[objNo]);
1960 inline TSize _tell() const {
1961 typedef Pair<unsigned, TSize> TPair;
1962 return posGlobalize(TPair(objNo, difference(_begin, _cur)), stringSetLimits(*host));
1966 //////////////////////////////////////////////////////////////////////////////
1967 // ConcatenatorNto1 meta functions
1968 //////////////////////////////////////////////////////////////////////////////
1969 //____________________________________________________________________________
1970 // default concatenator iterators
1972 template <typename TString, typename TSpec >
1973 struct Iterator< ConcatenatorNto1< StringSet<TString, TSpec> >, Standard > {
1974 typedef Iter<StringSet<TString, TSpec>, ConcatVirtual<> > Type;
1977 template <typename TString, typename TSpec >
1978 struct Iterator< ConcatenatorNto1< StringSet<TString, TSpec> const >, Standard > {
1979 typedef Iter<StringSet<TString, TSpec> const, ConcatVirtual<> > Type;
1982 template <typename TString, typename TSpec >
1983 struct Iterator< ConcatenatorNto1< StringSet<TString, TSpec> >, Rooted > {
1984 typedef Iter<StringSet<TString, TSpec>, ConcatVirtual<> > Type;
1987 template <typename TString, typename TSpec >
1988 struct Iterator< ConcatenatorNto1< StringSet<TString, TSpec> const >, Rooted > {
1989 typedef Iter<StringSet<TString, TSpec> const, ConcatVirtual<> > Type;
1991 //____________________________________________________________________________
1993 template <typename TStringSet >
1994 struct Iterator< ConcatenatorNto1<TStringSet> const, Standard > {
1995 typedef typename Iterator< ConcatenatorNto1<TStringSet>, Standard >::Type Type;
1998 template <typename TStringSet >
1999 struct Iterator< ConcatenatorNto1<TStringSet> const, Rooted > {
2000 typedef typename Iterator< ConcatenatorNto1<TStringSet>, Rooted >::Type Type;
2003 //////////////////////////////////////////////////////////////////////////////
2005 //////////////////////////////////////////////////////////////////////////////
2007 template <typename TStringSet, typename TSpec>
2008 struct Value< Iter< TStringSet, ConcatVirtual<TSpec> > >:
2009 Value< typename Value<TStringSet>::Type > {};
2011 template <typename TStringSet, typename TSpec>
2012 struct Value< Iter< TStringSet, ConcatVirtual<TSpec> > const >:
2013 Value< typename Value<TStringSet>::Type > {};
2015 template <typename TStringSet, typename TSpec>
2016 struct GetValue< Iter< TStringSet, ConcatVirtual<TSpec> > >:
2017 GetValue< typename Value<TStringSet>::Type > {};
2019 template <typename TStringSet, typename TSpec>
2020 struct GetValue< Iter< TStringSet, ConcatVirtual<TSpec> > const >:
2021 GetValue< typename Value<TStringSet>::Type > {};
2023 template <typename TStringSet, typename TSpec>
2024 struct Size< Iter< TStringSet, ConcatVirtual<TSpec> > >:
2025 Size< typename Value<TStringSet>::Type > {};
2027 template <typename TStringSet, typename TSpec>
2028 struct Reference< Iter< TStringSet, ConcatVirtual<TSpec> > >:
2029 Reference< typename Value<TStringSet>::Type > {};
2031 template <typename TStringSet, typename TSpec>
2032 struct Reference< Iter< TStringSet, ConcatVirtual<TSpec> > const >:
2033 Reference< typename Value<TStringSet>::Type > {};
2036 //////////////////////////////////////////////////////////////////////////////
2038 //////////////////////////////////////////////////////////////////////////////
2040 template <typename TStringSet, typename TSpec>
2041 inline typename Reference< Iter< TStringSet, ConcatVirtual<TSpec> > const>::Type
2042 value(Iter<TStringSet, ConcatVirtual<TSpec> > const & me) {
2046 template <typename TStringSet, typename TSpec>
2047 inline typename Reference< Iter< TStringSet, ConcatVirtual<TSpec> > >::Type
2048 value(Iter<TStringSet, ConcatVirtual<TSpec> > & me) {
2052 template <typename TStringSet, typename TSpec>
2053 inline typename Reference< Iter< TStringSet, ConcatVirtual<TSpec> > const>::Type
2054 operator * (Iter<TStringSet, ConcatVirtual<TSpec> > const & me) {
2058 template <typename TStringSet, typename TSpec>
2059 inline typename Reference< Iter< TStringSet, ConcatVirtual<TSpec> > >::Type
2060 operator * (Iter<TStringSet, ConcatVirtual<TSpec> > & me) {
2064 //////////////////////////////////////////////////////////////////////////////
2066 //////////////////////////////////////////////////////////////////////////////
2068 template <typename TStringSet, typename TSpec>
2070 goNext(Iter<TStringSet, ConcatVirtual<TSpec> > & me) {
2075 template <typename TStringSet, typename TSpec>
2076 inline Iter<TStringSet, ConcatVirtual<TSpec> > const &
2077 operator ++ (Iter<TStringSet, ConcatVirtual<TSpec> > & me) {
2082 template <typename TStringSet, typename TSpec>
2083 inline Iter<TStringSet, ConcatVirtual<TSpec> > const &
2084 operator ++ (Iter<TStringSet, ConcatVirtual<TSpec> > & me, int) {
2085 Iter<TStringSet, ConcatVirtual<TSpec> > before = me;
2090 //////////////////////////////////////////////////////////////////////////////
2092 //////////////////////////////////////////////////////////////////////////////
2094 template <typename TStringSet, typename TSpec>
2096 goPrevious(Iter<TStringSet, ConcatVirtual<TSpec> > & me) {
2101 template <typename TStringSet, typename TSpec>
2102 inline Iter<TStringSet, ConcatVirtual<TSpec> > const &
2103 operator -- (Iter<TStringSet, ConcatVirtual<TSpec> > & me) {
2108 template <typename TStringSet, typename TSpec>
2109 inline Iter<TStringSet, ConcatVirtual<TSpec> > const &
2110 operator -- (Iter<TStringSet, ConcatVirtual<TSpec> > & me, int) {
2111 Iter<TStringSet, ConcatVirtual<TSpec> > before = me;
2116 //////////////////////////////////////////////////////////////////////////////
2118 //////////////////////////////////////////////////////////////////////////////
2120 template <typename TStringSet, typename TSpec, typename TDelta>
2121 inline Iter<TStringSet, ConcatVirtual<TSpec> >
2122 operator + (Iter<TStringSet, ConcatVirtual<TSpec> > const & me, TDelta delta) {
2123 Pair<unsigned, typename Size< typename Value<TStringSet>::Type >::Type> pos;
2124 posLocalize(pos, me._tell() + delta, stringSetLimits(*me.host));
2125 return Iter<TStringSet, ConcatVirtual<TSpec> > (*me.host, getValueI1(pos), getValueI2(pos));
2128 template <typename TStringSet, typename TSpec, typename T1, typename T2, typename TCompression>
2129 inline Iter<TStringSet, ConcatVirtual<TSpec> >
2130 operator + (Iter<TStringSet, ConcatVirtual<TSpec> > const & me, Pair<T1, T2, TCompression> delta) {
2131 Pair<unsigned, typename Size< typename Value<TStringSet>::Type >::Type> pos;
2132 posLocalize(pos, me._tell() + delta, stringSetLimits(*me.host));
2133 return Iter<TStringSet, ConcatVirtual<TSpec> > (*me.host, getValueI1(pos), getValueI2(pos));
2136 //////////////////////////////////////////////////////////////////////////////
2138 //////////////////////////////////////////////////////////////////////////////
2140 template <typename TSSetL, typename TSpecL, typename TSSetR, typename TSpecR>
2141 typename Difference<Iter<TSSetL, ConcatVirtual<TSpecL> > >::Type
2143 Iter<TSSetL, ConcatVirtual<TSpecL> > const &L,
2144 Iter<TSSetR, ConcatVirtual<TSpecR> > const &R)
2146 return L._tell() - R._tell();
2149 template <typename TStringSet, typename TSpec, typename TDelta>
2150 inline Iter<TStringSet, ConcatVirtual<TSpec> >
2151 operator - (Iter<TStringSet, ConcatVirtual<TSpec> > const & me, TDelta delta) {
2152 Pair<unsigned, typename Size< typename Value<TStringSet>::Type >::Type> pos;
2153 posLocalize(pos, me._tell() - delta, stringSetLimits(*me.host));
2154 return Iter<TStringSet, ConcatVirtual<TSpec> > (*me.host, getValueI1(pos), getValueI2(pos));
2157 //////////////////////////////////////////////////////////////////////////////
2159 //////////////////////////////////////////////////////////////////////////////
2161 template <typename TSSetL, typename TSpecL, typename TSSetR, typename TSpecR>
2164 Iter<TSSetL, ConcatVirtual<TSpecL> > const &L,
2165 Iter<TSSetR, ConcatVirtual<TSpecR> > const &R)
2167 SEQAN_ASSERT(L.host == R.host);
2168 return L.objNo == R.objNo && L._cur == R._cur;
2171 template <typename TSSetL, typename TSpecL, typename TSSetR, typename TSpecR>
2174 Iter<TSSetL, ConcatVirtual<TSpecL> > const &L,
2175 Iter<TSSetR, ConcatVirtual<TSpecR> > const &R)
2177 SEQAN_ASSERT(L.host == R.host);
2178 return L.objNo != R.objNo || L._cur != R._cur;
2181 //////////////////////////////////////////////////////////////////////////////
2183 //////////////////////////////////////////////////////////////////////////////
2185 template <typename TSSetL, typename TSpecL, typename TSSetR, typename TSpecR>
2188 Iter<TSSetL, ConcatVirtual<TSpecL> > const &L,
2189 Iter<TSSetR, ConcatVirtual<TSpecR> > const &R)
2191 SEQAN_ASSERT(L.host == R.host);
2192 return L.objNo < R.objNo || (L.objNo == R.objNo && L._cur < R._cur);
2195 template <typename TSSetL, typename TSpecL, typename TSSetR, typename TSpecR>
2198 Iter<TSSetL, ConcatVirtual<TSpecL> > const &L,
2199 Iter<TSSetR, ConcatVirtual<TSpecR> > const &R)
2201 SEQAN_ASSERT(L.host == R.host);
2202 return L.objNo > R.objNo || (L.objNo == R.objNo && L._cur > R._cur);
2205 //////////////////////////////////////////////////////////////////////////////
2207 //////////////////////////////////////////////////////////////////////////////
2209 template <typename TSSet, typename TSpec>
2210 inline typename Concatenator<TSSet>::Type
2211 container(Iter<TSSet, ConcatVirtual<TSpec> > &me)
2213 return concat(*me.host);
2216 template <typename TSSet, typename TSpec>
2217 inline typename Concatenator<TSSet>::Type
2218 container(Iter<TSSet, ConcatVirtual<TSpec> > const &me)
2220 return concat(*me.host);
2223 //////////////////////////////////////////////////////////////////////////////
2225 //////////////////////////////////////////////////////////////////////////////
2227 template <typename TSSet, typename TSpec>
2229 atBegin(Iter<TSSet, ConcatVirtual<TSpec> > &me)
2231 return me._cur == me._begin && me.objNo == 0;
2234 template <typename TSSet, typename TSpec>
2236 atBegin(Iter<TSSet, ConcatVirtual<TSpec> > const &me)
2238 return me._cur == me._begin && me.objNo == 0;
2241 //////////////////////////////////////////////////////////////////////////////
2243 //////////////////////////////////////////////////////////////////////////////
2245 template <typename TSSet, typename TSpec>
2247 atEnd(Iter<TSSet, ConcatVirtual<TSpec> > &me)
2249 return me._cur == me._end && me.objNo == (length(*me.host) - 1);
2252 template <typename TSSet, typename TSpec>
2254 atEnd(Iter<TSSet, ConcatVirtual<TSpec> > const &me)
2256 return me._cur == me._end && me.objNo == (length(*me.host) - 1);
2259 //////////////////////////////////////////////////////////////////////////////
2261 //////////////////////////////////////////////////////////////////////////////
2263 template <typename TIterator>
2265 atEndOfSequence(TIterator const &me)
2270 template <typename TSSet, typename TSpec>
2272 atEndOfSequence(Iter<TSSet, ConcatVirtual<TSpec> > const &me)
2274 return me._atEndOfSequence();
2277 template <typename TIterator>
2279 atEndOfSequence(TIterator &me)
2281 return atEndOfSequence(reinterpret_cast<TIterator const &>(me));