Classes and templates
Hefferman via Digitalmars-d
digitalmars-d at puremagic.com
Mon Oct 31 12:24:46 PDT 2016
Hello,
I've been trying to implement a class for sorting which accepts
arrays which "different" data types. Hard to describe, here's my
example (filename sorting.d):
[code]
import std.random;
void main()
{
immutable uint N = 10_000;
double[] arr = new double[](N);
for (uint k = 0; k < N; k++) arr[k] = uniform(0, N);
BubbleSort b = new BubbleSort();
b.Sort(arr);
}
public class BubbleSort(T)
{
private T[] a;
private uint n;
public void Sort(T)(T[] a) {
this.a = a;
n = a.length;
bubblesort();
}
private void bubblesort() {
bool sorted;
do {
sorted = true;
for (uint k = 0; k < n; k++) {
if (a[k] > a[k+1]) {
T tmp = a[k];
a[k] = a[k+1];
a[k+1] = tmp;
sorted = false;
}
}
n--;
} while (!sorted);
}
}
[/code]
Whenever I try to compile this, it throws an error "class
sorting.BubbleSort(T) is used as a type". How do I get a
workaround?
More information about the Digitalmars-d
mailing list