#ifndef NXS_LRU_PSERVER_H #define NXS_LRU_PSERVER_H #include #include #include #include "pserver.h" namespace nxs { class LruPServer: public PServer { public: //TODO change name to Item typedef std::pair Item; std::list items; typedef std::list Items; std::map index; ~LruPServer() { Flush(); } Patch &Lookup(unsigned int patch, unsigned short nv, unsigned short nf) { if(index.count(patch)) { Items::iterator &i = index[patch]; Item item = *i; items.erase(i); items.push_front(item); i = items.begin(); return *((*i).second); } else { while(ram_used > ram_max) { Item item = items.back(); index.erase(item.first); FlushPatch(item.first, item.second); items.pop_back(); } Item item; item.first = patch; item.second = LoadPatch(patch, nv, nf); items.push_front(item); Items::iterator i = items.begin(); index[patch] = i; return *((*i).second); } } bool IsLoaded(unsigned int patch) { return index.count(patch); } void Flush() { std::map::iterator i; for(i = index.begin(); i != index.end(); i++) { Item &item = *((*i).second); FlushPatch((*i).first, item.second); } for(int k = 0; k < entries.size(); k++) entries[k].patch = NULL; items.clear(); index.clear(); } }; }//namespace #endif