Imported Upstream version 0.12.7
[bowtie.git] / SeqAn-1.1 / seqan / basic / basic_type.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: basic_type.h,v 1.1 2008/08/25 16:20:01 langmead Exp $
19  ==========================================================================*/
20
21 #ifndef SEQAN_HEADER_BASIC_TYPE_H
22 #define SEQAN_HEADER_BASIC_TYPE_H
23
24 namespace SEQAN_NAMESPACE_MAIN
25 {
26
27
28 //////////////////////////////////////////////////////////////////////////////
29
30 /**
31 .Metafunction.Value:
32 ..summary:Type of the items in the container. 
33 ..signature:Value<T>::Type
34 ..param.T:Type for which the value type is determined.
35 ..returns.param.Type:Value type of $T$.
36 ..remarks.text:The value type of a container $T$ is the type of the elements in $T$.
37     For example, the value type of a sequence of $int$ is $int$.
38 ..example.code:Value<String<char> >::Type c; //c has type char
39 */
40
41 template <typename T, const int i = 0>
42 struct Value
43 {
44         typedef T Type;
45 };
46 /*
47 template <typename T>
48 struct Value<T const>
49 {
50         typedef T Type;
51 };
52 */
53 //____________________________________________________________________________
54
55 /**
56 .Metafunction.GetValue:
57 ..summary:Type for reading values. 
58 ..signature:GetValue<T>::Type
59 ..param.T:Type of container that holds a value.
60 ..returns.param.Type:GetValue type of $T$.
61 ..remarks.text:Depending on $T$, the $GetValue$-type can either be $Value<T>::Type &$ or $Value<T>::Type$.
62 ..text:$GetValue$ is the return type of @Function.getValue@ that allows a (read-only) access to objects.
63 Do not confuse it with @Function.value@ that returns a @Metafunction.Reference.reference@ to the value.
64 ..see:Metafunction.Value
65 ..see:Function.getValue
66 */
67 template <typename T>
68 struct GetValue
69 {
70         typedef typename Value<T>::Type const & Type;
71 };
72 template <typename T>
73 struct GetValue<T const>:
74         public GetValue<T>
75 {
76 };
77
78 //____________________________________________________________________________
79
80 /**
81 .Metafunction.Reference:
82 ..summary:Reference type. 
83 ..signature:Reference<T>::Type
84 ..param.T:A Type.
85 ..returns.param.Type:Either $T &$ or a proxy object @Class.Proxy@ for $T$.
86 ..see:Metafunction.Value
87 ..see:Metafunction.GetValue
88 */
89 template <typename T>
90 struct Reference
91 {
92         typedef typename Value<T>::Type & Type;
93 };
94 template <typename T>
95 struct Reference<T const>
96 {
97         typedef typename Value<T>::Type const & Type;
98 };
99
100 //____________________________________________________________________________
101
102
103 /**
104 .Metafunction.Size:
105 ..summary:Type of an object that is suitable to hold size information.
106 ..signature:Size<T>::Type
107 ..param.T:Type for which the size type is determined.
108 ..returns.param.Type:Size type of $T$.
109 ..remarks.text:In most cases this type is $size_t$.
110 */
111 template <typename T>
112 struct Size
113 {
114         typedef size_t Type;
115 };
116 template <typename T>
117 struct Size<T const>:
118         Size<T>
119 {
120 };
121
122 //____________________________________________________________________________
123
124
125 /**
126 .Metafunction.Difference:
127 ..summary:Type of an object that stores the difference between two iterators.
128 ..signature:Difference<T>::Type
129 ..param.T:Type for which the difference type is determined.
130 ...type:Class.Iter
131 ..returns.param.Type:Difference type of $T$.
132 ..remarks.text:In most cases this type is $ptrdiff_t$.
133 ..see:Metafunction.Size
134 */
135 template <typename T>
136 struct Difference
137 {
138         typedef ptrdiff_t Type;
139 };
140 template <typename T>
141 struct Difference<T const>:
142         Difference<T>
143 {
144 };
145
146 //____________________________________________________________________________
147
148
149 /**
150 .Metafunction.Position:
151 ..summary:Type of an object that represents a position in a container.
152 ..signature:Position<T>::Type
153 ..param.T:Type for which the position type is determined.
154 ...type:Class.Iter
155 ...type:Class.String
156 ..returns.param.Type:Position type of $T$.
157 ..see:Metafunction.Iterator
158 */
159 template <typename T>
160 struct Position
161 {
162         typedef typename Size<T>::Type Type;
163 };
164 template <typename T>
165 struct Position<T const>:
166         Position<T>
167 {
168 };
169
170 //____________________________________________________________________________
171
172 /**
173 .Metafunction.Host:
174 ..summary:Type of the object a given object depends on.
175 ..signature:Host<T>::Type
176 ..param.T:Type for which the host type is determined.
177 ..returns.param.Type:Host type of $T$.
178 */
179 template <typename T>
180 struct Host
181 {
182         typedef T Type;
183 };
184
185 //____________________________________________________________________________
186
187 /**
188 .Metafunction.Spec:
189 ..summary:The spec of a class. 
190 ..signature:Spec<T>::Type
191 ..param.T:Type for which the spec is determined.
192 ..returns.param.Type:Spec of $T$.
193 ..remarks:The spec of a SeqAn type is the class that is used in template subclassing 
194  to specify the specialization. 
195  For example, the spec of $String<char, Alloc<> >$ is $Alloc<>$.
196 */
197
198 // default case
199 template <typename T>
200 struct Spec {
201         typedef void Type;
202 };
203
204
205 // one argument case
206 template <template <typename> class T, typename TSpec>
207 struct Spec< T<TSpec> > {
208         typedef TSpec Type;
209 };
210
211 template <typename T>
212 struct Spec<T const>:
213         public Spec<T> {};
214
215 //____________________________________________________________________________
216
217 /**
218 .Metafunction.DeepestSpec:
219 ..summary:The deepest spec of a class with nested template arguments.
220 ..signature:DeepestSpec<T>::Type
221 ..param.T:Type for which the deepest spec is determined.
222 ..returns.param.Type:Deepest spec of $T$.
223 ..remarks:The spec of a SeqAn type is the innermost class that is used in nested subclassing.
224  For example, the deepest spec of $Iter<..., VSTree<BottomUp<MUMs> > >$ is $MUMs$.
225 */
226
227 // default case
228 template <typename T>
229 struct DeepestSpec {
230         typedef T Type;
231 };
232
233 // recursion for 1 argument
234 template <
235         template <typename> class T, 
236         typename T1 >
237 struct DeepestSpec< T<T1> > {
238         typedef typename 
239                 IF<
240                         TYPECMP<T1, void>::VALUE,                                                                               // is T1 void?
241                         T<T1>,                                                                                                                  // yes, end of recursion
242                         typename DeepestSpec< typename Spec< T<T1> >::Type >::Type              // no,  recurse
243                 >::Type Type;
244 };
245
246 // recursion for 2 arguments
247 template <
248         template <typename, typename> class T, 
249         typename T1, typename T2 >
250 struct DeepestSpec< T<T1,T2> >:
251         DeepestSpec< typename Spec< T<T1,T2> >::Type > {};
252
253 // recursion for 3 arguments
254 template <
255         template <typename, typename, typename> class T, 
256         typename T1, typename T2, typename T3 >
257 struct DeepestSpec< T<T1,T2,T3> >:
258         DeepestSpec< typename Spec< T<T1,T2,T3> >::Type > {};
259
260 // recursion for 4 arguments
261 template <
262         template <typename, typename, typename, typename> class T, 
263         typename T1, typename T2, typename T3, typename T4 >
264 struct DeepestSpec< T<T1,T2,T3,T4> >:
265         DeepestSpec< typename Spec< T<T1,T2,T3,T4> >::Type > {};
266
267 // recursion for 5 arguments
268 template <
269         template <typename, typename, typename, typename, typename> class T, 
270         typename T1, typename T2, typename T3, typename T4, typename T5 >
271 struct DeepestSpec< T<T1,T2,T3,T4,T5> >:
272         DeepestSpec< typename Spec< T<T1,T2,T3,T4,T5> >::Type > {};
273
274 template <typename T>
275 struct DeepestSpec<T const>:
276         public DeepestSpec<T> {};
277
278 //____________________________________________________________________________
279
280 /**
281 .Metafunction.Cargo:
282 ..summary:Type of additional data stored in an object. 
283 ..signature:Cargo<T>::Type
284 ..param.T:Type for which the cargo tyoe is determined.
285 ..returns.param.Type:Cargo of $T$.
286 ..remarks:The definition of Cargo allows the addition of user specific data to existing data structures.
287 */
288
289 template <typename T>
290 struct Cargo {
291         typedef Nothing Type;
292 };
293 template <typename T>
294 struct Cargo<T const> {
295         typedef typename Cargo<T>::Type const Type;
296 };
297
298 //____________________________________________________________________________
299
300 /**
301 .Metafunction.VertexDescriptor:
302 ..summary:Type of an object that represents a vertex descriptor.
303 ..signature:VertexDescriptor<T>::Type
304 ..param.T:Type T must be a graph. All graphs currently use ids as vertex descriptors.
305 ..returns.param.Type:VertexDescriptor type.
306 ..remarks.text:The vertex descriptor is a unique handle to a vertex in a graph.
307 It is used in various graph functions, e.g., to add edges, to create OutEdge Iterators or to remove a vertex.
308 It is also used to attach properties to vertices.
309 ..example.code:VertexDescriptor<Graph<> >::Type vD; //vD is a vertex descriptor
310 */
311
312 template <typename T>
313 struct VertexDescriptor {
314         typedef void* Type;
315 };
316 template <typename T>
317 struct VertexDescriptor<T const>:
318         public VertexDescriptor<T> {};
319
320
321 //____________________________________________________________________________
322
323         
324 /**
325 .Metafunction.Id:
326 ..summary:Type of an object that represents an id.
327 ..signature:Id<T>::Type
328 ..param.T:Type for which a suitable id type is determined.
329 ..returns.param.Type:Id type.
330 ..remarks.text:The id type of a container is the type that is used to uniquely identify its elements.
331 In most cases this type is unsigned int.
332 ..example.code:Id<Graph<> >::Type id; //id has type unsigned int
333 */
334 template<typename T>
335 struct Id {
336         typedef unsigned int Type;
337 };
338
339 //____________________________________________________________________________
340
341 template<typename T>
342 struct Id<T const> {
343         typedef unsigned int Type;
344 };
345
346 //____________________________________________________________________________
347
348 /**
349 .Metafunction.Key:
350 ..summary:Key type of a key to cargo mapping.
351 ..signature:Key<T>::Type
352 ..param.T:Type for which a key type is determined.
353 ..returns.param.Type:Key type.
354 ...default:The type $T$ itself.
355 */
356 template< typename T >
357 struct Key
358 {
359         typedef T Type;
360 };
361
362 template <typename T>
363 struct Key<T const>:
364         Key<T> {};
365
366 //____________________________________________________________________________
367
368 /*VERALTET
369 .Metafunction.Object:
370 ..summary:Object type of a key to object mapping.
371 ..signature:Object<T>::Type
372 ..param.T:Type for which a object type is determined.
373 ..returns.param.Type:Object type.
374 */
375
376 template<typename T>
377 struct Object; 
378
379 template <typename T>
380 struct Object<T const>:
381         Object<T> {};
382
383
384 //____________________________________________________________________________
385
386 /**
387 .Metafunction.Source
388 */
389
390 template < typename TSpec = void >
391 struct Source
392 {
393         typedef TSpec Type;
394 };
395
396 template <typename T>
397 struct Source<T const>:
398         Source<T>
399 {
400 };
401
402 //____________________________________________________________________________
403
404 /**
405 .Internal._Parameter:
406 ..cat:Metafunctions
407 ..summary:Type for function parameters and return values.
408 ..signature:_Parameter<T>::Type
409 ..param.T:A type.
410 ..returns.param.Type:The parameter type for arguments of type $T$.
411 ...text:If $T$ is a pointer or array type, then $_Parameter<T>::Type$ is $T$, 
412 otherwise $_Parameter<T>::Type$ is $T &$.
413 */
414 template <typename T>
415 struct _Parameter
416 {
417         typedef T & Type;
418 };
419
420 template <typename T>
421 struct _Parameter<T *>
422 {
423         typedef T * Type;
424 };
425 template <typename T, size_t I>
426 struct _Parameter<T [I]>
427 {
428         typedef T * Type;
429 };
430
431
432 /**
433 .Internal._toParameter:
434 ..cat:Functions
435 ..summary:Transforms pointers to parameter types.
436 ..signature:_toParameter<T>(pointer)
437 ..param.pointer:A pointer.
438 ..param.T:A Type.
439 ...text:$object$ is transformed into the parameter type of $T$ that is given by @Internal._Parameter@.
440 ...note:This type must be explicitely specified.
441 ..returns:To $TParameter$ transformed $object$.
442 ..see:Internal._Parameter
443 */
444 template <typename T>
445 typename _Parameter<T>::Type
446 _toParameter(T * _object)
447 {
448 SEQAN_CHECKPOINT
449         return * _object;
450 }
451 template <typename T>
452 typename _Parameter<T>::Type
453 _toParameter(T _object)
454 {
455 SEQAN_CHECKPOINT
456         return _object;
457 }
458
459 //____________________________________________________________________________
460
461 /**
462 .Internal._ConstParameter:
463 ..cat:Metafunctions
464 ..summary:Type for constant function parameters and return values.
465 ..signature:_ConstParameter<T>::Type
466 ..param.T:A type.
467 ..returns.param.Type:The const parameter type for arguments of type $T$.
468 ...text:If $T$ is a pointer or array type, then $_Parameter<T>::Type$ is a pointer to a const array, 
469 otherwise $_Parameter<T>::Type$ is $T const &$.
470 ..see:Internal._Parameter
471 */
472 template <typename T>
473 struct _ConstParameter
474 {
475         typedef T const & Type;
476 };
477 template <typename T>
478 struct _ConstParameter<T const>:
479         public _ConstParameter<T> {};
480
481 template <typename T>
482 struct _ConstParameter<T *>
483 {
484         typedef T const * Type;
485 };
486 template <typename T>
487 struct _ConstParameter<T const *>
488 {
489         typedef T const * Type;
490 };
491
492 template <typename T, size_t I>
493 struct _ConstParameter<T [I]>
494 {
495         typedef T const * Type;
496 };
497 template <typename T, size_t I>
498 struct _ConstParameter<T const [I]>
499 {
500         typedef T const * Type;
501 };
502
503 //____________________________________________________________________________
504
505 /**
506 .Internal._Pointer:
507 ..cat:Metafunctions
508 ..summary:The associated pointer type.
509 ..signature:_Pointer<T>::Type
510 ..param.T:A type.
511 ..returns.param.Type:A pointer type for $T$.
512 ...text:if $T$ is already a pointer type, then $_Pointer<T>::Type$ is $T$,
513 otherwise $_Pointer<T>::Type$ is $T *$.
514 ..see:Internal._Parameter
515 ..see:Internal._toParameter
516 */
517 template <typename T>
518 struct _Pointer
519 {
520         typedef T * Type;
521 };
522
523 template <typename T>
524 struct _Pointer<T *>
525 {
526         typedef T * Type;
527 };
528 template <typename T>
529 struct _Pointer<T * const>
530 {
531         typedef T * const Type;
532 };
533
534 template <typename T, size_t I>
535 struct _Pointer<T [I]>
536 {
537         typedef T * Type;
538 };
539
540 /**
541 .Internal._toPointer:
542 ..cat:Functions
543 ..summary:Transforms types into pointers.
544 ..signature:_toPointer(object)
545 ..param.object:An object.
546 ..returns:$object$, transformed to a pointer. 
547 ...text:The type of the returned pointer is given by @Internal._Pointer@.
548 ..see:Internal._Pointer
549 */
550 template <typename T>
551 typename _Pointer<T>::Type
552 _toPointer(T & _object)
553 {
554 SEQAN_CHECKPOINT
555         return & _object;
556 }
557 template <typename T>
558 typename _Pointer<T const>::Type
559 _toPointer(T const & _object)
560 {
561 SEQAN_CHECKPOINT
562         return & _object;
563 }
564
565 template <typename T>
566 typename _Pointer<T *>::Type
567 _toPointer(T * _object)
568 {
569 SEQAN_CHECKPOINT
570         return _object;
571 }
572
573 //____________________________________________________________________________
574
575
576 /**
577 .Metafunction.LENGTH:
578 ..summary:Number of elements in a fixed-size container.
579 ..signature:LENGTH<T>::Type
580 ..param.T:Type for which the number of elements is determined.
581 ..returns.param.VALUE:Number of elements.
582 ..remarks.text:The default return value is 1 for dynamic-size containers.
583 */
584 template <typename T>
585 struct LENGTH
586 {
587         enum { VALUE = 1 };
588 };
589 template <typename T>
590 struct LENGTH<T const>:
591         LENGTH<T>
592 {
593 };
594
595 /**
596 .Metafunction.WEIGHT:
597 ..summary:Number of relevant positions in a shape.
598 ..signature:WEIGHT<T>::Type
599 ..param.T:Shape type for which the number of relevant positions is determined.
600 ...type:Class.Shape
601 ..returns.param.VALUE:Number of relevant positions.
602 ..remarks.text:The default return value is the result of the @Metafunction.LENGTH@ function.
603 For gapped shapes this is the number of '1's.
604 */
605 template <typename T>
606 struct WEIGHT:
607         LENGTH<T>
608 {
609 };
610 template <typename T>
611 struct WEIGHT<T const>:
612         WEIGHT<T>
613 {
614 };
615
616 //////////////////////////////////////////////////////////////////////////////
617
618 //Iterator: see basic_iterator.h
619
620 //////////////////////////////////////////////////////////////////////////////
621
622 }// namespace SEQAN_NAMESPACE_MAIN
623
624 #endif //#ifndef SEQAN_HEADER_...