Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) CalcComplexity

 

CalcComplexity is a Newick code snippets to calculate the complexity of a Newick.

 

CalcComplexity has two flavors:

  1. STL CalcComplexity: for complexities to around 2 billion
  2. CLN CalcComplexity: for nearly any complexity

 

 

 

 

STL CalcComplexity

 

If the complexity exceeds the maximum integer value, a std::range_error is thrown. Use the CLN version if you do need to calculate higher complexities.

 

///CalcComplexity calculates the complexity of a Newick.
///From http://www.richelbilderbeek.nl/CppCalcComplexity.htm
int CalcComplexity(const std::vector<int>& v)
{
  assert(IsNewick(v));
  int complexity = 1;
  int n_frequencies = 0;
  const int sz = v.size();
  for (int i=0; i!=sz; ++i)
  {
    const int x = v[i];
    if (x < 0) continue; //Ignore if x is not a number
    ++n_frequencies;
    complexity*=x;
    //Check if complexity exceeded integer limit
    if (complexity < 0)
    {
      throw
        std::range_error("Newick complexity exceeds integer maximum value");
    }
  }
  complexity*=n_frequencies;
  //Check if complexity exceeded integer limit
  if (complexity < 0)
  {
    throw
      std::range_error("Newick complexity exceeds integer maximum value");
  }
  return complexity;
}

 

 

 

 

 

CLN CalcComplexity

 

///CalcComplexity calculates the complexity of a Newick.
///From http://www.richelbilderbeek.nl/CppCalcComplexity.htm
const cln::cl_I CalcComplexity(const std::vector<int>& v)
{
  assert(IsNewick(v));
  cln::cl_I complexity = 1;
  int n_frequencies = 0;
  const int sz = v.size();
  for (int i=0; i!=sz; ++i)
  {
    const int x = v[i];
    if (x < 0) continue; //Ignore if x is not a number
    ++n_frequencies;
    complexity*=cln::cl_I(x);
  }
  complexity*=cln::cl_I(n_frequencies);
  return complexity;
}

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict