www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - How to cleanup array of structs?

reply Suliman <evermind live.ru> writes:
I remember that there was topic about remobing data from 
struct/arrays of structs. But I do not remember what is idiomatic 
way to do it, and can't google it.

something like:
     struct MyTrack
     {
         ulong id;
         string recordDate;
         int velocity;
         int maxAllowedSpeedForRoad;
     }

     MyTrack mytrack;
     MyTrack [] mytracks;

// filling

mytracks.clean() or what?
Jun 02
parent reply Biotronic <simen.kjaras gmail.com> writes:
On Friday, 2 June 2017 at 13:32:02 UTC, Suliman wrote:
 I remember that there was topic about remobing data from 
 struct/arrays of structs. But I do not remember what is 
 idiomatic way to do it, and can't google it.

 something like:
     struct MyTrack
     {
         ulong id;
         string recordDate;
         int velocity;
         int maxAllowedSpeedForRoad;
     }

     MyTrack mytrack;
     MyTrack [] mytracks;

 // filling

 mytracks.clean() or what?
There are multiple options // Will set the array to an empty one, and leave the // old one for the GC to clean up when it feels like it. // The safest way. mytracks = null; // Mostly equivalent: mytracks = []; // Will reuse the array, overwriting existing data. // If other parts of the program are using existing data // in the array, this will lead to hard-to-track-down bugs. mytracks.length = 0; mytracks.assumeSafeAppend(); // If you just want to get rid of the last element you added to the array: mytracks = mytracks[0..$-1]; -- Biotronic
Jun 02
parent reply Suliman <evermind live.ru> writes:
 // Will reuse the array, overwriting existing data.
 // If other parts of the program are using existing data
 // in the array, this will lead to hard-to-track-down bugs.
 mytracks.length = 0;
 mytracks.assumeSafeAppend();
Could you give an example where it can lead bugs? Do you mean multi-thread apps?
Jun 04
parent Nicholas Wilson <iamthewilsonator hotmail.com> writes:
On Sunday, 4 June 2017 at 12:24:44 UTC, Suliman wrote:
 // Will reuse the array, overwriting existing data.
 // If other parts of the program are using existing data
 // in the array, this will lead to hard-to-track-down bugs.
 mytracks.length = 0;
 mytracks.assumeSafeAppend();
Could you give an example where it can lead bugs? Do you mean multi-thread apps?
it is not restricted to multithreads but is perhaps easiest to think about it in those terms. so you do
 mytracks.length = 0;
 mytracks.assumeSafeAppend();
and then you start building up the array again with some new data so that the array is sorted (or some other property of the array) and you do this over a period of time.
 foreach(i; iota(N))
 {
     mytracks ~= MyTrack(i, "",0,0); // id is sorted
     Fibre.yield(); // do something else, maybe wating for more 
 data
 } func(mytracks); // precondition that arg is sorted.
meanwhile you have a reference to the `mytracks`buffer somewhere else (another global variable for instance) and it is not expecting to have its data 'stomped' and then it writes to it then it will this may make `mytracks` no longer sorted.
Jun 04