xxxxxxxxxx
struct Queue(T)
{
T[] store;
size_t ptr = size_t.max;
enum growthFactor = 2;
void push(return scope T val) scope
{
import std.functional: safeOp;
ptr += 1;
if (ptr >= store.length && ptr < size_t.max)
{
store.length = store.length == 0
? 8
: store.length * growthFactor;
}
//FIXME: either I don't understand DIP1000
//well enough, or this should compile
//Workaround: surround it with an @trusted lambda
store[ptr] = val;
}
}
struct Data
{
string val;
}
struct DataRange
{
string store;
:
this(string rawData)
{
store = rawData;
}
void popFront() scope
{
popWhiteSpace();
if (store.length > 0)
store = store[1..$];
}
void popWhiteSpace() scope
{
while (store.length > 0 && store[0] == ' ')
store = store[1..$];
}
Data front() return
{
if (store.length > 0 && store[0] == ' ')
popWhiteSpace();
return Data(store[0..1]);
}
bool empty() scope
{
return store.length <= 0;
}
}
Queue!Data copyToQueue(scope DataRange data)
{
Queue!Data output;
while (!data.empty)
{
auto d = data.front;
data.popFront();
import std.random: uniform;
if (uniform(0, 100) < 50)
{
output.push(d);
}
}
return output;
}
DataRange makeDataRange(string input)
{
auto range = DataRange(input);
return range;
}
void main()
{
auto rawData = "2 6 4 10 2 9 4 5";
auto dataRange = makeDataRange(rawData);
auto result = dataRange.copyToQueue();
import std.stdio;
writeln("The result of data processing is: ", result);
}