0

I have a huge data structure and it's content is updated regularly. I need to apply a filter to it each time I get a change request. So to avoid duplicating/copying it, I use an std::view + std::views::filter. (as lightweight container)

Having this class

class MyClass
{
   std::map<std::string, std::set<std::string>> myData;
   // How to add the view as class member based on myData ?
};

How can I create this view inside a class. What type to specify ? How to intialize it ? Is it possible to reassign it ?

Peter
  • 109
  • 7
  • I don't know what this question is asking. – Barry Aug 03 '23 at 18:18
  • The question is what's the concrete type to use and how to intialize, reassign the view class member – Peter Aug 03 '23 at 18:40
  • Having a view as a class member is pretty suspect, why do you think you need that? What are you trying to accomplish with it? – Barry Aug 03 '23 at 18:46
  • It's to avoid creating a filtred copy of myData. As views are lightweight containers – Peter Aug 03 '23 at 18:47
  • I... don't follow. You could write a function that returns a filtered view over myData. – Barry Aug 03 '23 at 18:49
  • This function will be called many times and do the same job and returning the same result. Why not storing the result as class member ? – Peter Aug 03 '23 at 18:51
  • Because the function is cheap, works, and is easy to reason about. – Barry Aug 03 '23 at 18:54
  • But this function will loop over all the map. In a hot/critic execution path it will be expensive – Peter Aug 03 '23 at 18:57
  • 1
    It will not "loop over all the map" - you're just creating a filtered view. That's a lazy operation, it does no work. – Barry Aug 03 '23 at 18:58
  • How is that possible? – Peter Aug 03 '23 at 19:17
  • 1
    Because that's how views work, that's what they are. I suggest you look into what views are, there are a few good talks on the topic (e.g. by Tristan Brindle). – Barry Aug 03 '23 at 19:49
  • @Barry, okay thank you very much for your assistance – Peter Aug 03 '23 at 20:02
  • @Barry, given those programs, https://godbolt.org/z/dT8eeW6jG and https://godbolt.org/z/345Wx1oeP. Built using gcc 13 and O3 optimization I got a huge difference regarding the execution time. 6 seconds for the version without view and 56 seconds for the program with view. Do I miss something? – Peter Aug 04 '23 at 15:04

0 Answers0