www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Classes and templates

reply Hefferman <orf736xhfc bzjwe.anonbox.net> writes:
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?
Oct 31 2016
next sibling parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Monday, 31 October 2016 at 19:24:46 UTC, Hefferman wrote:
 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?
Give a template type when crating it new BubbleSort!(string) for example
Oct 31 2016
parent reply Hefferman <orf736xhfc bzjwe.anonbox.net> writes:
On Monday, 31 October 2016 at 19:43:57 UTC, Adam D. Ruppe wrote:
 On Monday, 31 October 2016 at 19:24:46 UTC, Hefferman wrote:

 Give a template type when crating it

 new BubbleSort!(string)

 for example
Is it possible to create this using "typeof"? E. g. "BubbleSort b = new BubbleSort!(typeof(arr));" Compilation fails with that line....
Oct 31 2016
parent reply Meta <jared771 gmail.com> writes:
On Monday, 31 October 2016 at 20:06:22 UTC, Hefferman wrote:
 On Monday, 31 October 2016 at 19:43:57 UTC, Adam D. Ruppe wrote:
 On Monday, 31 October 2016 at 19:24:46 UTC, Hefferman wrote:

 Give a template type when crating it

 new BubbleSort!(string)

 for example
Is it possible to create this using "typeof"? E. g. "BubbleSort b = new BubbleSort!(typeof(arr));" Compilation fails with that line....
You can't use an un-instantiated template as a type anyway, unlike Java. There the above is illegal because of `BubbleSort b = ...`. The symbol BubbleSort by itself is not a type; you have to instantiate it with template arguments to create a type. So you *could* do it like this: BubbleSort!(typeof(arr)) b = new BubbleSort!(typeof(arr)); But that's too verbose. There's need need to repeat ourselves. You can instead use `auto` and omit the type of `b`, which will be inferred from the right hand side: auto b = new BubbleSort!(typeof(arr));
Oct 31 2016
parent reply Hefferman <orf736xhfc bzjwe.anonbox.net> writes:
On Monday, 31 October 2016 at 20:20:09 UTC, Meta wrote:
 [...]

 You can't use an un-instantiated template as a type anyway, 
 unlike Java. There the above is illegal because of `BubbleSort 
 b = ...`. The symbol BubbleSort by itself is not a type; you 
 have to instantiate it with template arguments to create a 
 type. So you *could* do it like this:

 BubbleSort!(typeof(arr)) b = new BubbleSort!(typeof(arr));

 But that's too verbose. There's need need to repeat ourselves. 
 You can instead use `auto` and omit the type of `b`, which will 
 be inferred from the right hand side:

 auto b = new BubbleSort!(typeof(arr));
Thanks! This compiled just fine... [code] import std.random; import std.stdio; void main() { immutable uint N = 10_000; double[] arr = new double[](N); for (uint k = 0; k < N; k++) arr[k] = uniform(0, N); auto b = new BubbleSort!(typeof(arr)); b.Sort(arr); } public class BubbleSort(T) { private T[] a; private uint n; public void Sort(T)(T[] a) { n = a.length; bubblesort(); } private void bubblesort() { bool sorted; do { sorted = true; for (uint k = 1; k < n; k++) { if (a[k-1] > a[k]) { T tmp = a[k]; a[k] = a[k+1]; a[k+1] = tmp; sorted = false; } } n--; } while (!sorted); } } [/code] It gives a Range Violation upon executing, but I guess it's part of the algorithm....
Oct 31 2016
parent Kapps <opantm2+spam gmail.com> writes:
On Monday, 31 October 2016 at 20:25:18 UTC, Hefferman wrote:
             for (uint k = 1; k < n; k++) {
                 if (a[k-1] > a[k]) {
                     T tmp = a[k];
                     a[k] = a[k+1];
                     a[k+1] = tmp;
                     sorted = false;
                 }
             }
             n--;
         } while (!sorted);
     }
 }
 [/code]

 It gives a Range Violation upon executing, but I guess it's 
 part of the algorithm....
The value of n is array length, so k reaches (n - 1), therefore k+1 gives you n which is out of bounds.
Oct 31 2016
prev sibling parent reply John Colvin <john.loughran.colvin gmail.com> writes:
On Monday, 31 October 2016 at 19:24:46 UTC, Hefferman wrote:
 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):

 [...]
Glad to see you're getting helpful responses here, but just an FYI: https://forum.dlang.org/group/learn would be a more appropriate forum for questions like this.
Oct 31 2016
parent Hefferman <3s3cv5r4dk b6fzp.anonbox.net> writes:
On Monday, 31 October 2016 at 22:33:11 UTC, John Colvin wrote:
 On Monday, 31 October 2016 at 19:24:46 UTC, Hefferman wrote:
 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):

 [...]
Glad to see you're getting helpful responses here, but just an FYI: https://forum.dlang.org/group/learn would be a more appropriate forum for questions like this.
Thanks to all for replying! J.C.: I'll keep it in mind for the next time.
Nov 01 2016