Skip to content

Commit

Permalink
Merge pull request #1198 from chalucha/ringbuff_opApply
Browse files Browse the repository at this point in the history
Implementation of indexed opApply for FixedRingBuffer
  • Loading branch information
s-ludwig committed Jul 29, 2015
2 parents 0c6e8ef + 5aeb9b4 commit f20da21
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions source/vibe/utils/array.d
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,24 @@ struct FixedRingBuffer(T, size_t N = 0) {
return 0;
}

/// iterate through elements with index
int opApply(scope int delegate(size_t i, ref T itm) del)
{
if( m_start+m_fill > m_buffer.length ){
foreach(i; m_start .. m_buffer.length)
if( auto ret = del(i - m_start, m_buffer[i]) )
return ret;
foreach(i; 0 .. mod(m_start+m_fill))
if( auto ret = del(i + m_buffer.length - m_start, m_buffer[i]) )
return ret;
} else {
foreach(i; m_start .. m_start+m_fill)
if( auto ret = del(i - m_start, m_buffer[i]) )
return ret;
}
return 0;
}

ref inout(T) opIndex(size_t idx) inout { assert(idx < length); return m_buffer[mod(m_start+idx)]; }

Range opSlice() { return Range(m_buffer, m_start, m_fill); }
Expand Down Expand Up @@ -459,6 +477,14 @@ unittest {
assert(buf.length == 2 && buf.freeSpace == 3);
buf.read(dst[0 .. 2]); //|. . . . .
assert(dst[0 .. 2] == [1, 2]);

buf.put([0, 0, 0, 1, 2]); //|0 0 0 1 2
buf.popFrontN(2); //. .|0 1 2
buf.put([3, 4]); // 3 4|0 1 2
foreach(i, item; buf)
{
assert(i == item);
}
}


Expand Down

0 comments on commit f20da21

Please sign in to comment.