On 05/01/2011 09:09 AM, Andrej Mitrovic wrote:
Yeah it seems a common interface is what I should have used. I've
tried it in numerous ways but I got the interface type wrong
apparently. Thanks Dmitry.

Here's a quick example:
http://codepad.org/RhNiUHU2

To make it more convenient to others, I paste Andrej Mitrovic's code:

module cyclicBuffer;

import std.stdio;
import std.range;
import core.thread;
import std.random;

enum BufferSize = 10;

struct Work
{
    private float[BufferSize] _buffer;
    InputRange!(float) buffer;

    this(T)(T unused)
    {
        _buffer[] = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];

        buffer = inputRangeObject(cycle(_buffer[]));
    }

    void setNewRange(size_t min, size_t max)
    {
        buffer = inputRangeObject(cycle(_buffer[min..max]));
    }

    void setStep(size_t newStep)
    {
        buffer = inputRangeObject(stride(buffer, newStep));
    }

}

void main()
{
    writeln();
    auto work = Work(1);

    size_t count;
    int min;
    int max;
    size_t step;
    while (!work.buffer.empty)
    {
        write(work.buffer.front);
        stdout.flush();
        work.buffer.popFront;

        if (++count == 20)
        {
            min = uniform(0, BufferSize / 2);
            max = uniform(BufferSize / 2, BufferSize);
            step = uniform(1, 5);
            work.setNewRange(min, max);
            work.setStep(step);
            writefln("\nmin: %s, max: %s, step: %s", min, max-1, step);
            count = 0;
        }

        Thread.sleep( dur!("msecs")(100) );
    }
}


I hope those bugs get squashed so I can have more fun with these ranges. :)

Ali

Reply via email to