| 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 TSAVELIST_H |
| 21 | #define TSAVELIST_H |
| 22 | |
| 23 | /** |
| 24 | * template list for Saving/Restoring internal state (saving class is template parameter). |
| 25 | * template class should define empty/copy c'tors, member function level() |
| 26 | */ |
| 27 | template <typename T> |
| 28 | class TSaveList |
| 29 | { |
| 30 | protected: // internal types |
| 31 | /// type of the list element |
| 32 | struct List : public T |
| 33 | { |
| 34 | public: // members |
| 35 | /// pointer to next element |
| 36 | List* next; |
| 37 | |
| 38 | public: // interface |
| 39 | /// empty c'tor |
| 40 | explicit List ( List* n = nullptr ) : next(n) {} |
| 41 | /// create object from given one and from given next |
| 42 | List ( const T& obj, List* n ) : T(obj), next(n) {} |
| 43 | /// no copy c'tor |
| 44 | List ( const List& ) = delete; |
| 45 | /// no assignment |
| 46 | List& operator = ( const List& ) = delete; |
| 47 | /// d'tor: do nothing |
| 48 | ~List() = default; |
| 49 | |
| 50 | /// clone given sub-list |
| 51 | List* clone ( void ) { return new List ( *this, next ? next->clone() : nullptr ); } |
| 52 | /// clear sub-list |
| 53 | void clear ( void ) |
| 54 | { |
| 55 | if ( next ) |
| 56 | { |
| 57 | next->clear(); |
| 58 | delete next; |
| 59 | } |
| 60 | } |
| 61 | }; // List |
| 62 | |
| 63 | protected: // members |
| 64 | /// pointer to head of list |
| 65 | List* head = nullptr; |
| 66 | |
| 67 | public: // interface |
| 68 | /// empty c'tor |
| 69 | TSaveList() = default; |
| 70 | /// copy c'tor |
| 71 | TSaveList ( const TSaveList& copy ) : head ( copy.head ? copy.head->clone() : nullptr ) {} |
| 72 | /// d'tor -- clear stack |
| 73 | ~TSaveList() { clear(); } |
| 74 | |
| 75 | // stack operations |
| 76 | |
| 77 | /// check that stack is empty |
| 78 | bool empty ( void ) const { return (head == nullptr); } |
| 79 | /// put empty element to stack; @return pointer to it |
| 80 | T* push ( void ) { head = new List ( head ); return head; } |
| 81 | /// put given element to stack; @return pointer to it |
| 82 | T* push ( const T& el ) { head = new List ( el, head ); return head; } |
| 83 | /// get top element from stack |
| 84 | T* pop ( void ) |
| 85 | { |
| 86 | T* ret = head; |
| 87 | if ( !empty() ) |
| 88 | head = head->next; |
| 89 | return ret; |
| 90 | } |
| 91 | /// get element from stack with given level |
| 92 | T* pop ( unsigned int level ) |
| 93 | { |
| 94 | List* p = head; |
| 95 | while ( p && p->level() > level ) |
| 96 | { |
| 97 | head = p->next; |
| 98 | delete p; |
| 99 | p = head; |
| 100 | } |
| 101 | // here p==head and either both == NULL or points to proper element |
| 102 | if ( p ) |
| 103 | head = head->next; |
| 104 | return p; |
| 105 | } |
| 106 | |
| 107 | // extra operations |
| 108 | |
| 109 | /// clear the stack |
| 110 | void clear ( void ) |
| 111 | { |
| 112 | if ( !empty() ) |
| 113 | { |
| 114 | head->clear(); |
| 115 | delete head; |
| 116 | head = nullptr; |
| 117 | } |
| 118 | } |
| 119 | }; // TSaveList |
| 120 | |
| 121 | #endif |