C++ circular iterator compliant with STL containers using Boost
circular_iterator converts a typical linear form container (like std::vector) to a circular form or so called ring. User would pass two iterators (e.g. begin and end) of the container to the circular_iterator constructor. circular_iterator will provide increment, decrement, operator ++ and operator --.
#include <circular_iterator.h>
#include <vector>
auto main()->int
{
auto const vec {std::vector<int>{1,2,3}};
auto c_iter {circular_iterator<decltype (vec.begin())>(vec.begin(), vec.end())};
while(true)
{
++c_iter;
}
}In the constructor it is checked that if the container is emtpy and if so it will throw an exception of type std::invalid_argument
Caveat: Using cyclic iterators has its drawbacks. It is never checked that the container is empty or not after the object has been constructed. It is expected that the container never going to be empty after that circular_iterator is constructed from it.
Note #1: Actualy that is why the standard library does not provide such iterators (see Caveat).
Note #2: Another way of having a cylic container is to construct a queue then in iterations push the most front element to the back of queue and pop it from the front. The drawback is the copy proccess of the front element to the back of the queue which is maybe an expensive operation to do.
circular_iterator is distributed under the MIT License.
Note: Depending on how you would use the library, you might not need to install it (see Basic Usage) ###Build from source: Clone the repository. Inside the directory run the following commands:
mkdir build
cmake -B buildNote: You might want to add
-DBUILD_TESTING=falseortruethen
cmake --build build --config Release --target installNote: The last step will build the library and install it system wide, therefore it might need admins rights.
The library officially supports two ways how it can be used with cmake. You can find examples for both methods in the examples folder.
- FetchContent (Recommended, no installation required)
- FindPackage (installation required, see above)
Both methods will provide the circular_iterator::circular_iterator target.
- Issues at Issues section
- Email at navidcity@gmail.com