Page 1 sur 1

bool alias

Posté : ven. 28 déc. 2012, 22:48
par zariski
Bonsoir !

Pourquoi ce code ne me rend pas le truc attendu, à savoir : a=b ? true .

Figure asymptote bd77fe8ac6d4115861244f6555c39111
*** Pour masquer/découvrir le code Asymptote qui a permis de créer la figure, il faut cliquer dessus. ;-) ***

CODE ASYMPTOTE de la figure ci-dessus : Tout sélectionner
  1. unitsize(1cm);
  2. real [] a={-4.7,1.2,4.5,6.3,8},
  3. b={-4.7,1.2,4.5,6.3,8},
  4. c={1,0,2,4};
  5.  
  6. bool p=alias(a,b), q=alias(b,c);
  7.  
  8. label(scale(0.8)*("Tableau a :"),(-1,4));
  9. label(scale(0.8)*("Tableau b :"),(-1,3));
  10. label(scale(0.8)*("Tableau c :"),(-1,2));
  11.  
  12. for (int i=0; i<a.length; ++i){
  13. label(scale(0.8)*(format("$a_{%i} = $",i) + string(a[i])),(1.5*i-1.5,3.5));
  14. label(scale(0.8)*(format("$b_{%i} = $",i) + string(b[i])),(1.5*i-1.5,2.5));
  15. }
  16. for (int i=0; i<c.length; ++i)
  17. label(scale(0.8)*(format("$c_{%i} = $",i) + string(c[i])),(1.5*i-1.5,1.5));
  18.  
  19. label(scale(0.8)*("a=b ? " + (p ? "true" : "false")),(-1,0.8));
  20. label(scale(0.8)*("a=c ? " + (q ? "true" : "false")),(-1,0));
  21.  
  22. shipout(bbox(.2cm,Fill(rgb(0.97,0.54,0.54))));

Re: bool alias

Posté : ven. 28 déc. 2012, 23:50
par GM
Bonsoir,

l'explication en anglais de la doc :
The function bool alias(T,T) checks to see if two structure references refer to the same instance of the structure (or both to null).


et un exemple commenté en français :

Code : Tout sélectionner

real[] a={2};
real[] b={2};
real[] c=a;
write(alias(a,b)); // renvoie false
write(alias(a,c)); // renvoie true car a et c sont la même instance de la structure real[]

a.push(3); // on ajoute 3 à a
write(c);  // et l'affichage de c renvoie donc {2,3}.......... alors que b reste évidemment inchangé.

Re: bool alias

Posté : sam. 29 déc. 2012, 09:42
par zariski
Mouais ... ben suis pas convaincu.
Je ne vois pas non plus l'utilité du truc mais j'imagine bien qu'il y en a une !!! :oops:
Merci tout de même !

Re: bool alias

Posté : sam. 29 déc. 2012, 11:34
par GM
zariski a écrit :Je ne vois pas non plus l'utilité du truc mais j'imagine bien qu'il y en a une !!! :oops:
Merci tout de même !


Si tu t'intéresses au sujet... fais des recherches sur "passage par valeur et passage par référence".

La doc d'Asymptote dit :
Functions arguments are passed by value.

To pass an argument by reference, simply enclose it in a structure


Illustration du comportement particulier dans le cas d'une structure :

Code : Tout sélectionner

int a=2, b=1;

write("a="+string(a)+" ------- b="+string(b));
int tempo=a; write(alias(tempo,a));
a = b;       write(alias(a,b));
b = tempo;   write(alias(b,tempo));
write("a="+string(a)+" ------- b="+string(b));

////////////////////////////////////
write('\n');
////////////////////////////////////

struct intGM{ int x; }

intGM a,b;
a.x=2;
b.x=1;

write("a.x="+string(a.x)+" ------- b.x="+string(b.x));
intGM tempo=a; write(alias(tempo,a));
a = b;       write(alias(a,b));
b = tempo;   write(alias(b,tempo));
write("a.x="+string(a.x)+" ------- b.x="+string(b.x));


Image

Si j'ai compris... dans le second cas, on a évité de devoir allouer de la mémoire pour tempo... puisque l'on a permuté des références et qu'il n'a pas fallu faire de la place en mémoire pour stocker une valeur temporaire.

Re: bool alias

Posté : sam. 29 déc. 2012, 11:59
par GM
... et donc pour le cas particulier de tes tableaux, on fait cette distinction entre passage par référence et passage par valeur (utilisation de copy) :

Code : Tout sélectionner

int[] a={2}, b={1};

write("a="+string(a[0])+" ------- b="+string(b[0]));
int[] tempo=a; write(alias(tempo,a));
a = b;         write(alias(a,b));
b = tempo;     write(alias(b,tempo));
write("a="+string(a[0])+" ------- b="+string(b[0]));

////////////////////////////////////
write('\n');
////////////////////////////////////

int[] a={2}, b={1};

write("a="+string(a[0])+" ------- b="+string(b[0]));
int[] tempo=copy(a); write(alias(tempo,a));
a = copy(b);         write(alias(a,b));
b = copy(tempo);     write(alias(b,tempo));
write("a="+string(a[0])+" ------- b="+string(b[0]));


Image

Re: bool alias

Posté : sam. 29 déc. 2012, 13:00
par zariski
houlaaaaa
va falloir que je cogite le truc !!!!
Merciiiiii