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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258
|
/**
* \file util.c
* \version 0.2
* \date 20 mai 2009
*
*/
#include "util.h"
#include <dirent.h>
/**
* \fn void check_pointer(void * t)
* \brief Verify the correct allocation of a table (pointer)
* \param t new allocated table (pointer), can't be null
*/
void check_pointer(void * t)
{
printf("debug dans check pointer\n");
// printf ("%d\n",&t);
if ( t == NULL )
{
printf("not enough memory");
exit (1);
}
}
/**
* \fn int * reallocate_table(int *table, int n)
* \brief Reallocate a table of integer
* \param table table to reallocate
* \param n new size of t&he table
* \return the reallocated table
*/
int * reallocate_table(int *table, int n)
{
printf("debug dans realloc table\n");
int *temp = realloc ( table, (n * sizeof(int)) );
check_pointer(temp);
return temp;
}
/**
* \fn int ** reallocate_matrix(int **m, int new_line_size, int new_row_size)
* \brief Reallocate a matrix of integer
* \param m the matrix to reallocate
* \param old_line_size the old number of lines of the matrix
* \param new_line_size the new number of lines of the matrix
* \param new_row_size the new number of rows of the matrix
* \return the reallocated matrix
*/
void reallocate_matrix(int ***m, int old_line_size, int new_line_size, int new_row_size)
{
int ** temp =realloc((*m), new_line_size * sizeof(int*) );
printf("debug dans reallocate matrix %d \n",temp[0][0]);
printf("*************************************\n");
printf("old line size = %d\n",old_line_size);
for (int i = 1; i < old_line_size; i++)
{
for(int j = 0; j < 5; j++)
{
printf("%d ",*m[i][j]);
}
printf("\n");
}
printf("*************************************\n");
// check_pointer(temp);
for (int i = old_line_size; i < new_line_size; i++)
{
temp[i] = calloc( new_row_size, sizeof(int) );
printf("hello\n");
check_pointer(temp[i]);
}
temp[1][0]=1;
// *m = temp;
// *m = &temp;
m=temp;
printf("debug dans reallocate matrix %d \n",*m[1][0]);
printf("*************************************\n");
printf("new line size = %d\n",new_line_size);
for (int i = 1; i < new_line_size; i++)
{
for(int j = 0; j < 5; j++)
{
printf("%d ",temp[i][j]);
// printf("%d ",*m[i][j]);
}
printf("ligne : %d \n",i);
}
for (int i = 1; i < new_line_size; i++)
{
for(int j = 0; j < 5; j++)
{
// printf("%d ",temp[i][j]);
printf("%d ",*m[i][j]);
}
printf("ligne : %d \n",i);
}
printf("*************************************\n");
printf("\n");
// printf("adresse 1 %d\n",&m);
printf ("bjour\n");
// return m;
// return temp;
}
/**
* \fn void free_matrix (int ** matrix, int size)
* \brief free a matrix
* \param matrix matrix to free
* \param line_number a line of the matrix
* \param new_line_size new size of the matrix line
*/
int ** expand_matrix_line(int ** m, int line_number, int new_line_size)
{
printf("debug dans epand matrix\n");
int ** temp = m;
temp[line_number] = realloc ( m[line_number], ((new_line_size) * sizeof(int)) );
check_pointer(temp[line_number]);
return temp;
}
/**
* \fn void free_matrix (int ** matrix, int size)
* \brief free a matrix
* \param matrix matrix to free
* \param size number of matrix line
*/
void free_matrix (int ** matrix, int size)
{
printf("debug dans free matrix\n");
for ( int i = 0; i < size; i++ )
{
free(matrix[i]);
}
free(matrix);
}
/**
* \fn void copy_table(int * temp, int temp_size, int ** result, int result_size)
* \brief Copy the value of table "temp" in the matrice "result" (last line)
* \param temp source table
* \param temp_size size of the table to copy
* \param result destination matrix
* \param result_size number of line of the matrix
* \return a matrix
*/
void copy_table(int *temp, int temp_size, int ** result, int result_size)
{
printf("debug dans copy table\n");
// printf ("%d\n",temp_size);
// printf ("%d\n",result_size);
// result =
printf ("result size %d\n",result_size);
reallocate_matrix(&result, result_size -1 ,result_size, temp_size );
// printf ("a toi de jouer: %d \n",&result);
printf ("taille de la matrice result: %d \n",result_size);
// printf("salut mon gars %d\n",result[result_size-1][0]);
if (result_size==4)
{
/* for ( int i = 0; i < 1; i++ )
{
printf("%d ",result[0][i]);
}
printf("\n");*/
// printf("taille de resultat : %d \n",result_size);
for ( int i = 0; i < temp_size; i++ )
{
printf("%d ",result[result_size-3][i]);
}
printf("\n");
for ( int i = 0; i < temp_size; i++ )
{
printf("%d ",result[result_size-2][i]);
}
printf("\n");
printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
for ( int i = 0; i < temp_size; i++ )
{
result[result_size-1][i] = 1;
}
printf("\n");
}
for ( int i = 0; i < temp_size; i++ )
{
printf("%d ",temp[i]);
}
printf("\n");
for ( int i = 0; i < temp_size; i++ )
{
result[result_size-1][i] = temp[i];
}
}
/**
* \fn void sum_decomposition(int k, int sum_member, int temporary_sum, int final_sum, int i, int *temp, int ** decomp ,int *cpt)
* \brief Compute recursively the decomposition of an integer n (ie:final_sum) in k parts
* \param k number of parts of the decomposition
* \param sum_member eventual member of the sum
* \param temporary_sum temporary sum must be less than final_sum
* \param final_sum equivalent to n
* \param i position of the number sum_member in the sum
* \param temp temporary table use to store a new decomposition
* \param decomp represent the result matrix
* \param cpt count the number of decomposition
* \return a matrix
*/
void sum_decomposition(int k, int sum_member, int temporary_sum, int final_sum, int i, int *temp, int ** decomp ,int *cpt)
{
printf("debug dans sumdecomposition\n");
int member = sum_member;
int c = i;
//for the last value of the sum
if ( k == 1 )
{
if ( (final_sum - temporary_sum) < member )
{
temp[c] = final_sum - temporary_sum;
temp[0] = c + 1; //temp[0] represent the number of elemements of the sum
*cpt = *cpt + 1;
copy_table ( temp, temp[0], decomp, *cpt );
printf("yep\n");
}
}
else
{
while ( member > 1 )
{
member--;
temporary_sum = temporary_sum + member;
if (temporary_sum < final_sum)
{
temp[c] = member;
c++;//TODO
printf("yep\n");
sum_decomposition(k - 1, member + 1 , temporary_sum, final_sum, c, temp, decomp, cpt); //sorted list of decomposition
temporary_sum = temporary_sum-member;
c--;
}
else
{
temporary_sum = temporary_sum - member;
}
}
}
} |