#include "jump.hpp" #include namespace jumpmodel { struct Machine::Impl { struct Frame { bool active=false; int parent=-1; int id=-1; int local=0; int returned=0; std::uint64_t token=0; }; struct Context { int state=0; int owner=-1; std::uint64_t token=0; int resumes=0; }; std::array frames; std::array contexts; int head=-1; std::uint64_t next=1; static bool id_valid(int id) { return id>=0 && id<=31; } static bool local_valid(int value) { return value>=-1000 && value<=1000; } static bool slot_valid(int slot) { return slot>=0 && slot<=7; } static bool jump_valid(int value) { return value>=-128 && value<=128; } int active_depth() const { int count=0; int cursor=head; while (cursor!=-1) { count+=1; const Frame& item=frames[static_cast(cursor)]; cursor=item.parent; } return count; } int find_id(int id) const { const Frame& item=frames[static_cast(id)]; if (!item.active) { return -1; } return id; } int find_token(std::uint64_t token) const { int cursor=head; while (cursor!=-1) { const Frame& item=frames[static_cast(cursor)]; if (item.token==token) { return cursor; } cursor=item.parent; } return -1; } int frame_at(int index) const { const int count=active_depth(); if (index>=count) { return -1; } int steps=count-index-1; int cursor=head; while (steps>0) { const Frame& item=frames[static_cast(cursor)]; cursor=item.parent; steps-=1; } return cursor; } void invalidate(std::uint64_t token) { for (Context& item:contexts) { if (item.state==1 && item.token==token) { item.state=2; } } } void remove_top() { const Frame& item=frames[static_cast(head)]; invalidate(item.token); detach_top(); } void detach_top() { const int removed=head; const Frame& item=frames[static_cast(removed)]; const int parent=item.parent; head=parent; frames[static_cast(removed)]=Frame{}; } FrameView view(const Frame& item) const { FrameView result; result.code=OK; result.id=item.id; result.local=item.local; result.returned=item.returned; result.token=item.token; return result; } }; Machine::Machine():p(new Impl) { } Machine::~Machine()=default; Reply Machine::enter(int id,int local) { if (!Impl::id_valid(id)) { return {RANGE,-1}; } if (!Impl::local_valid(local)) { return {RANGE,-1}; } if (p->find_id(id)>=0) { return {EXISTS,-1}; } const int previous_depth=p->active_depth(); if (previous_depth==8) { return {FULL,-1}; } Impl::Frame item; item.active=true; item.parent=p->head; item.id=id; item.local=local; item.returned=0; item.token=p->next; p->next+=1; p->frames[static_cast(id)]=item; p->head=id; return {OK,previous_depth+1}; } Reply Machine::leave() { if (p->head==-1) { return {EMPTY,-1}; } const Impl::Frame item=p->frames[static_cast(p->head)]; p->remove_top(); return {OK,item.id}; } Reply Machine::store(int id,int local) { if (!Impl::id_valid(id)) { return {RANGE,-1}; } if (!Impl::local_valid(local)) { return {RANGE,-1}; } const int index=p->find_id(id); if (index<0) { return {MISSING,-1}; } Impl::Frame& item=p->frames[static_cast(index)]; const int previous=item.local; item.local=local; return {OK,previous}; } Reply Machine::save(int slot) { if (!Impl::slot_valid(slot)) { return {RANGE,-1}; } if (p->head==-1) { return {EMPTY,-1}; } Impl::Frame& owner=p->frames[static_cast(p->head)]; Impl::Context fresh; fresh.state=1; fresh.owner=owner.id; fresh.token=owner.token; fresh.resumes=0; p->contexts[static_cast(slot)]=fresh; owner.returned=0; return {OK,0}; } Reply Machine::jump(int slot,int value) { if (!Impl::slot_valid(slot)) { return {RANGE,-1}; } if (!Impl::jump_valid(value)) { return {RANGE,-1}; } Impl::Context& target=p->contexts[static_cast(slot)]; if (target.state==0) { return {VACANT,-1}; } if (target.state==2) { return {STALE,-1}; } const int owner=p->find_token(target.token); if (owner<0) { return {STALE,-1}; } while (p->head!=owner) { p->remove_top(); } const int resumed=value==0 ? 1 : value; Impl::Frame& frame=p->frames[static_cast(owner)]; frame.returned=resumed; target.resumes+=1; return {OK,resumed}; } Reply Machine::clear(int slot) { if (!Impl::slot_valid(slot)) { return {RANGE,-1}; } Impl::Context& target=p->contexts[static_cast(slot)]; const int previous=target.state; target=Impl::Context{}; return {OK,previous}; } FrameView Machine::frame(int index) const { if (index<0 || index>7) { FrameView result; result.code=RANGE; return result; } const int id=p->frame_at(index); if (id==-1) { return FrameView{}; } const Impl::Frame& item=p->frames[static_cast(id)]; return p->view(item); } ContextView Machine::context(int slot) const { ContextView result; if (!Impl::slot_valid(slot)) { result.code=RANGE; return result; } const Impl::Context& item=p->contexts[static_cast(slot)]; result.state=item.state; result.owner=item.owner; result.token=item.token; result.resumes=item.resumes; return result; } FrameView Machine::top() const { if (p->head==-1) { return FrameView{}; } const Impl::Frame& item=p->frames[static_cast(p->head)]; return p->view(item); } int Machine::depth() const { return p->active_depth(); } }