File: DataTypeComparator.h

package info (click to toggle)
fact%2B%2B 1.6.5~dfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 4,496 kB
  • sloc: cpp: 28,000; java: 22,674; xml: 3,268; makefile: 102; ansic: 61; sh: 3
file content (217 lines) | stat: -rw-r--r-- 6,384 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/* This file is part of the FaCT++ DL reasoner
Copyright (C) 2005-2015 Dmitry Tsarkov and The University of Manchester
Copyright (C) 2015-2016 Dmitry Tsarkov

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/

#ifndef DATATYPECOMPARATOR_H
#define DATATYPECOMPARATOR_H

#include <time.h>
#include <string>
#include <ostream>

#include "fpp_assert.h"
#include "globaldef.h"

/// allows one to compare DataType values wrt appropriate type
class ComparableDT
{
private:
		/// value as a string
	std::string strValue;
		/// value as a number
	long longIntValue;
		/// value as a float
	float floatValue;
		/// value as a date
	long timeValue;	// FIXME!! FORNOW
		/// tag of a value
	enum ValueType { UNUSED = 0, INT, STR, FLOAT, TIME } vType;

public:
		/// create empty dt
	ComparableDT ( void )
		: strValue("")
		, longIntValue(0)
		, floatValue(0.0)
		, timeValue(0)
		, vType(UNUSED)
		{}
		/// create NUMBER's dt
	explicit ComparableDT ( long int value )
		: strValue("")
		, longIntValue(value)
		, floatValue(0.0)
		, timeValue(0)
		, vType(INT)
		{}
		/// create STRING's dt
	explicit ComparableDT ( const char* value )
		: strValue(value)
		, longIntValue(0)
		, floatValue(0.0)
		, timeValue(0)
		, vType(STR)
		{}
		/// create FLOAT's dt
	explicit ComparableDT ( float value )
		: strValue("")
		, longIntValue(0)
		, floatValue(value)
		, timeValue(0)
		, vType(FLOAT)
		{}
		/// create dateTime's dt; use dummy parameter to distinguish it from INT one
	explicit ComparableDT ( long value, int )
		: strValue("")
		, longIntValue(0)
		, floatValue(0.0)
		, timeValue(value)
		, vType(TIME)
		{}
		/// copy c'tor
	ComparableDT ( const ComparableDT& copy )
		: strValue(copy.strValue)
		, longIntValue(copy.longIntValue)
		, floatValue(copy.floatValue)
		, timeValue(copy.timeValue)
		, vType(copy.vType)
		{}
		/// assignment
	ComparableDT& operator= ( const ComparableDT& copy )
	{
		vType = copy.vType;
		strValue = copy.strValue;
		longIntValue = copy.longIntValue;
		floatValue = copy.floatValue;
		timeValue = copy.timeValue;
		return *this;
	}
		/// empty d'tor
	~ComparableDT ( void ) {}

		/// get NUMBER
	long int getLongIntValue ( void ) const { return longIntValue; }
		/// get STRING
	const std::string& getStringValue ( void ) const { return strValue; }
		/// get FLOAT
	float getFloatValue ( void ) const { return floatValue; }
		/// get TIME
	long getTimeValue ( void ) const { return timeValue; }
		/// check if the datatype is discrete
	bool hasDiscreteType ( void ) const { return vType == INT || vType == TIME; }
		/// check whether the comparator is inited
	bool inited ( void ) const { return vType != UNUSED; }
		/// check whether the comparator is compatible with another one
	bool compatible ( const ComparableDT& other ) const { return !inited() || !other.inited() || vType == other.vType; }
		/// correct min value if the DT is discrete and EXCL is true; @return new EXCL value
	bool correctMin ( bool excl )
	{
		if ( vType == INT && excl )
		{	// transform (n,} into [n+1,}
			longIntValue++;
			return false;
		}
		if ( vType == TIME && excl )
		{	// transform (n,} into [n+1,}
			timeValue++;
			return false;
		}
		return excl;
	}
		/// correct max value if the DT is discrete and EXCL is true; @return new EXCL value
	bool correctMax ( bool excl )
	{
		if ( vType == INT && excl )
		{	// transform (n,} into [n+1,}
			longIntValue--;
			return false;
		}
		if ( vType == TIME && excl )
		{	// transform (n,} into [n+1,}
			timeValue--;
			return false;
		}
		return excl;
	}

	// compare 2 values

		/// check whether 2 DT entries with the same TYPE are equal
	bool operator == ( const ComparableDT& other ) const
	{
		fpp_assert ( vType == other.vType );	// sanity check
		switch ( vType )
		{
		case INT:	return getLongIntValue() == other.getLongIntValue();
		case STR:	return getStringValue() == other.getStringValue();
		case FLOAT:	return getFloatValue() == other.getFloatValue();
		case TIME:	return getTimeValue() == other.getTimeValue();
		default:	fpp_unreachable(); return false;
		}
	}
		/// check whether 2 DT entries with the same TYPE are NOT equal
	bool operator != ( const ComparableDT& other ) const
		{ return !(*this == other); }
		/// check whether 2 DT entries with the same TYPE are in '<' relation
	bool operator < ( const ComparableDT& other ) const
	{
		fpp_assert ( vType == other.vType );	// sanity check
		switch ( vType )
		{
		case INT:	return getLongIntValue() < other.getLongIntValue();
		case STR:	return getStringValue() < other.getStringValue();
		case FLOAT:	return getFloatValue() < other.getFloatValue();
		case TIME:	return getTimeValue() < other.getTimeValue();
		default:	fpp_unreachable(); return false;
		}
	}
		/// check whether 2 DT entries with the same TYPE are in '>' relation
	bool operator > ( const ComparableDT& other ) const { return other < *this; }

		/// print the LISP value of a DTC; FIXME!! hack
	std::ostream& printValue ( std::ostream& o ) const
	{
		o << ' ';
		switch ( vType )
		{
		case INT:	o << getLongIntValue(); break;
		case FLOAT:	o << getFloatValue(); break;
		case STR:	o << '"' << getStringValue().c_str() << '"'; break;
		case TIME:	o << getTimeValue(); break;
		default:	fpp_unreachable();
		}
		return o;
	}
	friend std::ostream& operator << ( std::ostream& o, const ComparableDT& cdt );
}; // ComparableDT

inline
std::ostream& operator << ( std::ostream& o, const ComparableDT& cdt )
{
	switch ( cdt.vType )
	{
	case ComparableDT::INT:	o << cdt.getLongIntValue(); break;
	case ComparableDT::STR:	o << cdt.getStringValue().c_str(); break;
	case ComparableDT::FLOAT:	o << cdt.getFloatValue(); break;
	case ComparableDT::TIME:	o << cdt.getTimeValue(); break;
	default:	fpp_unreachable();
	}
	return o;
}

#endif