Commit patch to not break on spaces.
[bowtie.git] / SeqAn-1.1 / seqan / basic / basic_definition.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_definition.h,v 1.1 2008/08/25 16:20:01 langmead Exp $
19  ==========================================================================*/
20
21 #ifndef SEQAN_HEADER_BASIC_DEFINITION_H
22 #define SEQAN_HEADER_BASIC_DEFINITION_H
23
24 namespace SEQAN_NAMESPACE_MAIN
25 {
26
27 //////////////////////////////////////////////////////////////////////////////
28
29 template <typename T>
30 struct Tag
31 {
32 };
33
34 //////////////////////////////////////////////////////////////////////////////
35 /**
36 .Tag.Default:
37 ..summary:Tag that specifies default behavior.
38 ..tag.Default:Use default behavior. 
39 */
40 struct Default_;
41 typedef Tag<Default_> const Default;
42
43 //////////////////////////////////////////////////////////////////////////////
44 /**
45 .Tag.Move Switch:
46 ..summary:Switch to force move.
47 ..tag.Move:Move instead of assign. 
48 ..remarks.text:The difference between move constructor and copy constructor
49 is that the source object is not copied but moved into the target object.
50 The source object can lose its content and will be empty after
51 this operation in this case.
52 A move constructor can sigificantly faster than a copy constructor.
53 ..example.code:String source("hello");
54 String target(source, Move()); // source is moved to target
55 std::cout << source; //nothing printed since source lost content
56 std::cout << target; //"hello"
57 ..see:Function.move
58 */
59
60 struct Move_;
61 typedef Tag<Move_> const Move;
62
63 //////////////////////////////////////////////////////////////////////////////
64
65 //Pass to c'tor of iterator to move it to the end
66 struct GoEnd_;
67 typedef Tag<GoEnd_> const GoEnd;
68
69
70 //////////////////////////////////////////////////////////////////////////////
71
72 //construct without initializing
73 struct MinimalCtor_;
74 typedef Tag<MinimalCtor_> const MinimalCtor;
75
76 //construct with initializing
77 struct NonMinimalCtor_;
78 typedef Tag<NonMinimalCtor_> const NonMinimalCtor;
79
80 //////////////////////////////////////////////////////////////////////////////
81 /**
82 .Tag.Logical Values:
83 ..summary:Tag that represents true and false.
84 ..tag.True:The logical value "true".
85 ..tag.False:The logical value "false".
86 */
87 struct True { enum { VALUE = true }; };
88 struct False { enum { VALUE = false }; };
89
90
91 //////////////////////////////////////////////////////////////////////////////
92
93 /**
94 .Tag.Nothing:
95 ..summary:Tag that represents an absent parameter or an absent type.
96 ..tag.Nothing:Omit parameter.
97 */
98 ///Empty Data Class.
99 struct Nothing {};
100
101
102
103 //////////////////////////////////////////////////////////////////////////////
104 // returns TTo const, if TFrom is const, TTo otherwise
105
106 template <typename TFrom, typename TTo>
107 struct _CopyConst
108 {
109         typedef TTo Type;
110 };
111 template <typename TFrom, typename TTo>
112 struct _CopyConst<TFrom const, TTo>
113 {
114         typedef TTo const Type;
115 };
116
117 //////////////////////////////////////////////////////////////////////////////
118
119 /**
120 .Internal._RemoveConst:
121 ..signature:_RemoveConst<T>
122 ..returns:$t$ if $T$ is $t const$, otherwise $T$.
123 */
124 template <typename T>
125 struct _RemoveConst
126 {
127         typedef T Type;
128 };
129 template <typename T>
130 struct _RemoveConst<T const>:
131         public _RemoveConst<T> {};
132
133 template <typename T>
134 struct _RemoveConst<T &>
135 {
136         typedef typename _RemoveConst<T>::Type & Type;
137 };
138 template <typename T>
139 struct _RemoveConst<T *>
140 {
141         typedef typename _RemoveConst<T>::Type * Type;
142 };
143 template <typename T, size_t I>
144 struct _RemoveConst<T const [I]>
145 {
146         typedef T * Type;
147 };
148
149 //////////////////////////////////////////////////////////////////////////////
150 /**
151 .Internal._MakeUnsigned:
152 ..signature:_MakeUnsigned<T>
153 ..returns:$unsigned t$ if $T$ is not $unsigned t$, otherwise $T$.
154 */
155 template <typename T>
156 struct _MakeUnsigned
157 {
158         typedef T Type;
159 };
160
161 template <typename T>
162 struct _MakeUnsigned<T const> {
163         typedef typename _MakeUnsigned<T>::Type const Type;
164 };
165
166 template <>
167 struct _MakeUnsigned<char>
168 {
169         typedef unsigned char Type;
170 };
171
172 template <>
173 struct _MakeUnsigned<signed char>
174 {
175         typedef unsigned char Type;
176 };
177
178 template <>
179 struct _MakeUnsigned<int>
180 {
181         typedef unsigned int Type;
182 };
183
184 template <>
185 struct _MakeUnsigned<short>
186 {
187         typedef unsigned short Type;
188 };
189
190 template <>
191 struct _MakeUnsigned<long>
192 {
193         typedef unsigned long Type;
194 };
195
196 /*
197 template <>
198 struct _MakeUnsigned<long long>
199 {
200         typedef unsigned long long Type;
201 };
202 */
203
204 //////////////////////////////////////////////////////////////////////////////
205 /**
206 .Internal._MakeSigned:
207 ..signature:_MakeSigned<T>
208 ..returns:$signed t$ if $T$ is not $signed t$, otherwise $T$.
209 */
210 template <typename T>
211 struct _MakeSigned
212 {
213         typedef T Type;
214 };
215
216 template <typename T>
217 struct _MakeSigned<T const> {
218         typedef typename _MakeSigned<T>::Type const Type;
219 };
220
221 template <>
222 struct _MakeSigned<char>
223 {
224         typedef signed char Type;
225 };
226
227 template <>
228 struct _MakeSigned<unsigned char>
229 {
230         typedef signed char Type;
231 };
232
233 template <>
234 struct _MakeSigned<unsigned int>
235 {
236         typedef signed int Type;
237 };
238
239 template <>
240 struct _MakeSigned<unsigned short>
241 {
242         typedef signed short Type;
243 };
244
245 template <>
246 struct _MakeSigned<unsigned long>
247 {
248         typedef signed long Type;
249 };
250
251 /*
252 template <>
253 struct _MakeSigned<unsigned long long>
254 {
255         typedef signed long long Type;
256 };
257 */
258
259 //////////////////////////////////////////////////////////////////////////////
260 /**
261 .Internal._ClassIdentifier:
262 ..signature:void * _ClassIdentifier<T>::getID()
263 ..returns:A void * that identifies $T$.
264 ...text:The returned values of two calls of $getID$ are equal if and only if
265 the used type $T$ was the same.
266 */
267 template <typename T>
268 struct _ClassIdentifier
269 {
270         static inline void *
271         getID()
272         {
273 SEQAN_CHECKPOINT
274                 static bool _id_dummy;
275                 return &_id_dummy;
276         }
277 };
278
279 //////////////////////////////////////////////////////////////////////////////
280 /**
281 .Function.log2:
282 ..cat:Miscellaneous
283 ..summary:Computes logarithm of base 2 for integer types
284 ..signature:unsigned int log2(i)
285 ..param.i:An integer type.
286 ..returns:The largest integer smaller or equal than
287 the logarithm of $i$.
288 */
289
290 #if 0
291 template <int BITS_MAX>
292 struct _Log2_Impl
293 {
294         template <typename T>
295         static inline unsigned int
296         log2(T val, unsigned int offset)
297         {
298                 unsigned int val2 = val >> (BITS_MAX / 2);
299                 if (val2)
300                 {
301                         val = val2;
302                         offset += BITS_MAX / 2;
303                 }
304                 return _Log2_Impl<BITS_MAX / 2>::log2(val, offset);
305         }
306 };
307
308 template <>
309 struct _Log2_Impl<1>
310 {
311         template <typename T>
312         static inline unsigned int
313         log2(T /*val*/, unsigned int offset)
314         {
315                 return offset;
316         }
317 };
318
319
320 template <typename T>
321 inline unsigned int
322 log2(T val)
323 {
324         enum
325         {
326 //              BITS_PER_VALUE = BitsPerValue<T>::VALUE //TODO???
327                 BITS_PER_VALUE = sizeof(T) * 8
328         };
329
330         return _Log2_Impl<BITS_PER_VALUE>::log2(val, 0);
331 }
332 #endif
333
334 template <typename TValue, typename TExponent>
335 inline TValue _intPow(TValue a, TExponent b)
336 {
337 SEQAN_CHECKPOINT
338         TValue ret = 1;
339         while (b != 0)
340         {
341                 if (b & 1) ret *= a;
342                 a *= a;
343                 b >>= 1;
344         }       
345         return ret;
346 }
347
348 //////////////////////////////////////////////////////////////////////////////
349 // to avoid conflicts with non-standard macros and namespaces
350 // we define our own Min/Max functions
351
352 template<typename _Tx> inline
353 const _Tx& _min(const _Tx& _Left, const _Tx& _Right)
354 {       // return smaller of _Left and _Right
355         if (_Left < _Right)
356                 return _Left;
357         else
358                 return _Right;
359 }
360
361 template<typename _Tx, typename _Ty> inline
362 _Tx _min(const _Tx& _Left, const _Ty& _Right)
363 {       // return smaller of _Left and _Right
364     return (_Right < _Left ? _Right : _Left);
365 }
366
367 template<typename _Ty> inline
368 const _Ty& _max(const _Ty& _Left, const _Ty& _Right)
369 {       // return larger of _Left and _Right
370         if (_Left < _Right)
371                 return _Right;
372         else
373                 return _Left;
374 }
375
376 //////////////////////////////////////////////////////////////////////////////
377
378 template <typename T1, typename T2>
379 struct _IsSameType
380 {
381         enum {VALUE = false};
382         typedef False Type;
383 };
384
385 template <typename T>
386 struct _IsSameType<T, T>
387 {
388         enum {VALUE = true};
389         typedef True Type;
390 };
391
392 template <typename T1, typename T2>
393 inline bool 
394 _isSameType()
395 {
396         return _IsSameType<T1, T2>::VALUE;
397 }
398
399 //////////////////////////////////////////////////////////////////////////////
400
401 } //namespace SEQAN_NAMESPACE_MAIN
402
403 #endif //#ifndef SEQAN_HEADER_...
404
405