import std.stdio, std.algorithm, std.array, std.traits,std.range, std.typecons,std.complex;

enum AreSortableArrayItems(T) = isMutable!T &&
__traits(compiles, T.init < T.init) &&
                                !isNarrowString!(T[]);

void selectionSort(T)(T[] data) if (AreSortableArrayItems!T) {
    foreach (immutable i, ref d; data)
        data.drop(i).minPos[0].swap(d);
}
void main() {
    auto a = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9, 3, 2];
    a.selectionSort;
    a.writeln;
    int[] a0;
    a0.selectionSort;
    a0.writeln;

    auto a1 = [1];
    a1.selectionSort;
    a1.writeln;

    auto a2 = ["a", "b"];
    a2.selectionSort;
    a2.writeln;

    auto a3 = ["b", "a"];
    a3.selectionSort;
    a3.writeln;

//    char[] a4 = ['a', 'b'];
//    a4.selectionSort;
//    a4.writeln;

    dstring[] a5 = ["b", "a"];
    a5.selectionSort;
    a5.writeln;


    alias Nullable!int N;
    auto a6 = [N(2), N(1)];
    a6.selectionSort; // Not nothrow.
    a6.writeln;

//    auto a7 = [complex(1.0,0.0), complex(2.0,0.0)];
//    a7.selectionSort;
//    a7.writeln;


//  auto a8 = [complex(1.0L), complex(2.0L)];
//    a8.selectionSort;
//    a8.writeln;


    static struct F {
        int x;
        int opCmp(F f) { // Not pure.
            return x < f.x ? -1 : (x > f.x ? 1 : 0);
        }
    }
    auto a9 = [F(2), F(1)];
    a9.selectionSort;
    a9.writeln;
}
// dmd 2.069 rc2

Reply via email to