Imported Upstream version 0.12.7
[bowtie.git] / SeqAn-1.1 / seqan / sequence / string_value_expand.h
1  /*==========================================================================
2                 SeqAn - The Library for Sequence Analysis
3                           http://www.seqan.de 
4  ============================================================================
5   Copyright (C) 2007
6
7   This library is free software; you can redistribute it and/or
8   modify it under the terms of the GNU Lesser General Public
9   License as published by the Free Software Foundation; either
10   version 3 of the License, or (at your option) any later version.
11
12   This library is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   Lesser General Public License for more details.
16
17  ============================================================================
18   $Id: string_value_expand.h,v 1.1 2008/08/25 16:20:04 langmead Exp $
19  ==========================================================================*/
20
21 #ifndef SEQAN_HEADER_SEQUENCE_STRING_VALUEEXPAND_H
22 #define SEQAN_HEADER_SEQUENCE_STRING_VALUEEXPAND_H
23
24
25 namespace SEQAN_NAMESPACE_MAIN
26 {
27 //////////////////////////////////////////////////////////////////////////////
28 // Tags
29
30 template <typename THost, typename TMap, typename TSpec = Default>
31 struct ValueExpand;
32
33 struct ValueExpandIter;
34
35 //////////////////////////////////////////////////////////////////////////////
36
37 template <typename TValue, typename THost, typename TMap, typename TSpec>
38 class String<TValue, ValueExpand<THost, TMap, TSpec> >
39 {
40 //____________________________________________________________________________
41 private:
42
43         typedef typename Value<String>::Type TLargeValue;
44         typedef typename Value<THost>::Type TSmallValue;
45
46         Holder<THost> data_host;
47         Holder<TMap> data_map;
48
49 //____________________________________________________________________________
50
51 public:
52         String() 
53         {
54         }
55         String(String const & other_):
56                 data_host(other_.data_host),
57                 data_map(other_.data_map)
58         {
59         }
60         ~String()
61         {
62         }
63         String const &
64         operator = (String const & other_)
65         {
66                 data_host = other_.data_host;
67                 data_map = other_.data_map;
68         }
69 //____________________________________________________________________________
70
71         template <typename TPos>
72         inline typename Reference<String>::Type
73         operator [](TPos pos)
74         {
75                 return value(*this, pos);
76         }
77
78 //____________________________________________________________________________
79
80         friend inline Holder<THost> &
81         _dataHost(String & me)
82         {
83                 return me.data_host;
84         }
85 //____________________________________________________________________________
86
87         friend inline Holder<TMap> &
88         _dataMap(String & me)
89         {
90                 return me.data_map;
91         }
92 //____________________________________________________________________________
93 };
94
95 //////////////////////////////////////////////////////////////////////////////
96 // Metafunctions
97
98 template <typename TValue, typename THost, typename TMap, typename TSpec>
99 struct Value<String< TValue, ValueExpand<THost, TMap, TSpec> > >
100 {
101         typedef TValue Type;
102 };
103 template <typename TValue, typename THost, typename TMap, typename TSpec>
104 struct Value<String< TValue, ValueExpand<THost, TMap, TSpec> > const>
105 {
106         typedef TValue const Type;
107 };
108
109 //____________________________________________________________________________
110
111 template <typename TValue, typename THost, typename TMap, typename TSpec>
112 struct GetValue<String< TValue, ValueExpand<THost, TMap, TSpec> > >:
113         Value<String< TValue, ValueExpand<THost, TMap, TSpec> > >
114 {
115 };
116 template <typename TValue, typename THost, typename TMap, typename TSpec>
117 struct GetValue<String< TValue, ValueExpand<THost, TMap, TSpec> > const>:
118         Value<String< TValue, ValueExpand<THost, TMap, TSpec> > const>
119 {
120 };
121
122 //____________________________________________________________________________
123
124 template <typename TValue, typename THost, typename TMap, typename TSpec>
125 struct Reference<String< TValue, ValueExpand<THost, TMap, TSpec> > >    
126 {
127         typedef String< TValue, ValueExpand<THost, TMap, TSpec> > TMe;
128         typedef typename Iterator<TMe, Standard>::Type TIterator;
129         typedef Proxy<IteratorProxy<TIterator> > Type;
130 };
131 template <typename TValue, typename THost, typename TMap, typename TSpec>
132 struct Reference<String< TValue, ValueExpand<THost, TMap, TSpec> > const>       
133 {
134         typedef String< TValue, ValueExpand<THost, TMap, TSpec> > const TMe;
135         typedef typename Iterator<TMe, Standard>::Type TIterator;
136         typedef Proxy<IteratorProxy<TIterator> > Type;
137 };
138
139 //____________________________________________________________________________
140
141 template <typename TValue, typename THost, typename TMap, typename TSpec, typename TIteratorSpec>
142 struct Iterator<String< TValue, ValueExpand<THost, TMap, TSpec> >, TIteratorSpec>       
143 {
144         typedef ValueExpand<THost, TMap, TSpec> TValueExpand;
145         typedef String< TValue, TValueExpand> TMe;
146         typedef typename Iterator<THost, Standard>::Type THostIterator;
147
148         typedef Iter<TMe, AdaptorIterator<THostIterator, ValueExpandIter> > Type;
149 };
150 template <typename TValue, typename THost, typename TMap, typename TSpec, typename TIteratorSpec>
151 struct Iterator<String< TValue, ValueExpand<THost, TMap, TSpec> > const, TIteratorSpec> 
152 {
153         typedef ValueExpand<THost, TMap, TSpec> TValueExpand;
154         typedef String< TValue, TValueExpand> const TMe;
155         typedef typename Iterator<THost, Standard>::Type THostIterator;
156
157         typedef Iter<TMe, AdaptorIterator<THostIterator, ValueExpandIter> > Type;
158 };
159
160 //____________________________________________________________________________
161
162 template <typename TValue, typename THost, typename TMap, typename TSpec>
163 struct DefaultOverflowImplicit<String< TValue, ValueExpand<THost, TMap, TSpec> > >:
164         DefaultOverflowImplicit< typename Host<String< TValue, ValueExpand<THost, TMap, TSpec> > >::Type >
165 {
166 };
167 template <typename TValue, typename THost, typename TMap, typename TSpec>
168 struct DefaultOverflowImplicit<String< TValue, ValueExpand<THost, TMap, TSpec> > const>:
169         DefaultOverflowImplicit< typename Host<String< TValue, ValueExpand<THost, TMap, TSpec> > const>::Type >
170 {
171 };
172
173 //____________________________________________________________________________
174
175 template <typename TValue, typename THost, typename TMap, typename TSpec>
176 struct DefaultOverflowExplicit<String< TValue, ValueExpand<THost, TMap, TSpec> > >:
177         DefaultOverflowExplicit< typename Host<String< TValue, ValueExpand<THost, TMap, TSpec> > >::Type >
178 {
179 };
180 template <typename TValue, typename THost, typename TMap, typename TSpec>
181 struct DefaultOverflowExplicit<String< TValue, ValueExpand<THost, TMap, TSpec> > const>:
182         DefaultOverflowExplicit< typename Host<String< TValue, ValueExpand<THost, TMap, TSpec> > const>::Type >
183 {
184 };
185
186 //____________________________________________________________________________
187
188 template <typename TValue, typename THost, typename TMap, typename TSpec>
189 struct IsContiguous<String< TValue, ValueExpand<THost, TMap, TSpec> > >
190 {
191     typedef False Type;
192         enum { VALUE = false };
193 };
194
195 //____________________________________________________________________________
196
197 template <typename TValue, typename THost, typename TMap, typename TSpec>
198 struct Host<String< TValue, ValueExpand<THost, TMap, TSpec> > >
199 {
200         typedef THost Type;
201 };
202 template <typename TValue, typename THost, typename TMap, typename TSpec>
203 struct Host<String< TValue, ValueExpand<THost, TMap, TSpec> > const>
204 {
205         typedef THost const Type;
206 };
207
208 //____________________________________________________________________________
209
210 template <typename T>
211 struct MapType;
212
213 template <typename TValue, typename THost, typename TMap, typename TSpec>
214 struct MapType<String< TValue, ValueExpand<THost, TMap, TSpec> > >
215 {
216         typedef TMap Type;
217 };
218 template <typename TValue, typename THost, typename TMap, typename TSpec>
219 struct MapType<String< TValue, ValueExpand<THost, TMap, TSpec> > const>
220 {
221         typedef TMap const Type;
222 };
223
224 //////////////////////////////////////////////////////////////////////////////
225 // Special Functions
226
227 template <typename TValue, typename THost, typename TMap, typename TSpec>
228 inline TMap &
229 _getMap(String< TValue, ValueExpand<THost, TMap, TSpec> > & me)
230 {
231         return value(_dataMap(me));
232 }
233 template <typename TValue, typename THost, typename TMap, typename TSpec>
234 inline TMap &
235 _getMap(String< TValue, ValueExpand<THost, TMap, TSpec> > const & me)
236 {
237         return value(_dataMap(me));
238 }
239
240 //____________________________________________________________________________
241
242 template <typename TValue, typename THost, typename TMap, typename TSpec>
243 inline typename Value<THost>::Type
244 _getValueExpandFlagValue(String< TValue, ValueExpand<THost, TMap, TSpec> > const & me)
245 {
246         typedef typename Value<THost>::Type TSmallValue;
247         return supremumValue<TSmallValue>();
248 }
249
250 //////////////////////////////////////////////////////////////////////////////
251 // Public Functions
252
253
254 template <typename TValue, typename THost, typename TMap, typename TSpec>
255 inline void const * 
256 id(String< TValue, ValueExpand<THost, TMap, TSpec> > const & me)
257 {
258 SEQAN_CHECKPOINT
259         return id(host(me));
260 }
261
262 //____________________________________________________________________________
263
264 template <typename TValue, typename THost, typename TMap, typename TPos, typename TSpec, typename TTag>
265 inline typename Iterator<String< TValue, ValueExpand<THost, TMap, TSpec> >, Tag<TTag> const>::Type 
266 iter(String< TValue, ValueExpand<THost, TMap, TSpec> > & me,
267          TPos pos_,
268          Tag<TTag> const tag_)
269 {
270 SEQAN_CHECKPOINT
271         typedef String< TValue, ValueExpand<THost, TMap, TSpec> > TMe;
272         typedef typename Iterator<TMe, Tag<TTag> const>::Type TIterator;
273         return TIterator(me, begin(host(me), Standard()) + pos_);
274 }
275 template <typename TValue, typename THost, typename TMap, typename TPos, typename TSpec, typename TTag>
276 inline typename Iterator<String< TValue, ValueExpand<THost, TMap, TSpec> > const, Tag<TTag> const>::Type 
277 iter(String< TValue, ValueExpand<THost, TMap, TSpec> > const & me,
278          TPos pos_,
279          Tag<TTag> const tag_ )
280 {
281 SEQAN_CHECKPOINT
282         typedef String< TValue, ValueExpand<THost, TMap, TSpec> > const TMe;
283         typedef typename Iterator<TMe, Tag<TTag> const>::Type TIterator;
284         return TIterator(me, begin(host(me), Standard()) + pos_);
285 }
286
287 //____________________________________________________________________________
288
289 template <typename TValue, typename THost, typename TMap, typename TSpec, typename TTag>
290 inline typename Iterator<String< TValue, ValueExpand<THost, TMap, TSpec> >, Tag<TTag> const>::Type 
291 begin(String< TValue, ValueExpand<THost, TMap, TSpec> > & me,
292           Tag<TTag> const tag_)
293 {
294 SEQAN_CHECKPOINT
295         return iter(me, 0, tag_);
296 }
297 template <typename TValue, typename THost, typename TMap, typename TSpec, typename TTag>
298 inline typename Iterator<String< TValue, ValueExpand<THost, TMap, TSpec> > const, Tag<TTag> const>::Type 
299 begin(String< TValue, ValueExpand<THost, TMap, TSpec> > const & me,
300           Tag<TTag> const tag_)
301 {
302 SEQAN_CHECKPOINT
303         return iter(me, 0, tag_);
304 }
305
306 //____________________________________________________________________________
307
308 template <typename TValue, typename THost, typename TMap, typename TSpec, typename TTag>
309 inline typename Iterator<String< TValue, ValueExpand<THost, TMap, TSpec> >, Tag<TTag> const>::Type 
310 end(String< TValue, ValueExpand<THost, TMap, TSpec> > & me,
311         Tag<TTag> const tag_)
312 {
313 SEQAN_CHECKPOINT
314         return iter(me, length(me), tag_);
315 }
316 template <typename TValue, typename THost, typename TMap, typename TSpec, typename TTag>
317 inline typename Iterator<String< TValue, ValueExpand<THost, TMap, TSpec> > const, Tag<TTag> const>::Type 
318 end(String< TValue, ValueExpand<THost, TMap, TSpec> > const & me,
319         Tag<TTag> const tag_)
320 {
321 SEQAN_CHECKPOINT
322         return iter(me, length(me), tag_);
323 }
324
325 //____________________________________________________________________________
326
327 template <typename TValue, typename THost, typename TMap, typename TSpec, typename TPos>
328 inline typename Reference<String< TValue, ValueExpand<THost, TMap, TSpec> > >::Type
329 value(String< TValue, ValueExpand<THost, TMap, TSpec> > & me, 
330           TPos pos)
331 {
332 SEQAN_CHECKPOINT
333         
334         return *iter(me, pos, Standard());
335
336 template <typename TValue, typename THost, typename TMap, typename TSpec, typename TPos>
337 inline typename Reference<String< TValue, ValueExpand<THost, TMap, TSpec> > const>::Type
338 value(String< TValue, ValueExpand<THost, TMap, TSpec> > const & me, 
339           TPos pos)
340 {
341 SEQAN_CHECKPOINT
342         
343         return *iter(me, pos, Standard());
344
345
346 //____________________________________________________________________________
347
348 template <typename TValue, typename THost, typename TMap, typename TSpec>
349 inline typename Size<String< TValue, ValueExpand<THost, TMap, TSpec> > const>::Type
350 length(String< TValue, ValueExpand<THost, TMap, TSpec> > const & me)
351 {
352 SEQAN_CHECKPOINT
353         return length(host(me));
354 }
355
356 //____________________________________________________________________________
357
358 template <typename TValue, typename THost, typename TMap, typename TSpec>
359 inline typename Size<String< TValue, ValueExpand<THost, TMap, TSpec> > const>::Type
360 capacity(String< TValue, ValueExpand<THost, TMap, TSpec> > const & me)
361 {
362 SEQAN_CHECKPOINT
363         return capacity(host(me));
364 }
365
366 //____________________________________________________________________________
367
368 template <typename TValue, typename THost, typename TMap, typename TSpec, typename TSize>
369 inline TSize
370 resize(String< TValue, ValueExpand<THost, TMap, TSpec> > & me,
371            TSize new_length)
372 {
373 SEQAN_CHECKPOINT
374         return resize(host(me), new_length);
375 }
376
377 //____________________________________________________________________________
378
379 template <typename TValue, typename THost, typename TMap, typename TSpec, typename TSize, typename TExpand>
380 inline TSize 
381 reserve(String< TValue, ValueExpand<THost, TMap, TSpec> > & me,
382                 TSize new_length,
383                 Tag<TExpand> const tag)
384 {
385 SEQAN_CHECKPOINT
386         return reserve(host(me), new_length, tag);
387 }
388
389 //////////////////////////////////////////////////////////////////////////////
390 // Iterator for ValueExpandIterator string: Subclass of AdaptorIterator
391 //////////////////////////////////////////////////////////////////////////////
392
393 template <typename TContainer, typename TIterator>
394 inline typename Value<TContainer>::Type
395 getValue(Iter<TContainer, AdaptorIterator<TIterator, ValueExpandIter> > & me)
396 {
397 SEQAN_CHECKPOINT
398         typedef typename Host<TContainer>::Type THost;
399         typedef typename Value<THost>::Type TSmallValue;
400         TSmallValue c = value(hostIterator(me));
401         if (c == _getValueExpandFlagValue(container(me)))
402         {//value is large 
403                 return _getMap(container(me))[position(me)];
404         }
405         else
406         {
407                 return c;
408         }
409 }
410 template <typename TContainer, typename TIterator>
411 inline void
412 getValue(Iter<TContainer, AdaptorIterator<TIterator, ValueExpandIter> > const & me)
413 {
414 SEQAN_CHECKPOINT
415         typedef typename Host<TContainer>::Type THost;
416         typedef typename Value<THost>::Type TSmallValue;
417         TSmallValue c = value(hostIterator(me));
418         if (c == _getValueExpandFlagValue(container(me)))
419         {//value is large 
420                 return _getMap(container(me))[position(me)];
421         }
422         else
423         {
424                 return c;
425         }
426 }
427
428 //____________________________________________________________________________
429
430 template <typename TContainer, typename TIterator>
431 inline typename Reference<Iter<TContainer, AdaptorIterator<TIterator, ValueExpandIter> > >::Type 
432 value(Iter<TContainer, AdaptorIterator<TIterator, ValueExpandIter> > & me)
433 {
434 SEQAN_CHECKPOINT
435         typedef Iter<TContainer, AdaptorIterator<TIterator, ValueExpandIter> > TMe;
436         return typename Reference<TMe>::Type(me);
437 }
438 template <typename TContainer, typename TIterator>
439 inline typename Reference<Iter<TContainer, AdaptorIterator<TIterator, ValueExpandIter> > >::Type 
440 value(Iter<TContainer, AdaptorIterator<TIterator, ValueExpandIter> > const & me)
441 {
442 SEQAN_CHECKPOINT
443         typedef Iter<TContainer, AdaptorIterator<TIterator, ValueExpandIter> > const TMe;
444         return typename Reference<TMe>::Type(me);
445 }
446
447 //____________________________________________________________________________
448
449 template <typename TContainer, typename TIterator, typename TValue>
450 inline void
451 assignValue(Iter<TContainer, AdaptorIterator<TIterator, ValueExpandIter> > & me,
452                         TValue const & _value)
453 {
454 SEQAN_CHECKPOINT
455         typedef typename Host<TContainer>::Type THost;
456         typedef typename Value<THost>::Type TSmallValue;
457         
458         TSmallValue flagValue = _getValueExpandFlagValue(container(me));
459         if (_value >= flagValue)
460         {//use map to store LargeValue
461                 assignValue(hostIterator(me), flagValue);
462                 _getMap(container(me))[position(me)] = _value;
463         }
464         else
465         {
466                 assignValue(hostIterator(me), _value);
467         }
468 }
469 template <typename TContainer, typename TIterator, typename TValue>
470 inline void
471 assignValue(Iter<TContainer, AdaptorIterator<TIterator, ValueExpandIter> > const & me,
472                         TValue const & _value)
473 {
474 SEQAN_CHECKPOINT
475         typedef typename Host<TContainer>::Type THost;
476         typedef typename Value<THost>::Type TSmallValue;
477         
478         TSmallValue flagValue = _getValueExpandFlagValue(container(me));
479         if (_value >= flagValue)
480         {//use map to store LargeValue
481                 assignValue(hostIterator(me), flagValue);
482                 _getMap(container(me))[position(me)] = _value;
483         }
484         else
485         {
486                 assignValue(hostIterator(me), _value);
487         }
488 }
489
490 //____________________________________________________________________________
491
492 template <typename TContainer, typename TIterator, typename TValue>
493 inline void
494 moveValue(Iter<TContainer, AdaptorIterator<TIterator, ValueExpandIter> > & me,
495                   TValue const & _value)
496 {
497 SEQAN_CHECKPOINT
498         assignValue(me, _value);
499 }
500 template <typename TContainer, typename TIterator, typename TValue>
501 inline void
502 moveValue(Iter<TContainer, AdaptorIterator<TIterator, ValueExpandIter> > const & me,
503                   TValue const & _value)
504 {
505 SEQAN_CHECKPOINT
506         assignValue(me, _value);
507 }
508
509
510 //////////////////////////////////////////////////////////////////////////////
511
512 } //namespace SEQAN_NAMESPACE_MAIN
513
514 #endif //#ifndef SEQAN_HEADER_...