Logo Search packages:      
Sourcecode: gaffitter version File versions  Download package

bool Optimizer::Output (  )  [inherited]

Print the result to standard output.

Print selected files, a brief summary (sum, diff and number of selected files) and optionally the same for unselected files.

Definition at line 46 of file Optimizer.cc.

References SizeName::CmpName(), SizeName::CmpNameNocase(), SizeName::CmpNameRev(), SizeName::CmpNameRevNocase(), SizeName::CmpSize(), SizeName::CmpSizeRev(), Optimizer::m_best, Optimizer::m_cur_iter, Params::m_delimit_chr, Params::m_enclose, Params::m_enclose_chr, Optimizer::m_files, Params::m_hide_selected, Params::m_hide_summary, Params::m_iterations, Params::m_no_case, Params::m_no_metric, Optimizer::m_params, Params::m_show_size, Params::m_show_unselected, Params::m_sort_by_size, Params::m_sort_reverse, Params::m_target, Params::m_verbose, and Params::PrettySize().

{
   // increment iter
   ++m_cur_iter;

   if (m_params.m_verbose)
   {
      cout << "> Best individual is: ";
      copy(m_best->begin(), m_best->end(), ostream_iterator<bool>(cout));
      cout << endl << flush;
   }

   // comparison function. Should be "by size" or "by name" (reverse or not)
   bool (*cmp_function)(const SizeName&, const SizeName&);

   // sort by name or size, ascending or descending
   if (m_params.m_sort_by_size)
      if (m_params.m_sort_reverse) 
         cmp_function = SizeName::CmpSizeRev;
      else 
         cmp_function = SizeName::CmpSize;
   else // sort by name
      if (m_params.m_sort_reverse)
         if (m_params.m_no_case)
            cmp_function = SizeName::CmpNameRevNocase;
         else
            cmp_function = SizeName::CmpNameRev;
      else
         if (m_params.m_no_case)
            cmp_function = SizeName::CmpNameNocase;
         else
            cmp_function = SizeName::CmpName;

  const int num_input_files = m_files.size();
  vector<SizeName> buffer; // output buffer
  Params::Size_t sum=0, grand_total=0;

  int v_index = 0; // because m_files items will be removed (and so 
                   // the vector will be automatically resized
  for (unsigned int i=0; i<m_best->size(); ++i)
  {
     grand_total += m_files[v_index].Size();

     if ((*m_best)[i]) // selected!
     {
        if (!m_params.m_hide_selected)
        {
           buffer.push_back(m_files[v_index]);
           sum += m_files[v_index].Size();
        }

        // remove it from m_files
        m_files.erase(m_files.begin()+v_index);
     }
     else ++v_index;
  }

  if (!m_params.m_hide_selected)
  {
     // sort the output
     sort(buffer.begin(), buffer.end(), cmp_function);

     // make a blank line (separator) after first iteration
     // In verbose mode this separator is always needed
     if (m_cur_iter > 1 || m_params.m_verbose) cout << endl;

     for (unsigned int i=0; i<buffer.size(); ++i)
     {
        if (m_params.m_enclose)
           cout << m_params.m_enclose_chr << buffer[i].Name() 
                << m_params.m_enclose_chr;
        else
           cout << buffer[i].Name();

        if (m_params.m_show_size) 
           cout << '\t' << m_params.PrettySize(buffer[i].Size());

        cout << m_params.m_delimit_chr;
     }

     // print the summary
     if (!m_params.m_hide_summary)
     {
        cout << endl << "[" << m_cur_iter 
             << "] Sum: " << m_params.PrettySize(sum) << " of "
             << m_params.PrettySize(grand_total) << ", Diff: "
             << m_params.PrettySize(m_params.m_target - sum)
             << (m_params.m_no_metric?", Items: ":", Files: ")
             << buffer.size() << "/" << num_input_files << endl;
     }
  }

  //---- continue to process? ------------------------------------------
  if (m_files.size() > 0)
  {
     if (m_params.m_show_unselected) // print unselected files (+ summary)
     {
        buffer.clear(); sum = 0;

        // copy m_files to buffer for sorting
        buffer.assign(m_files.begin(), m_files.end());

        // sort by name or size, ascending or descending
        sort(buffer.begin(), buffer.end(), cmp_function);

        // make a blank line after first iteration or if already exist
        // any previous output from "selected files"
        // In verbose mode this separator is always needed
        if (m_cur_iter > 1 || !m_params.m_hide_selected || m_params.m_verbose) 
           cout << endl;

        // output
        for (unsigned int i=0; i<buffer.size(); ++i)
        {
           if (m_params.m_enclose)
              cout << m_params.m_enclose_chr << buffer[i].Name() 
                   << m_params.m_enclose_chr;
           else
              cout << buffer[i].Name();

           if (m_params.m_show_size) 
              cout << '\t' << m_params.PrettySize(buffer[i].Size());

           cout << m_params.m_delimit_chr;

           sum += buffer[i].Size();
        }

        // print the summary for the unselected files
        if (!m_params.m_hide_summary)
        {
           cout << endl << "[" << m_cur_iter << "] <UNSELECTED> Sum: " 
                << m_params.PrettySize(sum) << " of "
                << m_params.PrettySize(grand_total)
                << (m_params.m_no_metric?", Items: ":", Files: ")
                << m_files.size() << "/" << num_input_files
                << endl;
        }
     }

     // Check if the user want more iterations
     if (--m_params.m_iterations > 0) return true;
  }

  // Work complete, terminate execution
  return false;
}


Generated by  Doxygen 1.6.0   Back to index