[Issue 4761] std.array.mul()

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Sep 1 14:35:19 PDT 2010


http://d.puremagic.com/issues/show_bug.cgi?id=4761



--- Comment #2 from bearophile_hugs at eml.cc 2010-09-01 14:35:02 PDT ---
A more general version:


import std.traits: isArray, Unqual;

/// ...
T mul(T)(T items, int times) if(isArray!T) {
    alias Unqual!(typeof(items[0])) E; // dangerous?
    E[] result;

    static if(!is( typeof(items) == void[] )) {
        if (times > 0 && items.length > 0) {
            if (items.length == 1) {
                result.length = times;
                result[] = items[0];
            } else {
                result.length = items.length * times;
                for (size_t pos; pos < result.length; pos += items.length)
                    result[pos .. pos+items.length] = items[];
            }
        }
    }

    return cast(T)result; // assumeUnique?
}

unittest {
    assert("".mul(-10) == "");
    assert("".mul(0) == "");
    assert("".mul(1) == "");
    assert("".mul(10) == "");

    assert("a".mul(-10) == "");
    assert("a".mul(0) == "");
    assert("a".mul(1) == "a");
    assert("a".mul(10) == "aaaaaaaaaa");

    assert("abc".mul(-10) == "");
    assert("abc".mul(0) == "");
    assert("abc".mul(1) == "abc");
    assert("abc".mul(4) == "abcabcabcabc");


    assert(""w.mul(-10) == ""w);
    assert(""w.mul(0) == ""w);
    assert(""w.mul(1) == ""w);
    assert(""w.mul(10) == ""w);

    assert("a"w.mul(-10) == ""w);
    assert("a"w.mul(0) == ""w);
    assert("a"w.mul(1) == "a"w);
    assert("a"w.mul(10) == "aaaaaaaaaa"w);

    assert("abc"w.mul(-10) == ""w);
    assert("abc"w.mul(0) == ""w);
    assert("abc"w.mul(1) == "abc"w);
    assert("abc"w.mul(4) == "abcabcabcabc"w);


    assert(""d.mul(-10) == ""d);
    assert(""d.mul(0) == ""d);
    assert(""d.mul(1) == ""d);
    assert(""d.mul(10) == ""d);

    assert("a"d.mul(-10) == ""d);
    assert("a"d.mul(0) == ""d);
    assert("a"d.mul(1) == "a"d);
    assert("a"d.mul(10) == "aaaaaaaaaa"d);

    assert("abc"d.mul(-10) == ""d);
    assert("abc"d.mul(0) == ""d);
    assert("abc"d.mul(1) == "abc"d);
    assert("abc"d.mul(4) == "abcabcabcabc"d);


    assert((new int[0]).mul(-10) == (new int[0]));
    assert((new int[0]).mul(0) == (new int[0]));
    assert((new int[0]).mul(1) == (new int[0]));
    assert((new int[0]).mul(10) == (new int[0]));

    assert([1].mul(-10) == (new int[0]));
    assert([1].mul(0) == (new int[0]));
    assert([1].mul(1) == [1]);
    assert([1].mul(5) == [1,1,1,1,1]);

    assert([1.5,2.3,3.1].mul(-10) == (new double[0]));
    assert([1.5,2.3,3.1].mul(0) == (new double[0]));
    assert([1.5,2.3,3.1].mul(1) == [1.5,2.3,3.1]);
    assert([1.5,2.3,3.1].mul(2) == [1.5,2.3,3.1,1.5,2.3,3.1]);


    const int[] a1;
    assert(a1.mul(-10) == a1);
    assert(a1.mul(0) == a1);
    assert(a1.mul(1) == a1);
    assert(a1.mul(10) == a1);

    const int[] a2 = [1];
    assert(a2.mul(-10) == a1);
    assert(a2.mul(0) == a1);
    assert(a2.mul(1) == a2);
    assert(a2.mul(5) == [1,1,1,1,1]);

    const int[] a3 = [1, 2, 3];
    assert(a3.mul(-10) == a1);
    assert(a3.mul(0) == a1);
    assert(a3.mul(1) == a3);
    assert(a3.mul(4) == [1,2,3,1,2,3,1,2,3,1,2,3]);
    static assert(is(typeof(a3.mul(4)) == const int[]));


    immutable int[] a4;
    assert(a4.mul(-10) == a4);
    assert(a4.mul(0) == a4);
    assert(a4.mul(1) == a4);
    assert(a4.mul(10) == a4);

    immutable int[] a5 = [1];
    assert(a5.mul(-10) == a4);
    assert(a5.mul(0) == a4);
    assert(a5.mul(1) == a5);
    assert(a5.mul(5) == [1,1,1,1,1]);

    immutable int[] a6 = [1, 2, 3];
    assert(a6.mul(-10) == a4);
    assert(a6.mul(0) == a4);
    assert(a6.mul(1) == a6);
    assert(a6.mul(4) == [1,2,3,1,2,3,1,2,3,1,2,3]);
    static assert(is(typeof(a6.mul(4)) == immutable int[]));

    immutable(int)[] a7 = [1, 2, 3];
    static assert(is(typeof(a7.mul(0)) == immutable(int)[]));
    static assert(is(typeof(a7.mul(1)) == immutable(int)[]));
    static assert(is(typeof(a7.mul(3)) == immutable(int)[]));
}

void main() {}

-- 
Configure issuemail: http://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------


More information about the Digitalmars-d-bugs mailing list