| 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 DATATYPECOMPARATOR_H |
| 21 | #define DATATYPECOMPARATOR_H |
| 22 | |
| 23 | #include <ctime> |
| 24 | #include <string> |
| 25 | #include <ostream> |
| 26 | |
| 27 | #include "fpp_assert.h" |
| 28 | #include "globaldef.h" |
| 29 | |
| 30 | /// allows one to compare DataType values wrt appropriate type |
| 31 | class ComparableDT |
| 32 | { |
| 33 | private: // members |
| 34 | /// value as a string |
| 35 | std::string strValue; |
| 36 | /// value as a number |
| 37 | long longIntValue = 0; |
| 38 | /// value as a float |
| 39 | float floatValue = 0.0; |
| 40 | /// value as a date |
| 41 | long timeValue = 0; // FIXME!! FORNOW |
| 42 | /// tag of a value |
| 43 | enum ValueType { UNUSED = 0, INT, STR, FLOAT, TIME } vType = UNUSED; |
| 44 | |
| 45 | public: // interface |
| 46 | /// create empty dt |
| 47 | ComparableDT() = default; |
| 48 | /// create NUMBER's dt |
| 49 | explicit ComparableDT ( long int value ) |
| 50 | : longIntValue(value) |
| 51 | , vType(INT) |
| 52 | {} |
| 53 | /// create STRING's dt |
| 54 | explicit ComparableDT ( const char* value ) |
| 55 | : strValue(value) |
| 56 | , vType(STR) |
| 57 | {} |
| 58 | /// create FLOAT's dt |
| 59 | explicit ComparableDT ( float value ) |
| 60 | : floatValue(value) |
| 61 | , vType(FLOAT) |
| 62 | {} |
| 63 | /// create dateTime's dt; use dummy parameter to distinguish it from INT one |
| 64 | explicit ComparableDT ( long value, int ) |
| 65 | : timeValue(value) |
| 66 | , vType(TIME) |
| 67 | {} |
| 68 | /// copy c'tor |
| 69 | ComparableDT ( const ComparableDT& copy ) = default; |
| 70 | /// assignment |
| 71 | ComparableDT& operator= ( const ComparableDT& copy ) = default; |
| 72 | |
| 73 | /// get NUMBER |
| 74 | long int getLongIntValue ( void ) const { return longIntValue; } |
| 75 | /// get STRING |
| 76 | const std::string& getStringValue ( void ) const { return strValue; } |
| 77 | /// get FLOAT |
| 78 | float getFloatValue ( void ) const { return floatValue; } |
| 79 | /// get TIME |
| 80 | long getTimeValue ( void ) const { return timeValue; } |
| 81 | /// check if the datatype is discrete |
| 82 | bool hasDiscreteType ( void ) const { return vType == INT || vType == TIME; } |
| 83 | /// check whether the comparator is inited |
| 84 | bool inited ( void ) const { return vType != UNUSED; } |
| 85 | /// check whether the comparator is compatible with another one |
| 86 | bool compatible ( const ComparableDT& other ) const { return !inited() || !other.inited() || vType == other.vType; } |
| 87 | /// correct min value if the DT is discrete and EXCL is true; @return new EXCL value |
| 88 | bool correctMin ( bool excl ) |
| 89 | { |
| 90 | if ( vType == INT && excl ) |
| 91 | { // transform (n,} into [n+1,} |
| 92 | longIntValue++; |
| 93 | return false; |
| 94 | } |
| 95 | if ( vType == TIME && excl ) |
| 96 | { // transform (n,} into [n+1,} |
| 97 | timeValue++; |
| 98 | return false; |
| 99 | } |
| 100 | return excl; |
| 101 | } |
| 102 | /// correct max value if the DT is discrete and EXCL is true; @return new EXCL value |
| 103 | bool correctMax ( bool excl ) |
| 104 | { |
| 105 | if ( vType == INT && excl ) |
| 106 | { // transform (n,} into [n+1,} |
| 107 | longIntValue--; |
| 108 | return false; |
| 109 | } |
| 110 | if ( vType == TIME && excl ) |
| 111 | { // transform (n,} into [n+1,} |
| 112 | timeValue--; |
| 113 | return false; |
| 114 | } |
| 115 | return excl; |
| 116 | } |
| 117 | |
| 118 | // compare 2 values |
| 119 | |
| 120 | /// check whether 2 DT entries with the same TYPE are equal |
| 121 | bool operator == ( const ComparableDT& other ) const |
| 122 | { |
| 123 | fpp_assert ( vType == other.vType ); // sanity check |
| 124 | switch ( vType ) |
| 125 | { |
| 126 | case INT: return getLongIntValue() == other.getLongIntValue(); |
| 127 | case STR: return getStringValue() == other.getStringValue(); |
| 128 | case FLOAT: return getFloatValue() == other.getFloatValue(); |
| 129 | case TIME: return getTimeValue() == other.getTimeValue(); |
| 130 | default: fpp_unreachable(); |
| 131 | } |
| 132 | } |
| 133 | /// check whether 2 DT entries with the same TYPE are NOT equal |
| 134 | bool operator != ( const ComparableDT& other ) const |
| 135 | { return !(*this == other); } |
| 136 | /// check whether 2 DT entries with the same TYPE are in '<' relation |
| 137 | bool operator < ( const ComparableDT& other ) const |
| 138 | { |
| 139 | fpp_assert ( vType == other.vType ); // sanity check |
| 140 | switch ( vType ) |
| 141 | { |
| 142 | case INT: return getLongIntValue() < other.getLongIntValue(); |
| 143 | case STR: return getStringValue() < other.getStringValue(); |
| 144 | case FLOAT: return getFloatValue() < other.getFloatValue(); |
| 145 | case TIME: return getTimeValue() < other.getTimeValue(); |
| 146 | default: fpp_unreachable(); |
| 147 | } |
| 148 | } |
| 149 | /// check whether 2 DT entries with the same TYPE are in '>' relation |
| 150 | bool operator > ( const ComparableDT& other ) const { return other < *this; } |
| 151 | |
| 152 | /// print the LISP value of a DTC; FIXME!! hack |
| 153 | std::ostream& printValue ( std::ostream& o ) const |
| 154 | { |
| 155 | o << ' '; |
| 156 | switch ( vType ) |
| 157 | { |
| 158 | case INT: o << getLongIntValue(); break; |
| 159 | case FLOAT: o << getFloatValue(); break; |
| 160 | case STR: o << '"' << getStringValue() << '"'; break; |
| 161 | case TIME: o << getTimeValue(); break; |
| 162 | default: fpp_unreachable(); |
| 163 | } |
| 164 | return o; |
| 165 | } |
| 166 | friend std::ostream& operator << ( std::ostream& o, const ComparableDT& cdt ); |
| 167 | }; // ComparableDT |
| 168 | |
| 169 | inline |
| 170 | std::ostream& operator << ( std::ostream& o, const ComparableDT& cdt ) |
| 171 | { |
| 172 | switch ( cdt.vType ) |
| 173 | { |
| 174 | case ComparableDT::INT: o << cdt.getLongIntValue(); break; |
| 175 | case ComparableDT::STR: o << cdt.getStringValue(); break; |
| 176 | case ComparableDT::FLOAT: o << cdt.getFloatValue(); break; |
| 177 | case ComparableDT::TIME: o << cdt.getTimeValue(); break; |
| 178 | default: fpp_unreachable(); |
| 179 | } |
| 180 | return o; |
| 181 | } |
| 182 | |
| 183 | #endif |