#include "histogram.h"

bool find_minmax(vector<double> vec, double& min, double& max) {

    if (vec.size()== 0){
        cerr<<"Empty vec";
        return false;
    }

    min = vec[0];
    max = vec[0];

    for (double x : vec) {
        if (x < min) min = x;
        if (x > max) max = x;
    }
    return true;
}

vector<size_t> make_histogram(size_t number, vector<double> vec) {
    vector<size_t> bins(number);
    if (vec.empty()) return bins;

    double mn, mx;
    find_minmax(vec, mn, mx);
    float bin_size = (mx - mn) / number;

    for (size_t i = 0; i < vec.size(); i++) {
        bool fl = false;
        for (size_t j = 0; (j < number - 1) && !fl; j++) {
            auto lo = mn + j * bin_size;
            auto hi = mn + (j + 1) * bin_size;
            if ((lo <= vec[i]) && (vec[i] < hi)) {
                bins[j]++;
                fl = true;
            }
        }
        if (!fl) {
            bins[number - 1]++;
        }
    }
    return bins;
}
