#include "jump.hpp" #include #include #include namespace jumpmodel { struct Machine::Impl { struct Event { int operation; int a; int b; }; struct Image { std::vector frames; std::array slots; std::uint64_t next=1; }; std::vector history; Image reconstruct() const { Image out; for (const Event& event:history) { if (event.operation==0) { out.frames.push_back({OK,event.a,event.b,0,out.next++}); } else if (event.operation==1) { out.frames.pop_back(); } else if (event.operation==2) { for (auto& frame:out.frames) { if (frame.id==event.a) frame.local=event.b; } } else if (event.operation==3) { auto& frame=out.frames.back(); out.slots[event.a]={OK,1,frame.id,frame.token,0}; frame.returned=0; } else if (event.operation==4) { auto& slot=out.slots[event.a]; auto it=std::find_if(out.frames.begin(),out.frames.end(),[&](const FrameView& f){return f.token==slot.token;}); out.frames.erase(it+1,out.frames.end()); out.frames.back().returned=event.b ? event.b : 1; ++slot.resumes; } else { out.slots[event.a]=ContextView{}; } for (auto& slot:out.slots) { if (slot.state!=1) continue; const bool alive=std::any_of(out.frames.begin(),out.frames.end(),[&](const FrameView& frame){return frame.token==slot.token;}); if (!alive) slot.state=2; } } return out; } void add(int operation,int a=0,int b=0) { history.push_back({operation,a,b}); } }; Machine::Machine():p(new Impl) {} Machine::~Machine()=default; Reply Machine::enter(int id,int local) { if(id<0 || id>31 || local<-1000 || local>1000) return {RANGE,-1}; const auto image=p->reconstruct(); for(const auto& frame:image.frames) if(frame.id==id) return {EXISTS,-1}; if(image.frames.size()==8) return {FULL,-1}; p->add(0,id,local); return {OK,static_cast(image.frames.size()+1)}; } Reply Machine::leave() { const auto image=p->reconstruct(); if(image.frames.empty()) return {EMPTY,-1}; const int id=image.frames.back().id; p->add(1); return {OK,id}; } Reply Machine::store(int id,int local) { if(id<0 || id>31 || local<-1000 || local>1000) return {RANGE,-1}; const auto image=p->reconstruct(); for(const auto& frame:image.frames) { if(frame.id!=id) continue; p->add(2,id,local); return {OK,frame.local}; } return {MISSING,-1}; } Reply Machine::save(int slot) { if(slot<0 || slot>7) return {RANGE,-1}; const auto image=p->reconstruct(); if(image.frames.empty()) return {EMPTY,-1}; p->add(3,slot); return {OK,0}; } Reply Machine::jump(int slot,int value) { if(slot<0 || slot>7 || value<-128 || value>128) return {RANGE,-1}; const auto image=p->reconstruct(); if(image.slots[slot].state==0) return {VACANT,-1}; if(image.slots[slot].state==2) return {STALE,-1}; p->add(4,slot,value); return {OK,value ? value : 1}; } Reply Machine::clear(int slot) { if(slot<0 || slot>7) return {RANGE,-1}; const int old=p->reconstruct().slots[slot].state; p->add(5,slot); return {OK,old}; } FrameView Machine::frame(int index) const { if(index<0 || index>7) return {RANGE,-1,0,0,0}; const auto image=p->reconstruct(); if(static_cast(index)>=image.frames.size()) return {}; return image.frames[index]; } ContextView Machine::context(int slot) const { if(slot<0 || slot>7) return {RANGE,0,-1,0,0}; return p->reconstruct().slots[slot]; } FrameView Machine::top() const { const auto image=p->reconstruct(); return image.frames.empty() ? FrameView{} : image.frames.back(); } int Machine::depth() const { return static_cast(p->reconstruct().frames.size()); } }