#include "cache.hpp" namespace tile { long long Ledger::allocate(std::size_t length) { ++created; live_bytes += static_cast(length); return created; } void Ledger::resized(std::size_t before, std::size_t after) { live_bytes -= static_cast(before); live_bytes += static_cast(after); } void Ledger::release(long long serial, std::size_t length) { ++destroyed; live_bytes -= static_cast(length); deaths.push_back(serial); } long long Ledger::live() const { return created - destroyed; } unsigned char Payload::pattern(std::size_t index, unsigned seed) { const std::size_t square = (index * index) % 251; const std::size_t value = seed + 17 * index + square; return static_cast(value % 256); } Payload::Payload(std::shared_ptr ledger, std::size_t size, unsigned seed) : ledger_(std::move(ledger)), bytes_(size), serial_(0) { for (std::size_t index = 0; index < size; ++index) { bytes_[index] = pattern(index, seed); } serial_ = ledger_->allocate(size); } Payload::~Payload() { ledger_->release(serial_, bytes_.size()); } long long Payload::serial() const { return serial_; } std::size_t Payload::size() const { return bytes_.size(); } long long Payload::checksum() const { constexpr long long modulus = 1000000007; long long total = 0; for (std::size_t index = 0; index < bytes_.size(); ++index) { const long long weight = static_cast(index + 1); const long long value = static_cast(bytes_[index]); total = (total + weight * value) % modulus; } return total; } bool Payload::write(std::size_t offset, unsigned value) { if (offset >= bytes_.size() || value > 255) { return false; } bytes_[offset] = static_cast(value); return true; } bool Payload::fill(std::size_t offset, std::size_t count, unsigned value) { if (offset > bytes_.size() || count > bytes_.size() - offset || value > 255) { return false; } const auto first = bytes_.begin() + static_cast(offset); const auto last = first + static_cast(count); std::fill(first, last, static_cast(value)); return true; } void Payload::resize(std::size_t length, unsigned seed) { const std::size_t before = bytes_.size(); bytes_.resize(length); for (std::size_t index = before; index < length; ++index) { bytes_[index] = pattern(index, seed); } ledger_->resized(before, length); } bool Payload::read(std::size_t offset, std::size_t count, Numbers& output) const { if (offset > bytes_.size() || count > bytes_.size() - offset) { return false; } output = {0, static_cast(count)}; output.reserve(count + 2); for (std::size_t index = offset; index < offset + count; ++index) { output.push_back(bytes_[index]); } return true; } bool Payload::copy_range(std::size_t source_offset, Payload& target, std::size_t target_offset, std::size_t count) const { if (source_offset > bytes_.size()) { return false; } if (count > bytes_.size() - source_offset) { return false; } if (target_offset > target.bytes_.size()) { return false; } if (count > target.bytes_.size() - target_offset) { return false; } const auto first = bytes_.begin() + static_cast(source_offset); const auto last = first + static_cast(count); const std::vector snapshot(first, last); const auto destination = target.bytes_.begin() + static_cast(target_offset); std::copy(snapshot.begin(), snapshot.end(), destination); return true; } bool Payload::same_bytes(const Payload& other) const { return bytes_ == other.bytes_; } }