automate tuple creation

forkit forkit at gmail.com
Thu Jan 20 12:15:56 UTC 2022


On Thursday, 20 January 2022 at 10:11:10 UTC, bauss wrote:

>
> Don't make them random then, but use an incrementor.
>
> If you can have ids that aren't integers then you could use 
> uuids too.
>
> https://dlang.org/phobos/std_uuid.html

The 'uniqueness' of id would actually be created in the database.

I just creating a dataset to simulate an export.

I'm pretty much done, just wish -profile=gc was working in 
createUniqueIDArray(..)

// ---------------

module test;
@safe:

import std.stdio : write, writef, writeln, writefln;
import std.range : iota, isForwardRange, hasSlicing, hasLength, 
isInfinite;
import std.array : array, byPair;
import std.random : Random, unpredictableSeed, dice, choice;
import std.algorithm : map, uniq, canFind;

debug { import std; }

Random rnd;
static this() {  rnd = Random(unpredictableSeed); }

void main()
{
     const int recordsNeeded = 10;
     const int valuesPerRecord = 8;

     int[] idArray;
     createUniqueIDArray(idArray, recordsNeeded);

     int[][] valuesArray;
     createValuesArray(valuesArray, recordsNeeded, 
valuesPerRecord);

     int[][int][] records = CreateDataSet(idArray, valuesArray, 
recordsNeeded);
     ProcessRecords(records);
}

void ProcessRecords(ref const(int[][int][]) recArray)
{
     void processRecord(ref int id, ref const(int)[] result)
     {
         writef("%s\t%s", id, result);
     }

     foreach(ref record; recArray)
     {
         foreach (ref rp; record.byPair)
         {
             processRecord(rp.expand);
         }
         writeln;
     }
}

int[][int][] CreateDataSet(ref int[] idArray, ref int[][] 
valuesArray, int numRecords)
{
     int[][int][] records;
     records.reserve(numRecords);
     debug { writefln("records.capacity is %s", records.capacity); 
}

     foreach(i, id; idArray)
         records ~= [ idArray[i] : valuesArray[i] ]; // NOTE: does 
register with -profile=gc

     return records.dup;
}

void createValuesArray(ref int[][] m, size_t recordsNeeded, 
size_t valuesPerRecord)
{
     m = iota(recordsNeeded)
             .map!(i => iota(valuesPerRecord)
             .map!(valuesPerRecord => cast(int)rnd.dice(0.6, 1.4))
             .array).array;  // NOTE: does register with 
-profile=gc
}


void createUniqueIDArray(ref int[] idArray, int recordsNeeded)
{
     idArray.reserve(recordsNeeded);
     debug { writefln("idArray.capacity is %s", idArray.capacity); 
}

     // id needs to be 9 digits, and needs to start with 999
     // below will contain 1_000_000 records that we can choose 
from.
     int[] ids = iota(999_000_000, 1_000_000_000).array; // NOTE: 
does NOT register with -profile=gc

     int i = 0;
     int x;
     while(i != recordsNeeded)
     {
        x = ids.choice(rnd);

        // ensure every id added is unique.
        if (!idArray.canFind(x))
        {
            idArray ~= x; // NOTE: does NOT register with 
-profile=gc
            i++;
        }
     }
}

/+
sample output:

999623777	[0, 0, 1, 1, 1, 0, 0, 0]
999017078	[1, 0, 1, 1, 1, 1, 1, 1]
999269073	[1, 1, 0, 0, 1, 1, 0, 1]
999408504	[0, 1, 1, 1, 1, 1, 0, 0]
999752314	[1, 0, 0, 1, 1, 1, 1, 0]
999660730	[0, 1, 0, 0, 1, 1, 1, 1]
999709822	[1, 1, 1, 0, 1, 1, 0, 0]
999642248	[1, 1, 1, 0, 0, 1, 1, 0]
999533069	[1, 1, 1, 0, 0, 0, 0, 0]
999661591	[1, 1, 1, 1, 1, 0, 1, 1]

+/

// ---------------




More information about the Digitalmars-d-learn mailing list