What is the most concise/efficient function (in C++) to convert a “cyclic” index in a real index ?
My problem can be illustrated by the following image :

I have a real list (a C++ vector for instance) of size size containing elements (here A, B, C, D, E). In order to mimic a “cyclic” list I am searching for a function to convert an input index (from -inf to +inf) to the real index of the list. With this function and the image example the code :
for(i=-10; i < 10; ++i) std::cout<<list[myFunction(i, list.size())]<<" ";
will print 4 times the list as displayed in the image.
My current expression of myFunction is :
inline int myFunction(const int i, const int size)
{
return (i >= 0) ? (i%size) : ((size-((-i)%size))%size);
}
Do you think that there is a more simple/concise/efficient way to write this ?
There are a couple of more concise ways of representing this. Both involve the fact that if
i < 0,(i % size) + sizeis the number you are looking for. So, you can useOR