Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
Nadi Tomeh
SDA
Commits
931cc99a
Commit
931cc99a
authored
Nov 21, 2021
by
Rafik_Hammoutene
Browse files
Ajout des fixed binary avec p=0.5
parent
f6a71c9c
Pipeline
#4656
failed with stage
in 6 seconds
Changes
7
Pipelines
1
Show whitespace changes
Inline
Side-by-side
C/main.c
View file @
931cc99a
...
...
@@ -10,8 +10,8 @@
int
main
(
int
argc
,
char
**
argv
){
int
i
;
// Creation du tas binaire
de taille 90
tas_binaire_t
*
a
=
creer_tas_binaire
(
90
);
// Creation du tas binaire
tas_binaire_t
*
a
=
creer_tas_binaire
(
90
0000
);
// Analyse du temps pris par les opérations.
analyzer_t
*
time_analysis
=
analyzer_create
();
// Analyse du nombre de copies faites par les opérations.
...
...
@@ -24,35 +24,53 @@ int main(int argc,char **argv){
struct
timespec
before
,
after
;
clockid_t
clk_id
=
CLOCK_REALTIME
;
// utilisé comme booléen pour savoir si une allocation a été effectuée.
char
memory_allocation
;
float
random_number
;
//Nombre aléatooire a geneer
int
r_number
;
//char memory_allocation;
srand
(
time
(
NULL
));
float
random_number
;
// var aléatoire
float
proba
=
0
.
5
;
// Probabilité d'insertion à 0.5
int
nombre_echange
;
// Nombre de swipe effectués
int
nombreDansTas
;
//Nombre qu'on insère dans le tas
for
(
i
=
0
;
i
<
90
;
i
++
){
for
(
i
=
0
;
i
<
90
0000
;
i
++
){
random_number
=
rand
()
/
(
RAND_MAX
+
200
.
0
);
r_number
=
(
int
)
random_number
;
//caster en int
random_number
=
(
float
)
rand
()
/
(
float
)
RAND_MAX
;
nombreDansTas
=
rand
()
%
900000
;
if
(
random_number
<
proba
){
// Ajout d'un élément et mesure du temps pris par l'opération.
clock_gettime
(
clk_id
,
&
before
);
memory_allocation
=
insertion_tas_binaire
(
a
,
r_number
);
nombre_echange
=
tas_append
(
a
,
nombreDansTas
);
clock_gettime
(
clk_id
,
&
after
);
// Enregistrement du temps pris par l'opération
analyzer_append
(
time_analysis
,
after
.
tv_nsec
-
before
.
tv_nsec
);
// Enregistrement du nombre de copies efféctuées par l'opération.
// S'il y a eu réallocation de mémoire, il a fallu recopier tout le tableau.
analyzer_append
(
copy_analysis
,
(
memory_allocation
)
?
i
:
1
);
//
analyzer_append(copy_analysis, (memory_allocation)? i:1 );
// Enregistrement de l'espace mémoire non-utilisé.
analyzer_append
(
memory_analysis
,
tas_binaire_capacite
(
a
)
-
tas_binaire_size
(
a
));
// Enregistrement du nombre d'echange effectuée
analyzer_append
(
swap_analysis
,
tas_binaire_echange
(
a
));
}
else
{
clock_gettime
(
clk_id
,
&
before
);
nombre_echange
=
tas_pop_back
(
a
);
clock_gettime
(
clk_id
,
&
after
);
// Enregistrement du temps pris par l'opération
analyzer_append
(
time_analysis
,
after
.
tv_nsec
-
before
.
tv_nsec
);
// Enregistrement du nombre de copies efféctuées par l'opération.
// S'il y a eu réallocation de mémoire, il a fallu recopier tout le tableau.
//analyzer_append(copy_analysis, (memory_allocation)? i:1 );
// Enregistrement de l'espace mémoire non-utilisé.
analyzer_append
(
memory_analysis
,
tas_binaire_capacite
(
a
)
-
tas_binaire_size
(
a
));
// Enregistrement du nombre d'echange effectuée
analyzer_append
(
swap_analysis
,
tas_binaire_echange
(
a
));
}
}
// Affichage de quelques statistiques sur l'expérience.
fprintf
(
stderr
,
"Total cost: %Lf
\n
"
,
get_total_cost
(
time_analysis
));
fprintf
(
stderr
,
"Average cost: %Lf
\n
"
,
get_average_cost
(
time_analysis
));
...
...
@@ -60,15 +78,16 @@ int main(int argc,char **argv){
fprintf
(
stderr
,
"Standard deviation: %Lf
\n
"
,
get_standard_deviation
(
time_analysis
));
// Sauvegarde les données de l'expérience.
save_values
(
time_analysis
,
"../plots/
dynamic_array_time_c_alpha_random
.plot"
);
save_values
(
swap_analysis
,
"../plots/
dynamic_array_swap_c_alpha_random
.plot"
);
save_values
(
memory_analysis
,
"../plots/
dynamic_array_memory_c_alpha_random
.plot"
);
save_values
(
time_analysis
,
"../plots/
fixed_tas_time_p0.5
.plot"
);
save_values
(
swap_analysis
,
"../plots/
fixed_tas_swap_p0.5
.plot"
);
save_values
(
memory_analysis
,
"../plots/
fixed_tas_memory_p0.5
.plot"
);
// Nettoyage de la mémoire avant la sortie du programme
tas_binaire_detruire
(
a
);
analyzer_destroy
(
time_analysis
);
analyzer_destroy
(
copy_analysis
);
//
analyzer_destroy(copy_analysis);
analyzer_destroy
(
memory_analysis
);
analyzer_destroy
(
swap_analysis
);
return
EXIT_SUCCESS
;
}
C/tas_binaire.c
View file @
931cc99a
...
...
@@ -37,7 +37,9 @@ void tas_binaire_echanger_valeurs(tas_binaire_t * t, int clef, int val) {
// Nombre d echange dans un tas binaire
int
tas_binaire_echange
(
tas_binaire_t
*
t
){
if
(
t
!=
NULL
)
return
t
->
echange
;
}
...
...
@@ -58,6 +60,7 @@ char insertion_tas_binaire(tas_binaire_t* t , int cle){
char
memory_allocation
=
FALSE
;
if
(
t
!=
NULL
){
if
(
t
->
size
<
t
->
taille
){
memory_allocation
=
TRUE
;
tas_binaire_echanger_valeurs
(
t
,
t
->
size
,
cle
);
t
->
size
++
;
}
else
{
...
...
@@ -80,5 +83,118 @@ size_t tas_binaire_capacite(tas_binaire_t * t){
}
int
extraire_heap_minimum
(
tas_binaire_t
*
t
){
int
cle
=
0
;
//int cle_min;
//cle_min=t->tab[0];
int
nombre_echange
=
0
;
int
temp
=
0
;
int
fg
=
2
*
cle
+
1
;
int
fd
=
2
*
cle
+
2
;
while
((
cle
<
(
tas_binaire_size
(
t
)
-
1
))
&&
(
fg
<
tas_binaire_size
(
t
)
&&
fd
<
tas_binaire_size
(
t
))
&&
(
t
->
tab
[
cle
]
>
t
->
tab
[
fg
]
||
t
->
tab
[
cle
]
>
t
->
tab
[
fd
])){
if
(
t
->
tab
[
fg
]
>
t
->
tab
[
fd
]){
// si fg>fd alors
temp
=
t
->
tab
[
cle
];
t
->
tab
[
cle
]
=
t
->
tab
[
fd
];
t
->
tab
[
fd
]
=
temp
;
cle
=
fd
;
}
else
{
temp
=
t
->
tab
[
cle
];
t
->
tab
[
cle
]
=
t
->
tab
[
fg
];
t
->
tab
[
fg
]
=
temp
;
cle
=
fg
;
}
nombre_echange
++
;
fg
=
2
*
cle
+
1
;
fd
=
2
*
cle
+
2
;
return
nombre_echange
;
}
}
int
tasbinaire_get
(
tas_binaire_t
*
t
,
int
pos
){
if
(
t
!=
NULL
&&
pos
>
0
&&
pos
<
t
->
size
){
return
t
->
tab
[
pos
];
}
printf
(
"Wrong paramaeter pos=%d or null list"
,
pos
);
return
-
1
;
}
char
tas_do_we_need_to_enlarge_capacite
(
tas_binaire_t
*
t
){
return
t
->
size
==
t
->
taille
?
TRUE
:
FALSE
;
}
void
tas_enlarge_capacite
(
tas_binaire_t
*
t
){
t
->
taille
*=
2
;
t
->
tab
=
(
int
*
)
realloc
(
t
->
tab
,
sizeof
(
int
)
*
t
->
taille
);
}
void
tas_reduce_capacite
(
tas_binaire_t
*
t
){
t
->
taille
/=
2
;
t
->
tab
=
(
int
*
)
realloc
(
t
->
tab
,
sizeof
(
int
)
*
t
->
taille
);
}
char
tas_do_we_need_to_reduce_capacite
(
tas_binaire_t
*
t
){
return
(
t
->
size
<=
t
->
taille
/
4
&&
t
->
size
>
4
)
?
TRUE
:
FALSE
;
}
int
tas_diminuer_clef
(
tas_binaire_t
*
t
,
int
cle
,
int
val
){
t
->
tab
[
cle
]
=
val
;
int
nombre_echange
=
0
;
while
(
cle
>
0
&&
t
->
tab
[
tas_binaire_pere
(
cle
)]
>
t
->
tab
[
cle
]){
int
tmp
=
t
->
tab
[
tas_binaire_pere
(
cle
)];
t
->
tab
[
tas_binaire_pere
(
cle
)]
=
t
->
tab
[
cle
];
t
->
tab
[
cle
]
=
tmp
;
nombre_echange
++
;
cle
=
tas_binaire_pere
(
cle
);
}
return
nombre_echange
;
}
/*** version d'insertion comme les arraylist **/
char
tas_append
(
tas_binaire_t
*
t
,
int
x
){
char
memory_allocation
=
FALSE
;
if
(
t
!=
NULL
){
if
(
tas_do_we_need_to_enlarge_capacite
(
t
)){
memory_allocation
=
TRUE
;
tas_enlarge_capacite
(
t
);
}
t
->
echange
=
tas_diminuer_clef
(
t
,
t
->
size
,
x
);
t
->
size
++
;
}
return
memory_allocation
;
}
/** REPRISE DU CODE SUR ARRAYLIST DU TP2 ,pop back **/
char
tas_pop_back
(
tas_binaire_t
*
t
){
char
memory_reduction
=
FALSE
;
if
(
t
!=
NULL
&&
t
->
size
>
0
){
if
(
tas_do_we_need_to_reduce_capacite
(
t
)){
memory_reduction
=
TRUE
;
tas_reduce_capacite
(
t
);
}
t
->
tab
[
0
]
=
t
->
tab
[
tas_binaire_size
(
t
)
-
1
];
t
->
size
--
;
t
->
echange
=
extraire_heap_minimum
(
t
);
}
return
memory_reduction
;
}
C/tas_binaire.h
View file @
931cc99a
...
...
@@ -8,7 +8,7 @@
typedef
struct
tas_binaire_str
tas_binaire_t
;
struct
tas_binaire_str
{
size_t
taille
;
// Taille du tableeau
size_t
taille
;
// Taille du tableeau
(capacité)
size_t
size
;
//Nombres d'élement qui seront stockés dans le tableau
int
echange
;
// Nombre de echanges a faire
int
*
tab
;
// Pointeur vers une case mémoire ou les elem sont stockés
...
...
@@ -90,4 +90,77 @@ size_t tas_binaire_size(tas_binaire_t * t);
size_t
tas_binaire_capacite
(
tas_binaire_t
*
t
);
/**
FOnciton qui extrait la clé minimale du tas
@param t un tas binaire
@returns le nombre d'echanges faits
**/
int
extraire_heap_minimum
(
tas_binaire_t
*
t
);
/**
Fonction qui renvoie la valeur située à la position donnée par l'utilisateur
@param t un tas binaire
@param pos l'indice de la case
@returns la valeur située à la position donnée
**/
int
tasbinaire_get
(
tas_binaire_t
*
t
,
int
pos
);
/**
FOnction qui determine la règle selon laquelle un espace mémoire plus grand sera alloué ou non
@param t un tas binaire
@returns TRUE si le tableau doit etre agrandi, FALSE SINON
**/
char
tas_do_we_need_to_enlarge_capacite
(
tas_binaire_t
*
t
);
/**
FOnction qui élargit la capacité ou la taille du tableau
@param t un tas binaire
**/
void
tas_enlarge_capacite
(
tas_binaire_t
*
t
);
/**
FOnciton qui réduit la capacité du tableau
@param t un pointeur vers un tableau
**/
void
tas_reduce_capacite
(
tas_binaire_t
*
t
);
/**
FOnction qui determine la règle selon laquelle un espace mémoire plus petit sera alloué ou non
@param t un tas binaire
@returns TRUE si le tableau doit etre amoindri, FALSE SINON
**/
char
tas_do_we_need_to_reduce_capacite
(
tas_binaire_t
*
t
);
/**
FOnction qui retourne le nombre d'échange apres avoir
attribué la val (valeur) au noeud de position clef et trie le tas binaire
@param t un tas binaire
@param cle la clef postions
@param val la valeur
@returns le nombre d'echange
**/
int
tas_diminuer_clef
(
tas_binaire_t
*
t
,
int
cle
,
int
val
);
/**
Ajoute une valeur dans le tas binaire (dans le tab)
@param t un tas binaire
@param x la valeur que l'on souhaite ajouter.
@returns TRUE si le talbrau a été agrandit , FALSE Sinon
**/
char
tas_append
(
tas_binaire_t
*
t
,
int
x
);
/**
Fonciton qui supprime la dernière valeur du tableau
@param t un tas binaire
@returns TRUE si le tableau a été réduit , FALSE sinon
**/
char
tas_pop_back
(
tas_binaire_t
*
t
);
#endif
plots/fixed_binary_heap_extract_amortized_time_C.pdf
0 → 100644
View file @
931cc99a
File added
plots/fixed_binary_heap_extract_swap_C.pdf
0 → 100644
View file @
931cc99a
File added
plots/fixed_binary_heap_extract_wasted_memory_C.pdf
0 → 100644
View file @
931cc99a
File added
plots/plot_result
View file @
931cc99a
...
...
@@ -12,9 +12,9 @@ set xlabel "Nombre d'elements ajoutes" font "Helvetica,24"
set ylabel "Temps amorti" font "Helvetica,24"
# Nom du fichier Postscript en sortie
set output 'fixed_binary_heap_amortized_time_C
_random
.pdf'
set output 'fixed_binary_heap_
extract_
amortized_time_C.pdf'
plot [0:100][0:100] '
dynamic_array_time_c_alpha_random
.plot' using 1:3 w lines title "Amortized C "
plot [0:100][0:100] '
fixed_tas_time_p0.5
.plot' using 1:3 w lines title "Amortized C
avec p=0.5
"
###############################################################
########### Affichage de l'espace mémoire gaspillé ###########
...
...
@@ -23,9 +23,9 @@ plot [0:100][0:100] 'dynamic_array_time_c_alpha_random.plot' using 1:3 w lines t
set ylabel "Memoire gaspillee" font "Helvetica,24"
# Nom du fichier Postscript en sortie
set output 'fixed_binary_heap_wasted_memory_C
_random
.pdf'
set output 'fixed_binary_heap_
extract_
wasted_memory_C.pdf'
plot [0:300][0:300] '
dynamic_array_memory_c_alpha_random
.plot' using 1:2 w lines title "Espace memoire inutilise C "
plot [0:300][0:300] '
fixed_tas_memory_p0.5
.plot' using 1:2 w lines title "Espace memoire inutilise C
avec p=0.5
"
#################################################################
########### Affichage du nombre de copies effectuées ###########
#################################################################
...
...
@@ -33,8 +33,8 @@ plot [0:300][0:300] 'dynamic_array_memory_c_alpha_random.plot' using 1:2 w lines
set ylabel "Nombre de copies effectuees" font "Helvetica,24"
# Nom du fichier Postscript en sortie
set output 'fixed_binary_heap_
swap_C_random
.pdf'
set output 'fixed_binary_heap_
extract_swap_C
.pdf'
plot '
dynamic_array_swap_c_alpha_random
.plot' using 1:2 w boxes title "Nombre d'echange effectuees C "
plot '
fixed_tas_swap_p0.5
.plot' using 1:2 w boxes title "Nombre d'echange effectuees C
avec p=0.5
"
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment