5.8.5.3.1 : Le fichier header
Écrivons le fichier temporary_alloc.h :
Nous commençons par la classique définition de macro pour éviter les inclusions multiples de notre header :
1 2 3 4 |
#ifndef __TEMPORARY_ALLOC_H__ #define __TEMPORARY_ALLOC_H__ #include <string.h> |
Ensuite nous incluons le header qui nous permettra de manipuler des vector :
1 2 |
#include <vector> #include <iostream> |
Nous ajoutons une petite fonction template bonus pour interchanger des valeurs (on les utilisera plus tard avec des pointeurs) :
1 2 3 4 5 6 7 8 9 10 |
///Swap two values /** @param[out] a : value will be b * @param[out] b : value will be a */ template<typename T> void swapValue(T & a, T & b){ T c(a); a = b; b = c; } |
Nous définissons le prototype de notre fonction d'initialisation de temporaires. Ils sont très utiles pour éviter les allocations redondantes et inutile dans un programme. Nous passons donc 4 temporaires la matrice U entrée/sortie et la la matrice V entrée/sortie.
Nous passons 4 pointeur car ils seront simple à interchanger et 4 PTensor car ils géreront la désallocation pour nous.
Enfin nous passons la taille des images temporaires (nbRow, nbCol) :
1 2 |
void allocate_temporary(std::vector<float> & tmpInU, std::vector<float> & tmpInV, std::vector<float> & tmpOutU, std::vector<float> & tmpOutV, size_t nbRow, size_t nbCol); |
Finalement nous fermons la condition du preprocesseur pour éviter les inclusions multiples de notre header :
1 |
#endif
|
Le fichier temporary_alloc.h complet :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
/*************************************** Auteur : Pierre Aubert Mail : pierre.aubert@lapp.in2p3.fr Licence : CeCILL-C ****************************************/ #ifndef __TEMPORARY_ALLOC_H__ #define __TEMPORARY_ALLOC_H__ #include <string.h> #include <vector> #include <iostream> ///Swap two values /** @param[out] a : value will be b * @param[out] b : value will be a */ template<typename T> void swapValue(T & a, T & b){ T c(a); a = b; b = c; } void allocate_temporary(std::vector<float> & tmpInU, std::vector<float> & tmpInV, std::vector<float> & tmpOutU, std::vector<float> & tmpOutV, size_t nbRow, size_t nbCol); #endif |
Vous pouvez le télécharger ici.