The Fib multimedia system
Fib is a system for storing multimedia data (like images or films).
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
fibDatatyps.h
Go to the documentation of this file.
1 /**
2  * @file fibDatatyps
3  * file name: fibDatatyps.h
4  * @author Betti Oesterholz
5  * @date 17.04.2009
6  * @mail webmaster@BioKom.info
7  *
8  * System: C++
9  *
10  * This header specifies the primitiv datatyps for fib.
11  * Copyright (C) @c LGPL3 2009 Betti Oesterholz
12  *
13  * This program is free software: you can redistribute it and/or modify
14  * it under the terms of the GNU Lesser General Public License (LGPL) as
15  * published by the Free Software Foundation, either version 3 of the
16  * License, or any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  * GNU Lesser General Public License for more details.
22  *
23  * You should have received a copy of the GNU Lesser General Public License
24  * along with this program. If not, see <http://www.gnu.org/licenses/>.
25  *
26  *
27  * This file contains the basic datatyps for fib.
28  * The main reason for Fib specific datatyps is: that the Fib datatyps
29  * can be changed easily with this file.
30  *
31  * The basic datatyps for Fib are defined as the basic datatyps of C++.
32  *
33  */
34 /*
35 History:
36 17.04.2009 Oesterholz created
37 16.01.2011 Oesterholz functions isEqualNull(), compareDouble() and
38  compareVectorDouble() added
39 22.01.2011 Oesterholz functions add(), sub(), addToFirst() and
40  subFromFirst() added
41 13.10.2011 Oesterholz functions getListElement() added
42 13.10.2011 Oesterholz min() function replaced
43 */
44 
45 
46 #ifndef ___FIB_DATATYPS_H__
47 #define ___FIB_DATATYPS_H__
48 
49 
50 #include "version.h"
51 
52 #include <cstdlib>
53 #include <vector>
54 #include <list>
55 
56 
57 //#define REALLY_SMALL_DOUBLE_DIFFERENCE 0.000000000001
58 #define REALLY_SMALL_DOUBLE_DIFFERENCE 0.0000000000001
59 #define REALLY_SMALL_FLOAT_DIFFERENCE 0.000001
60 
61 
62 
63 #define intFib int
64 #define longFib long long
65 #define doubleFib double
66 #define unsignedIntFib unsigned int
67 #define unsignedLongFib unsigned long long
68 
69 namespace fib{
70 
71  /**
72  * This function rounds an double value to an longFib value.
73  *
74  * @param value the value to round
75  * @return the rounded value
76  */
77  template <class Type> longFib roundToLongFib( const Type &value ){
78 
79  if( 0 <= value){
80  return (longFib) (value + 0.5);
81  }else{
82  return (longFib) (value - 0.5);
83  }
84  }
85 
86  /**
87  * This function evalues the maximum of two values.
88  *
89  * @param value1 the first value
90  * @param value2 the second value
91  * @return the maximum of the first and the second value
92  */
93  template <class Type> Type max( const Type &value1, const Type &value2 ){
94 
95  if( value1 < value2 ){
96  return value2;
97  }else{
98  return value1;
99  }
100  }
101 
102  /**
103  * This returns the ulElementNumber'th element from an given list.
104  *
105  * @param liList the list from wher to return the list iterator
106  * @param ulElementNumber the number of the list element to return
107  * @return an iterator to the ulElementNumber'th list element
108  */
109  template <class Type> typename std::list< Type >::iterator getListElement(
110  std::list< Type > & liList, unsigned long ulElementNumber ){
111 
112  if ( ( ulElementNumber < 1 ) || ( liList.size() < ulElementNumber ) ){
113  return liList.end();
114  }
115  typename std::list< Type >::iterator itrList = liList.begin();
116  for ( ; 1 < ulElementNumber; ulElementNumber--, itrList++ ){
117  //nothing to do
118  }
119  return itrList;
120  }
121 
122 
123  /**
124  * This returns the ulElementNumber'th element from an given list.
125  *
126  * @param liList the list from wher to return the list iterator
127  * @param ulElementNumber the number of the list element to return
128  * @return an iterator to the ulElementNumber'th list element
129  */
130  template <class Type> typename std::list< Type >::const_iterator getListElement(
131  const std::list< Type > & liList, unsigned long ulElementNumber ){
132 
133  if ( ( ulElementNumber < 1 ) || ( liList.size() < ulElementNumber ) ){
134  return liList.end();
135  }
136  typename std::list< Type >::const_iterator itrList = liList.begin();
137  for ( ; 1 < ulElementNumber; ulElementNumber--, itrList++ ){
138  //nothing to do
139  }
140  return itrList;
141  }
142 
143 
144  /**
145  * This function evalues the digits needed to store a value as an
146  * natural number.
147  *
148  * @param llValue the value to evalue the digits for
149  * @return the digits needed to store a value as an natural number
150  */
151  unsigned int getDigits( unsigned long long llValue );
152 
153 
154  /**
155  * Decompses a doubleFib number into it's mantissa and exponent part.
156  *
157  * @param dNumber the number to decompose
158  * @param lMantissa a pointer to the longFib field wher the mantissa
159  * should be stored
160  * @param lExponent a pointer to the longFib field wher the exponent
161  * should be stored
162  * @param iSizeMantissa a pointer to the intFib field wher the size of
163  * the mantissa in bits should be stored or NULL if it shouldn't be
164  * stored
165  * @param iSizeExponent a pointer to the intFib field wher the size of
166  * the exponent in bits should be stored or NULL if it shouldn't be
167  * stored
168  */
169  void decomposeDoubleFib( const doubleFib dNumber,
170  longFib * lMantissa, longFib * lExponent,
171  intFib * iSizeMantissa = NULL, intFib * iSizeExponent = NULL );
172 //TODO NULL not possible intFib * iSizeMantissa = NULL, intFib * iSizeExponent = NULL );
173 
174  /**
175  * This function rounds the given number of bits ulNumberOfBits up
176  * to a full number of byte;
177  *
178  * @param ulNumberOfBits the number of bits to round
179  * @return the, to full byte rounded up, number of bits
180  */
181  unsigned long roundUpToFullByte( unsigned long ulNumberOfBits );
182 
183  /**
184  * This function evalues the greatest common divisor of the two given
185  * numbers.
186  *
187  * @param dValue1 the first number
188  * @param dValue2 the second number
189  * @return the greatest common divisor of the two given numbers dValue1
190  * and dValue2
191  */
192  doubleFib gcd( doubleFib dValue1, doubleFib dValue2 );
193 
194  /**
195  * @param dValue the value, for which to evalue the positiv value
196  * @return the positive value for the given number dValue
197  */
198  float absF( const float & dValue );
199 
200  /**
201  * @param dValue the value, for which to evalue the positiv value
202  * @return the positive value for the given number dValue
203  */
204  double absF( const double & dValue );
205 
206  /**
207  * This functions checks if the given double number is equal 0.
208  * Really small differences will be ignored.
209  *
210  * @param dValue the number to check
211  * @return true if the given number is equal 0, else false
212  */
213  bool isEqualNull( const double & dValue );
214 
215  /**
216  * This functions checks if the given float number is equal 0.
217  * Really small differences will be ignored.
218  *
219  * @param dValue the number to check
220  * @return true if the given number is equal 0, else false
221  */
222  bool isEqualNull( const float & dValue );
223 
224  /**
225  * This functions compares two double numbers.
226  * Really small differences will be ignored.
227  *
228  * @param dValue1 the first number to compare
229  * @param dValue2 the second number to compare
230  * @return true if the first number is equal to the second, else false
231  */
232  bool compareDouble( const double & dValue1, const double & dValue2 );
233 
234  /**
235  * This functions compares two float numbers.
236  * Really small differences will be ignored.
237  *
238  * @param dValue1 the first number to compare
239  * @param dValue2 the second number to compare
240  * @return true if the first number is equal to the second, else false
241  */
242  bool compareDouble( const float & dValue1, const float & dValue2 );
243 
244 
245  /**
246  * This functions compares two vectors with double numbers.
247  * Really small differences betwean the vector element numbers will be ignored.
248  *
249  * @param vector1 the first vector to compare
250  * @param vector2 the second vector to compare
251  * @return true if the first vector is equal to the second, else false
252  */
253  template<class tDouble>
254  bool compareVectorDouble( const std::vector< tDouble > & vector1,
255  const std::vector< tDouble > & vector2 ){
256 
257  if ( vector1.size() != vector2.size() ){
258  //not the same number of elements -> vectors not equal
259  return false;
260  }
261  const size_t uiNumberOfElements = vector1.size();
262 
263  for ( size_t uiActualElement = 0; uiActualElement < uiNumberOfElements;
264  uiActualElement++ ){
265 
266  if ( ! fib::compareDouble( vector1[ uiActualElement ], vector2[ uiActualElement ] ) ){
267  //actual vector element not equal -> vectors not equal
268  return false;
269  }
270  }//else all vector elements equal
271  return true;
272  }
273 
274 
275  /**
276  * This functions adds two floating point numbers.
277  * Really small differences will be ignored, this means:
278  * If one number is allmost equal to the negativ of the other the result is 0.
279  *
280  * @param dValue1 the first number to add
281  * @param dValue2 the second number to add
282  * @return the sum of the two given values
283  */
284  template<class tDouble>
285  tDouble add( const tDouble & dValue1, const tDouble & dValue2 ){
286 
287  if ( compareDouble( dValue1, ((tDouble)(-1)) * dValue2 ) ){
288  //the sum is 0
289  return ((tDouble)(0));
290  }//else
291  return dValue1 + dValue2;
292  }
293 
294 
295  /**
296  * This functions subtracts two floating point numbers.
297  * Really small differences will be ignored, this means:
298  * If one number is allmost equal to the other the result is 0.
299  *
300  * @param dValue1 the minuend of the subtraction
301  * @param dValue2 the subtrahend of the subtraction
302  * @return the difference of the two given values
303  */
304  template<class tDouble>
305  tDouble sub( const tDouble & dValue1, const tDouble & dValue2 ){
306 
307  if ( compareDouble( dValue1, dValue2 ) ){
308  //the sum is 0
309  return ((tDouble)(0));
310  }//else
311  return dValue1 - dValue2;
312  }
313 
314 
315  /**
316  * This functions adds two floating point numbers.
317  * Really small differences will be ignored, this means:
318  * If one number is allmost equal to the negativ of the other the result is 0.
319  *
320  * @param dValue1 the first number to add, the sum will also be stored
321  * in this variable
322  * @param dValue2 the second number to add
323  * @return the sum of the two given values
324  */
325  template<class tDouble>
326  tDouble addToFirst( tDouble & dValue1, const tDouble & dValue2 ){
327 
328  if ( compareDouble( dValue1, ((tDouble)(-1)) * dValue2 ) ){
329  //the sum is 0
330  dValue1 = ((tDouble)(0));
331  return ((tDouble)(0));
332  }//else
333  dValue1 += dValue2;
334  return dValue1;
335  }
336 
337 
338  /**
339  * This functions subtracts two floating point numbers.
340  * Really small differences will be ignored, this means:
341  * If one number is allmost equal to the other the result is 0.
342  *
343  * @param dValue1 the minuend of the subtraction, the difference will
344  * also be stored in this variable
345  * @param dValue2 the subtrahend of the subtraction
346  * @return the difference of the two given values
347  */
348  template<class tDouble>
349  tDouble subFromFirst( tDouble & dValue1, const tDouble & dValue2 ){
350 
351  if ( compareDouble( dValue1, dValue2 ) ){
352  //the sum is 0
353  dValue1 = ((tDouble)(0));
354  return ((tDouble)(0));
355  }//else
356  dValue1 -= dValue2;
357  return dValue1;
358  }
359 
360 
361  /**
362  * This functions adds two floating point number vectors.
363  * Really small differences will be ignored, this means:
364  * If one number is allmost equal to the negativ of the other the result is 0.
365  *
366  * @param vecValue1 the first vector to add, the sum will also be stored
367  * in this variable
368  * @param vecValue2 the second vector to add
369  * @return the sum of the two given vectors
370  */
371  template<class tDouble>
372  std::vector< tDouble > addToFirst( std::vector< tDouble > & vecValue1,
373  const std::vector< tDouble > & vecValue2 ){
374 
375  /*const size_t uiNumberOfElements = min(
376  vecValue1.size(), vecValue2.size() );*/
377  const size_t uiVecValue1Size = vecValue1.size();
378  const size_t uiVecValue2Size = vecValue2.size();
379  const size_t uiNumberOfElements = ( uiVecValue1Size < uiVecValue2Size )?
380  uiVecValue1Size : uiVecValue2Size ;
381 
382  for ( size_t uiActualElement = 0; uiActualElement < uiNumberOfElements;
383  uiActualElement++ ){
384 
385  addToFirst( vecValue1[ uiActualElement ], vecValue2[ uiActualElement ] );
386  }
387  return vecValue1;
388  }
389 
390  /**
391  * This functions multiplys a floating point number vector with a number.
392  *
393  * @param vecValue the vector to multiply with the number, the product
394  * will also be stored in this variable
395  * @param dValue the value to multiply the vector with
396  * @return the product of the given vector and the value
397  */
398  template<class tDouble>
399  std::vector< tDouble > multToFirst( std::vector< tDouble > & vecValue,
400  const tDouble & dValue ){
401 
402  const size_t uiNumberOfElements = vecValue.size();
403 
404  for ( size_t uiActualElement = 0; uiActualElement < uiNumberOfElements;
405  uiActualElement++ ){
406 
407  vecValue[ uiActualElement ] *= dValue;
408  }
409  return vecValue;
410  }
411 
412 
413 }//end namespace fib
414 
415 
416 #endif //___FIB_DATATYPS_H__
417 
418 
419 
420 
421 
422 
423 
424 
425