| 1 | /* This file is part of the FaCT++ DL reasoner |
| 2 | Copyright (C) 2003-2015 Dmitry Tsarkov and The University of Manchester |
| 3 | Copyright (C) 2015-2017 Dmitry Tsarkov |
| 4 | |
| 5 | This library is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU Lesser General Public |
| 7 | License as published by the Free Software Foundation; either |
| 8 | version 2.1 of the License, or (at your option) any later version. |
| 9 | |
| 10 | This library is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | Lesser General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU Lesser General Public |
| 16 | License along with this library; if not, write to the Free Software |
| 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | */ |
| 19 | |
| 20 | #ifndef TRARESAVESTACK_H |
| 21 | #define TRARESAVESTACK_H |
| 22 | |
| 23 | #include <vector> |
| 24 | |
| 25 | #include "globaldef.h" |
| 26 | #include "tRestorer.h" |
| 27 | |
| 28 | /** |
| 29 | * Stack for Saving/Restoring rarely changing information. |
| 30 | * Uses self-contained Restorer as a way to update state of object. |
| 31 | */ |
| 32 | class TRareSaveStack |
| 33 | { |
| 34 | protected: // typedefs |
| 35 | /// vector of restorers |
| 36 | typedef std::vector<TRestorer*> TBaseType; |
| 37 | |
| 38 | protected: // members |
| 39 | /// heap of saved objects |
| 40 | TBaseType Base; |
| 41 | /// current level |
| 42 | unsigned int curLevel = InitBranchingLevelValue; |
| 43 | |
| 44 | public: // interface |
| 45 | /// empty c'tor: stack will most likely be empty |
| 46 | TRareSaveStack() = default; |
| 47 | /// d'tor |
| 48 | ~TRareSaveStack() { clear(); } |
| 49 | |
| 50 | // stack operations |
| 51 | |
| 52 | /// increment current level |
| 53 | void incLevel ( void ) { ++curLevel; } |
| 54 | /// check that stack is empty |
| 55 | bool empty ( void ) const { return Base.empty(); } |
| 56 | /// add a new object to the stack |
| 57 | void push ( TRestorer* p ) |
| 58 | { |
| 59 | p->setLevel(curLevel); |
| 60 | Base.push_back(p); |
| 61 | } |
| 62 | /// get all object from the top of the stack with levels >= LEVEL |
| 63 | void restore ( unsigned int level ) |
| 64 | { |
| 65 | curLevel = level; |
| 66 | while ( !Base.empty() ) |
| 67 | { |
| 68 | TRestorer* cur = Base.back(); |
| 69 | if ( cur->level() <= level ) |
| 70 | break; |
| 71 | |
| 72 | // need to restore: restore last element, remove it from stack |
| 73 | cur->restore(); |
| 74 | delete cur; |
| 75 | Base.pop_back(); |
| 76 | } |
| 77 | } |
| 78 | /// clear stack |
| 79 | void clear ( void ) |
| 80 | { |
| 81 | for ( TBaseType::iterator p = Base.begin(), p_end = Base.end(); p < p_end; ++p ) |
| 82 | delete *p; |
| 83 | Base.clear(); |
| 84 | curLevel = InitBranchingLevelValue; |
| 85 | } |
| 86 | }; // TRareSaveStack |
| 87 | |
| 88 | #endif |