Phobos2: sorting and std.typecons.Tuple

bearophile bearophileHUGS at lycos.com
Mon Apr 27 09:14:58 PDT 2009


dsimcha:
> int opCmp(const ref typeof(this) other) const {
>     foreach(ti, elem; other.tupleof) {
>         if(this.tupleof[ti] < elem) {
>             return -1;
>         } else if(this.tupleof[ti] > elem) {
>             return 1;
>         }
>     }
>     return 0;
> }

The unittest of my structCmp(s1, s2) function template:

unittest { // Tests of structCmp()
    struct S0 { }
    assert(structCmp(S0(), S0()) == 0);

    struct S1 { int x; }
    assert(structCmp(S1(7), S1(7)) == 0);
    assert(structCmp(S1(8), S1(7)) == 1);
    assert(structCmp(S1(7), S1(8)) == -1);

    assert(structCmp(S0(), S1(7)) == -1);
    assert(structCmp(S1(7), S0()) == 1);

    struct S2 { int x, y; }
    assert(structCmp(S2(7, 8), S2(7, 8)) == 0);
    assert(structCmp(S2(7, 8), S2(7, 7)) == 1);
    assert(structCmp(S2(7, 7), S2(7, 8)) == -1);

    assert(structCmp(S0(), S2(7, 5)) == -1);
    assert(structCmp(S2(7, 5), S0()) == 1);
    assert(structCmp(S1(4), S2(7, 5)) == -1);

    assert(structCmp(S1(7), S2(7, 5)) == -1);
    assert(structCmp(S1(8), S2(7, 5)) == 1);
    assert(structCmp(S2(7, 5), S1(7)) == 1);
    assert(structCmp(S2(7, 5), S1(8)) == -1);

    struct S3 { int x; float y; int z; }
    assert(structCmp(S3(2, 7.1, 8), S3(2, 7.1, 8)) == 0);
    assert(structCmp(S3(2, 7.1, 8), S3(2, 7.1, 7)) == 1);
    assert(structCmp(S3(2, 7.1, 7), S3(2, 7.1, 8)) == -1);

    assert(structCmp(S0(), S3(2, 7.1, 8)) == -1);
    assert(structCmp(S3(2, 7.1, 8), S0()) == 1);

    assert(structCmp(S1(2), S3(2, 7.1, 8)) == -1);
    assert(structCmp(S1(3), S3(2, 7.1, 8)) == 1);
    assert(structCmp(S3(2, 7.1, 8), S1(2)) == 1);
    assert(structCmp(S3(2, 7.1, 8), S1(3)) == -1);

    struct S3b {
        int x; float y; int z;
        int opCmp(S3b other) {
            return structCmp(this, other);
        }
    }
    assert( (S3b(2, 7.1, 8) > S3b(2, 7.1, 8)) == false);
    assert( (S3b(2, 7.1, 8) > S3b(2, 7.1, 7)) == true);
    assert( (S3b(2, 7.1, 7) > S3b(2, 7.1, 8)) == false);

    struct S4 { int x1, x2; float y; int z; }
    assert(structCmp(S4(1, 2, 7.1, 8), S4(1, 2, 7.1, 8)) == 0);
    assert(structCmp(S4(1, 2, 7.1, 8), S4(1, 2, 7.1, 7)) == 1);
    assert(structCmp(S4(1, 2, 7.1, 7), S4(1, 2, 7.1, 8)) == -1);


    struct St1 { int i, j; }
    struct St2 { int i, j, k; }
    auto s1 = St1(10, 20);
    auto s2 = St2(10, 30, 2);
    assert(structCmp(s1, s2) < 0);

    struct St3 { St1 s1; St2 s2; }
    auto s3a = St3(St1(10, 20), St2(10, 30, 2));
    auto s3b = St3(St1(10, 20), St2(10, 30, 2));
    auto s3c = St3(St1(10, 20), St2(10, 10, 2));
    auto s3d = St3(St1(10, 20), St2(10, 30, 1));
    struct St4 { St2 s2; St1 s1; }
    auto s4a = St4(St2(10, 20, 50), St1(10, 30));
    assert( structCmp(s3a, s3b) == 0);
    assert( structCmp(s3a, s3c) > 0);
    assert( structCmp(s3c, s3a) < 0);
    assert( structCmp(s3a, s3d) > 0);
    assert( structCmp(s3d, s3a) < 0);
    assert( structCmp(s3a, s4a) < 0);
    assert( structCmp(s4a, s3a) > 0);
} // End tests of structCmp()

Bye,
bearophile



More information about the Digitalmars-d mailing list