#ifndef SERIALIZE_SETS_HPP_
#define SERIALIZE_SETS_HPP_

#include <Serialize/Base.hpp>
#include <unordered_set>

template<typename TElement>
struct SerialCodec<std::unordered_set<TElement>>
{
    static void encode(std::vector<uint8_t> &outBytes, const std::unordered_set<TElement> &set)
    {
        uint32_t setSize = set.size();
        serializeInto(outBytes, setSize);

        for (const TElement &element : set)
        {
            SerialCodec<TElement>::encode(outBytes, element);
        }
    }

    static size_t decode(const uint8_t *data, size_t size, std::unordered_set<TElement> &set)
    {
        set.clear();

        size_t bytesRead = 0;

        uint32_t setSize = 0;
        bytesRead += deserialize(data + bytesRead, size - bytesRead, setSize);

        for (size_t i = 0; i < setSize; i++)
        {
            TElement element;
            bytesRead += SerialCodec<TElement>::decode(data + bytesRead, size - bytesRead, element);
            set.insert(element);
        }

        return bytesRead;
    }
};

#endif