circular_buffer: How to make push_back() increase capacity?
Hello, I have a list of colors that I need to be able to cycle through, either by rotating the container or by using an iterator that will rotate (i.e. return to the beginning when I increment at the end, and vice-versa). The goal is to be able to iterate through the container in a circular way and process all of the colors repeatedly. I chose a boost::circular_buffer for this job, I assumed it was the perfect tool for the job. It works great, except I would like to be able to call push_back() to add all of my colors to the buffer without having to call set_capacity() on it first. It's a minor thing, but omitting the call to set_capacity() would make it slightly more scalable/manageable since I won't have to keep incrementing the size of the capacity each time I want to add colors to the buffer. How can I achieve this? --------- Robert Dailey
Hi Robert,
I'm afraid circular_buffer is not suitable for this task. You need something like circular iterator that will iterate over a vector or a list. There used to be circular iterator implementation in the boost sandbox; not sure what is the status of this library at the moment.
Regards,
Jan
________________________________
From: Robert Dailey
On Fri, Aug 28, 2009 at 3:17 PM, Robert Dailey
Hello, I have a list of colors that I need to be able to cycle through, either by rotating the container or by using an iterator that will rotate (i.e. return to the beginning when I increment at the end, and vice-versa). The goal is to be able to iterate through the container in a circular way and process all of the colors repeatedly. I chose a boost::circular_buffer for this job, I assumed it was the perfect tool for the job. It works great, except I would like to be able to call push_back() to add all of my colors to the buffer without having to call set_capacity() on it first. It's a minor thing, but omitting the call to set_capacity() would make it slightly more scalable/manageable since I won't have to keep incrementing the size of the capacity each time I want to add colors to the buffer. How can I achieve this?
I'm not familiar with circular_buffer, but something quick n' dirty: std::vector<color> colors; colors.push_back(color()); ... for(std::vector<color>::size_type i = 0; i < colors.size(); i = (i + 1) % colors.size()) { color &c = colors[i]; } -- Cory Nelson http://int64.org
Zitat von Cory Nelson
On Fri, Aug 28, 2009 at 3:17 PM, Robert Dailey
wrote: Hello, I have a list of colors that I need to be able to cycle through, either by rotating the container or by using an iterator that will rotate (i.e. return to the beginning when I increment at the end, and vice-versa). The goal is to be able to iterate through the container in a circular way and process all of the colors repeatedly. I chose a boost::circular_buffer for this job, I assumed it was the perfect tool for the job. It works great, except I would like to be able to call push_back() to add all of my colors to the buffer without having to call set_capacity() on it first. It's a minor thing, but omitting the call to set_capacity() would make it slightly more scalable/manageable since I won't have to keep incrementing the size of the capacity each time I want to add colors to the buffer. How can I achieve this?
a circular buffer is designed to have a specific capacity. you can cycle through any container.
I'm not familiar with circular_buffer, but something quick n' dirty:
std::vector<color> colors;
colors.push_back(color()); ...
for(std::vector<color>::size_type i = 0; i < colors.size(); i = (i + 1) % colors.size()) { color &c = colors[i]; }
I've got an easier way to write that: while(true); if you want to end the iteration at some point: container::iterator it=start; while(...){ ... if(it == container.end()) it=container.begin(); } if you often need to do something like that consider writing an own iterator. that can be achieved in just a view lines of code in this case with boost::iterator_adaptor.
participants (4)
-
Cory Nelson
-
Jan Gaspar
-
Robert Dailey
-
strasser@uni-bremen.de