# (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

## STLCalcComplexity

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& 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; } ```

## CLNCalcComplexity

 ``` ///CalcComplexity calculates the complexity of a Newick. ///From http://www.richelbilderbeek.nl/CppCalcComplexity.htm const cln::cl_I CalcComplexity(const std::vector& 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; } ```