Hello,
I'm using the nrf5 sdk 17.0.2.
I'm wounder why the current API of the nrf_queue doesn't implement a peek with random access? I know by definition a queue is a collection of sequential items, thus a random access in a queue would be O(n). However since this queue implementation uses an internal array with front and back indexes, we should be able to perform a random access in O(1), by playing with a front offset. There is possible implementation:static ret_code_t queue_peek(nrf_queue_t const *queue, void *elem, size_t index) {
ret_code_t status = NRF_SUCCESS;
ASSERT(queue != NULL);
ASSERT(elem != NULL);
CRITICAL_REGION_ENTER();
if (!nrf_queue_is_empty(queue)) {
// Get read position.
size_t read_pos = (queue->p_cb->front + index) % queue->size;
// Read element.
switch (queue->element_size) {
case sizeof(uint8_t):
*((uint8_t *) elem) = ((uint8_t *) queue->p_buffer)[read_pos];
break;
case sizeof(uint16_t):
*((uint16_t *) elem) = ((uint16_t *) queue->p_buffer)[read_pos];
break;
case sizeof(uint32_t):
*((uint32_t *) elem) = ((uint32_t *) queue->p_buffer)[read_pos];
break;
case sizeof(uint64_t):
*((uint64_t *) elem) = ((uint64_t *) queue->p_buffer)[read_pos];
break;
default:
memcpy(elem,
(void const *) ((size_t) queue->p_buffer + read_pos * queue->element_size),
queue->element_size);
break;
}
} else {
status = NRF_ERROR_NOT_FOUND;
}
CRITICAL_REGION_EXIT();
return status;
}
It is very similar to the current nrf_queue_generic_pop, maybe it could be merged in...
Let me know.
Best,
Joël