| 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 TSETASTREE_H |
| 21 | #define TSETASTREE_H |
| 22 | |
| 23 | #include <set> |
| 24 | |
| 25 | // implement model cache set as a tree-set |
| 26 | class TSetAsTree |
| 27 | { |
| 28 | protected: // types |
| 29 | /// base type |
| 30 | typedef std::set<unsigned int> BaseType; |
| 31 | |
| 32 | protected: // members |
| 33 | /// set implementation |
| 34 | BaseType Base; |
| 35 | /// maximal number of elements |
| 36 | unsigned int nElems; |
| 37 | |
| 38 | public: // interface |
| 39 | /// empty c'tor taking max possible number of elements in the set |
| 40 | explicit TSetAsTree ( unsigned int size ) : nElems(size) {} |
| 41 | /// copy c'tor |
| 42 | TSetAsTree ( const TSetAsTree& ) = default; |
| 43 | /// move c'tor |
| 44 | TSetAsTree ( TSetAsTree&& ) = default; |
| 45 | /// assignment |
| 46 | TSetAsTree& operator= ( const TSetAsTree& ) = default; |
| 47 | /// move assignment |
| 48 | TSetAsTree& operator= ( TSetAsTree&& ) = default; |
| 49 | /// empty d'tor |
| 50 | ~TSetAsTree() = default; |
| 51 | |
| 52 | /// adds given index to the set |
| 53 | void insert ( unsigned int i ) |
| 54 | { |
| 55 | # ifdef ENABLE_CHECKING |
| 56 | fpp_assert ( i > 0 ); |
| 57 | # endif |
| 58 | Base.insert(i); |
| 59 | } |
| 60 | /// completes the set with [1,n) |
| 61 | void completeSet ( void ) |
| 62 | { |
| 63 | for ( unsigned int i = 1; i < nElems; ++i ) |
| 64 | Base.insert(i); |
| 65 | } |
| 66 | /// adds the given set to the current one |
| 67 | TSetAsTree& operator |= ( const TSetAsTree& is ) |
| 68 | { |
| 69 | Base.insert ( is.Base.begin(), is.Base.end() ); |
| 70 | return *this; |
| 71 | } |
| 72 | /// clear the set |
| 73 | void clear ( void ) { Base.clear(); } |
| 74 | |
| 75 | /// check whether the set is empty |
| 76 | bool empty ( void ) const { return Base.empty(); } |
| 77 | /// check whether I contains in the set |
| 78 | bool contains ( unsigned int i ) const { return Base.find(i) != Base.end(); } |
| 79 | /// check whether the intersection between the current set and IS is nonempty |
| 80 | bool intersects ( const TSetAsTree& is ) const |
| 81 | { |
| 82 | if ( Base.empty() || is.Base.empty() ) |
| 83 | return false; |
| 84 | |
| 85 | BaseType::const_iterator p1 = Base.begin(), p1_end = Base.end(), p2 = is.Base.begin(), p2_end = is.Base.end(); |
| 86 | while ( p1 != p1_end && p2 != p2_end ) |
| 87 | if ( *p1 == *p2 ) |
| 88 | return true; |
| 89 | else if ( *p1 < *p2 ) |
| 90 | ++p1; |
| 91 | else |
| 92 | ++p2; |
| 93 | |
| 94 | return false; |
| 95 | } |
| 96 | /// prints the set in a human-readable form |
| 97 | void print ( std::ostream& o ) const |
| 98 | { |
| 99 | o << "{"; |
| 100 | if ( !empty() ) |
| 101 | { |
| 102 | BaseType::const_iterator p = Base.begin(), p_end = Base.end(); |
| 103 | o << *p; |
| 104 | while ( ++p != p_end ) |
| 105 | o << ',' << *p; |
| 106 | } |
| 107 | o << "}"; |
| 108 | } |
| 109 | typedef BaseType::const_iterator const_iterator; |
| 110 | const_iterator begin ( void ) const { return Base.begin(); } |
| 111 | const_iterator end ( void ) const { return Base.end(); } |
| 112 | |
| 113 | /// size of a set |
| 114 | size_t size ( void ) const { return Base.size(); } |
| 115 | /// maximal size of a set |
| 116 | unsigned int maxSize ( void ) const { return nElems; } |
| 117 | }; // TSetAsTree |
| 118 | |
| 119 | #endif |