chunkBy bug?

JG someone at somewhere.com
Sat Sep 5 03:30:33 UTC 2020


The following code won't compile:
----
import std.algorithm;
import std.range;
import std.stdio;

void main() {
  auto v = [2,4,8,3,6,9,1,5,7];
  foreach(i; 2..4) {
   writeln(v.myChunkBy!((a,b)=>a%i==b%i));
  }
}
----
/opt/local/include/phobos/std/algorithm/iteration.d(2009): Error: 
function tmp.main.ChunkByImpl!(__lambda1, 
int[]).ChunkByImpl.Group.popFront cannot access frame of function 
D main
----

Is this expected behavior?

I can get it to run as expected by modifying the source of 
chunkBy as follows:

----

import std.range;
import std.stdio;
import std.traits;
import std.functional;
private template ChunkByImplIsUnary(alias pred, Range)
{
  alias e = lvalueOf!(ElementType!Range);

  static if (is(typeof(binaryFun!pred(e, e)) : bool))
                                               enum 
ChunkByImplIsUnary = false;
  else static if (is(typeof(unaryFun!pred(e) == unaryFun!pred(e)) 
: bool))
                                                                   
  enum ChunkByImplIsUnary = true;
  else
   static assert(0, "chunkBy expects either a binary predicate or 
"~
     "a unary predicate on range elements of type: "~
     ElementType!Range.stringof);
}

// Outer range
struct Impl(Range)
{
  size_t groupNum;
  Range  current;
  Range  next;
}
// Inner range
struct Group(alias eq, Range)
{
  import std.typecons : RefCounted;
  private size_t groupNum;
  private Range  start;
  private Range  current;

  private RefCounted!(Impl!Range) mothership;

  this(RefCounted!(Impl!Range) origin)
  {
   groupNum = origin.groupNum;

   start = origin.current.save;
   current = origin.current.save;
   assert(!start.empty, "Passed range 'r' must not be empty");

   mothership = origin;

   // Note: this requires reflexivity.
   assert(eq(start.front, current.front),
     "predicate is not reflexive");
  }

  @property bool empty() { return groupNum == size_t.max; }
  @property auto ref front() { return current.front; }

  void popFront()
  {
   current.popFront();

   // Note: this requires transitivity.
   if (current.empty || !eq(start.front, current.front))
   {
    if (groupNum == mothership.groupNum)
    {
     // If parent range hasn't moved on yet, help it along by
     // saving location of start of next Group.
     mothership.next = current.save;
    }

    groupNum = size_t.max;
   }
  }

  @property auto save()
  {
   auto copy = this;
   copy.current = current.save;
   return copy;
  }
}

private struct ChunkByImpl(alias pred, Range)
if (isForwardRange!Range)
{
  import std.typecons : RefCounted;

  enum bool isUnary = ChunkByImplIsUnary!(pred, Range);

  static if (isUnary)
   alias eq = binaryFun!((a, b) => unaryFun!pred(a) == 
unaryFun!pred(b));
  else
   alias eq = binaryFun!pred;

  static assert(isForwardRange!(Group!(eq,Range)));

  private RefCounted!(Impl!Range) impl;

  this(Range r)
  {
   impl = RefCounted!(Impl!Range)(0, r, r.save);
  }

  @property bool empty() { return impl.current.empty; }

  @property auto front()
  {
   static if (isUnary)
   {
    import std.typecons : tuple;
    return tuple(unaryFun!pred(impl.current.front), Group(impl));
   }
   else
   {
    return Group!(eq,Range)(impl);
   }
  }

  void popFront()
  {
   // Scan for next group. If we're lucky, one of our Groups would 
have
   // already set .next to the start of the next group, in which 
case the
   // loop is skipped.
   while (!impl.next.empty && eq(impl.current.front, 
impl.next.front))
   {
    impl.next.popFront();
   }

   impl.current = impl.next.save;

   // Indicate to any remaining Groups that we have moved on.
   impl.groupNum++;
  }

  @property auto save()
  {
   // Note: the new copy of the range will be detached from any 
existing
   // satellite Groups, and will not benefit from the .next 
acceleration.
   return typeof(this)(impl.current.save);
  }

  static assert(isForwardRange!(typeof(this)), 
typeof(this).stringof
    ~ " must be a forward range");
}

auto chunkBy(alias pred, Range)(Range r)
  if (isInputRange!Range)
{
      return ChunkByImpl!(pred, Range)(r);
}

void main() {
  auto v = [2,4,8,3,6,9,1,5,7];
  foreach(i; 2..4) {
   writeln(v.chunkBy!((a,b)=>a%i==b%i));
  }
}
---


More information about the Digitalmars-d mailing list