vendredi 2 février 2018

C++11 lookup table container for model output using switch/case or if/else

I have a model system ready to produce a list of variables at some time step. The users, on the other hand, are only interested in a subset of these variables at each time step. And I need to save them at each time step.

So how can I design a container/map to handle this? Currently I used a switch/case at each time step: within a iterator loop, if a desired variable (std::string) is inside the container, then it will be saved.

I fear this is not efficient enough because the program needs to judge that at every time step even though once is enough and I have 100+ variables in the table, and user desired could be 10+.

Is there a better way to do it?

Below is a pseudo code example I can now provide:

//vector or map for lookup table
map.clear();
map.insert(key-value pair);
map.insert(key-value pair);
map.insert(key-value pair);
map.insert(key-value pair);
map.insert(key-value pair);
...

void main()
{
   get_user_data();
   run_model();
} 
function get_user_data()
{
   while(~eof)
   {
     read_line_data;
     map.insert(a desired variable key-vale);
     //or vector.push_bash(variable) 
   }

}
function run_model()
{
   for(iTime_step=1; 100000+; iTimestep++)
   {
     some process;
     save();  
   }
}
function save()
{
  for(i=0;i<desired_container_length,i++) //use iterator
  {
    key = desired_container.at(i);
    switch(key)
    {
      case 1: do something
      case 2: do something else
      ...       
    }     
  }
}

The key may be a std::string, or something else. In this case, using switch/case requires that the map key-value must be strictly ordered. If I use if/else, then the program must determine/compare with each lookup table object.

In both cases, in time step 2-~, the program have to repeat the same switch/case/ or if/else, which seems to be unnecessary.

Thank you.

Aucun commentaire:

Enregistrer un commentaire