opSlice and $

Jarrett Billingsley kb3ctd2 at yahoo.com
Tue Sep 11 15:08:12 PDT 2007


"Robert Fraser" <fraserofthenight at gmail.com> wrote in message 
news:fc70uu$o8r$1 at digitalmars.com...
> Sean Kelly Wrote:
>>
>> I would love this.  It would be quite useful for ordered associative
>> sequences (red-black tree, etc).
>
> Got my vote!
>

...?

class Blah
{
    private int[char[]] mData;

    void opIndexAssign(int val, char[] key)
    {
        mData[key] = val;
    }

    int opIndex(char[] key)
    {
        return mData[key];
    }

    int[] opSlice(char[] lo, char[] hi)
    {
        int[] ret;

        foreach(k, v; mData)
            if(k >= lo && k < hi)
                ret ~= v;

        return ret;
    }
}

void main()
{
    scope b = new Blah();
    b["a"] = 5;
    b["b"] = 10;
    b["hello"] = 13;
    b["x"] = 15;
    b["y"] = 20;
    b["z"] = 25;

    int[] x = b["b" .. "n"];
    int[] y = b["x" .. "z"];

    foreach(v; x)
        Stdout.format("{} ", v);

    Stdout.newline;

    foreach(v; y)
        Stdout.format("{} ", v);

    Stdout.newline;
}

(*%##@**@#^ Tango not being able to print arrays....) 





More information about the Digitalmars-d mailing list