Variable Arguments

For discussion of the xlw project for producing xlls.

Variable Arguments

Postby gleesonger » Wed Jun 24, 2015 11:51 pm

I'm looking to add in variable arguments (params array), similar to what you would get in SumIfs, ie. SumIfs(sum_range,[criteria_range1,criteria1],[criteria_range2,criteria2],[criteria_range3,criteria3],.....)
At the moment I am using optional arguments as shown below, which forms a vector with all arguments up to the last non-empty argument.
Is there a nicer\recommended way of doing this?

Code: Select all
CellMatrix SortArrayV(CellMatrix rng,
   CellMatrix sort_key_01, CellMatrix sort_dir_01,
   CellMatrix sort_key_02, CellMatrix sort_dir_02,
   CellMatrix sort_key_03, CellMatrix sort_dir_03,
   ...
   CellMatrix sort_key_20, CellMatrix sort_dir_20
   )
{
   std::vector<CellMatrix> args;

   args.push_back(sort_key_01); args.push_back(sort_dir_01);
   args.push_back(sort_key_02); args.push_back(sort_dir_02);
   args.push_back(sort_key_03); args.push_back(sort_dir_03);
   ...
   args.push_back(sort_key_20); args.push_back(sort_dir_20);

   while (args.size()>0)
   {
      if (args.back().ColumnsInStructure() == 1 &&
         args.back().RowsInStructure() == 1 &&
         args.back()(0, 0).IsEmpty())
      {
         args.pop_back();
      }
      else
         break;
   }

   return CellMatrix((double)args.size());
}


Note I also tried another method which took a pointer to the first variable argument, max number of arguments and went from there.
It caused Excel to crash so I abandoned that method, I also wasn't sure how the compiler would allocate the args so it seems very suspect either way.

Code: Select all
std::vector<CellMatrix> GetParamsArray(IN CellMatrix* ary, IN uint16_t max_num_params, OUT std::vector<CellMatrix>& args) {

   std::vector<CellMatrix> args_skipped;
   args_skipped.reserve(max_num_params);

   for (uint16_t i = 0; i < max_num_params; i++) {
      bool skip = ary[i].ColumnsInStructure() == 1 && ary[i].RowsInStructure() == 1 && ary[i](0, 0).IsEmpty();

      if (skip)
      {
         args_skipped.push_back(ary[i]);
      }
      else
      {
         if (args_skipped.size()>0){
            args.insert(args.end(), args_skipped.begin(), args_skipped.end());
            args_skipped.clear();
         }

         args.push_back(ary[i]);
      }
   }
}
gleesonger
 
Posts: 2
Joined: Wed Jun 24, 2015 11:39 pm

Re: Variable Arguments

Postby gleesonger » Thu Jun 25, 2015 9:25 am

Made a silly mistake late last night (head is working a bit better this morning).
My current method is below, I would still like it to be cleaner but this will do for now.

I also find the identification of a missing argument a bit odd, I would have expected CellMatrix(0,0) instead of CellMatrix(1,1).
However, except maybe for the case in which the user implicitly takes an empty cell as a double of value 0, I suppose in all piratical uses it is fine.

Code: Select all
void GetParamsArray(IN CellMatrix* ary, IN uint16_t max_num_params, OUT std::vector<CellMatrix>& args) {

   std::vector<CellMatrix> args_skipped;
   args_skipped.reserve(max_num_params);

   for (uint16_t i = 0; i < max_num_params; i++) {
      CellMatrix& arg = ary[i];

      bool skip = arg.ColumnsInStructure() == 1 && arg.RowsInStructure() == 1 && arg(0, 0).IsEmpty();

      if (skip)
      {
         args_skipped.push_back(arg);
      }
      else
      {
         if (args_skipped.size()>0){
            args.insert(args.end(), args_skipped.begin(), args_skipped.end());
            args_skipped.clear();
         }

         args.push_back(arg);
      }
   }
}

CellMatrix SortArrayV(CellMatrix& rng,
   CellMatrix sort_key_01, CellMatrix sort_dir_01,
   CellMatrix sort_key_02, CellMatrix sort_dir_02,
   CellMatrix sort_key_03, CellMatrix sort_dir_03,
   CellMatrix sort_key_04, CellMatrix sort_dir_04,
   CellMatrix sort_key_05, CellMatrix sort_dir_05,
   CellMatrix sort_key_06, CellMatrix sort_dir_06,
   CellMatrix sort_key_07, CellMatrix sort_dir_07,
   CellMatrix sort_key_08, CellMatrix sort_dir_08,
   CellMatrix sort_key_09, CellMatrix sort_dir_09,
   CellMatrix sort_key_10, CellMatrix sort_dir_10,
   CellMatrix sort_key_11, CellMatrix sort_dir_11,
   CellMatrix sort_key_12, CellMatrix sort_dir_12,
   CellMatrix sort_key_13, CellMatrix sort_dir_13,
   CellMatrix sort_key_14, CellMatrix sort_dir_14,
   CellMatrix sort_key_15, CellMatrix sort_dir_15,
   CellMatrix sort_key_16, CellMatrix sort_dir_16,
   CellMatrix sort_key_17, CellMatrix sort_dir_17,
   CellMatrix sort_key_18, CellMatrix sort_dir_18,
   CellMatrix sort_key_19, CellMatrix sort_dir_19,
   CellMatrix sort_key_20, CellMatrix sort_dir_20
   )
{
   std::vector<CellMatrix> args;

   GetParamsArray(&sort_key_01, 40, args);

   return CellMatrix((double)args.size());
}

gleesonger
 
Posts: 2
Joined: Wed Jun 24, 2015 11:39 pm


Return to xlw

Who is online

Users browsing this forum: Google [Bot] and 1 guest

cron