/* * HotReserve.hpp * * Created on: 9 мар. 2020 г. * Author: titov */ #ifndef UMLIBRARY_ALGORITHM_HOTRESERVE_HPP_ #define UMLIBRARY_ALGORITHM_HOTRESERVE_HPP_ namespace algorithm { template< template class ContainerType, typename Key, template class Locator > class HotLinearReserve { public: typedef std::size_t Index; struct Node { Key key; Index before; Index after; bool state; Node(Key k, Index b, Index a, bool s) : key(k), before(b), after(a), state(s) {} }; typedef Locator Allocator; typedef ContainerType Container; HotLinearReserve(Key passive, Allocator alloca = Allocator() ); Index push(Key key); bool activate(Index token); void deactivate(Index token); Key top() const; private: Container container; Index active; Index last_item; void do_deactivate(Index index, const HotLinearReserve::Node & item); void do_activate(Index index, HotLinearReserve::Node & item); }; template< template class ContainerType, typename Key, template class Locator > HotLinearReserve::HotLinearReserve(Key passive, Allocator alloca) : container(alloca), active(push(passive)), last_item(active) {} template< template class ContainerType, typename Key, template class Locator > typename HotLinearReserve::Index HotLinearReserve::push(Key key) { container.push_back( Node( key, 0, 0, false ) ); return container.size() - 1; } template< template class ContainerType, typename Key, template class Locator > bool HotLinearReserve::activate(Index index) { Node & item = container.at(index); if( not item.state) { do_activate(index, item); item.state = true; } return active == index; } template< template class ContainerType, typename Key, template class Locator > void HotLinearReserve::do_activate( HotLinearReserve::Index index, HotLinearReserve::Node & item ) { item.after = 0; item.before = last_item; if( not active) { active = last_item = index; } else { container[last_item].after = index; last_item = index; } } template< template class ContainerType, typename Key, template class Locator > void HotLinearReserve::deactivate(Index index) { Node & item = container.at(index); if( item.state ) { do_deactivate(index, item); item.state = false; } } template< template class ContainerType, typename Key, template class Locator > void HotLinearReserve::do_deactivate( HotLinearReserve::Index index, const HotLinearReserve::Node & item ) { if(active == index ) { active = item.after; } else { container[item.before].after = item.after; container[item.after].before = item.before; } } template< template class ContainerType, typename Key, template class Locator > Key HotLinearReserve::top() const { return container[active].key; } } #endif /* UMLIBRARY_ALGORITHM_HOTRESERVE_HPP_ */