boost range dynamic joined_range

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

boost range dynamic joined_range

Boost - Users mailing list
Hello,

How can I create a Boost Range for multiple vectors without setting any limitation to the number of vector at compile time? 

Using Boost 1.70 and it is not an option to store all data in one vector.

The following code compile on Visual Studio 2017 and will output the number 1 to 9. However, this code is compiled to support 3 vectors inside the foo struct. In order to support 4 vectors I would need to re-compile the code. The number of vectors is only known at runtime. How can this code be improved to support a dynamic number of vectors ?

#include <iostream>
#include <vector>
#include <boost/range.hpp>
#include <boost/range/join.hpp>

struct foo
{
  typedef boost::range::joined_range<
    boost::range::joined_range<
      std::vector<int>,
      std::vector<int>>,
    std::vector<int>> range;

  std::vector<std::vector<int>> vec;

  foo()
  {
    vec = { {1,2,3}, {4,5,6}, {7,8,9} };
  }

  range my_range()
  {
    auto range1 = boost::join(vec[0], vec[1]);
    return boost::join(range1, vec[2]);
  }
};

int main()
{
  foo f1;
  for (auto item : f1.my_range())
    std::cout << item << " ";

  return 0;
}

Appreciate any input.

Kind regards, Lars

_______________________________________________
Boost-users mailing list
[hidden email]
https://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: boost range dynamic joined_range

Boost - Users mailing list

On Sun, 5 Apr 2020 at 14:31, Lars Sunde via Boost-users <[hidden email]> wrote:
The following code compile on Visual Studio 2017 and will output the number 1 to 9.

This code will do so: https://stackoverflow.com/a/50660987 .

degski
--
@systemdeg
"We value your privacy, click here!" Sod off! - degski
"Anyone who believes that exponential growth can go on forever in a finite world is either a madman or an economist" - Kenneth E. Boulding
"Growth for the sake of growth is the ideology of the cancer cell" - Edward P. Abbey

_______________________________________________
Boost-users mailing list
[hidden email]
https://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: boost range dynamic joined_range

Boost - Users mailing list
Appreciate the response but do not think this answers my question.

Maybe I did not explain myself properly. I would like the foo object to return a (boost) range object that the caller can traverse without knowing the details of the internal structure. The foo object might contain a configurable number of vector of int. 

So in one scenario foo will be initalized like this; 
vec = { {1,2,3}, {4,5,6}, {7,8,9} }; 
and in another scenario like this;
vec = { {1,2,3}, {4,5,6}, {7,8,9}, {10,11,12} }; 

I do not want to create another vector "master" that contains all int.

Appreciate any input 🙂

kind regards, Lars


Fra: degski <[hidden email]>
Sendt: søndag 5. april 2020 23:08
Til: Boost users list <[hidden email]>
Kopi: Lars Sunde <[hidden email]>
Emne: Re: [Boost-users] boost range dynamic joined_range
 

On Sun, 5 Apr 2020 at 14:31, Lars Sunde via Boost-users <[hidden email]> wrote:
The following code compile on Visual Studio 2017 and will output the number 1 to 9.

This code will do so: https://stackoverflow.com/a/50660987 .

degski
--
@systemdeg
"We value your privacy, click here!" Sod off! - degski
"Anyone who believes that exponential growth can go on forever in a finite world is either a madman or an economist" - Kenneth E. Boulding
"Growth for the sake of growth is the ideology of the cancer cell" - Edward P. Abbey

_______________________________________________
Boost-users mailing list
[hidden email]
https://lists.boost.org/mailman/listinfo.cgi/boost-users
Reply | Threaded
Open this post in threaded view
|

Re: boost range dynamic joined_range

Boost - Users mailing list
This stack-overflow quarion and answer looks relevant.

It seems as if boost could benefit from a flattening_iterator type:

 

On Mon, 6 Apr 2020 at 08:41, Lars Sunde via Boost-users <[hidden email]> wrote:
Appreciate the response but do not think this answers my question.

Maybe I did not explain myself properly. I would like the foo object to return a (boost) range object that the caller can traverse without knowing the details of the internal structure. The foo object might contain a configurable number of vector of int. 

So in one scenario foo will be initalized like this; 
vec = { {1,2,3}, {4,5,6}, {7,8,9} }; 
and in another scenario like this;
vec = { {1,2,3}, {4,5,6}, {7,8,9}, {10,11,12} }; 

I do not want to create another vector "master" that contains all int.

Appreciate any input 🙂

kind regards, Lars


Fra: degski <[hidden email]>
Sendt: søndag 5. april 2020 23:08
Til: Boost users list <[hidden email]>
Kopi: Lars Sunde <[hidden email]>
Emne: Re: [Boost-users] boost range dynamic joined_range
 

On Sun, 5 Apr 2020 at 14:31, Lars Sunde via Boost-users <[hidden email]> wrote:
The following code compile on Visual Studio 2017 and will output the number 1 to 9.

This code will do so: https://stackoverflow.com/a/50660987 .

degski
--
@systemdeg
"We value your privacy, click here!" Sod off! - degski
"Anyone who believes that exponential growth can go on forever in a finite world is either a madman or an economist" - Kenneth E. Boulding
"Growth for the sake of growth is the ideology of the cancer cell" - Edward P. Abbey
_______________________________________________
Boost-users mailing list
[hidden email]
https://lists.boost.org/mailman/listinfo.cgi/boost-users


--
Richard Hodges
office: +442032898513
home: +376841522
mobile: +376380212


_______________________________________________
Boost-users mailing list
[hidden email]
https://lists.boost.org/mailman/listinfo.cgi/boost-users