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 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540
|
// Copyright (c) 2005 Utrecht University (The Netherlands),
// ETH Zurich (Switzerland), Freie Universitaet Berlin (Germany),
// INRIA Sophia-Antipolis (France), Martin-Luther-University Halle-Wittenberg
// (Germany), Max-Planck-Institute Saarbruecken (Germany), RISC Linz (Austria),
// and Tel-Aviv University (Israel). All rights reserved.
//
// This file is part of CGAL (www.cgal.org); 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; version 2.1 of the License.
// See the file LICENSE.LGPL distributed with CGAL.
//
// Licensees holding a valid commercial license may use this file in
// accordance with the commercial license agreement provided with the software.
//
// This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
// WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
//
// $URL: svn+ssh://scm.gforge.inria.fr/svn/cgal/branches/CGAL-3.2-branch/Number_types/include/CGAL/Number_type_checker.h $
// $Id: Number_type_checker.h 28567 2006-02-16 14:30:13Z lsaboret $
//
//
// Author(s) : Sylvain Pion
#ifndef CGAL_NUMBER_TYPE_CHECKER_H
#define CGAL_NUMBER_TYPE_CHECKER_H
#include <CGAL/basic.h>
#include <CGAL/Number_type_checker_fwd.h>
// A number type class, parameterized by 2 number types NT1 and NT2.
// It runs all operations on parallel over NT1 and NT2.
// It is also parameterized by a comparator which compares the values
// of NT1 and NT2 after all arithmetic operations.
// #define CGAL_NT_CHECK_DEBUG(s) std::cerr << s << std::endl
#define CGAL_NT_CHECK_DEBUG(s)
CGAL_BEGIN_NAMESPACE
namespace CGALi {
// std::equal_to<T> sticks things to one type, so I define my own.
struct Equal_to
{
template < typename NT1, typename NT2 >
bool operator()(const NT1& a, const NT2& b) const
{ return a == b; }
};
} // namespace CGALi
template < typename NT1, typename NT2, typename Cmp = CGALi::Equal_to >
class Number_type_checker
{
NT1 _n1;
NT2 _n2;
Cmp cmp;
public:
typedef Number_type_checker<NT1, NT2, Cmp> Self;
// What if they differ between NT1 and NT2 ?
typedef typename Number_type_traits<NT1>::Has_gcd Has_gcd;
typedef typename Number_type_traits<NT1>::Has_division Has_division;
typedef typename Number_type_traits<NT1>::Has_sqrt Has_sqrt;
typedef typename Number_type_traits<NT1>::Has_exact_ring_operations
Has_exact_ring_operations;
typedef typename Number_type_traits<NT1>::Has_exact_division
Has_exact_division;
typedef typename Number_type_traits<NT1>::Has_exact_sqrt Has_exact_sqrt;
Number_type_checker() {}
Number_type_checker(int i)
: _n1(i), _n2(i) { CGAL_assertion(is_valid()); }
Number_type_checker(double d)
: _n1(d), _n2(d) { CGAL_assertion(is_valid()); }
Number_type_checker(const NT1 &n1, const NT2 &n2)
: _n1(n1), _n2(n2) { CGAL_assertion(is_valid()); }
// The following need to be dependant on NT1 != {NT2,int,double} ...
//Number_type_checker(const NT1 &n1) : _n1(n1), _n2(n1) {}
//Number_type_checker(const NT2 &n2) : _n1(n2), _n2(n2) {}
Self& operator+=(const Self &a)
{ n1() += a.n1(); n2() += a.n2(); CGAL_assertion(is_valid()); return *this; }
Self& operator-=(const Self &a)
{ n1() -= a.n1(); n2() -= a.n2(); CGAL_assertion(is_valid()); return *this; }
Self& operator*=(const Self &a)
{ n1() *= a.n1(); n2() *= a.n2(); CGAL_assertion(is_valid()); return *this; }
Self& operator/=(const Self &a)
{ n1() /= a.n1(); n2() /= a.n2(); CGAL_assertion(is_valid()); return *this; }
// Accessors and setters.
const NT1& n1() const { return _n1; }
const NT2& n2() const { return _n2; }
NT1& n1() { return _n1; }
NT2& n2() { return _n2; }
// Validity checking.
bool is_valid() const
{
CGAL_NT_CHECK_DEBUG("Checking...");
bool b = cmp(_n1, _n2);
if (!b)
{
CGAL_NT_CHECK_DEBUG("Different values :");
CGAL_NT_CHECK_DEBUG("n1 = " << _n1);
CGAL_NT_CHECK_DEBUG("n2 = " << _n2);
}
return b;
}
};
template < typename NT1, typename NT2, typename Cmp >
Number_type_checker<NT1, NT2, Cmp>
operator+(const Number_type_checker<NT1, NT2, Cmp> &a,
const Number_type_checker<NT1, NT2, Cmp> &b)
{
CGAL_NT_CHECK_DEBUG("operator+");
return Number_type_checker<NT1, NT2, Cmp>(a.n1() + b.n1(), a.n2() + b.n2());
}
template < typename NT1, typename NT2, typename Cmp >
Number_type_checker<NT1, NT2, Cmp>
operator+(int a, const Number_type_checker<NT1, NT2, Cmp> &b)
{
CGAL_NT_CHECK_DEBUG("operator+");
return Number_type_checker<NT1, NT2, Cmp>(a + b.n1(), a + b.n2());
}
template < typename NT1, typename NT2, typename Cmp >
Number_type_checker<NT1, NT2, Cmp>
operator+(const Number_type_checker<NT1, NT2, Cmp> &a, int b)
{
CGAL_NT_CHECK_DEBUG("operator+");
return Number_type_checker<NT1, NT2, Cmp>(a.n1() + b, a.n2() + b);
}
template < typename NT1, typename NT2, typename Cmp >
Number_type_checker<NT1, NT2, Cmp>
operator-(const Number_type_checker<NT1, NT2, Cmp> &a,
const Number_type_checker<NT1, NT2, Cmp> &b)
{
CGAL_NT_CHECK_DEBUG("operator-");
return Number_type_checker<NT1, NT2, Cmp>(a.n1() - b.n1(), a.n2() - b.n2());
}
template < typename NT1, typename NT2, typename Cmp >
Number_type_checker<NT1, NT2, Cmp>
operator-(int a, const Number_type_checker<NT1, NT2, Cmp> &b)
{
CGAL_NT_CHECK_DEBUG("operator-");
return Number_type_checker<NT1, NT2, Cmp>(a - b.n1(), a - b.n2());
}
template < typename NT1, typename NT2, typename Cmp >
Number_type_checker<NT1, NT2, Cmp>
operator-(const Number_type_checker<NT1, NT2, Cmp> &a, int b)
{
CGAL_NT_CHECK_DEBUG("operator-");
return Number_type_checker<NT1, NT2, Cmp>(a.n1() - b, a.n2() - b);
}
template < typename NT1, typename NT2, typename Cmp >
Number_type_checker<NT1, NT2, Cmp>
operator-(const Number_type_checker<NT1, NT2, Cmp> &a)
{
CGAL_NT_CHECK_DEBUG("unary operator-");
return Number_type_checker<NT1, NT2, Cmp>(-a.n1(), -a.n2());
}
template < typename NT1, typename NT2, typename Cmp >
Number_type_checker<NT1, NT2, Cmp>
operator*(const Number_type_checker<NT1, NT2, Cmp> &a,
const Number_type_checker<NT1, NT2, Cmp> &b)
{
CGAL_NT_CHECK_DEBUG("operator*");
return Number_type_checker<NT1, NT2, Cmp>(a.n1() * b.n1(), a.n2() * b.n2());
}
template < typename NT1, typename NT2, typename Cmp >
Number_type_checker<NT1, NT2, Cmp>
operator*(int a, const Number_type_checker<NT1, NT2, Cmp> &b)
{
CGAL_NT_CHECK_DEBUG("operator*");
return Number_type_checker<NT1, NT2, Cmp>(a * b.n1(), a * b.n2());
}
template < typename NT1, typename NT2, typename Cmp >
Number_type_checker<NT1, NT2, Cmp>
operator*(const Number_type_checker<NT1, NT2, Cmp> &a, int b)
{
CGAL_NT_CHECK_DEBUG("operator*");
return Number_type_checker<NT1, NT2, Cmp>(a.n1() * b, a.n2() * b);
}
template < typename NT1, typename NT2, typename Cmp >
Number_type_checker<NT1, NT2, Cmp>
operator/(const Number_type_checker<NT1, NT2, Cmp> &a,
const Number_type_checker<NT1, NT2, Cmp> &b)
{
CGAL_NT_CHECK_DEBUG("operator/");
return Number_type_checker<NT1, NT2, Cmp>(a.n1() / b.n1(), a.n2() / b.n2());
}
template < typename NT1, typename NT2, typename Cmp >
Number_type_checker<NT1, NT2, Cmp>
operator/(int a, const Number_type_checker<NT1, NT2, Cmp> &b)
{
CGAL_NT_CHECK_DEBUG("operator/");
return Number_type_checker<NT1, NT2, Cmp>(a / b.n1(), a / b.n2());
}
template < typename NT1, typename NT2, typename Cmp >
Number_type_checker<NT1, NT2, Cmp>
operator/(const Number_type_checker<NT1, NT2, Cmp> &a, int b)
{
CGAL_NT_CHECK_DEBUG("operator/");
return Number_type_checker<NT1, NT2, Cmp>(a.n1() / b, a.n2() / b);
}
template < typename NT1, typename NT2, typename Cmp >
Number_type_checker<NT1, NT2, Cmp>
sqrt(const Number_type_checker<NT1, NT2, Cmp> &a)
{
CGAL_NT_CHECK_DEBUG("operator/");
return Number_type_checker<NT1, NT2, Cmp>(CGAL::sqrt(a.n1()),
CGAL::sqrt(a.n2()));
}
template < typename NT1, typename NT2, typename Cmp >
bool
operator==(const Number_type_checker<NT1, NT2, Cmp> &a,
const Number_type_checker<NT1, NT2, Cmp> &b)
{
bool b1 = a.n1() == b.n1();
bool b2 = a.n2() == b.n2();
CGAL_assertion(b1 == b2);
return b1;
}
template < typename NT1, typename NT2, typename Cmp >
bool
operator!=(const Number_type_checker<NT1, NT2, Cmp> &a,
const Number_type_checker<NT1, NT2, Cmp> &b)
{
bool b1 = a.n1() != b.n1();
bool b2 = a.n2() != b.n2();
CGAL_assertion(b1 == b2);
return b1;
}
template < typename NT1, typename NT2, typename Cmp >
bool
operator<(const Number_type_checker<NT1, NT2, Cmp> &a,
const Number_type_checker<NT1, NT2, Cmp> &b)
{
bool b1 = a.n1() < b.n1();
bool b2 = a.n2() < b.n2();
CGAL_assertion(b1 == b2);
return b1;
}
template < typename NT1, typename NT2, typename Cmp >
bool
operator>(const Number_type_checker<NT1, NT2, Cmp> &a,
const Number_type_checker<NT1, NT2, Cmp> &b)
{
bool b1 = a.n1() > b.n1();
bool b2 = a.n2() > b.n2();
CGAL_assertion(b1 == b2);
return b1;
}
template < typename NT1, typename NT2, typename Cmp >
bool
operator<=(const Number_type_checker<NT1, NT2, Cmp> &a,
const Number_type_checker<NT1, NT2, Cmp> &b)
{
bool b1 = a.n1() <= b.n1();
bool b2 = a.n2() <= b.n2();
CGAL_assertion(b1 == b2);
return b1;
}
template < typename NT1, typename NT2, typename Cmp >
bool
operator>=(const Number_type_checker<NT1, NT2, Cmp> &a,
const Number_type_checker<NT1, NT2, Cmp> &b)
{
bool b1 = a.n1() >= b.n1();
bool b2 = a.n2() >= b.n2();
CGAL_assertion(b1 == b2);
return b1;
}
template < typename NT1, typename NT2, typename Cmp >
bool
operator==(const Number_type_checker<NT1, NT2, Cmp> &a, int i)
{
bool b1 = a.n1() == i;
bool b2 = a.n2() == i;
CGAL_assertion(b1 == b2);
return b1;
}
template < typename NT1, typename NT2, typename Cmp >
bool
operator!=(const Number_type_checker<NT1, NT2, Cmp> &a, int i)
{
bool b1 = a.n1() != i;
bool b2 = a.n2() != i;
CGAL_assertion(b1 == b2);
return b1;
}
template < typename NT1, typename NT2, typename Cmp >
bool
operator<(const Number_type_checker<NT1, NT2, Cmp> &a, int i)
{
bool b1 = a.n1() < i;
bool b2 = a.n2() < i;
CGAL_assertion(b1 == b2);
return b1;
}
template < typename NT1, typename NT2, typename Cmp >
bool
operator>(const Number_type_checker<NT1, NT2, Cmp> &a, int i)
{
bool b1 = a.n1() > i;
bool b2 = a.n2() > i;
CGAL_assertion(b1 == b2);
return b1;
}
template < typename NT1, typename NT2, typename Cmp >
bool
operator<=(const Number_type_checker<NT1, NT2, Cmp> &a, int i)
{
bool b1 = a.n1() <= i;
bool b2 = a.n2() <= i;
CGAL_assertion(b1 == b2);
return b1;
}
template < typename NT1, typename NT2, typename Cmp >
bool
operator>=(const Number_type_checker<NT1, NT2, Cmp> &a, int i)
{
bool b1 = a.n1() >= i;
bool b2 = a.n2() >= i;
CGAL_assertion(b1 == b2);
return b1;
}
template < typename NT1, typename NT2, typename Cmp >
bool
operator==(int i, const Number_type_checker<NT1, NT2, Cmp> &b)
{
bool b1 = i == b.n1();
bool b2 = i == b.n2();
CGAL_assertion(b1 == b2);
return b1;
}
template < typename NT1, typename NT2, typename Cmp >
bool
operator!=(int i, const Number_type_checker<NT1, NT2, Cmp> &b)
{
bool b1 = i != b.n1();
bool b2 = i != b.n2();
CGAL_assertion(b1 == b2);
return b1;
}
template < typename NT1, typename NT2, typename Cmp >
bool
operator<(int i, const Number_type_checker<NT1, NT2, Cmp> &b)
{
bool b1 = i < b.n1();
bool b2 = i < b.n2();
CGAL_assertion(b1 == b2);
return b1;
}
template < typename NT1, typename NT2, typename Cmp >
bool
operator>(int i, const Number_type_checker<NT1, NT2, Cmp> &b)
{
bool b1 = i > b.n1();
bool b2 = i > b.n2();
CGAL_assertion(b1 == b2);
return b1;
}
template < typename NT1, typename NT2, typename Cmp >
bool
operator<=(int i, const Number_type_checker<NT1, NT2, Cmp> &b)
{
bool b1 = i <= b.n1();
bool b2 = i <= b.n2();
CGAL_assertion(b1 == b2);
return b1;
}
template < typename NT1, typename NT2, typename Cmp >
bool
operator>=(int i, const Number_type_checker<NT1, NT2, Cmp> &b)
{
bool b1 = i >= b.n1();
bool b2 = i >= b.n2();
CGAL_assertion(b1 == b2);
return b1;
}
template < typename NT1, typename NT2, typename Cmp >
Sign
sign(const Number_type_checker<NT1, NT2, Cmp> &a)
{
Sign s1 = CGAL::sign(a.n1());
Sign s2 = CGAL::sign(a.n2());
CGAL_assertion(s1 == s2);
return s1;
}
template < typename NT1, typename NT2, typename Cmp >
Comparison_result
compare(const Number_type_checker<NT1, NT2, Cmp> &a,
const Number_type_checker<NT1, NT2, Cmp> &b)
{
Comparison_result c1 = CGAL::compare(a.n1(), b.n1());
Comparison_result c2 = CGAL::compare(a.n2(), b.n2());
CGAL_assertion(c1 == c2);
return c1;
}
template < typename NT1, typename NT2, typename Cmp >
Comparison_result
compare(int a, const Number_type_checker<NT1, NT2, Cmp> &b)
{
Comparison_result c1 = CGAL::compare(a, b.n1());
Comparison_result c2 = CGAL::compare(a, b.n2());
CGAL_assertion(c1 == c2);
return c1;
}
template < typename NT1, typename NT2, typename Cmp >
Comparison_result
compare(const Number_type_checker<NT1, NT2, Cmp> &a, int b)
{
Comparison_result c1 = CGAL::compare(a.n1(), b);
Comparison_result c2 = CGAL::compare(a.n2(), b);
CGAL_assertion(c1 == c2);
return c1;
}
template < typename NT1, typename NT2, typename Cmp >
bool
is_finite(const Number_type_checker<NT1, NT2, Cmp> &a)
{
bool b1 = CGAL::is_finite(a.n1());
bool b2 = CGAL::is_finite(a.n2());
CGAL_assertion(b1 == b2);
return b1;
}
template < typename NT1, typename NT2, typename Cmp >
bool
is_valid(const Number_type_checker<NT1, NT2, Cmp> &a)
{
bool b1 = CGAL::is_valid(a.n1());
bool b2 = CGAL::is_valid(a.n2());
CGAL_assertion(b1 == b2);
// Should we also call a.is_valid() ?
return b1;
}
template < typename NT1, typename NT2, typename Cmp >
std::pair<double, double>
to_interval(const Number_type_checker<NT1, NT2, Cmp> &a)
{
std::pair<double, double> i1 = CGAL::to_interval(a.n1());
std::pair<double, double> i2 = CGAL::to_interval(a.n2());
// Here we could check that there is a common point.
// CGAL_assertion( ??? );
// We return one of the two.
return i1;
}
template < typename NT1, typename NT2, typename Cmp >
double
to_double(const Number_type_checker<NT1, NT2, Cmp> &a)
{
double d1 = CGAL::to_double(a.n1());
double d2 = CGAL::to_double(a.n2());
// What can we check ?
// We return one of the two.
return d1;
}
// What to do with the IO ?
// - either pick one as the main, and convert to the second.
// - output/input pairs, and read both (=> problems with FP values).
template < typename NT1, typename NT2, typename Cmp >
std::ostream &
operator<< (std::ostream & os, const Number_type_checker<NT1, NT2, Cmp> &b)
{
return os << b.n1();
}
template < typename NT1, typename NT2, typename Cmp >
std::istream &
operator>> (std::istream & is, Number_type_checker<NT1, NT2, Cmp> &b)
{
is >> b.n1();
b.n2() = b.n1(); // We hope that there is a conversion.
return is;
}
template < typename NT1, typename NT2, typename Cmp >
io_Operator
io_tag(const Number_type_checker<NT1, NT2, Cmp> &)
{
// Not sure what do to here.
return io_Operator();
}
CGAL_END_NAMESPACE
#endif // CGAL_NUMBER_TYPE_CHECKER_H
|