#include "node.h"

namespace uts {
node_state node::generate_child_state(uint32_t index) {
  node_state result;

  picosha2::hash256_one_by_one hasher;
  hasher.process(state_.begin(), state_.end());
  auto index_begin = reinterpret_cast<uint8_t *>(&index);
  hasher.process(index_begin, index_begin + 4);
  hasher.finish();
  hasher.get_hash_bytes(result.begin(), result.end());

  return result;
}

double node::get_state_random() {
  int32_t state_random_integer;
  uint32_t b = ((uint32_t) state_[16] << 24) |
      ((uint32_t) state_[17] << 16) |
      ((uint32_t) state_[18] << 8) |
      ((uint32_t) state_[19] << 0);
  b = b & 0x7fffffff; // Mask out negative values
  state_random_integer = static_cast<int32_t>(b);

  return (double) state_random_integer / (double) INT32_MAX;
}
}
