Re: Is removing elements of AA in foreach loop safe?

2019-09-04 Thread Ferhat Kurtulmuş via Digitalmars-d-learn
On Wednesday, 4 September 2019 at 06:20:00 UTC, berni wrote: On Tuesday, 3 September 2019 at 20:06:27 UTC, Ferhat Kurtulmuş wrote: I know, it is foreach loop in question. How about using a reverse for loop like: for (size_t i = arr.length ; i-- > 0 ; ){ arr.remove(i); } This would be

Re: Is removing elements of AA in foreach loop safe?

2019-09-04 Thread berni via Digitalmars-d-learn
On Tuesday, 3 September 2019 at 20:06:27 UTC, Ferhat Kurtulmuş wrote: I know, it is foreach loop in question. How about using a reverse for loop like: for (size_t i = arr.length ; i-- > 0 ; ){ arr.remove(i); } This would be good, if it where for slices. But with associative arrays, this

Re: Is removing elements of AA in foreach loop safe?

2019-09-03 Thread Ferhat Kurtulmuş via Digitalmars-d-learn
On Thursday, 29 August 2019 at 10:11:58 UTC, berni wrote: Iterating of some structure and removing elements thereby is always errorprone and should be avoided. But: In case of AA, I've got the feeling, that it might be safe: foreach (k,v;ways) if (v.empty) ways.remove(k); Do

Re: Is removing elements of AA in foreach loop safe?

2019-08-30 Thread Jordan Wilson via Digitalmars-d-learn
On Thursday, 29 August 2019 at 10:11:58 UTC, berni wrote: Iterating of some structure and removing elements thereby is always errorprone and should be avoided. But: In case of AA, I've got the feeling, that it might be safe: foreach (k,v;ways) if (v.empty) ways.remove(k); Do

Re: Is removing elements of AA in foreach loop safe?

2019-08-30 Thread H. S. Teoh via Digitalmars-d-learn
On Fri, Aug 30, 2019 at 04:45:20PM +, berni via Digitalmars-d-learn wrote: > On Friday, 30 August 2019 at 15:00:59 UTC, Paul Backus wrote: > > Whether you actually get an error at runtime depends on the load > > factor of the AA. If it drops below a certain threshold, the AA will > > be

Re: Is removing elements of AA in foreach loop safe?

2019-08-30 Thread berni via Digitalmars-d-learn
On Friday, 30 August 2019 at 15:00:59 UTC, Paul Backus wrote: Whether you actually get an error at runtime depends on the load factor of the AA. If it drops below a certain threshold, the AA will be resized [1], and its original memory will be freed [2]. It could still work, depending on how

Re: Is removing elements of AA in foreach loop safe?

2019-08-30 Thread Paul Backus via Digitalmars-d-learn
On Friday, 30 August 2019 at 13:43:54 UTC, XavierAP wrote: On Thursday, 29 August 2019 at 10:11:58 UTC, berni wrote: Iterating of some structure and removing elements thereby is always errorprone and should be avoided. But: In case of AA, I've got the feeling, that it might be safe: foreach

Re: Is removing elements of AA in foreach loop safe?

2019-08-30 Thread XavierAP via Digitalmars-d-learn
On Thursday, 29 August 2019 at 10:11:58 UTC, berni wrote: Do you agree? Or is there a better way to achieve this? An alternative would be to reassign the AAA to the output of std.algorithm.filter()... but assignment between AAs and Ranges isn't so type-direct.

Re: Is removing elements of AA in foreach loop safe?

2019-08-30 Thread XavierAP via Digitalmars-d-learn
On Thursday, 29 August 2019 at 10:11:58 UTC, berni wrote: Iterating of some structure and removing elements thereby is always errorprone and should be avoided. But: In case of AA, I've got the feeling, that it might be safe: foreach (k,v;ways) if (v.empty) ways.remove(k); Do

Re: Is removing elements of AA in foreach loop safe?

2019-08-29 Thread Jonathan M Davis via Digitalmars-d-learn
On Thursday, August 29, 2019 4:11:58 AM MDT berni via Digitalmars-d-learn wrote: > Iterating of some structure and removing elements thereby is > always errorprone and should be avoided. But: In case of AA, I've > > got the feeling, that it might be safe: > > foreach (k,v;ways) > > > > if

Is removing elements of AA in foreach loop safe?

2019-08-29 Thread berni via Digitalmars-d-learn
Iterating of some structure and removing elements thereby is always errorprone and should be avoided. But: In case of AA, I've got the feeling, that it might be safe: foreach (k,v;ways) if (v.empty) ways.remove(k); Do you agree? Or is there a better way to achieve this?