[Issue 3199] New: sort(chain(...)) doesn't work in some cases

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Jul 21 14:19:00 PDT 2009


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

           Summary: sort(chain(...)) doesn't work in some cases
           Product: D
           Version: 2.031
          Platform: x86
        OS/Version: Linux
            Status: NEW
          Severity: normal
          Priority: P2
         Component: Phobos
        AssignedTo: nobody at puremagic.com
        ReportedBy: adolf.mathias at googlemail.com


Having become curious after reading the Dr Dobb's article by Andrei
Alexandrescu, I tried:

import std.algorithm, std.random, std.stdio, std.range;

void main() {
  int[] a,b,c;

  auto abc=[a,b,c];

  foreach(ref t;abc) {
    t.length = uniform(8,17);
    foreach(ref v;t) v = uniform(0,100);
  }

  foreach(t;abc) writefln("[%s]",t);
  sort(chain(a,b,c));
  foreach(t;abc) writefln("[%s]",t);
}

which, when run, doesn't print anything different from the first foreach in the
second foreach.

Only when I replace the dynamic initialization of a, b, c:

import std.algorithm, std.random, std.stdio, std.range;

void main() {
  int[] a,b,c;

  auto abc=[a,b,c];

  foreach(t;abc) writefln("[%s]",t);
  sort(chain(a,b,c));
  foreach(t;abc) writefln("[%s]",t);
}

things work as expected.

When I overwrite a,b,c in the second example with random values like in the
first example:

import std.algorithm, std.random, std.stdio, std.range;

void main() {
  int[] a = [16,80,6,43,43,6,29,16,1,64,75,59,40,5,63], 
        b = [43,11,71,39,32,82,94,42,18],
        c = [17,70,93,96,64,3,24,84,88];

  auto abc=[a,b,c];

  foreach(ref t;abc) {
    t.length = uniform(8,17);
    foreach(ref v;t) v = uniform(0,100);
  }

  foreach(t;abc) writefln("[%s]",t);
  sort(chain(a,b,c));
  foreach(t;abc) writefln("[%s]",t);
}

the data in the arrays is reordered, but the sorting seems to stop somewhere
in-between, usually either b or c have not been sorted.

-- 
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