mirror of
https://github.com/gnif/LookingGlass.git
synced 2025-01-09 22:37:04 +00:00
[common] ringbuffer: allow reverse iteration
This commit is contained in:
parent
91d1b8d2cd
commit
69f6532b8d
@ -158,7 +158,7 @@ static int graphs_render(void * udata, bool interactive,
|
|||||||
continue;
|
continue;
|
||||||
|
|
||||||
struct BufferMetrics metrics = {};
|
struct BufferMetrics metrics = {};
|
||||||
ringbuffer_forEach(graph->buffer, rbCalcMetrics, &metrics);
|
ringbuffer_forEach(graph->buffer, rbCalcMetrics, &metrics, false);
|
||||||
|
|
||||||
if (metrics.sum > 0.0f)
|
if (metrics.sum > 0.0f)
|
||||||
{
|
{
|
||||||
|
@ -42,4 +42,4 @@ void ringbuffer_setPreOverwriteFn(RingBuffer rb, RingBufferValueFn fn,
|
|||||||
|
|
||||||
typedef bool (*RingBufferIterator)(int index, void * value, void * udata);
|
typedef bool (*RingBufferIterator)(int index, void * value, void * udata);
|
||||||
void ringbuffer_forEach(const RingBuffer rb, RingBufferIterator fn,
|
void ringbuffer_forEach(const RingBuffer rb, RingBufferIterator fn,
|
||||||
void * udata);
|
void * udata, bool reverse);
|
||||||
|
@ -106,8 +106,27 @@ void ringbuffer_setPreOverwriteFn(const RingBuffer rb, RingBufferValueFn fn,
|
|||||||
rb->preOverwriteUdata = udata;
|
rb->preOverwriteUdata = udata;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ringbuffer_forEach(const RingBuffer rb, RingBufferIterator fn, void * udata)
|
void ringbuffer_forEach(const RingBuffer rb, RingBufferIterator fn, void * udata,
|
||||||
|
bool reverse)
|
||||||
{
|
{
|
||||||
|
if (reverse)
|
||||||
|
{
|
||||||
|
int index = rb->start + rb->count - 1;
|
||||||
|
if (index >= rb->length)
|
||||||
|
index -= rb->length;
|
||||||
|
|
||||||
|
for(int i = 0; i < rb->count; ++i)
|
||||||
|
{
|
||||||
|
void * value = rb->values + index * rb->valueSize;
|
||||||
|
if (--index == -1)
|
||||||
|
index = rb->length - 1;
|
||||||
|
|
||||||
|
if (!fn(i, value, udata))
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
int index = rb->start;
|
int index = rb->start;
|
||||||
for(int i = 0; i < rb->count; ++i)
|
for(int i = 0; i < rb->count; ++i)
|
||||||
{
|
{
|
||||||
@ -118,4 +137,5 @@ void ringbuffer_forEach(const RingBuffer rb, RingBufferIterator fn, void * udata
|
|||||||
if (!fn(i, value, udata))
|
if (!fn(i, value, udata))
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user