| 1 | /* This file is part of the FaCT++ DL reasoner |
| 2 | Copyright (C) 2005-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 TNECOLLECTION_H |
| 21 | #define TNECOLLECTION_H |
| 22 | |
| 23 | #include <vector> |
| 24 | #include <set> |
| 25 | |
| 26 | #include "tNamedEntry.h" |
| 27 | #include "tNameSet.h" |
| 28 | #include "eFPPCantRegName.h" |
| 29 | |
| 30 | /** class for collect TNamedEntry'es together. Template parameter should be |
| 31 | inherited from TNamedEntry. Implemented as vector of T*, |
| 32 | with Base[i]->getId() == i. |
| 33 | **/ |
| 34 | template <typename T> |
| 35 | class TNECollection |
| 36 | { |
| 37 | protected: // typedefs |
| 38 | typedef std::vector<T*> BaseType; |
| 39 | |
| 40 | public: // typedefs |
| 41 | typedef typename BaseType::iterator iterator; |
| 42 | typedef typename BaseType::const_iterator const_iterator; |
| 43 | |
| 44 | protected: // members |
| 45 | /// vector of elements |
| 46 | BaseType Base; |
| 47 | /// nameset to hold the elements |
| 48 | TNameSet<T> NameSet; |
| 49 | /// name of the type |
| 50 | std::string TypeName; |
| 51 | /// flag to lock the nameset (ie, prohibit to add new names there) |
| 52 | bool locked = false; |
| 53 | /// if true, allow fresh entities even when locked. treat them as System in this case |
| 54 | bool allowFresh = false; |
| 55 | |
| 56 | protected: // methods |
| 57 | /// virtual method for additional tuning of newly created element |
| 58 | virtual void registerNew ( T* ) {} |
| 59 | /// register new element in a collection; return this element |
| 60 | T* registerElem ( T* p ) |
| 61 | { |
| 62 | p->setId((int)Base.size()); |
| 63 | Base.push_back(p); |
| 64 | registerNew(p); |
| 65 | return p; |
| 66 | } |
| 67 | |
| 68 | public: // interface |
| 69 | /// c'tor: clear 0-th element |
| 70 | explicit TNECollection ( const std::string& name ) |
| 71 | : TypeName(name) |
| 72 | { Base.push_back(nullptr); } |
| 73 | /// empty d'tor: all elements will be deleted in other place |
| 74 | virtual ~TNECollection() = default; |
| 75 | |
| 76 | // locked interface |
| 77 | |
| 78 | /// check if collection is locked |
| 79 | bool isLocked ( void ) const { return locked; } |
| 80 | /// set LOCKED value to a VAL; @return old value of LOCKED |
| 81 | bool setLocked ( bool val ) { bool old = locked; locked = val; return old; } |
| 82 | /// set FRESH value to a VAL; @return the old value |
| 83 | bool setAllowFresh ( bool val ) { bool old = allowFresh; allowFresh = val; return old; } |
| 84 | |
| 85 | // add/remove elements |
| 86 | |
| 87 | /// check if entry with a NAME is registered in given collection |
| 88 | bool isRegistered ( const std::string& name ) const { return NameSet.get(name) != nullptr; } |
| 89 | /// get entry by NAME from the collection; register it if necessary |
| 90 | T* get ( const std::string& name ) |
| 91 | { |
| 92 | T* p = NameSet.get(name); |
| 93 | |
| 94 | // check if name is already defined |
| 95 | if ( p != nullptr ) |
| 96 | return p; |
| 97 | |
| 98 | // check if it is possible to insert name |
| 99 | if ( isLocked() && !allowFresh ) |
| 100 | throw EFPPCantRegName ( name, TypeName ); |
| 101 | |
| 102 | // create name in name set, and register it |
| 103 | p = registerElem(NameSet.add(name)); |
| 104 | |
| 105 | // if fresh entity -- mark it System |
| 106 | if ( isLocked() ) |
| 107 | { |
| 108 | p->setSystem(); |
| 109 | if ( auto ce = dynamic_cast<ClassifiableEntry*>(p) ) |
| 110 | ce->setNonClassifiable(); |
| 111 | } |
| 112 | return p; |
| 113 | } |
| 114 | /// remove given entry from the collection; @return true iff it was NOT the last entry. |
| 115 | bool Remove ( T* p ) |
| 116 | { |
| 117 | if ( !isRegistered(p->getName()) ) // not in a name-set: just delete it |
| 118 | { |
| 119 | delete p; |
| 120 | return false; |
| 121 | } |
| 122 | // we might delete vars in order (6,7), so the resize should be done to 6 |
| 123 | if ( p->getId() > 0 && Base.size() > (size_t)p->getId() ) |
| 124 | Base.resize(p->getId()); |
| 125 | NameSet.remove(p->getName()); |
| 126 | return false; |
| 127 | } |
| 128 | |
| 129 | // access to elements |
| 130 | |
| 131 | iterator begin ( void ) { return Base.begin()+1; } |
| 132 | const_iterator begin ( void ) const { return Base.begin()+1; } |
| 133 | |
| 134 | iterator end ( void ) { return Base.end(); } |
| 135 | const_iterator end ( void ) const { return Base.end(); } |
| 136 | |
| 137 | size_t size ( void ) const { return Base.size()-1; } |
| 138 | }; // TNECollection |
| 139 | |
| 140 | #endif |