forkit
Posted in reply to Ali Çehreli
| On Thursday, 20 January 2022 at 23:49:59 UTC, Ali Çehreli wrote:
>
so here is final code, in idiomatic D, as far as I can tell ;-)
curious output when using -profile=gc
.. a line referring to: std.array.Appender!(immutable(char)[]).Appender.Data std.array.Appender!string.Appender.this C:\D\dmd2\windows\bin\..\..\src\phobos\std\array.d:3330
That's not real helpful, as I'm not sure what line of my code its referrring to.
// ---------------
/+
=====================================================================
This program create a sample dataset consisting of 'random' records,
and then outputs that dataset to a file.
Arguments can be passed on the command line,
or otherwise default values are used instead.
Example of that output can be seen at the end of this code.
=====================================================================
+/
module test;
@safe
import std.stdio : write, writef, writeln, writefln;
import std.range : iota;
import std.array : array, byPair;
import std.random : Random, unpredictableSeed, dice, choice, uniform;
import std.algorithm : map, uniq, canFind;
import std.conv : to;
import std.stdio : File;
import std.format;
debug { import std; }
Random rnd;
static this() { rnd = Random(unpredictableSeed); } // thanks Ali
void main(string[] args)
{
int recordsNeeded, valuesPerRecord;
string fname;
if(args.length < 4)
{
recordsNeeded = 10;
valuesPerRecord= 8;
fname = "D:/rnd_records.txt";
}
else
{
// assumes valid values being passed in ;-)
recordsNeeded = to!int(args[1]);
valuesPerRecord = to!int(args[2]);
fname = args[3];
}
int[] idArray;
createUniqueIDArray(idArray, recordsNeeded);
int[][] valuesArray;
createValuesArray(valuesArray, recordsNeeded, valuesPerRecord);
int[][int][] records = CreateDataSet(idArray, valuesArray, recordsNeeded);
ProcessRecords(records, fname);
writefln("All done. Check if records written to %s", fname);
}
void createUniqueIDArray
(ref int[] idArray, const(int) recordsNeeded)
{
idArray.reserve(recordsNeeded);
debug { writefln("idArray.capacity is %s", idArray.capacity); }
int i = 0;
int x;
while(i != recordsNeeded)
{
// id needs to be 9 digits, and needs to start with 999
x = uniform(999*10^^6, 10^^9); // thanks Stanislav
// ensure every id added is unique.
if (!idArray.canFind(x))
{
idArray ~= x; // NOTE: does NOT appear to register with -profile=gc
i++;
}
}
}
void createValuesArray
(ref int[][] valuesArray, const(int) recordsNeeded, const(int) valuesPerRecord)
{
valuesArray = iota(recordsNeeded)
.map!(i => iota(valuesPerRecord)
.map!(valuesPerRecord => cast(int)rnd.dice(0.6, 1.4))
.array).array; // NOTE: does register with -profile=gc
}
int[][int][] CreateDataSet
(const(int)[] idArray, int[][] valuesArray, const(int) numRecords)
{
int[][int][] records;
records.reserve(numRecords);
debug { writefln("records.capacity is %s", records.capacity); }
foreach(i, const id; idArray)
{
// NOTE: below does register with -profile=gc
records ~= [ idArray[i] : valuesArray[i] ];
}
return records.dup;
}
void ProcessRecords
(in int[][int][] recArray, const(string) fname)
{
auto file = File(fname, "w");
scope(exit) file.close;
string[] formattedRecords;
formattedRecords.reserve(recArray.length);
debug { writefln("formattedRecords.capacity is %s", formattedRecords.capacity); }
void processRecord(const(int) id, const(int)[] values)
{
// NOTE: below does register with -profile=gc
formattedRecords ~= id.to!string ~ values.format!"%(%s,%)";
}
foreach(ref const record; recArray)
{
foreach (ref rp; record.byPair)
{
processRecord(rp.expand);
}
}
foreach(ref rec; formattedRecords)
file.writeln(rec);
}
/+
sample file output:
9992511730,1,0,1,0,1,0,1
9995369731,1,1,1,1,1,1,1
9993136031,1,0,0,0,1,0,0
9998979051,1,1,1,1,0,1,1
9998438090,1,1,0,1,1,0,0
9995132750,0,0,1,0,1,1,1
9997123630,0,1,1,1,0,1,1
9998351590,1,0,0,1,1,1,1
9991454121,1,1,1,1,1,0,1
9997673520,1,1,1,1,1,1,1
+/
// ---------------
|