www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - May std.range.cycle be empty?

I filed bug https://issues.dlang.org/show_bug.cgi?id=13390 but have a 
question with it. I use cycle to implement simple ring buffer.

struct RingBuffer(T)
{
     private T[] _data;
     private size_t _index;
     private size_t _length;

     auto opSlice() const
     {
	return cycle(_data[0.._length]).take(_length);
     }

      property
     auto length() { return _length; }

     this(T[] data, size_t length = 0)
     {
         enforce(data.length, "empty ring buffer is prohibited");
         enforce(length <= data.length, "buffer length shall not be more 
than buffer capacity");
         _data = data;
         _index = 0;
         _length = length;
     }
}

Ring buffer has its own length besides length of underlying buffer. The 
underlying buffer may not have zero length and it checking for it can be 
realized easy in ctor. The ring buffer itself may have length equal to 
zero. The question is should checking for zero equality be moved to 
cycle or it should stay in ring buffer implementation like this?

     auto opSlice() const
     {
         if(_length == 0)
             return cycle(_data).take(_length);
	else
             return cycle(_data[0.._length]).take(_length);
     }

If cycle may not be empty, then we have to explicitly state it. But I 
guess then cycle loss its generality, because
	auto foo = some_range.take(bar).cycle;
if bar become equal to zero code will fail.
Aug 29 2014