#include "minmax.h"

MinMax::MinMax()
{
}

/**
 * @brief The max function counting maximum probability in given vector
 * @param vec - vector for counting
 * @return string - value with maximum probabilty
 */
std::string MinMax::max(std::vector <probabilityPair> vec){

    double max = vec[0].second;
    std::string maxS;

    for (size_t k = 0; k < vec.size(); ++k) {
       if (max <= vec[k].second) {
           max = vec[k].second; // returning max value
       }
    }

  for (size_t k = 0; k < vec.size(); ++k) {
      if (vec[k].second == max) {
          maxS = vec[k].first; // returning name of max value
      }
  }
    return maxS;
}

/**
 * @brief The countMax function counting maximum in given column
 * @param column - column for counting
 * @return double - maximum value
 */
double MinMax::countMax(std::vector <double> column) {

    double max = column[0];

    for (size_t k = 0; k < column.size(); ++k) {
       if (max < column[k]) {
           max = column[k];
       }
    }

    return max;

}

/**
 * @brief The countMin function counting minimum in given column
 * @param column - column for counting
 * @return double - minimum value
 */
double MinMax::countMin(std::vector <double> column) {

    double min = column[0];

    for (size_t k = 0; k < column.size(); ++k) {
       if (min > column[k]) {
           min = column[k];
       }
    }

    return min;

}

/**
 * @brief The returnOneClassFromMany function counting maximum in nominal values based on counts
 * @param pomMinTest- vector for counting
 * @return string - value with maximum counts
 */
std::string MinMax::returnOneClassFromMany(std::vector <std::string> pomMinTest){

    std::map <std::string, double> pomMap;
    std::string classMax;
    std::vector <double> pomMax;
    double pom;

    // counting count for instances
    for (size_t i = 0; i < pomMinTest.size(); i++) {
        if (pomMinTest[i] != "?")
         pomMap.insert(std::pair<std::string, double>(pomMinTest[i], pomMap[pomMinTest[i]]++));
    }

    // pushing counted counts to another map
    for(std::map<std::string, double>::iterator it = pomMap.begin(); it != pomMap.end(); ++it) {
        pomMax.push_back(it->second);
    }

    pom = countMax(pomMax); // returning max value

    // returning name of max value
    for(std::map<std::string, double>::iterator it = pomMap.begin(); it != pomMap.end(); ++it) {
        if (pom == it->second) {
            classMax = it->first;
        }
    }

    return classMax;
}
