#include "sg.hpp" #include #include namespace sg { bool Engine::bounded(I value, I upper) const { if (value < 0) { return false; } if (value > upper) { return false; } return true; } bool Engine::valid_id(I id) const { return bounded(id, 31); } bool Engine::valid_cursor(I id) const { return bounded(id, 15); } bool Engine::valid_alignment(I alignment) const { if (alignment < 1 || alignment > 64) { return false; } return (alignment & (alignment - 1)) == 0; } bool Engine::interval(I offset, I count, I length) const { if (!bounded(offset, length)) { return false; } if (!bounded(count, length)) { return false; } return count <= length - offset; } Result Engine::failure(Code code) const { Result out; out.code = code; return out; } I Engine::total() const { I sum = 0; for (I id : order_) { auto found = segments_.find(id); if (found != segments_.end()) { sum += static_cast(found->second.bytes.size()); } } return sum; } bool Engine::ordered(I id) const { for (I item : order_) { if (item == id) { return true; } } return false; } Result Engine::put(I id, I length, I alignment, I value) { if (!valid_id(id) || !bounded(length, 256)) { return failure(RANGE); } if (!valid_alignment(alignment) || !bounded(value, 255)) { return failure(RANGE); } if (segments_.find(id) != segments_.end()) { return failure(EXISTS); } if (!cursors_.empty()) { return failure(BUSY); } Segment segment; segment.alignment = alignment; segment.bytes.assign(static_cast(length), static_cast(value)); segments_.emplace(id, std::move(segment)); Result out; out.data = {id, length, alignment}; return out; } Result Engine::remove(I id) { if (!valid_id(id)) { return failure(RANGE); } auto found = segments_.find(id); if (found == segments_.end()) { return failure(MISSING); } if (!cursors_.empty()) { return failure(BUSY); } if (ordered(id)) { return failure(IN_USE); } I length = static_cast(found->second.bytes.size()); segments_.erase(found); Result out; out.data = {id, length}; return out; } Result Engine::order(const std::vector& ids) { if (ids.size() > 32) { return failure(RANGE); } for (I id : ids) { if (!valid_id(id)) { return failure(RANGE); } } std::set seen; for (I id : ids) { if (!seen.insert(id).second) { return failure(DUPLICATE); } } for (I id : ids) { if (segments_.find(id) == segments_.end()) { return failure(MISSING); } } if (!cursors_.empty()) { return failure(BUSY); } order_ = ids; Result out; out.data.push_back(total()); out.data.push_back(static_cast(order_.size())); for (I id : order_) { out.data.push_back(id); } return out; } Result Engine::write(I id, I offset, I count, I value) { if (!valid_id(id) || !bounded(offset, 256) || !bounded(count, 256)) { return failure(RANGE); } if (!bounded(value, 255)) { return failure(RANGE); } auto found = segments_.find(id); if (found == segments_.end()) { return failure(MISSING); } I size = static_cast(found->second.bytes.size()); if (!interval(offset, count, size)) { return failure(RANGE); } if (!tickets_.empty()) { return failure(BUSY); } for (I index = 0; index < count; ++index) { found->second.bytes[static_cast(offset + index)] = static_cast(value); } Result out; out.data = {count}; return out; } Result Engine::read(I id, I offset, I count) const { if (!valid_id(id) || !bounded(offset, 256) || !bounded(count, 256)) { return failure(RANGE); } auto found = segments_.find(id); if (found == segments_.end()) { return failure(MISSING); } I size = static_cast(found->second.bytes.size()); if (!interval(offset, count, size)) { return failure(RANGE); } Result out; for (I index = 0; index < count; ++index) { unsigned char byte = found->second.bytes[static_cast(offset + index)]; out.data.push_back(static_cast(byte)); } return out; } Result Engine::segment(I id) const { if (!valid_id(id)) { return failure(RANGE); } auto found = segments_.find(id); if (found == segments_.end()) { return failure(MISSING); } I length = static_cast(found->second.bytes.size()); I sum = 0; for (unsigned char byte : found->second.bytes) { sum += static_cast(byte); } Result out; out.data = {id, length, found->second.alignment, ordered(id) ? 1 : 0, sum}; return out; } std::vector Engine::point(I offset) const { std::vector> cells; for (I id : order_) { for (I local = 0; local < static_cast(segments_.at(id).bytes.size()); ++local) { cells.emplace_back(id, local); } } if (offset == static_cast(cells.size())) { return {-1,0,0}; } auto cell = cells.at(static_cast(offset)); I remaining = static_cast(segments_.at(cell.first).bytes.size()) - cell.second; return {cell.first, cell.second, remaining}; } std::vector Engine::pieces(I offset, I count, I chunk) const { std::vector> cells; for (I id : order_) { for (I local = 0; local < static_cast(segments_.at(id).bytes.size()); ++local) { cells.emplace_back(id, local); } } std::vector answer; I cursor = offset; while (cursor < offset + count) { auto first = cells.at(static_cast(cursor)); I take = 1; while (take < chunk && cursor + take < offset + count) { auto next = cells.at(static_cast(cursor + take)); if (next.first != first.first || next.second != first.second + take) { break; } ++take; } answer.insert(answer.end(), {first.first, first.second, take}); cursor += take; } return answer; } Result Engine::locate(I offset) const { if (!bounded(offset, total())) { return failure(RANGE); } Result out; out.data = point(offset); return out; } Result Engine::slice(I offset, I count, I chunk) const { if (!bounded(offset, 8192) || !bounded(count, 8192)) { return failure(RANGE); } if (chunk < 1 || chunk > 256) { return failure(RANGE); } if (!interval(offset, count, total())) { return failure(RANGE); } Result out; out.data = pieces(offset, count, chunk); return out; } Result Engine::gather(I offset, I count) const { if (!bounded(offset, 8192) || !bounded(count, 8192)) { return failure(RANGE); } if (!interval(offset, count, total())) { return failure(RANGE); } std::vector ranges = pieces(offset, count, 256); Result out; for (std::size_t index = 0; index < ranges.size(); index += 3) { I id = ranges[index]; I local = ranges[index + 1]; I length = ranges[index + 2]; const Segment& segment = segments_.at(id); for (I byte = 0; byte < length; ++byte) { std::size_t at = static_cast(local + byte); out.data.push_back(static_cast(segment.bytes[at])); } } return out; } Result Engine::scatter(I offset, const std::vector& bytes) { if (!bounded(offset, 8192) || bytes.size() > 8192) { return failure(RANGE); } for (I value : bytes) { if (!bounded(value, 255)) { return failure(RANGE); } } I count = static_cast(bytes.size()); if (!interval(offset, count, total())) { return failure(RANGE); } if (!tickets_.empty()) { return failure(BUSY); } std::vector ranges = pieces(offset, count, 256); std::size_t consumed = 0; for (std::size_t index = 0; index < ranges.size(); index += 3) { I id = ranges[index]; I local = ranges[index + 1]; I length = ranges[index + 2]; Segment& segment = segments_.at(id); for (I byte = 0; byte < length; ++byte) { std::size_t at = static_cast(local + byte); segment.bytes[at] = static_cast(bytes[consumed]); ++consumed; } } Result out; out.data = {count}; return out; } Result Engine::copy(I source, I destination, I count) { if (!bounded(source, 8192)) { return failure(RANGE); } if (!bounded(destination, 8192)) { return failure(RANGE); } if (!bounded(count, 8192)) { return failure(RANGE); } I length = total(); if (!interval(source, count, length)) { return failure(RANGE); } if (!interval(destination, count, length)) { return failure(RANGE); } if (!tickets_.empty()) { return failure(BUSY); } std::vector from = pieces(source, count, 256); std::vector saved; saved.reserve(static_cast(count)); for (std::size_t part = 0; part < from.size(); part += 3) { I id = from[part]; I first = from[part + 1]; I length = from[part + 2]; const Segment& segment = segments_.at(id); for (I index = 0; index < length; ++index) { std::size_t at = static_cast(first + index); saved.push_back(segment.bytes[at]); } } std::vector to = pieces(destination, count, 256); std::size_t consumed = 0; for (std::size_t part = 0; part < to.size(); part += 3) { I id = to[part]; I first = to[part + 1]; I length = to[part + 2]; Segment& segment = segments_.at(id); for (I index = 0; index < length; ++index) { std::size_t at = static_cast(first + index); segment.bytes[at] = saved[consumed]; ++consumed; } } Result out; out.data = {count}; return out; } }