/**
* Get the colors corresponding to their position in the workspace list.
*
* @param wsList :: List of all workspaces with fitted parameters.
* @return colors :: List of colors (as numbers) with the key being position in wsList.
*/
QMap<int, int> MuonAnalysisResultTableTab::getWorkspaceColors(const QStringList& wsList)
{
  QMap<int,int> colors; //position, color
  int posCount(0);
  int colorCount(0);

  while (wsList.size() != posCount)
  {
    // If a color has already been chosen for the current workspace then skip
    if (!colors.contains(posCount))
    {
      std::vector<std::string> firstParams;
      // Find the first parameter table and use this as a comparison for all the other tables.
      auto paramWs = retrieveWSChecked<ITableWorkspace>(wsList[posCount].toStdString() + PARAMS_POSTFIX);

      Mantid::API::TableRow paramRow = paramWs->getFirstRow();
      do
      {  
        std::string key;
        paramRow >> key;
        firstParams.push_back(key);
      }
      while(paramRow.next());

      colors.insert(posCount, colorCount);

      // Compare to all the other parameters. +1 don't compare with self.
      for (int i=(posCount + 1); i<wsList.size(); ++i)
      {
        if (!colors.contains(i))
        {
          std::vector<std::string> nextParams;
          auto paramWs = retrieveWSChecked<ITableWorkspace>(wsList[i].toStdString() + PARAMS_POSTFIX);

          Mantid::API::TableRow paramRow = paramWs->getFirstRow();
          do
          {  
            std::string key;
            paramRow >> key;
            nextParams.push_back(key);
          }
          while(paramRow.next());

          if (firstParams == nextParams)
          {
            colors.insert(i, colorCount);
          }
        }
      }
      colorCount++;
    }
/**
 * Checks the given set of fit tables to see if all fits had same parameters.
 * @param tables :: [input] Fit tables
 * @returns :: True if all fits used same model, otherwise false.
 */
bool MuonAnalysisResultTableCreator::haveSameParameters(
    const std::vector<ITableWorkspace_sptr> &tables) const {
  bool sameParams = true;

  // lambda to pull keys out of table
  auto getKeysFromTable = [](const Mantid::API::ITableWorkspace_sptr &tab) {
    std::vector<std::string> keys;
    if (tab) {
      Mantid::API::TableRow row = tab->getFirstRow();
      do {
        std::string key;
        row >> key;
        keys.push_back(key);
      } while (row.next());
    }
    return keys;
  };

  if (tables.size() > 1) {
    const auto &firstKeys = getKeysFromTable(tables.front());
    for (size_t i = 1; i < tables.size(); ++i) {
      const auto &keys = getKeysFromTable(tables[i]);
      if (keys != firstKeys) {
        sameParams = false;
        break;
      }
    }
  }
  return sameParams;
}