Array of circular buffers

Hello all,

I'm aiming to make a large program that scans through a lot of simulated data.
In order to do this, I'm using circular buffers (or deques). However, I now need to build up the program with arrays of circular buffers.

I would do something like this in 1D (i.e. just using a single circular buffer):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
  Int_t ringSize = 100;

  Int_t iMax = 20000; // in the physics simulation, iMax is extremely large, so I need to avoid arrays due to memory limits
  
  boost::circular_buffer<double> y(ringSize);
 

  // Fill up buffer to all zeros
  for(Int_t i = 0; i < ringSize; i++)
    {
      y.push_back(0);
    }

  // Fill up buffer with desired values and print full buffer each iteration
  for(Int_t i = 0; i < iMax; i++)
    {

      y.push_back(rand()/100000000);

      for(Int_t j = 0; j < ringSize; j++)
	{
	  cout << "y["<<j<<"] = " << y[j] << endl;
	}
      cout << endl;
    }
    


How would I extend this so that I have a circular buffer inside an array, where the size of both the array and the circular buffer fixed length is declared at the start?

After this, how would I manipulate (i.e. fill, print) elements of each circular buffer?

I've tried using:

std::vector <boost::circular_buffer<int>> arrayOfCBs(5, boost::circular_buffer<int>(50))

but I'm unable to easily manipulate elements of the circular buffers.

Many thanks
Last edited on
boost::circular_buffer is a standard library compatible sequence container.
(except for the distinction between resize and rresize)

Something along these lines:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <boost/circular_buffer.hpp>
#include <vector>
#include <random>
#include <ctime>
#include <iomanip>

template < typename SEQ >
void print_seq( const SEQ& sequence, int width = 12, std::ostream& stm = std::cout )
{
    for( const auto& value : sequence ) stm << std::setw(width) << value << ' ' ;
    stm << '\n' ;
}

std::vector< boost::circular_buffer<double> > make_buffers( std::size_t num_buffers, std::size_t  buff_sz )
{
    std::vector< boost::circular_buffer<double> > buffers ;
    while( buffers.size() < num_buffers )
    {
        buffers.emplace_back(buff_sz) ; // add a ring buffer with capacity of buff_sz
        buffers.back().rresize(buff_sz) ; // resize to full capacity; initialise with zeroes
    }
    return buffers ;
}

double random_value()
{
    static std::mt19937 rng( std::time(nullptr) ) ; // random number generatot
    static std::uniform_real_distribution<double> distrib( 0, 99'999'999 ) ; // random number distribution
    return distrib(rng) ;
}

void random_fill( boost::circular_buffer<double>& ring_buffer )
{ for( double& value : ring_buffer ) value = random_value() ; }

int main()
{
    constexpr std::size_t ARRAY_SIZE = 4 ;
    constexpr std::size_t RING_BUFFER_SIZE = 8 ; // just an example

    // make an array of ARRAY_SIZE circular buffers each with RING_BUFFER_SIZE values
    std::vector< boost::circular_buffer<double> > buffers = make_buffers( ARRAY_SIZE, RING_BUFFER_SIZE );

    // print the content of the buffers
    std::cout << std::fixed << std::setprecision(2) ;
    for( const auto& ring_buffer : buffers ) print_seq(ring_buffer) ; // all zeroes right now
    
    std::cout << "-------------------------------------------------------------------------------------\n" ;

    // fill the buffers with random values
    for( auto& ring_buffer : buffers ) random_fill(ring_buffer) ;

    // print the contents again
    for( const auto& ring_buffer : buffers ) print_seq(ring_buffer) ;
}

http://coliru.stacked-crooked.com/a/526efef447cf63f1
http://rextester.com/TEFIH2842
Topic archived. No new replies allowed.