#include "RunLengthEncoder.h"

std::vector<RunLengthEncoder::Hit> RunLengthEncoder::encode(const std::vector<unsigned char>& input)
{
    std::vector<RunLengthEncoder::Hit> ret;
    if (input.empty())
    {
        return ret;
    }

    char working_char{0};
    unsigned count = 1;
    for(unsigned idx=0; idx<input.size(); idx++)
    {
        auto c = input[idx];
        if (idx == 0)
        {
            working_char = c;
            continue;
        }

        if (c == working_char)
        {
            count++;
        }
        else
        {
            ret.push_back({working_char, count});
            working_char = c;
            count = 1;
        }
    }
    ret.push_back({working_char, count});
    return ret;
}

std::vector<unsigned char> RunLengthEncoder::decode(const std::vector<RunLengthEncoder::Hit>& input)
{
    std::vector<unsigned char> ret;
    if (input.empty())
    {
        return ret;
    }

    for (const auto& hit : input)
    {
        for(unsigned idx=0; idx< hit.second; idx++)
        {
            ret.push_back(hit.first);
        }
    }
    return ret;
}

