#include <cstdlib>

#include <GeneratorNode/GeneratorNode.hpp>
#include <SHA256/SHA256.hpp>

GeneratorNode::ID GeneratorNode::permute(GeneratorNode::ID id)
{
    SHA256 ctx;
    ctx.feed(&id, 8);
    ctx.feed(&seed, 8);
    
    union
    {
        SHA256::Digest digest;
        GeneratorNode::ID result;
    } ures;

    ctx.digest(ures.digest);
    return ures.result;
}

GeneratorNode::GeneratorNode(GeneratorNode::ID seed) : seed(seed) {}
GeneratorNode::GeneratorNode(const GeneratorNode &node) : seed(node.seed) {}
GeneratorNode::GeneratorNode(GeneratorNode &&node) : seed(node.seed) {}

GeneratorNode::ID GeneratorNode::operator[](GeneratorNode::ID key)
{
    return permute(key);
}

GeneratorNode& GeneratorNode::operator=(GeneratorNode node)
{
    seed = node.seed;
    return *this;
}

GeneratorNode GeneratorNode::operator()(GeneratorNode::ID key)
{
    return GeneratorNode(permute(key));
}