Skip to content

Commit c4f23dc

Browse files
committed
Interval <-> string conversion: std::string operators
1 parent 8848430 commit c4f23dc

File tree

4 files changed

+37
-21
lines changed

4 files changed

+37
-21
lines changed

src/arithmetic/ibex_Interval.cpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -291,18 +291,17 @@ int Interval::diff(const Interval& y, Interval& c1, Interval& c2) const {
291291
return res;
292292
}
293293

294-
Interval::Interval(const std::string& s)
295-
{
296-
if(s.find(EMPTY_SET.str()) != std::string::npos)
294+
Interval::Interval(const std::string& s) {
295+
if(s.find((std::string)EMPTY_SET) != std::string::npos)
297296
*this = EMPTY_SET;
298297

299-
else if(s.find(POS_REALS.str()) != std::string::npos)
298+
else if(s.find((std::string)POS_REALS) != std::string::npos)
300299
*this = POS_REALS;
301300

302-
else if(s.find(NEG_REALS.str()) != std::string::npos)
301+
else if(s.find((std::string)NEG_REALS) != std::string::npos)
303302
*this = NEG_REALS;
304303

305-
else if(s.find(ALL_REALS.str()) != std::string::npos)
304+
else if(s.find((std::string)ALL_REALS) != std::string::npos)
306305
*this = ALL_REALS;
307306

308307
else { // string of the form "[lb, ub]"
@@ -348,10 +347,9 @@ std::ostream& operator<<(std::ostream& os, const Interval& x) {
348347
return os;
349348
}
350349

351-
std::string Interval::str(int precision) const
352-
{
350+
Interval::operator std::string() const {
353351
std::stringstream sstream;
354-
sstream << std::setprecision(precision) << *this;
352+
sstream << *this;
355353
return sstream.str();
356354
}
357355

src/arithmetic/ibex_Interval.h

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -508,12 +508,6 @@ class Interval {
508508
*/
509509
std::pair<Interval,Interval> bisect(double ratio=0.5) const;
510510

511-
/** \brief String
512-
*
513-
* Return string stream of \a x with a given precision.
514-
*/
515-
std::string str(int precision = 6) const;
516-
517511
/** \brief pi. */
518512
static const Interval PI;
519513
/** \brief 2*pi. */
@@ -544,6 +538,10 @@ class Interval {
544538
*/
545539
operator const ExprConstant&() const;
546540

541+
/** \brief Cast the interval into a const std::string
542+
*/
543+
operator std::string() const;
544+
547545
//private:
548546
#ifdef _IBEX_WITH_GAOL_
549547
/* \brief Wrap the gaol-interval [x]. */
@@ -951,8 +949,6 @@ inline Interval& Interval::operator=(double x) {
951949
return *this;
952950
}
953951

954-
955-
956952
inline Interval& Interval::operator=(const Interval& x) {
957953
itv = x.itv;
958954
return *this;
@@ -969,7 +965,6 @@ inline Interval& Interval::inflate(double delta, double chi) {
969965
return *this;
970966
}
971967

972-
973968
inline bool Interval::operator!=(const Interval& x) const {
974969
return !(*this==x);
975970
}
@@ -1207,7 +1202,6 @@ inline bool bwd_atan(const Interval& y, Interval& x) {
12071202
return !x.is_empty();
12081203
}
12091204

1210-
12111205
inline bool bwd_acosh(const Interval& y, Interval& x) {
12121206
if (y.is_empty() || y.ub()<0.0) {
12131207
x.set_empty(); return false;
@@ -1536,6 +1530,14 @@ inline bool bwd_imod(Interval& x, Interval& y, const double& p) {
15361530
return true;
15371531
}
15381532

1533+
inline std::string operator+(const std::string& s, const Interval& x) {
1534+
return s + (std::string)x;
1535+
}
1536+
1537+
inline std::string operator+(const Interval& x, const std::string& s) {
1538+
return (std::string)x + s;
1539+
}
1540+
15391541
} // end namespace ibex
15401542

15411543
#endif // _IBEX_INTERVAL_H_

tests/TestString.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* Updated : April 18, 2016
1111
* ---------------------------------------------------------------------------- */
1212

13+
#include <iomanip>
1314
#include "TestString.h"
1415

1516
using namespace std;
@@ -32,8 +33,21 @@ void TestString::test02() {
3233

3334
bool TestString::testConversion(const Interval& intv, int precision)
3435
{
35-
string intv_string = " " + intv.str(precision) + " "; // adding unwanted spaces
36-
Interval intv_parsed = Interval(intv_string);
36+
return testConversionString(intv) && testConversionOstream(intv, precision);
37+
}
38+
39+
bool TestString::testConversionString(const Interval& intv)
40+
{
41+
Interval intv_parsed = Interval(" " + intv + " "); // adding unwanted spaces
42+
return intv_parsed == intv ||
43+
fabs(intv_parsed.lb() - intv.lb()) < 1.0e-1 && fabs(intv_parsed.ub() - intv.ub()) < 1.0e-1;
44+
}
45+
46+
bool TestString::testConversionOstream(const Interval& intv, int precision)
47+
{
48+
stringstream sstream;
49+
sstream << setprecision(precision) << " " << intv << " "; // adding unwanted spaces
50+
Interval intv_parsed = Interval(sstream.str());
3751
return intv_parsed == intv ||
3852
fabs(intv_parsed.lb() - intv.lb()) < 1.0e-1 && fabs(intv_parsed.ub() - intv.ub()) < 1.0e-1;
3953
}

tests/TestString.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ class TestString : public CppUnit::TestFixture {
3939
protected:
4040

4141
bool testConversion(const Interval& intv, int precision);
42+
bool testConversionString(const Interval& intv);
43+
bool testConversionOstream(const Interval& intv, int precision);
4244
};
4345

4446
CPPUNIT_TEST_SUITE_REGISTRATION(TestString);

0 commit comments

Comments
 (0)