| 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 CWDARRAY_H |
| 21 | #define CWDARRAY_H |
| 22 | |
| 23 | #include <ostream> |
| 24 | #include <algorithm> // find |
| 25 | |
| 26 | #include "globaldef.h" |
| 27 | #include "ConceptWithDep.h" |
| 28 | |
| 29 | enum addConceptResult { acrClash, acrExist, acrDone }; |
| 30 | |
| 31 | class TRestorer; |
| 32 | |
| 33 | /// array of concepts with dep-set, which may be viewed as a label of a completion-graph |
| 34 | class CWDArray |
| 35 | { |
| 36 | protected: // internal typedefs |
| 37 | /// base type |
| 38 | typedef std::vector<ConceptWDep> ConceptSet; |
| 39 | /// RW iterator |
| 40 | typedef ConceptSet::iterator iterator; |
| 41 | |
| 42 | /// restorer for the merge |
| 43 | friend class UnMerge; |
| 44 | |
| 45 | public: // type interface |
| 46 | /// class for saving one label |
| 47 | class SaveState |
| 48 | { |
| 49 | public: |
| 50 | /// end pointer of the label |
| 51 | size_t ep = 0; |
| 52 | }; // SaveState |
| 53 | |
| 54 | /// const iterator on label |
| 55 | typedef ConceptSet::const_iterator const_iterator; |
| 56 | |
| 57 | protected: // members |
| 58 | /// array of concepts together with dep-sets |
| 59 | ConceptSet Base; |
| 60 | |
| 61 | public: // interface |
| 62 | /// init/clear label with given size |
| 63 | void init ( size_t size ) |
| 64 | { |
| 65 | Base.reserve(size); |
| 66 | Base.clear(); |
| 67 | } |
| 68 | |
| 69 | //---------------------------------------------- |
| 70 | // Label access interface |
| 71 | //---------------------------------------------- |
| 72 | |
| 73 | // label iterators |
| 74 | |
| 75 | /// begin RO iterator |
| 76 | const_iterator begin ( void ) const { return Base.begin(); } |
| 77 | /// end RO iterator |
| 78 | const_iterator end ( void ) const { return Base.end(); } |
| 79 | |
| 80 | // add concept |
| 81 | |
| 82 | /// adds concept P to a label |
| 83 | void add ( const ConceptWDep& p ) { Base.push_back(p); } |
| 84 | /// update concept BP with a dep-set DEP; @return the appropriate restorer |
| 85 | TRestorer* updateDepSet ( BipolarPointer bp, const DepSet& dep ); |
| 86 | |
| 87 | // access concepts |
| 88 | |
| 89 | /// check whether label contains BP (ignoring dep-set) |
| 90 | bool contains ( BipolarPointer bp ) const { return std::find ( begin(), end(), bp ) != end(); } |
| 91 | /// get the concept by given index in the node's label |
| 92 | const ConceptWDep& getConcept ( size_t n ) const { return Base[n]; } |
| 93 | |
| 94 | //---------------------------------------------- |
| 95 | // Blocking support |
| 96 | //---------------------------------------------- |
| 97 | |
| 98 | /// check whether LABEL is a superset of a current one |
| 99 | bool operator <= ( const CWDArray& label ) const |
| 100 | { |
| 101 | for ( const_iterator p = begin(), p_end = end(); p < p_end; ++p ) |
| 102 | if ( !label.contains(p->bp()) ) |
| 103 | return false; |
| 104 | |
| 105 | return true; |
| 106 | } |
| 107 | /// check whether LABEL is a subset of a current one |
| 108 | bool operator >= ( const CWDArray& label ) const { return label <= *this; } |
| 109 | /// check whether LABEL is the same as a current one |
| 110 | bool operator == ( const CWDArray& label ) const |
| 111 | { return (*this <= label) && (label <= *this); } |
| 112 | |
| 113 | //---------------------------------------------- |
| 114 | // Save/restore interface |
| 115 | //---------------------------------------------- |
| 116 | |
| 117 | /// save label using given SS |
| 118 | void save ( SaveState& ss ) const { ss.ep = Base.size(); } |
| 119 | /// restore label to given LEVEL using given SS |
| 120 | void restore ( const SaveState& ss, unsigned int level ); |
| 121 | |
| 122 | //---------------------------------------------- |
| 123 | // Output |
| 124 | //---------------------------------------------- |
| 125 | |
| 126 | /// print the whole label |
| 127 | void print ( std::ostream& o ) const; |
| 128 | }; // CWDArray |
| 129 | |
| 130 | #endif |