/*
     Description: 做一些量化处理

     Copyright : All right reserved by ICT

     Author : Zhang Zhonghai
     Date : 2025/12/24
*/
#pragma once

#include <vector>
#include <stdint.h>
#include "recal_tables.h"
#include "qual_utils.h"
#include "util/math/math_utils.h"
#include "qual_quantizer.h"

using std::vector;

struct QuantizationInfo {
    vector<uint8_t> quantizedQuals;
    vector<int64_t> empiricalQualCounts; // 直方图
    int quantizationLevels; // 量化等级

    QuantizationInfo() {}

    QuantizationInfo(RecalTables &recalTables, const int levels) {
        quantizationLevels = levels;
        empiricalQualCounts.resize(QualityUtils::MAX_SAM_QUAL_SCORE + 1, 0);
        auto& qualTable = recalTables.qualityScoreTable;
        _Foreach3D(qualTable, val, {
            // convert the empirical quality to an integer ( it is already capped by MAX_QUAL )
            const int empiricalQual =  MathUtils::fastRound(val.getEmpiricalQuality());
            empiricalQualCounts[empiricalQual] += val.getNumObservations();  // add the number of observations for every key
        });

        // quantizeQualityScores
        QualQuantizer quantizer(empiricalQualCounts, levels, QualityUtils::MIN_USABLE_Q_SCORE);
        quantizer.getOriginalToQuantizedMap(quantizedQuals);
    }
};