0
3
0
| 1 | 1 |
/* -*- C++ -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library |
| 4 | 4 |
* |
| 5 | 5 |
* Copyright (C) 2003-2008 |
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_DIM2_H |
| 20 | 20 |
#define LEMON_DIM2_H |
| 21 | 21 |
|
| 22 | 22 |
#include <iostream> |
| 23 | 23 |
#include <lemon/bits/utility.h> |
| 24 | 24 |
|
| 25 | 25 |
///\ingroup misc |
| 26 | 26 |
///\file |
| 27 | 27 |
///\brief A simple two dimensional vector and a bounding box implementation |
| 28 | 28 |
/// |
| 29 | 29 |
/// The class \ref lemon::dim2::Point "dim2::Point" implements |
| 30 | 30 |
///a two dimensional vector with the usual |
| 31 | 31 |
/// operations. |
| 32 | 32 |
/// |
| 33 | 33 |
/// The class \ref lemon::dim2::BoundingBox "dim2::BoundingBox" |
| 34 | 34 |
/// can be used to determine |
| 35 | 35 |
/// the rectangular bounding box of a set of |
| 36 | 36 |
/// \ref lemon::dim2::Point "dim2::Point"'s. |
| 37 | 37 |
|
| 38 | 38 |
namespace lemon {
|
| 39 | 39 |
|
| 40 | 40 |
///Tools for handling two dimensional coordinates |
| 41 | 41 |
|
| 42 | 42 |
///This namespace is a storage of several |
| 43 | 43 |
///tools for handling two dimensional coordinates |
| 44 | 44 |
namespace dim2 {
|
| 45 | 45 |
|
| 46 | 46 |
/// \addtogroup misc |
| 47 | 47 |
/// @{
|
| 48 | 48 |
|
| 49 | 49 |
/// A simple two dimensional vector (plainvector) implementation |
| 50 | 50 |
|
| 51 | 51 |
/// A simple two dimensional vector (plainvector) implementation |
| 52 | 52 |
///with the usual vector |
| 53 | 53 |
/// operators. |
| 54 | 54 |
/// |
| 55 | 55 |
template<typename T> |
| 56 | 56 |
class Point {
|
| 57 | 57 |
|
| 58 | 58 |
public: |
| 59 | 59 |
|
| 60 | 60 |
typedef T Value; |
| 61 | 61 |
|
| 62 | 62 |
///First coordinate |
| 63 | 63 |
T x; |
| 64 | 64 |
///Second coordinate |
| 65 | 65 |
T y; |
| 66 | 66 |
|
| 67 | 67 |
///Default constructor |
| 68 | 68 |
Point() {}
|
| 69 | 69 |
|
| 70 | 70 |
///Construct an instance from coordinates |
| 71 | 71 |
Point(T a, T b) : x(a), y(b) { }
|
| 72 | 72 |
|
| 73 | 73 |
///The dimension of the vector. |
| 74 | 74 |
|
| 75 | 75 |
///The dimension of the vector. |
| 76 | 76 |
///This function always returns 2. |
| 77 | 77 |
int size() const { return 2; }
|
| 78 | 78 |
|
| 79 | 79 |
///Subscripting operator |
| 80 | 80 |
|
| 81 | 81 |
///\c p[0] is \c p.x and \c p[1] is \c p.y |
| 82 | 82 |
/// |
| 83 | 83 |
T& operator[](int idx) { return idx == 0 ? x : y; }
|
| 84 | 84 |
|
| 85 | 85 |
///Const subscripting operator |
| 86 | 86 |
|
| 87 | 87 |
///\c p[0] is \c p.x and \c p[1] is \c p.y |
| 88 | 88 |
/// |
| 89 | 89 |
const T& operator[](int idx) const { return idx == 0 ? x : y; }
|
| 90 | 90 |
|
| 91 | 91 |
///Conversion constructor |
| 92 | 92 |
template<class TT> Point(const Point<TT> &p) : x(p.x), y(p.y) {}
|
| 93 | 93 |
|
| 94 | 94 |
///Give back the square of the norm of the vector |
| 95 | 95 |
T normSquare() const {
|
| 96 | 96 |
return x*x+y*y; |
| 97 | 97 |
} |
| 98 | 98 |
|
| 99 | 99 |
///Increment the left hand side by u |
| 100 | 100 |
Point<T>& operator +=(const Point<T>& u) {
|
| 101 | 101 |
x += u.x; |
| 102 | 102 |
y += u.y; |
| 103 | 103 |
return *this; |
| 104 | 104 |
} |
| 105 | 105 |
|
| 106 | 106 |
///Decrement the left hand side by u |
| 107 | 107 |
Point<T>& operator -=(const Point<T>& u) {
|
| 108 | 108 |
x -= u.x; |
| 109 | 109 |
y -= u.y; |
| 110 | 110 |
return *this; |
| 111 | 111 |
} |
| 112 | 112 |
|
| 113 | 113 |
///Multiply the left hand side with a scalar |
| 114 | 114 |
Point<T>& operator *=(const T &u) {
|
| 115 | 115 |
x *= u; |
| 116 | 116 |
y *= u; |
| 117 | 117 |
return *this; |
| 118 | 118 |
} |
| 119 | 119 |
|
| 120 | 120 |
///Divide the left hand side by a scalar |
| 121 | 121 |
Point<T>& operator /=(const T &u) {
|
| 122 | 122 |
x /= u; |
| 123 | 123 |
y /= u; |
| 124 | 124 |
return *this; |
| 125 | 125 |
} |
| 126 | 126 |
|
| 127 | 127 |
///Return the scalar product of two vectors |
| 128 | 128 |
T operator *(const Point<T>& u) const {
|
| 129 | 129 |
return x*u.x+y*u.y; |
| 130 | 130 |
} |
| 131 | 131 |
|
| 132 | 132 |
///Return the sum of two vectors |
| 133 | 133 |
Point<T> operator+(const Point<T> &u) const {
|
| 134 | 134 |
Point<T> b=*this; |
| 135 | 135 |
return b+=u; |
| 136 | 136 |
} |
| 137 | 137 |
|
| 138 | 138 |
///Return the negative of the vector |
| 139 | 139 |
Point<T> operator-() const {
|
| 140 | 140 |
Point<T> b=*this; |
| 141 | 141 |
b.x=-b.x; b.y=-b.y; |
| 142 | 142 |
return b; |
| 143 | 143 |
} |
| 144 | 144 |
|
| 145 | 145 |
///Return the difference of two vectors |
| 146 | 146 |
Point<T> operator-(const Point<T> &u) const {
|
| 147 | 147 |
Point<T> b=*this; |
| 148 | 148 |
return b-=u; |
| 149 | 149 |
} |
| 150 | 150 |
|
| 151 | 151 |
///Return a vector multiplied by a scalar |
| 152 | 152 |
Point<T> operator*(const T &u) const {
|
| 153 | 153 |
Point<T> b=*this; |
| 154 | 154 |
return b*=u; |
| 155 | 155 |
} |
| 156 | 156 |
|
| 157 | 157 |
///Return a vector divided by a scalar |
| 158 | 158 |
Point<T> operator/(const T &u) const {
|
| 159 | 159 |
Point<T> b=*this; |
| 160 | 160 |
return b/=u; |
| 161 | 161 |
} |
| 162 | 162 |
|
| 163 | 163 |
///Test equality |
| 164 | 164 |
bool operator==(const Point<T> &u) const {
|
| 165 | 165 |
return (x==u.x) && (y==u.y); |
| 166 | 166 |
} |
| 167 | 167 |
|
| 168 | 168 |
///Test inequality |
| 169 | 169 |
bool operator!=(Point u) const {
|
| 170 | 170 |
return (x!=u.x) || (y!=u.y); |
| 171 | 171 |
} |
| 172 | 172 |
|
| 173 | 173 |
}; |
| 174 | 174 |
|
| 175 | 175 |
///Return a Point |
| 176 | 176 |
|
| 177 | 177 |
///Return a Point. |
| 178 | 178 |
///\relates Point |
| 179 | 179 |
template <typename T> |
| 180 | 180 |
inline Point<T> makePoint(const T& x, const T& y) {
|
| 181 | 181 |
return Point<T>(x, y); |
| 182 | 182 |
} |
| 183 | 183 |
|
| 184 | 184 |
///Return a vector multiplied by a scalar |
| 185 | 185 |
|
| 186 | 186 |
///Return a vector multiplied by a scalar. |
| 187 | 187 |
///\relates Point |
| 188 | 188 |
template<typename T> Point<T> operator*(const T &u,const Point<T> &x) {
|
| 189 | 189 |
return x*u; |
| 190 | 190 |
} |
| 191 | 191 |
|
| 192 | 192 |
///Read a plainvector from a stream |
| 193 | 193 |
|
| 194 | 194 |
///Read a plainvector from a stream. |
| 195 | 195 |
///\relates Point |
| 196 | 196 |
/// |
| 197 | 197 |
template<typename T> |
| 198 | 198 |
inline std::istream& operator>>(std::istream &is, Point<T> &z) {
|
| 199 | 199 |
char c; |
| 200 | 200 |
if (is >> c) {
|
| 201 | 201 |
if (c != '(') is.putback(c);
|
| 202 | 202 |
} else {
|
| 203 | 203 |
is.clear(); |
| 204 | 204 |
} |
| 205 | 205 |
if (!(is >> z.x)) return is; |
| 206 | 206 |
if (is >> c) {
|
| 207 | 207 |
if (c != ',') is.putback(c); |
| 208 | 208 |
} else {
|
| 209 | 209 |
is.clear(); |
| 210 | 210 |
} |
| 211 | 211 |
if (!(is >> z.y)) return is; |
| 212 | 212 |
if (is >> c) {
|
| 213 | 213 |
if (c != ')') is.putback(c); |
| 214 | 214 |
} else {
|
| 215 | 215 |
is.clear(); |
| 216 | 216 |
} |
| 217 | 217 |
return is; |
| 218 | 218 |
} |
| 219 | 219 |
|
| 220 | 220 |
///Write a plainvector to a stream |
| 221 | 221 |
|
| 222 | 222 |
///Write a plainvector to a stream. |
| 223 | 223 |
///\relates Point |
| 224 | 224 |
/// |
| 225 | 225 |
template<typename T> |
| 226 | 226 |
inline std::ostream& operator<<(std::ostream &os, const Point<T>& z) |
| 227 | 227 |
{
|
| 228 | 228 |
os << "(" << z.x << ", " << z.y << ")";
|
| 229 | 229 |
return os; |
| 230 | 230 |
} |
| 231 | 231 |
|
| 232 | 232 |
///Rotate by 90 degrees |
| 233 | 233 |
|
| 234 | 234 |
///Returns the parameter rotated by 90 degrees in positive direction. |
| 235 | 235 |
///\relates Point |
| 236 | 236 |
/// |
| 237 | 237 |
template<typename T> |
| 238 | 238 |
inline Point<T> rot90(const Point<T> &z) |
| 239 | 239 |
{
|
| 240 | 240 |
return Point<T>(-z.y,z.x); |
| 241 | 241 |
} |
| 242 | 242 |
|
| 243 | 243 |
///Rotate by 180 degrees |
| 244 | 244 |
|
| 245 | 245 |
///Returns the parameter rotated by 180 degrees. |
| 246 | 246 |
///\relates Point |
| 247 | 247 |
/// |
| 248 | 248 |
template<typename T> |
| 249 | 249 |
inline Point<T> rot180(const Point<T> &z) |
| 250 | 250 |
{
|
| 251 | 251 |
return Point<T>(-z.x,-z.y); |
| 252 | 252 |
} |
| 253 | 253 |
|
| 254 | 254 |
///Rotate by 270 degrees |
| 255 | 255 |
|
| 256 | 256 |
///Returns the parameter rotated by 90 degrees in negative direction. |
| 257 | 257 |
///\relates Point |
| 258 | 258 |
/// |
| 259 | 259 |
template<typename T> |
| 260 | 260 |
inline Point<T> rot270(const Point<T> &z) |
| 261 | 261 |
{
|
| 262 | 262 |
return Point<T>(z.y,-z.x); |
| 263 | 263 |
} |
| 264 | 264 |
|
| 265 | 265 |
|
| 266 | 266 |
|
| 267 | 267 |
/// A class to calculate or store the bounding box of plainvectors. |
| 268 | 268 |
|
| 269 | 269 |
/// A class to calculate or store the bounding box of plainvectors. |
| 270 | 270 |
/// |
| 271 | 271 |
template<typename T> |
| 272 | 272 |
class BoundingBox {
|
| 273 | 273 |
Point<T> bottom_left, top_right; |
| 274 | 274 |
bool _empty; |
| 275 | 275 |
public: |
| 276 | 276 |
|
| 277 | 277 |
///Default constructor: creates an empty bounding box |
| 278 | 278 |
BoundingBox() { _empty = true; }
|
| 279 | 279 |
|
| 280 | 280 |
///Construct an instance from one point |
| 281 | 281 |
BoundingBox(Point<T> a) { bottom_left=top_right=a; _empty = false; }
|
| 282 | 282 |
|
| 283 | 283 |
///Construct an instance from two points |
| 284 | 284 |
|
| 285 | 285 |
///Construct an instance from two points. |
| 286 | 286 |
///\param a The bottom left corner. |
| 287 | 287 |
///\param b The top right corner. |
| 288 | 288 |
///\warning The coordinates of the bottom left corner must be no more |
| 289 | 289 |
///than those of the top right one. |
| 290 | 290 |
BoundingBox(Point<T> a,Point<T> b) |
| 291 | 291 |
{
|
| 292 | 292 |
bottom_left=a; |
| 293 | 293 |
top_right=b; |
| 294 | 294 |
_empty = false; |
| 295 | 295 |
} |
| 296 | 296 |
|
| 297 | 297 |
///Construct an instance from four numbers |
| 298 | 298 |
|
| 299 | 299 |
///Construct an instance from four numbers. |
| 300 | 300 |
///\param l The left side of the box. |
| 301 | 301 |
///\param b The bottom of the box. |
| 302 | 302 |
///\param r The right side of the box. |
| 303 | 303 |
///\param t The top of the box. |
| 304 | 304 |
///\warning The left side must be no more than the right side and |
| 305 | 305 |
///bottom must be no more than the top. |
| 306 | 306 |
BoundingBox(T l,T b,T r,T t) |
| 307 | 307 |
{
|
| 308 | 308 |
bottom_left=Point<T>(l,b); |
| 309 | 309 |
top_right=Point<T>(r,t); |
| 310 | 310 |
_empty = false; |
| 311 | 311 |
} |
| 312 | 312 |
|
| 313 | 313 |
///Return \c true if the bounding box is empty. |
| 314 | 314 |
|
| 315 | 315 |
///Return \c true if the bounding box is empty (i.e. return \c false |
| 316 | 316 |
///if at least one point was added to the box or the coordinates of |
| 317 | 317 |
///the box were set). |
| 318 | 318 |
///The coordinates of an empty bounding box are not defined. |
| 319 | 319 |
bool empty() const {
|
| 320 | 320 |
return _empty; |
| 321 | 321 |
} |
| 322 | 322 |
|
| 323 | 323 |
///Make the BoundingBox empty |
| 324 | 324 |
void clear() {
|
| 325 | 325 |
_empty=1; |
| 326 | 326 |
} |
| 327 | 327 |
|
| 328 | 328 |
///Give back the bottom left corner |
| 329 | 329 |
|
| 330 | 330 |
///Give back the bottom left corner. |
| 331 | 331 |
///If the bounding box is empty, then the return value is not defined. |
| 332 | 332 |
Point<T> bottomLeft() const {
|
| 333 | 333 |
return bottom_left; |
| 334 | 334 |
} |
| 335 | 335 |
|
| 336 | 336 |
///Set the bottom left corner |
| 337 | 337 |
|
| 338 | 338 |
///Set the bottom left corner. |
| 339 | 339 |
///It should only be used for non-empty box. |
| 340 | 340 |
void bottomLeft(Point<T> p) {
|
| 341 | 341 |
bottom_left = p; |
| 342 | 342 |
} |
| 343 | 343 |
|
| 344 | 344 |
///Give back the top right corner |
| 345 | 345 |
|
| 346 | 346 |
///Give back the top right corner. |
| 347 | 347 |
///If the bounding box is empty, then the return value is not defined. |
| 348 | 348 |
Point<T> topRight() const {
|
| 349 | 349 |
return top_right; |
| 350 | 350 |
} |
| 351 | 351 |
|
| 352 | 352 |
///Set the top right corner |
| 353 | 353 |
|
| 354 | 354 |
///Set the top right corner. |
| 355 | 355 |
///It should only be used for non-empty box. |
| 356 | 356 |
void topRight(Point<T> p) {
|
| 357 | 357 |
top_right = p; |
| 358 | 358 |
} |
| 359 | 359 |
|
| 360 | 360 |
///Give back the bottom right corner |
| 361 | 361 |
|
| 362 | 362 |
///Give back the bottom right corner. |
| 363 | 363 |
///If the bounding box is empty, then the return value is not defined. |
| 364 | 364 |
Point<T> bottomRight() const {
|
| 365 | 365 |
return Point<T>(top_right.x,bottom_left.y); |
| 366 | 366 |
} |
| 367 | 367 |
|
| 368 | 368 |
///Set the bottom right corner |
| 369 | 369 |
|
| 370 | 370 |
///Set the bottom right corner. |
| 371 | 371 |
///It should only be used for non-empty box. |
| 372 | 372 |
void bottomRight(Point<T> p) {
|
| 373 | 373 |
top_right.x = p.x; |
| 374 | 374 |
bottom_left.y = p.y; |
| 375 | 375 |
} |
| 376 | 376 |
|
| 377 | 377 |
///Give back the top left corner |
| 378 | 378 |
|
| 379 | 379 |
///Give back the top left corner. |
| 380 | 380 |
///If the bounding box is empty, then the return value is not defined. |
| 381 | 381 |
Point<T> topLeft() const {
|
| 382 | 382 |
return Point<T>(bottom_left.x,top_right.y); |
| 383 | 383 |
} |
| 384 | 384 |
|
| 385 | 385 |
///Set the top left corner |
| 386 | 386 |
|
| 387 | 387 |
///Set the top left corner. |
| 388 | 388 |
///It should only be used for non-empty box. |
| 389 | 389 |
void topLeft(Point<T> p) {
|
| 390 | 390 |
top_right.y = p.y; |
| 391 | 391 |
bottom_left.x = p.x; |
| 392 | 392 |
} |
| 393 | 393 |
|
| 394 | 394 |
///Give back the bottom of the box |
| 395 | 395 |
|
| 396 | 396 |
///Give back the bottom of the box. |
| 397 | 397 |
///If the bounding box is empty, then the return value is not defined. |
| 398 | 398 |
T bottom() const {
|
| 399 | 399 |
return bottom_left.y; |
| 400 | 400 |
} |
| 401 | 401 |
|
| 402 | 402 |
///Set the bottom of the box |
| 403 | 403 |
|
| 404 | 404 |
///Set the bottom of the box. |
| 405 | 405 |
///It should only be used for non-empty box. |
| 406 | 406 |
void bottom(T t) {
|
| 407 | 407 |
bottom_left.y = t; |
| 408 | 408 |
} |
| 409 | 409 |
|
| 410 | 410 |
///Give back the top of the box |
| 411 | 411 |
|
| 412 | 412 |
///Give back the top of the box. |
| 413 | 413 |
///If the bounding box is empty, then the return value is not defined. |
| 414 | 414 |
T top() const {
|
| 415 | 415 |
return top_right.y; |
| 416 | 416 |
} |
| 417 | 417 |
|
| 418 | 418 |
///Set the top of the box |
| 419 | 419 |
|
| 420 | 420 |
///Set the top of the box. |
| 421 | 421 |
///It should only be used for non-empty box. |
| 422 | 422 |
void top(T t) {
|
| 423 | 423 |
top_right.y = t; |
| 424 | 424 |
} |
| 425 | 425 |
|
| 426 | 426 |
///Give back the left side of the box |
| 427 | 427 |
|
| 428 | 428 |
///Give back the left side of the box. |
| 429 | 429 |
///If the bounding box is empty, then the return value is not defined. |
| 430 | 430 |
T left() const {
|
| 431 | 431 |
return bottom_left.x; |
| 432 | 432 |
} |
| 433 | 433 |
|
| 434 | 434 |
///Set the left side of the box |
| 435 | 435 |
|
| 436 | 436 |
///Set the left side of the box. |
| 437 | 437 |
///It should only be used for non-empty box. |
| 438 | 438 |
void left(T t) {
|
| 439 | 439 |
bottom_left.x = t; |
| 440 | 440 |
} |
| 441 | 441 |
|
| 442 | 442 |
/// Give back the right side of the box |
| 443 | 443 |
|
| 444 | 444 |
/// Give back the right side of the box. |
| 445 | 445 |
///If the bounding box is empty, then the return value is not defined. |
| 446 | 446 |
T right() const {
|
| 447 | 447 |
return top_right.x; |
| 448 | 448 |
} |
| 449 | 449 |
|
| 450 | 450 |
///Set the right side of the box |
| 451 | 451 |
|
| 452 | 452 |
///Set the right side of the box. |
| 453 | 453 |
///It should only be used for non-empty box. |
| 454 | 454 |
void right(T t) {
|
| 455 | 455 |
top_right.x = t; |
| 456 | 456 |
} |
| 457 | 457 |
|
| 458 | 458 |
///Give back the height of the box |
| 459 | 459 |
|
| 460 | 460 |
///Give back the height of the box. |
| 461 | 461 |
///If the bounding box is empty, then the return value is not defined. |
| 462 | 462 |
T height() const {
|
| 463 | 463 |
return top_right.y-bottom_left.y; |
| 464 | 464 |
} |
| 465 | 465 |
|
| 466 | 466 |
///Give back the width of the box |
| 467 | 467 |
|
| 468 | 468 |
///Give back the width of the box. |
| 469 | 469 |
///If the bounding box is empty, then the return value is not defined. |
| 470 | 470 |
T width() const {
|
| 471 | 471 |
return top_right.x-bottom_left.x; |
| 472 | 472 |
} |
| 473 | 473 |
|
| 474 | 474 |
///Checks whether a point is inside a bounding box |
| 475 | 475 |
bool inside(const Point<T>& u) const {
|
| 476 | 476 |
if (_empty) |
| 477 | 477 |
return false; |
| 478 | 478 |
else{
|
| 479 | 479 |
return ((u.x-bottom_left.x)*(top_right.x-u.x) >= 0 && |
| 480 | 480 |
(u.y-bottom_left.y)*(top_right.y-u.y) >= 0 ); |
| 481 | 481 |
} |
| 482 | 482 |
} |
| 483 | 483 |
|
| 484 | 484 |
///Increments a bounding box with a point |
| 485 | 485 |
|
| 486 | 486 |
///Increments a bounding box with a point. |
| 487 | 487 |
/// |
| 488 | 488 |
BoundingBox& add(const Point<T>& u){
|
| 489 | 489 |
if (_empty){
|
| 490 | 490 |
bottom_left=top_right=u; |
| 491 | 491 |
_empty = false; |
| 492 | 492 |
} |
| 493 | 493 |
else{
|
| 494 | 494 |
if (bottom_left.x > u.x) bottom_left.x = u.x; |
| 495 | 495 |
if (bottom_left.y > u.y) bottom_left.y = u.y; |
| 496 | 496 |
if (top_right.x < u.x) top_right.x = u.x; |
| 497 | 497 |
if (top_right.y < u.y) top_right.y = u.y; |
| 498 | 498 |
} |
| 499 | 499 |
return *this; |
| 500 | 500 |
} |
| 501 | 501 |
|
| 502 | 502 |
///Increments a bounding box to contain another bounding box |
| 503 | 503 |
|
| 504 | 504 |
///Increments a bounding box to contain another bounding box. |
| 505 | 505 |
/// |
| 506 | 506 |
BoundingBox& add(const BoundingBox &u){
|
| 507 | 507 |
if ( !u.empty() ){
|
| 508 | 508 |
this->add(u.bottomLeft()); |
| 509 | 509 |
this->add(u.topRight()); |
| 510 | 510 |
} |
| 511 | 511 |
return *this; |
| 512 | 512 |
} |
| 513 | 513 |
|
| 514 | 514 |
///Intersection of two bounding boxes |
| 515 | 515 |
|
| 516 | 516 |
///Intersection of two bounding boxes. |
| 517 | 517 |
/// |
| 518 | 518 |
BoundingBox operator&(const BoundingBox& u) const {
|
| 519 | 519 |
BoundingBox b; |
| 520 | 520 |
if (this->_empty || u._empty) {
|
| 521 | 521 |
b._empty = true; |
| 522 | 522 |
} else {
|
| 523 | 523 |
b.bottom_left.x = std::max(this->bottom_left.x,u.bottom_left.x); |
| 524 | 524 |
b.bottom_left.y = std::max(this->bottom_left.y,u.bottom_left.y); |
| 525 | 525 |
b.top_right.x = std::min(this->top_right.x,u.top_right.x); |
| 526 | 526 |
b.top_right.y = std::min(this->top_right.y,u.top_right.y); |
| 527 | 527 |
b._empty = b.bottom_left.x > b.top_right.x || |
| 528 | 528 |
b.bottom_left.y > b.top_right.y; |
| 529 | 529 |
} |
| 530 | 530 |
return b; |
| 531 | 531 |
} |
| 532 | 532 |
|
| 533 | 533 |
};//class Boundingbox |
| 534 | 534 |
|
| 535 | 535 |
|
| 536 |
///Map of x-coordinates of a |
|
| 536 |
///Map of x-coordinates of a Point map |
|
| 537 | 537 |
|
| 538 | 538 |
///\ingroup maps |
| 539 |
///Map of x-coordinates of a \ref Point "Point"-map. |
|
| 539 |
///Map of x-coordinates of a \ref dim2::Point "Point"-map. |
|
| 540 | 540 |
/// |
| 541 | 541 |
template<class M> |
| 542 | 542 |
class XMap |
| 543 | 543 |
{
|
| 544 | 544 |
M& _map; |
| 545 | 545 |
public: |
| 546 | 546 |
|
| 547 | 547 |
typedef typename M::Value::Value Value; |
| 548 | 548 |
typedef typename M::Key Key; |
| 549 | 549 |
///\e |
| 550 | 550 |
XMap(M& map) : _map(map) {}
|
| 551 | 551 |
Value operator[](Key k) const {return _map[k].x;}
|
| 552 | 552 |
void set(Key k,Value v) {_map.set(k,typename M::Value(v,_map[k].y));}
|
| 553 | 553 |
}; |
| 554 | 554 |
|
| 555 | 555 |
///Returns an \ref XMap class |
| 556 | 556 |
|
| 557 | 557 |
///This function just returns an \ref XMap class. |
| 558 | 558 |
/// |
| 559 | 559 |
///\ingroup maps |
| 560 | 560 |
///\relates XMap |
| 561 | 561 |
template<class M> |
| 562 | 562 |
inline XMap<M> xMap(M &m) |
| 563 | 563 |
{
|
| 564 | 564 |
return XMap<M>(m); |
| 565 | 565 |
} |
| 566 | 566 |
|
| 567 | 567 |
template<class M> |
| 568 | 568 |
inline XMap<M> xMap(const M &m) |
| 569 | 569 |
{
|
| 570 | 570 |
return XMap<M>(m); |
| 571 | 571 |
} |
| 572 | 572 |
|
| 573 |
///Constant (read only) version of |
|
| 573 |
///Constant (read only) version of XMap |
|
| 574 | 574 |
|
| 575 | 575 |
///\ingroup maps |
| 576 | 576 |
///Constant (read only) version of \ref XMap |
| 577 | 577 |
/// |
| 578 | 578 |
template<class M> |
| 579 | 579 |
class ConstXMap |
| 580 | 580 |
{
|
| 581 | 581 |
const M& _map; |
| 582 | 582 |
public: |
| 583 | 583 |
|
| 584 | 584 |
typedef typename M::Value::Value Value; |
| 585 | 585 |
typedef typename M::Key Key; |
| 586 | 586 |
///\e |
| 587 | 587 |
ConstXMap(const M &map) : _map(map) {}
|
| 588 | 588 |
Value operator[](Key k) const {return _map[k].x;}
|
| 589 | 589 |
}; |
| 590 | 590 |
|
| 591 | 591 |
///Returns a \ref ConstXMap class |
| 592 | 592 |
|
| 593 | 593 |
///This function just returns a \ref ConstXMap class. |
| 594 | 594 |
/// |
| 595 | 595 |
///\ingroup maps |
| 596 | 596 |
///\relates ConstXMap |
| 597 | 597 |
template<class M> |
| 598 | 598 |
inline ConstXMap<M> xMap(const M &m) |
| 599 | 599 |
{
|
| 600 | 600 |
return ConstXMap<M>(m); |
| 601 | 601 |
} |
| 602 | 602 |
|
| 603 |
///Map of y-coordinates of a |
|
| 603 |
///Map of y-coordinates of a Point map |
|
| 604 | 604 |
|
| 605 | 605 |
///\ingroup maps |
| 606 | 606 |
///Map of y-coordinates of a \ref Point "Point"-map. |
| 607 | 607 |
/// |
| 608 | 608 |
template<class M> |
| 609 | 609 |
class YMap |
| 610 | 610 |
{
|
| 611 | 611 |
M& _map; |
| 612 | 612 |
public: |
| 613 | 613 |
|
| 614 | 614 |
typedef typename M::Value::Value Value; |
| 615 | 615 |
typedef typename M::Key Key; |
| 616 | 616 |
///\e |
| 617 | 617 |
YMap(M& map) : _map(map) {}
|
| 618 | 618 |
Value operator[](Key k) const {return _map[k].y;}
|
| 619 | 619 |
void set(Key k,Value v) {_map.set(k,typename M::Value(_map[k].x,v));}
|
| 620 | 620 |
}; |
| 621 | 621 |
|
| 622 | 622 |
///Returns a \ref YMap class |
| 623 | 623 |
|
| 624 | 624 |
///This function just returns a \ref YMap class. |
| 625 | 625 |
/// |
| 626 | 626 |
///\ingroup maps |
| 627 | 627 |
///\relates YMap |
| 628 | 628 |
template<class M> |
| 629 | 629 |
inline YMap<M> yMap(M &m) |
| 630 | 630 |
{
|
| 631 | 631 |
return YMap<M>(m); |
| 632 | 632 |
} |
| 633 | 633 |
|
| 634 | 634 |
template<class M> |
| 635 | 635 |
inline YMap<M> yMap(const M &m) |
| 636 | 636 |
{
|
| 637 | 637 |
return YMap<M>(m); |
| 638 | 638 |
} |
| 639 | 639 |
|
| 640 |
///Constant (read only) version of |
|
| 640 |
///Constant (read only) version of YMap |
|
| 641 | 641 |
|
| 642 | 642 |
///\ingroup maps |
| 643 | 643 |
///Constant (read only) version of \ref YMap |
| 644 | 644 |
/// |
| 645 | 645 |
template<class M> |
| 646 | 646 |
class ConstYMap |
| 647 | 647 |
{
|
| 648 | 648 |
const M& _map; |
| 649 | 649 |
public: |
| 650 | 650 |
|
| 651 | 651 |
typedef typename M::Value::Value Value; |
| 652 | 652 |
typedef typename M::Key Key; |
| 653 | 653 |
///\e |
| 654 | 654 |
ConstYMap(const M &map) : _map(map) {}
|
| 655 | 655 |
Value operator[](Key k) const {return _map[k].y;}
|
| 656 | 656 |
}; |
| 657 | 657 |
|
| 658 | 658 |
///Returns a \ref ConstYMap class |
| 659 | 659 |
|
| 660 | 660 |
///This function just returns a \ref ConstYMap class. |
| 661 | 661 |
/// |
| 662 | 662 |
///\ingroup maps |
| 663 | 663 |
///\relates ConstYMap |
| 664 | 664 |
template<class M> |
| 665 | 665 |
inline ConstYMap<M> yMap(const M &m) |
| 666 | 666 |
{
|
| 667 | 667 |
return ConstYMap<M>(m); |
| 668 | 668 |
} |
| 669 | 669 |
|
| 670 | 670 |
|
| 671 |
///\brief Map of the \ref Point::normSquare() "normSquare()" |
|
| 672 |
///of a \ref Point "Point"-map |
|
| 671 |
///\brief Map of the normSquare() |
|
| 672 |
///of a Point map |
|
| 673 | 673 |
/// |
| 674 | 674 |
///Map of the \ref Point::normSquare() "normSquare()" |
| 675 | 675 |
///of a \ref Point "Point"-map. |
| 676 | 676 |
///\ingroup maps |
| 677 | 677 |
/// |
| 678 | 678 |
template<class M> |
| 679 | 679 |
class NormSquareMap |
| 680 | 680 |
{
|
| 681 | 681 |
const M& _map; |
| 682 | 682 |
public: |
| 683 | 683 |
|
| 684 | 684 |
typedef typename M::Value::Value Value; |
| 685 | 685 |
typedef typename M::Key Key; |
| 686 | 686 |
///\e |
| 687 | 687 |
NormSquareMap(const M &map) : _map(map) {}
|
| 688 | 688 |
Value operator[](Key k) const {return _map[k].normSquare();}
|
| 689 | 689 |
}; |
| 690 | 690 |
|
| 691 | 691 |
///Returns a \ref NormSquareMap class |
| 692 | 692 |
|
| 693 | 693 |
///This function just returns a \ref NormSquareMap class. |
| 694 | 694 |
/// |
| 695 | 695 |
///\ingroup maps |
| 696 | 696 |
///\relates NormSquareMap |
| 697 | 697 |
template<class M> |
| 698 | 698 |
inline NormSquareMap<M> normSquareMap(const M &m) |
| 699 | 699 |
{
|
| 700 | 700 |
return NormSquareMap<M>(m); |
| 701 | 701 |
} |
| 702 | 702 |
|
| 703 | 703 |
/// @} |
| 704 | 704 |
|
| 705 | 705 |
} //namespce dim2 |
| 706 | 706 |
|
| 707 | 707 |
} //namespace lemon |
| 708 | 708 |
|
| 709 | 709 |
#endif //LEMON_DIM2_H |
| 1 | 1 |
/* -*- C++ -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library |
| 4 | 4 |
* |
| 5 | 5 |
* Copyright (C) 2003-2008 |
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_MAPS_H |
| 20 | 20 |
#define LEMON_MAPS_H |
| 21 | 21 |
|
| 22 | 22 |
#include <iterator> |
| 23 | 23 |
#include <functional> |
| 24 | 24 |
#include <vector> |
| 25 | 25 |
|
| 26 | 26 |
#include <lemon/bits/utility.h> |
| 27 | 27 |
// #include <lemon/bits/traits.h> |
| 28 | 28 |
|
| 29 | 29 |
///\file |
| 30 | 30 |
///\ingroup maps |
| 31 | 31 |
///\brief Miscellaneous property maps |
| 32 | 32 |
/// |
| 33 | 33 |
#include <map> |
| 34 | 34 |
|
| 35 | 35 |
namespace lemon {
|
| 36 | 36 |
|
| 37 | 37 |
/// \addtogroup maps |
| 38 | 38 |
/// @{
|
| 39 | 39 |
|
| 40 | 40 |
/// Base class of maps. |
| 41 | 41 |
|
| 42 | 42 |
/// Base class of maps. |
| 43 | 43 |
/// It provides the necessary <tt>typedef</tt>s required by the map concept. |
| 44 | 44 |
template<typename K, typename T> |
| 45 | 45 |
class MapBase {
|
| 46 | 46 |
public: |
| 47 | 47 |
/// The key type of the map. |
| 48 | 48 |
typedef K Key; |
| 49 | 49 |
/// The value type of the map. (The type of objects associated with the keys). |
| 50 | 50 |
typedef T Value; |
| 51 | 51 |
}; |
| 52 | 52 |
|
| 53 | 53 |
/// Null map. (a.k.a. DoNothingMap) |
| 54 | 54 |
|
| 55 | 55 |
/// This map can be used if you have to provide a map only for |
| 56 | 56 |
/// its type definitions, or if you have to provide a writable map, |
| 57 | 57 |
/// but data written to it is not required (i.e. it will be sent to |
| 58 | 58 |
/// <tt>/dev/null</tt>). |
| 59 | 59 |
template<typename K, typename T> |
| 60 | 60 |
class NullMap : public MapBase<K, T> {
|
| 61 | 61 |
public: |
| 62 | 62 |
typedef MapBase<K, T> Parent; |
| 63 | 63 |
typedef typename Parent::Key Key; |
| 64 | 64 |
typedef typename Parent::Value Value; |
| 65 | 65 |
|
| 66 | 66 |
/// Gives back a default constructed element. |
| 67 | 67 |
T operator[](const K&) const { return T(); }
|
| 68 | 68 |
/// Absorbs the value. |
| 69 | 69 |
void set(const K&, const T&) {}
|
| 70 | 70 |
}; |
| 71 | 71 |
|
| 72 | 72 |
///Returns a \c NullMap class |
| 73 | 73 |
|
| 74 | 74 |
///This function just returns a \c NullMap class. |
| 75 | 75 |
///\relates NullMap |
| 76 | 76 |
template <typename K, typename V> |
| 77 | 77 |
NullMap<K, V> nullMap() {
|
| 78 | 78 |
return NullMap<K, V>(); |
| 79 | 79 |
} |
| 80 | 80 |
|
| 81 | 81 |
|
| 82 | 82 |
/// Constant map. |
| 83 | 83 |
|
| 84 | 84 |
/// This is a readable map which assigns a specified value to each key. |
| 85 | 85 |
/// In other aspects it is equivalent to the \c NullMap. |
| 86 | 86 |
template<typename K, typename T> |
| 87 | 87 |
class ConstMap : public MapBase<K, T> {
|
| 88 | 88 |
private: |
| 89 | 89 |
T v; |
| 90 | 90 |
public: |
| 91 | 91 |
|
| 92 | 92 |
typedef MapBase<K, T> Parent; |
| 93 | 93 |
typedef typename Parent::Key Key; |
| 94 | 94 |
typedef typename Parent::Value Value; |
| 95 | 95 |
|
| 96 | 96 |
/// Default constructor |
| 97 | 97 |
|
| 98 | 98 |
/// Default constructor. |
| 99 | 99 |
/// The value of the map will be uninitialized. |
| 100 | 100 |
/// (More exactly it will be default constructed.) |
| 101 | 101 |
ConstMap() {}
|
| 102 | 102 |
|
| 103 | 103 |
/// Constructor with specified initial value |
| 104 | 104 |
|
| 105 | 105 |
/// Constructor with specified initial value. |
| 106 | 106 |
/// \param _v is the initial value of the map. |
| 107 | 107 |
ConstMap(const T &_v) : v(_v) {}
|
| 108 | 108 |
|
| 109 | 109 |
///\e |
| 110 | 110 |
T operator[](const K&) const { return v; }
|
| 111 | 111 |
|
| 112 | 112 |
///\e |
| 113 | 113 |
void setAll(const T &t) {
|
| 114 | 114 |
v = t; |
| 115 | 115 |
} |
| 116 | 116 |
|
| 117 | 117 |
template<typename T1> |
| 118 | 118 |
struct rebind {
|
| 119 | 119 |
typedef ConstMap<K, T1> other; |
| 120 | 120 |
}; |
| 121 | 121 |
|
| 122 | 122 |
template<typename T1> |
| 123 | 123 |
ConstMap(const ConstMap<K, T1> &, const T &_v) : v(_v) {}
|
| 124 | 124 |
}; |
| 125 | 125 |
|
| 126 | 126 |
///Returns a \c ConstMap class |
| 127 | 127 |
|
| 128 | 128 |
///This function just returns a \c ConstMap class. |
| 129 | 129 |
///\relates ConstMap |
| 130 | 130 |
template<typename K, typename V> |
| 131 | 131 |
inline ConstMap<K, V> constMap(const V &v) {
|
| 132 | 132 |
return ConstMap<K, V>(v); |
| 133 | 133 |
} |
| 134 | 134 |
|
| 135 | 135 |
|
| 136 | 136 |
template<typename T, T v> |
| 137 | 137 |
struct Const { };
|
| 138 | 138 |
|
| 139 | 139 |
/// Constant map with inlined constant value. |
| 140 | 140 |
|
| 141 | 141 |
/// This is a readable map which assigns a specified value to each key. |
| 142 | 142 |
/// In other aspects it is equivalent to the \c NullMap. |
| 143 | 143 |
template<typename K, typename V, V v> |
| 144 | 144 |
class ConstMap<K, Const<V, v> > : public MapBase<K, V> {
|
| 145 | 145 |
public: |
| 146 | 146 |
typedef MapBase<K, V> Parent; |
| 147 | 147 |
typedef typename Parent::Key Key; |
| 148 | 148 |
typedef typename Parent::Value Value; |
| 149 | 149 |
|
| 150 | 150 |
ConstMap() { }
|
| 151 | 151 |
///\e |
| 152 | 152 |
V operator[](const K&) const { return v; }
|
| 153 | 153 |
///\e |
| 154 | 154 |
void set(const K&, const V&) { }
|
| 155 | 155 |
}; |
| 156 | 156 |
|
| 157 | 157 |
///Returns a \c ConstMap class |
| 158 | 158 |
|
| 159 | 159 |
///This function just returns a \c ConstMap class with inlined value. |
| 160 | 160 |
///\relates ConstMap |
| 161 | 161 |
template<typename K, typename V, V v> |
| 162 | 162 |
inline ConstMap<K, Const<V, v> > constMap() {
|
| 163 | 163 |
return ConstMap<K, Const<V, v> >(); |
| 164 | 164 |
} |
| 165 | 165 |
|
| 166 | 166 |
///Map based on std::map |
| 167 | 167 |
|
| 168 | 168 |
///This is essentially a wrapper for \c std::map with addition that |
| 169 | 169 |
///you can specify a default value different from \c Value(). |
| 170 | 170 |
template <typename K, typename T, typename Compare = std::less<K> > |
| 171 | 171 |
class StdMap {
|
| 172 | 172 |
template <typename K1, typename T1, typename C1> |
| 173 | 173 |
friend class StdMap; |
| 174 | 174 |
public: |
| 175 | 175 |
|
| 176 | 176 |
typedef True ReferenceMapTag; |
| 177 |
/// |
|
| 177 |
///Key type |
|
| 178 | 178 |
typedef K Key; |
| 179 |
/// |
|
| 179 |
///Value type |
|
| 180 | 180 |
typedef T Value; |
| 181 |
/// |
|
| 181 |
///Reference Type |
|
| 182 | 182 |
typedef T& Reference; |
| 183 |
/// |
|
| 183 |
///Const reference type |
|
| 184 | 184 |
typedef const T& ConstReference; |
| 185 | 185 |
|
| 186 | 186 |
private: |
| 187 | 187 |
|
| 188 | 188 |
typedef std::map<K, T, Compare> Map; |
| 189 | 189 |
Value _value; |
| 190 | 190 |
Map _map; |
| 191 | 191 |
|
| 192 | 192 |
public: |
| 193 | 193 |
|
| 194 | 194 |
/// Constructor with specified default value |
| 195 | 195 |
StdMap(const T& value = T()) : _value(value) {}
|
| 196 | 196 |
/// \brief Constructs the map from an appropriate std::map, and explicitly |
| 197 | 197 |
/// specifies a default value. |
| 198 | 198 |
template <typename T1, typename Comp1> |
| 199 | 199 |
StdMap(const std::map<Key, T1, Comp1> &map, const T& value = T()) |
| 200 | 200 |
: _map(map.begin(), map.end()), _value(value) {}
|
| 201 | 201 |
|
| 202 | 202 |
/// \brief Constructs a map from an other StdMap. |
| 203 | 203 |
template<typename T1, typename Comp1> |
| 204 | 204 |
StdMap(const StdMap<Key, T1, Comp1> &c) |
| 205 | 205 |
: _map(c._map.begin(), c._map.end()), _value(c._value) {}
|
| 206 | 206 |
|
| 207 | 207 |
private: |
| 208 | 208 |
|
| 209 | 209 |
StdMap& operator=(const StdMap&); |
| 210 | 210 |
|
| 211 | 211 |
public: |
| 212 | 212 |
|
| 213 | 213 |
///\e |
| 214 | 214 |
Reference operator[](const Key &k) {
|
| 215 | 215 |
typename Map::iterator it = _map.lower_bound(k); |
| 216 | 216 |
if (it != _map.end() && !_map.key_comp()(k, it->first)) |
| 217 | 217 |
return it->second; |
| 218 | 218 |
else |
| 219 | 219 |
return _map.insert(it, std::make_pair(k, _value))->second; |
| 220 | 220 |
} |
| 221 | 221 |
|
| 222 | 222 |
/// \e |
| 223 | 223 |
ConstReference operator[](const Key &k) const {
|
| 224 | 224 |
typename Map::const_iterator it = _map.find(k); |
| 225 | 225 |
if (it != _map.end()) |
| 226 | 226 |
return it->second; |
| 227 | 227 |
else |
| 228 | 228 |
return _value; |
| 229 | 229 |
} |
| 230 | 230 |
|
| 231 | 231 |
/// \e |
| 232 | 232 |
void set(const Key &k, const T &t) {
|
| 233 | 233 |
typename Map::iterator it = _map.lower_bound(k); |
| 234 | 234 |
if (it != _map.end() && !_map.key_comp()(k, it->first)) |
| 235 | 235 |
it->second = t; |
| 236 | 236 |
else |
| 237 | 237 |
_map.insert(it, std::make_pair(k, t)); |
| 238 | 238 |
} |
| 239 | 239 |
|
| 240 | 240 |
/// \e |
| 241 | 241 |
void setAll(const T &t) {
|
| 242 | 242 |
_value = t; |
| 243 | 243 |
_map.clear(); |
| 244 | 244 |
} |
| 245 | 245 |
|
| 246 | 246 |
template <typename T1, typename C1 = std::less<T1> > |
| 247 | 247 |
struct rebind {
|
| 248 | 248 |
typedef StdMap<Key, T1, C1> other; |
| 249 | 249 |
}; |
| 250 | 250 |
}; |
| 251 | 251 |
|
| 252 | 252 |
/// \brief Map for storing values for keys from the range <tt>[0..size-1]</tt> |
| 253 | 253 |
/// |
| 254 | 254 |
/// The current map has the <tt>[0..size-1]</tt> keyset and the values |
| 255 | 255 |
/// are stored in a \c std::vector<T> container. It can be used with |
| 256 | 256 |
/// some data structures, for example \c UnionFind, \c BinHeap, when |
| 257 | 257 |
/// the used items are small integer numbers. |
| 258 | 258 |
/// |
| 259 | 259 |
/// \todo Revise its name |
| 260 | 260 |
template <typename T> |
| 261 | 261 |
class IntegerMap {
|
| 262 | 262 |
|
| 263 | 263 |
template <typename T1> |
| 264 | 264 |
friend class IntegerMap; |
| 265 | 265 |
|
| 266 | 266 |
public: |
| 267 | 267 |
|
| 268 | 268 |
typedef True ReferenceMapTag; |
| 269 | 269 |
///\e |
| 270 | 270 |
typedef int Key; |
| 271 | 271 |
///\e |
| 272 | 272 |
typedef T Value; |
| 273 | 273 |
///\e |
| 274 | 274 |
typedef T& Reference; |
| 275 | 275 |
///\e |
| 276 | 276 |
typedef const T& ConstReference; |
| 277 | 277 |
|
| 278 | 278 |
private: |
| 279 | 279 |
|
| 280 | 280 |
typedef std::vector<T> Vector; |
| 281 | 281 |
Vector _vector; |
| 282 | 282 |
|
| 283 | 283 |
public: |
| 284 | 284 |
|
| 285 | 285 |
/// Constructor with specified default value |
| 286 | 286 |
IntegerMap(int size = 0, const T& value = T()) : _vector(size, value) {}
|
| 287 | 287 |
|
| 288 | 288 |
/// \brief Constructs the map from an appropriate std::vector. |
| 289 | 289 |
template <typename T1> |
| 290 | 290 |
IntegerMap(const std::vector<T1>& vector) |
| 291 | 291 |
: _vector(vector.begin(), vector.end()) {}
|
| 292 | 292 |
|
| 293 | 293 |
/// \brief Constructs a map from an other IntegerMap. |
| 294 | 294 |
template <typename T1> |
| 295 | 295 |
IntegerMap(const IntegerMap<T1> &c) |
| 296 | 296 |
: _vector(c._vector.begin(), c._vector.end()) {}
|
| 297 | 297 |
|
| 298 | 298 |
/// \brief Resize the container |
| 299 | 299 |
void resize(int size, const T& value = T()) {
|
| 300 | 300 |
_vector.resize(size, value); |
| 301 | 301 |
} |
| 302 | 302 |
|
| 303 | 303 |
private: |
| 304 | 304 |
|
| 305 | 305 |
IntegerMap& operator=(const IntegerMap&); |
| 306 | 306 |
|
| 307 | 307 |
public: |
| 308 | 308 |
|
| 309 | 309 |
///\e |
| 310 | 310 |
Reference operator[](Key k) {
|
| 311 | 311 |
return _vector[k]; |
| 312 | 312 |
} |
| 313 | 313 |
|
| 314 | 314 |
/// \e |
| 315 | 315 |
ConstReference operator[](Key k) const {
|
| 316 | 316 |
return _vector[k]; |
| 317 | 317 |
} |
| 318 | 318 |
|
| 319 | 319 |
/// \e |
| 320 | 320 |
void set(const Key &k, const T& t) {
|
| 321 | 321 |
_vector[k] = t; |
| 322 | 322 |
} |
| 323 | 323 |
|
| 324 | 324 |
}; |
| 325 | 325 |
|
| 326 | 326 |
/// @} |
| 327 | 327 |
|
| 328 | 328 |
/// \addtogroup map_adaptors |
| 329 | 329 |
/// @{
|
| 330 | 330 |
|
| 331 | 331 |
/// \brief Identity map. |
| 332 | 332 |
/// |
| 333 | 333 |
/// This map gives back the given key as value without any |
| 334 | 334 |
/// modification. |
| 335 | 335 |
template <typename T> |
| 336 | 336 |
class IdentityMap : public MapBase<T, T> {
|
| 337 | 337 |
public: |
| 338 | 338 |
typedef MapBase<T, T> Parent; |
| 339 | 339 |
typedef typename Parent::Key Key; |
| 340 | 340 |
typedef typename Parent::Value Value; |
| 341 | 341 |
|
| 342 | 342 |
/// \e |
| 343 | 343 |
const T& operator[](const T& t) const {
|
| 344 | 344 |
return t; |
| 345 | 345 |
} |
| 346 | 346 |
}; |
| 347 | 347 |
|
| 348 | 348 |
///Returns an \c IdentityMap class |
| 349 | 349 |
|
| 350 | 350 |
///This function just returns an \c IdentityMap class. |
| 351 | 351 |
///\relates IdentityMap |
| 352 | 352 |
template<typename T> |
| 353 | 353 |
inline IdentityMap<T> identityMap() {
|
| 354 | 354 |
return IdentityMap<T>(); |
| 355 | 355 |
} |
| 356 | 356 |
|
| 357 | 357 |
|
| 358 | 358 |
///\brief Convert the \c Value of a map to another type using |
| 359 | 359 |
///the default conversion. |
| 360 | 360 |
/// |
| 361 | 361 |
///This \c concepts::ReadMap "read only map" |
| 362 | 362 |
///converts the \c Value of a map to type \c T. |
| 363 | 363 |
///Its \c Key is inherited from \c M. |
| 364 | 364 |
template <typename M, typename T> |
| 365 | 365 |
class ConvertMap : public MapBase<typename M::Key, T> {
|
| 366 | 366 |
const M& m; |
| 367 | 367 |
public: |
| 368 | 368 |
typedef MapBase<typename M::Key, T> Parent; |
| 369 | 369 |
typedef typename Parent::Key Key; |
| 370 | 370 |
typedef typename Parent::Value Value; |
| 371 | 371 |
|
| 372 | 372 |
///Constructor |
| 373 | 373 |
|
| 374 | 374 |
///Constructor. |
| 375 | 375 |
///\param _m is the underlying map. |
| 376 | 376 |
ConvertMap(const M &_m) : m(_m) {};
|
| 377 | 377 |
|
| 378 | 378 |
/// \brief The subscript operator. |
| 379 | 379 |
/// |
| 380 | 380 |
/// The subscript operator. |
| 381 | 381 |
Value operator[](const Key& k) const {return m[k];}
|
| 382 | 382 |
}; |
| 383 | 383 |
|
| 384 | 384 |
///Returns a \c ConvertMap class |
| 385 | 385 |
|
| 386 | 386 |
///This function just returns a \c ConvertMap class. |
| 387 | 387 |
///\relates ConvertMap |
| 388 | 388 |
template<typename T, typename M> |
| 389 | 389 |
inline ConvertMap<M, T> convertMap(const M &m) {
|
| 390 | 390 |
return ConvertMap<M, T>(m); |
| 391 | 391 |
} |
| 392 | 392 |
|
| 393 | 393 |
///Simple wrapping of a map |
| 394 | 394 |
|
| 395 | 395 |
///This \c concepts::ReadMap "read only map" returns the simple |
| 396 | 396 |
///wrapping of the given map. Sometimes the reference maps cannot be |
| 397 | 397 |
///combined with simple read maps. This map adaptor wraps the given |
| 398 | 398 |
///map to simple read map. |
| 399 | 399 |
/// |
| 400 | 400 |
///\sa SimpleWriteMap |
| 401 | 401 |
/// |
| 402 | 402 |
/// \todo Revise the misleading name |
| 403 | 403 |
template<typename M> |
| 404 | 404 |
class SimpleMap : public MapBase<typename M::Key, typename M::Value> {
|
| 405 | 405 |
const M& m; |
| 406 | 406 |
|
| 407 | 407 |
public: |
| 408 | 408 |
typedef MapBase<typename M::Key, typename M::Value> Parent; |
| 409 | 409 |
typedef typename Parent::Key Key; |
| 410 | 410 |
typedef typename Parent::Value Value; |
| 411 | 411 |
|
| 412 | 412 |
///Constructor |
| 413 | 413 |
SimpleMap(const M &_m) : m(_m) {};
|
| 414 | 414 |
///\e |
| 415 | 415 |
Value operator[](Key k) const {return m[k];}
|
| 416 | 416 |
}; |
| 417 | 417 |
|
| 418 | 418 |
///Simple writable wrapping of the map |
| 419 | 419 |
|
| 420 | 420 |
///This \c concepts::WriteMap "write map" returns the simple |
| 421 | 421 |
///wrapping of the given map. Sometimes the reference maps cannot be |
| 422 | 422 |
///combined with simple read-write maps. This map adaptor wraps the |
| 423 | 423 |
///given map to simple read-write map. |
| 424 | 424 |
/// |
| 425 | 425 |
///\sa SimpleMap |
| 426 | 426 |
/// |
| 427 | 427 |
/// \todo Revise the misleading name |
| 428 | 428 |
template<typename M> |
| 429 | 429 |
class SimpleWriteMap : public MapBase<typename M::Key, typename M::Value> {
|
| 430 | 430 |
M& m; |
| 431 | 431 |
|
| 432 | 432 |
public: |
| 433 | 433 |
typedef MapBase<typename M::Key, typename M::Value> Parent; |
| 434 | 434 |
typedef typename Parent::Key Key; |
| 435 | 435 |
typedef typename Parent::Value Value; |
| 436 | 436 |
|
| 437 | 437 |
///Constructor |
| 438 | 438 |
SimpleWriteMap(M &_m) : m(_m) {};
|
| 439 | 439 |
///\e |
| 440 | 440 |
Value operator[](Key k) const {return m[k];}
|
| 441 | 441 |
///\e |
| 442 | 442 |
void set(Key k, const Value& c) { m.set(k, c); }
|
| 443 | 443 |
}; |
| 444 | 444 |
|
| 445 | 445 |
///Sum of two maps |
| 446 | 446 |
|
| 447 | 447 |
///This \c concepts::ReadMap "read only map" returns the sum of the two |
| 448 | 448 |
///given maps. |
| 449 | 449 |
///Its \c Key and \c Value are inherited from \c M1. |
| 450 | 450 |
///The \c Key and \c Value of M2 must be convertible to those of \c M1. |
| 451 | 451 |
template<typename M1, typename M2> |
| 452 | 452 |
class AddMap : public MapBase<typename M1::Key, typename M1::Value> {
|
| 453 | 453 |
const M1& m1; |
| 454 | 454 |
const M2& m2; |
| 455 | 455 |
|
| 456 | 456 |
public: |
| 457 | 457 |
typedef MapBase<typename M1::Key, typename M1::Value> Parent; |
| 458 | 458 |
typedef typename Parent::Key Key; |
| 459 | 459 |
typedef typename Parent::Value Value; |
| 460 | 460 |
|
| 461 | 461 |
///Constructor |
| 462 | 462 |
AddMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
|
| 463 | 463 |
///\e |
| 464 | 464 |
Value operator[](Key k) const {return m1[k]+m2[k];}
|
| 465 | 465 |
}; |
| 466 | 466 |
|
| 467 | 467 |
///Returns an \c AddMap class |
| 468 | 468 |
|
| 469 | 469 |
///This function just returns an \c AddMap class. |
| 470 | 470 |
///\todo How to call these type of functions? |
| 471 | 471 |
/// |
| 472 | 472 |
///\relates AddMap |
| 473 | 473 |
template<typename M1, typename M2> |
| 474 | 474 |
inline AddMap<M1, M2> addMap(const M1 &m1,const M2 &m2) {
|
| 475 | 475 |
return AddMap<M1, M2>(m1,m2); |
| 476 | 476 |
} |
| 477 | 477 |
|
| 478 | 478 |
///Shift a map with a constant. |
| 479 | 479 |
|
| 480 | 480 |
///This \c concepts::ReadMap "read only map" returns the sum of the |
| 481 | 481 |
///given map and a constant value. |
| 482 | 482 |
///Its \c Key and \c Value are inherited from \c M. |
| 483 | 483 |
/// |
| 484 | 484 |
///Actually, |
| 485 | 485 |
///\code |
| 486 | 486 |
/// ShiftMap<X> sh(x,v); |
| 487 | 487 |
///\endcode |
| 488 | 488 |
///is equivalent to |
| 489 | 489 |
///\code |
| 490 | 490 |
/// ConstMap<X::Key, X::Value> c_tmp(v); |
| 491 | 491 |
/// AddMap<X, ConstMap<X::Key, X::Value> > sh(x,v); |
| 492 | 492 |
///\endcode |
| 493 | 493 |
/// |
| 494 | 494 |
///\sa ShiftWriteMap |
| 495 | 495 |
template<typename M, typename C = typename M::Value> |
| 496 | 496 |
class ShiftMap : public MapBase<typename M::Key, typename M::Value> {
|
| 497 | 497 |
const M& m; |
| 498 | 498 |
C v; |
| 499 | 499 |
public: |
| 500 | 500 |
typedef MapBase<typename M::Key, typename M::Value> Parent; |
| 501 | 501 |
typedef typename Parent::Key Key; |
| 502 | 502 |
typedef typename Parent::Value Value; |
| 503 | 503 |
|
| 504 | 504 |
///Constructor |
| 505 | 505 |
|
| 506 | 506 |
///Constructor. |
| 507 | 507 |
///\param _m is the undelying map. |
| 508 | 508 |
///\param _v is the shift value. |
| 509 | 509 |
ShiftMap(const M &_m, const C &_v ) : m(_m), v(_v) {};
|
| 510 | 510 |
///\e |
| 511 | 511 |
Value operator[](Key k) const {return m[k] + v;}
|
| 512 | 512 |
}; |
| 513 | 513 |
|
| 514 | 514 |
///Shift a map with a constant (ReadWrite version). |
| 515 | 515 |
|
| 516 | 516 |
///This \c concepts::ReadWriteMap "read-write map" returns the sum of the |
| 517 | 517 |
///given map and a constant value. It makes also possible to write the map. |
| 518 | 518 |
///Its \c Key and \c Value are inherited from \c M. |
| 519 | 519 |
/// |
| 520 | 520 |
///\sa ShiftMap |
| 521 | 521 |
template<typename M, typename C = typename M::Value> |
| 522 | 522 |
class ShiftWriteMap : public MapBase<typename M::Key, typename M::Value> {
|
| 523 | 523 |
M& m; |
| 524 | 524 |
C v; |
| 525 | 525 |
public: |
| 526 | 526 |
typedef MapBase<typename M::Key, typename M::Value> Parent; |
| 527 | 527 |
typedef typename Parent::Key Key; |
| 528 | 528 |
typedef typename Parent::Value Value; |
| 529 | 529 |
|
| 530 | 530 |
///Constructor |
| 531 | 531 |
|
| 532 | 532 |
///Constructor. |
| 533 | 533 |
///\param _m is the undelying map. |
| 534 | 534 |
///\param _v is the shift value. |
| 535 | 535 |
ShiftWriteMap(M &_m, const C &_v ) : m(_m), v(_v) {};
|
| 536 | 536 |
/// \e |
| 537 | 537 |
Value operator[](Key k) const {return m[k] + v;}
|
| 538 | 538 |
/// \e |
| 539 | 539 |
void set(Key k, const Value& c) { m.set(k, c - v); }
|
| 540 | 540 |
}; |
| 541 | 541 |
|
| 542 | 542 |
///Returns a \c ShiftMap class |
| 543 | 543 |
|
| 544 | 544 |
///This function just returns a \c ShiftMap class. |
| 545 | 545 |
///\relates ShiftMap |
| 546 | 546 |
template<typename M, typename C> |
| 547 | 547 |
inline ShiftMap<M, C> shiftMap(const M &m,const C &v) {
|
| 548 | 548 |
return ShiftMap<M, C>(m,v); |
| 549 | 549 |
} |
| 550 | 550 |
|
| 551 | 551 |
///Returns a \c ShiftWriteMap class |
| 552 | 552 |
|
| 553 | 553 |
///This function just returns a \c ShiftWriteMap class. |
| 554 | 554 |
///\relates ShiftWriteMap |
| 555 | 555 |
template<typename M, typename C> |
| 556 | 556 |
inline ShiftWriteMap<M, C> shiftMap(M &m,const C &v) {
|
| 557 | 557 |
return ShiftWriteMap<M, C>(m,v); |
| 558 | 558 |
} |
| 559 | 559 |
|
| 560 | 560 |
///Difference of two maps |
| 561 | 561 |
|
| 562 | 562 |
///This \c concepts::ReadMap "read only map" returns the difference |
| 563 | 563 |
///of the values of the two given maps. |
| 564 | 564 |
///Its \c Key and \c Value are inherited from \c M1. |
| 565 | 565 |
///The \c Key and \c Value of \c M2 must be convertible to those of \c M1. |
| 566 | 566 |
/// |
| 567 | 567 |
/// \todo Revise the misleading name |
| 568 | 568 |
template<typename M1, typename M2> |
| 569 | 569 |
class SubMap : public MapBase<typename M1::Key, typename M1::Value> {
|
| 570 | 570 |
const M1& m1; |
| 571 | 571 |
const M2& m2; |
| 572 | 572 |
public: |
| 573 | 573 |
typedef MapBase<typename M1::Key, typename M1::Value> Parent; |
| 574 | 574 |
typedef typename Parent::Key Key; |
| 575 | 575 |
typedef typename Parent::Value Value; |
| 576 | 576 |
|
| 577 | 577 |
///Constructor |
| 578 | 578 |
SubMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
|
| 579 | 579 |
/// \e |
| 580 | 580 |
Value operator[](Key k) const {return m1[k]-m2[k];}
|
| 581 | 581 |
}; |
| 582 | 582 |
|
| 583 | 583 |
///Returns a \c SubMap class |
| 584 | 584 |
|
| 585 | 585 |
///This function just returns a \c SubMap class. |
| 586 | 586 |
/// |
| 587 | 587 |
///\relates SubMap |
| 588 | 588 |
template<typename M1, typename M2> |
| 589 | 589 |
inline SubMap<M1, M2> subMap(const M1 &m1, const M2 &m2) {
|
| 590 | 590 |
return SubMap<M1, M2>(m1, m2); |
| 591 | 591 |
} |
| 592 | 592 |
|
| 593 | 593 |
///Product of two maps |
| 594 | 594 |
|
| 595 | 595 |
///This \c concepts::ReadMap "read only map" returns the product of the |
| 596 | 596 |
///values of the two given maps. |
| 597 | 597 |
///Its \c Key and \c Value are inherited from \c M1. |
| 598 | 598 |
///The \c Key and \c Value of \c M2 must be convertible to those of \c M1. |
| 599 | 599 |
template<typename M1, typename M2> |
| 600 | 600 |
class MulMap : public MapBase<typename M1::Key, typename M1::Value> {
|
| 601 | 601 |
const M1& m1; |
| 602 | 602 |
const M2& m2; |
| 603 | 603 |
public: |
| 604 | 604 |
typedef MapBase<typename M1::Key, typename M1::Value> Parent; |
| 605 | 605 |
typedef typename Parent::Key Key; |
| 606 | 606 |
typedef typename Parent::Value Value; |
| 607 | 607 |
|
| 608 | 608 |
///Constructor |
| 609 | 609 |
MulMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
|
| 610 | 610 |
/// \e |
| 611 | 611 |
Value operator[](Key k) const {return m1[k]*m2[k];}
|
| 612 | 612 |
}; |
| 613 | 613 |
|
| 614 | 614 |
///Returns a \c MulMap class |
| 615 | 615 |
|
| 616 | 616 |
///This function just returns a \c MulMap class. |
| 617 | 617 |
///\relates MulMap |
| 618 | 618 |
template<typename M1, typename M2> |
| 619 | 619 |
inline MulMap<M1, M2> mulMap(const M1 &m1,const M2 &m2) {
|
| 620 | 620 |
return MulMap<M1, M2>(m1,m2); |
| 621 | 621 |
} |
| 622 | 622 |
|
| 623 | 623 |
///Scales a map with a constant. |
| 624 | 624 |
|
| 625 | 625 |
///This \c concepts::ReadMap "read only map" returns the value of the |
| 626 | 626 |
///given map multiplied from the left side with a constant value. |
| 627 | 627 |
///Its \c Key and \c Value are inherited from \c M. |
| 628 | 628 |
/// |
| 629 | 629 |
///Actually, |
| 630 | 630 |
///\code |
| 631 | 631 |
/// ScaleMap<X> sc(x,v); |
| 632 | 632 |
///\endcode |
| 633 | 633 |
///is equivalent to |
| 634 | 634 |
///\code |
| 635 | 635 |
/// ConstMap<X::Key, X::Value> c_tmp(v); |
| 636 | 636 |
/// MulMap<X, ConstMap<X::Key, X::Value> > sc(x,v); |
| 637 | 637 |
///\endcode |
| 638 | 638 |
/// |
| 639 | 639 |
///\sa ScaleWriteMap |
| 640 | 640 |
template<typename M, typename C = typename M::Value> |
| 641 | 641 |
class ScaleMap : public MapBase<typename M::Key, typename M::Value> {
|
| 642 | 642 |
const M& m; |
| 643 | 643 |
C v; |
| 644 | 644 |
public: |
| 645 | 645 |
typedef MapBase<typename M::Key, typename M::Value> Parent; |
| 646 | 646 |
typedef typename Parent::Key Key; |
| 647 | 647 |
typedef typename Parent::Value Value; |
| 648 | 648 |
|
| 649 | 649 |
///Constructor |
| 650 | 650 |
|
| 651 | 651 |
///Constructor. |
| 652 | 652 |
///\param _m is the undelying map. |
| 653 | 653 |
///\param _v is the scaling value. |
| 654 | 654 |
ScaleMap(const M &_m, const C &_v ) : m(_m), v(_v) {};
|
| 655 | 655 |
/// \e |
| 656 | 656 |
Value operator[](Key k) const {return v * m[k];}
|
| 657 | 657 |
}; |
| 658 | 658 |
|
| 659 | 659 |
///Scales a map with a constant (ReadWrite version). |
| 660 | 660 |
|
| 661 | 661 |
///This \c concepts::ReadWriteMap "read-write map" returns the value of the |
| 662 | 662 |
///given map multiplied from the left side with a constant value. It can |
| 663 | 663 |
///also be used as write map if the \c / operator is defined between |
| 664 | 664 |
///\c Value and \c C and the given multiplier is not zero. |
| 665 | 665 |
///Its \c Key and \c Value are inherited from \c M. |
| 666 | 666 |
/// |
| 667 | 667 |
///\sa ScaleMap |
| 668 | 668 |
template<typename M, typename C = typename M::Value> |
| 669 | 669 |
class ScaleWriteMap : public MapBase<typename M::Key, typename M::Value> {
|
| 670 | 670 |
M& m; |
| 671 | 671 |
C v; |
| 672 | 672 |
public: |
| 673 | 673 |
typedef MapBase<typename M::Key, typename M::Value> Parent; |
| 674 | 674 |
typedef typename Parent::Key Key; |
| 675 | 675 |
typedef typename Parent::Value Value; |
| 676 | 676 |
|
| 677 | 677 |
///Constructor |
| 678 | 678 |
|
| 679 | 679 |
///Constructor. |
| 680 | 680 |
///\param _m is the undelying map. |
| 681 | 681 |
///\param _v is the scaling value. |
| 682 | 682 |
ScaleWriteMap(M &_m, const C &_v ) : m(_m), v(_v) {};
|
| 683 | 683 |
/// \e |
| 684 | 684 |
Value operator[](Key k) const {return v * m[k];}
|
| 685 | 685 |
/// \e |
| 686 | 686 |
void set(Key k, const Value& c) { m.set(k, c / v);}
|
| 687 | 687 |
}; |
| 688 | 688 |
|
| 689 | 689 |
///Returns a \c ScaleMap class |
| 690 | 690 |
|
| 691 | 691 |
///This function just returns a \c ScaleMap class. |
| 692 | 692 |
///\relates ScaleMap |
| 693 | 693 |
template<typename M, typename C> |
| 694 | 694 |
inline ScaleMap<M, C> scaleMap(const M &m,const C &v) {
|
| 695 | 695 |
return ScaleMap<M, C>(m,v); |
| 696 | 696 |
} |
| 697 | 697 |
|
| 698 | 698 |
///Returns a \c ScaleWriteMap class |
| 699 | 699 |
|
| 700 | 700 |
///This function just returns a \c ScaleWriteMap class. |
| 701 | 701 |
///\relates ScaleWriteMap |
| 702 | 702 |
template<typename M, typename C> |
| 703 | 703 |
inline ScaleWriteMap<M, C> scaleMap(M &m,const C &v) {
|
| 704 | 704 |
return ScaleWriteMap<M, C>(m,v); |
| 705 | 705 |
} |
| 706 | 706 |
|
| 707 | 707 |
///Quotient of two maps |
| 708 | 708 |
|
| 709 | 709 |
///This \c concepts::ReadMap "read only map" returns the quotient of the |
| 710 | 710 |
///values of the two given maps. |
| 711 | 711 |
///Its \c Key and \c Value are inherited from \c M1. |
| 712 | 712 |
///The \c Key and \c Value of \c M2 must be convertible to those of \c M1. |
| 713 | 713 |
template<typename M1, typename M2> |
| 714 | 714 |
class DivMap : public MapBase<typename M1::Key, typename M1::Value> {
|
| 715 | 715 |
const M1& m1; |
| 716 | 716 |
const M2& m2; |
| 717 | 717 |
public: |
| 718 | 718 |
typedef MapBase<typename M1::Key, typename M1::Value> Parent; |
| 719 | 719 |
typedef typename Parent::Key Key; |
| 720 | 720 |
typedef typename Parent::Value Value; |
| 721 | 721 |
|
| 722 | 722 |
///Constructor |
| 723 | 723 |
DivMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
|
| 724 | 724 |
/// \e |
| 725 | 725 |
Value operator[](Key k) const {return m1[k]/m2[k];}
|
| 726 | 726 |
}; |
| 727 | 727 |
|
| 728 | 728 |
///Returns a \c DivMap class |
| 729 | 729 |
|
| 730 | 730 |
///This function just returns a \c DivMap class. |
| 731 | 731 |
///\relates DivMap |
| 732 | 732 |
template<typename M1, typename M2> |
| 733 | 733 |
inline DivMap<M1, M2> divMap(const M1 &m1,const M2 &m2) {
|
| 734 | 734 |
return DivMap<M1, M2>(m1,m2); |
| 735 | 735 |
} |
| 736 | 736 |
|
| 737 | 737 |
///Composition of two maps |
| 738 | 738 |
|
| 739 | 739 |
///This \c concepts::ReadMap "read only map" returns the composition of |
| 740 | 740 |
///two given maps. |
| 741 | 741 |
///That is to say, if \c m1 is of type \c M1 and \c m2 is of \c M2, |
| 742 | 742 |
///then for |
| 743 | 743 |
///\code |
| 744 | 744 |
/// ComposeMap<M1, M2> cm(m1,m2); |
| 745 | 745 |
///\endcode |
| 746 | 746 |
/// <tt>cm[x]</tt> will be equal to <tt>m1[m2[x]]</tt>. |
| 747 | 747 |
/// |
| 748 | 748 |
///Its \c Key is inherited from \c M2 and its \c Value is from \c M1. |
| 749 | 749 |
///\c M2::Value must be convertible to \c M1::Key. |
| 750 | 750 |
/// |
| 751 | 751 |
///\sa CombineMap |
| 752 | 752 |
/// |
| 753 | 753 |
///\todo Check the requirements. |
| 754 | 754 |
template <typename M1, typename M2> |
| 755 | 755 |
class ComposeMap : public MapBase<typename M2::Key, typename M1::Value> {
|
| 756 | 756 |
const M1& m1; |
| 757 | 757 |
const M2& m2; |
| 758 | 758 |
public: |
| 759 | 759 |
typedef MapBase<typename M2::Key, typename M1::Value> Parent; |
| 760 | 760 |
typedef typename Parent::Key Key; |
| 761 | 761 |
typedef typename Parent::Value Value; |
| 762 | 762 |
|
| 763 | 763 |
///Constructor |
| 764 | 764 |
ComposeMap(const M1 &_m1,const M2 &_m2) : m1(_m1), m2(_m2) {};
|
| 765 | 765 |
|
| 766 | 766 |
/// \e |
| 767 | 767 |
|
| 768 | 768 |
|
| 769 | 769 |
/// \todo Use the MapTraits once it is ported. |
| 770 | 770 |
/// |
| 771 | 771 |
|
| 772 | 772 |
//typename MapTraits<M1>::ConstReturnValue |
| 773 | 773 |
typename M1::Value |
| 774 | 774 |
operator[](Key k) const {return m1[m2[k]];}
|
| 775 | 775 |
}; |
| 776 | 776 |
|
| 777 | 777 |
///Returns a \c ComposeMap class |
| 778 | 778 |
|
| 779 | 779 |
///This function just returns a \c ComposeMap class. |
| 780 | 780 |
///\relates ComposeMap |
| 781 | 781 |
template <typename M1, typename M2> |
| 782 | 782 |
inline ComposeMap<M1, M2> composeMap(const M1 &m1,const M2 &m2) {
|
| 783 | 783 |
return ComposeMap<M1, M2>(m1,m2); |
| 784 | 784 |
} |
| 785 | 785 |
|
| 786 | 786 |
///Combine of two maps using an STL (binary) functor. |
| 787 | 787 |
|
| 788 | 788 |
///Combine of two maps using an STL (binary) functor. |
| 789 | 789 |
/// |
| 790 | 790 |
///This \c concepts::ReadMap "read only map" takes two maps and a |
| 791 | 791 |
///binary functor and returns the composition of the two |
| 792 | 792 |
///given maps unsing the functor. |
| 793 | 793 |
///That is to say, if \c m1 and \c m2 is of type \c M1 and \c M2 |
| 794 | 794 |
///and \c f is of \c F, then for |
| 795 | 795 |
///\code |
| 796 | 796 |
/// CombineMap<M1,M2,F,V> cm(m1,m2,f); |
| 797 | 797 |
///\endcode |
| 798 | 798 |
/// <tt>cm[x]</tt> will be equal to <tt>f(m1[x],m2[x])</tt> |
| 799 | 799 |
/// |
| 800 | 800 |
///Its \c Key is inherited from \c M1 and its \c Value is \c V. |
| 801 | 801 |
///\c M2::Value and \c M1::Value must be convertible to the corresponding |
| 802 | 802 |
///input parameter of \c F and the return type of \c F must be convertible |
| 803 | 803 |
///to \c V. |
| 804 | 804 |
/// |
| 805 | 805 |
///\sa ComposeMap |
| 806 | 806 |
/// |
| 807 | 807 |
///\todo Check the requirements. |
| 808 | 808 |
template<typename M1, typename M2, typename F, |
| 809 | 809 |
typename V = typename F::result_type> |
| 810 | 810 |
class CombineMap : public MapBase<typename M1::Key, V> {
|
| 811 | 811 |
const M1& m1; |
| 812 | 812 |
const M2& m2; |
| 813 | 813 |
F f; |
| 814 | 814 |
public: |
| 815 | 815 |
typedef MapBase<typename M1::Key, V> Parent; |
| 816 | 816 |
typedef typename Parent::Key Key; |
| 817 | 817 |
typedef typename Parent::Value Value; |
| 818 | 818 |
|
| 819 | 819 |
///Constructor |
| 820 | 820 |
CombineMap(const M1 &_m1,const M2 &_m2,const F &_f = F()) |
| 821 | 821 |
: m1(_m1), m2(_m2), f(_f) {};
|
| 822 | 822 |
/// \e |
| 823 | 823 |
Value operator[](Key k) const {return f(m1[k],m2[k]);}
|
| 824 | 824 |
}; |
| 825 | 825 |
|
| 826 | 826 |
///Returns a \c CombineMap class |
| 827 | 827 |
|
| 828 | 828 |
///This function just returns a \c CombineMap class. |
| 829 | 829 |
/// |
| 830 | 830 |
///For example if \c m1 and \c m2 are both \c double valued maps, then |
| 831 | 831 |
///\code |
| 832 | 832 |
///combineMap(m1,m2,std::plus<double>()) |
| 833 | 833 |
///\endcode |
| 834 | 834 |
///is equivalent to |
| 835 | 835 |
///\code |
| 836 | 836 |
///addMap(m1,m2) |
| 837 | 837 |
///\endcode |
| 838 | 838 |
/// |
| 839 | 839 |
///This function is specialized for adaptable binary function |
| 840 | 840 |
///classes and C++ functions. |
| 841 | 841 |
/// |
| 842 | 842 |
///\relates CombineMap |
| 843 | 843 |
template<typename M1, typename M2, typename F, typename V> |
| 844 | 844 |
inline CombineMap<M1, M2, F, V> |
| 845 | 845 |
combineMap(const M1& m1,const M2& m2, const F& f) {
|
| 846 | 846 |
return CombineMap<M1, M2, F, V>(m1,m2,f); |
| 847 | 847 |
} |
| 848 | 848 |
|
| 849 | 849 |
template<typename M1, typename M2, typename F> |
| 850 | 850 |
inline CombineMap<M1, M2, F, typename F::result_type> |
| 851 | 851 |
combineMap(const M1& m1, const M2& m2, const F& f) {
|
| 852 | 852 |
return combineMap<M1, M2, F, typename F::result_type>(m1,m2,f); |
| 853 | 853 |
} |
| 854 | 854 |
|
| 855 | 855 |
template<typename M1, typename M2, typename K1, typename K2, typename V> |
| 856 | 856 |
inline CombineMap<M1, M2, V (*)(K1, K2), V> |
| 857 | 857 |
combineMap(const M1 &m1, const M2 &m2, V (*f)(K1, K2)) {
|
| 858 | 858 |
return combineMap<M1, M2, V (*)(K1, K2), V>(m1,m2,f); |
| 859 | 859 |
} |
| 860 | 860 |
|
| 861 | 861 |
///Negative value of a map |
| 862 | 862 |
|
| 863 | 863 |
///This \c concepts::ReadMap "read only map" returns the negative |
| 864 | 864 |
///value of the value returned by the given map. |
| 865 | 865 |
///Its \c Key and \c Value are inherited from \c M. |
| 866 | 866 |
///The unary \c - operator must be defined for \c Value, of course. |
| 867 | 867 |
/// |
| 868 | 868 |
///\sa NegWriteMap |
| 869 | 869 |
template<typename M> |
| 870 | 870 |
class NegMap : public MapBase<typename M::Key, typename M::Value> {
|
| 871 | 871 |
const M& m; |
| 872 | 872 |
public: |
| 873 | 873 |
typedef MapBase<typename M::Key, typename M::Value> Parent; |
| 874 | 874 |
typedef typename Parent::Key Key; |
| 875 | 875 |
typedef typename Parent::Value Value; |
| 876 | 876 |
|
| 877 | 877 |
///Constructor |
| 878 | 878 |
NegMap(const M &_m) : m(_m) {};
|
| 879 | 879 |
/// \e |
| 880 | 880 |
Value operator[](Key k) const {return -m[k];}
|
| 881 | 881 |
}; |
| 882 | 882 |
|
| 883 | 883 |
///Negative value of a map (ReadWrite version) |
| 884 | 884 |
|
| 885 | 885 |
///This \c concepts::ReadWriteMap "read-write map" returns the negative |
| 886 | 886 |
///value of the value returned by the given map. |
| 887 | 887 |
///Its \c Key and \c Value are inherited from \c M. |
| 888 | 888 |
///The unary \c - operator must be defined for \c Value, of course. |
| 889 | 889 |
/// |
| 890 | 890 |
/// \sa NegMap |
| 891 | 891 |
template<typename M> |
| 892 | 892 |
class NegWriteMap : public MapBase<typename M::Key, typename M::Value> {
|
| 893 | 893 |
M& m; |
| 894 | 894 |
public: |
| 895 | 895 |
typedef MapBase<typename M::Key, typename M::Value> Parent; |
| 896 | 896 |
typedef typename Parent::Key Key; |
| 897 | 897 |
typedef typename Parent::Value Value; |
| 898 | 898 |
|
| 899 | 899 |
///Constructor |
| 900 | 900 |
NegWriteMap(M &_m) : m(_m) {};
|
| 901 | 901 |
/// \e |
| 902 | 902 |
Value operator[](Key k) const {return -m[k];}
|
| 903 | 903 |
/// \e |
| 904 | 904 |
void set(Key k, const Value& v) { m.set(k, -v); }
|
| 905 | 905 |
}; |
| 906 | 906 |
|
| 907 | 907 |
///Returns a \c NegMap class |
| 908 | 908 |
|
| 909 | 909 |
///This function just returns a \c NegMap class. |
| 910 | 910 |
///\relates NegMap |
| 911 | 911 |
template <typename M> |
| 912 | 912 |
inline NegMap<M> negMap(const M &m) {
|
| 913 | 913 |
return NegMap<M>(m); |
| 914 | 914 |
} |
| 915 | 915 |
|
| 916 | 916 |
///Returns a \c NegWriteMap class |
| 917 | 917 |
|
| 918 | 918 |
///This function just returns a \c NegWriteMap class. |
| 919 | 919 |
///\relates NegWriteMap |
| 920 | 920 |
template <typename M> |
| 921 | 921 |
inline NegWriteMap<M> negMap(M &m) {
|
| 922 | 922 |
return NegWriteMap<M>(m); |
| 923 | 923 |
} |
| 924 | 924 |
|
| 925 | 925 |
///Absolute value of a map |
| 926 | 926 |
|
| 927 | 927 |
///This \c concepts::ReadMap "read only map" returns the absolute value |
| 928 | 928 |
///of the value returned by the given map. |
| 929 | 929 |
///Its \c Key and \c Value are inherited from \c M. |
| 930 | 930 |
///\c Value must be comparable to \c 0 and the unary \c - |
| 931 | 931 |
///operator must be defined for it, of course. |
| 932 | 932 |
template<typename M> |
| 933 | 933 |
class AbsMap : public MapBase<typename M::Key, typename M::Value> {
|
| 934 | 934 |
const M& m; |
| 935 | 935 |
public: |
| 936 | 936 |
typedef MapBase<typename M::Key, typename M::Value> Parent; |
| 937 | 937 |
typedef typename Parent::Key Key; |
| 938 | 938 |
typedef typename Parent::Value Value; |
| 939 | 939 |
|
| 940 | 940 |
///Constructor |
| 941 | 941 |
AbsMap(const M &_m) : m(_m) {};
|
| 942 | 942 |
/// \e |
| 943 | 943 |
Value operator[](Key k) const {
|
| 944 | 944 |
Value tmp = m[k]; |
| 945 | 945 |
return tmp >= 0 ? tmp : -tmp; |
| 946 | 946 |
} |
| 947 | 947 |
|
| 948 | 948 |
}; |
| 949 | 949 |
|
| 950 | 950 |
///Returns an \c AbsMap class |
| 951 | 951 |
| 1 | 1 |
/* -*- C++ -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library |
| 4 | 4 |
* |
| 5 | 5 |
* Copyright (C) 2003-2008 |
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_TOLERANCE_H |
| 20 | 20 |
#define LEMON_TOLERANCE_H |
| 21 | 21 |
|
| 22 | 22 |
///\ingroup misc |
| 23 | 23 |
///\file |
| 24 | 24 |
///\brief A basic tool to handle the anomalies of calculation with |
| 25 | 25 |
///floating point numbers. |
| 26 | 26 |
/// |
| 27 | 27 |
///\todo It should be in a module like "Basic tools" |
| 28 | 28 |
|
| 29 | 29 |
|
| 30 | 30 |
namespace lemon {
|
| 31 | 31 |
|
| 32 | 32 |
/// \addtogroup misc |
| 33 | 33 |
/// @{
|
| 34 | 34 |
|
| 35 | 35 |
///\brief A class to provide a basic way to |
| 36 | 36 |
///handle the comparison of numbers that are obtained |
| 37 | 37 |
///as a result of a probably inexact computation. |
| 38 | 38 |
/// |
| 39 | 39 |
///Tolerance is a class to provide a basic way to |
| 40 | 40 |
///handle the comparison of numbers that are obtained |
| 41 | 41 |
///as a result of a probably inexact computation. |
| 42 | 42 |
/// |
| 43 | 43 |
///This is an abstract class, it should be specialized for all numerical |
| 44 |
///data types. These specialized classes like |
|
| 44 |
///data types. These specialized classes like Tolerance<double> |
|
| 45 | 45 |
///may offer additional tuning parameters. |
| 46 | 46 |
/// |
| 47 | 47 |
///\sa Tolerance<float> |
| 48 | 48 |
///\sa Tolerance<double> |
| 49 | 49 |
///\sa Tolerance<long double> |
| 50 | 50 |
///\sa Tolerance<int> |
| 51 | 51 |
#if defined __GNUC__ && !defined __STRICT_ANSI__ |
| 52 | 52 |
///\sa Tolerance<long long int> |
| 53 | 53 |
#endif |
| 54 | 54 |
///\sa Tolerance<unsigned int> |
| 55 | 55 |
#if defined __GNUC__ && !defined __STRICT_ANSI__ |
| 56 | 56 |
///\sa Tolerance<unsigned long long int> |
| 57 | 57 |
#endif |
| 58 | 58 |
|
| 59 | 59 |
template<class T> |
| 60 | 60 |
class Tolerance |
| 61 | 61 |
{
|
| 62 | 62 |
public: |
| 63 | 63 |
typedef T Value; |
| 64 | 64 |
|
| 65 | 65 |
///\name Comparisons |
| 66 | 66 |
///The concept is that these bool functions return with \c true only if |
| 67 | 67 |
///the related comparisons hold even if some numerical error appeared |
| 68 | 68 |
///during the computations. |
| 69 | 69 |
|
| 70 | 70 |
///@{
|
| 71 | 71 |
|
| 72 | 72 |
///Returns \c true if \c a is \e surely strictly less than \c b |
| 73 | 73 |
static bool less(Value a,Value b) {return false;}
|
| 74 | 74 |
///Returns \c true if \c a is \e surely different from \c b |
| 75 | 75 |
static bool different(Value a,Value b) {return false;}
|
| 76 | 76 |
///Returns \c true if \c a is \e surely positive |
| 77 | 77 |
static bool positive(Value a) {return false;}
|
| 78 | 78 |
///Returns \c true if \c a is \e surely negative |
| 79 | 79 |
static bool negative(Value a) {return false;}
|
| 80 | 80 |
///Returns \c true if \c a is \e surely non-zero |
| 81 | 81 |
static bool nonZero(Value a) {return false;}
|
| 82 | 82 |
|
| 83 | 83 |
///@} |
| 84 | 84 |
|
| 85 | 85 |
///Returns the zero value. |
| 86 | 86 |
static Value zero() {return T();}
|
| 87 | 87 |
|
| 88 | 88 |
// static bool finite(Value a) {}
|
| 89 | 89 |
// static Value big() {}
|
| 90 | 90 |
// static Value negativeBig() {}
|
| 91 | 91 |
}; |
| 92 | 92 |
|
| 93 | 93 |
|
| 94 |
///Float specialization of |
|
| 94 |
///Float specialization of Tolerance. |
|
| 95 | 95 |
|
| 96 |
///Float specialization of |
|
| 96 |
///Float specialization of Tolerance. |
|
| 97 | 97 |
///\sa Tolerance |
| 98 | 98 |
///\relates Tolerance |
| 99 | 99 |
template<> |
| 100 | 100 |
class Tolerance<float> |
| 101 | 101 |
{
|
| 102 | 102 |
static float def_epsilon; |
| 103 | 103 |
float _epsilon; |
| 104 | 104 |
public: |
| 105 | 105 |
///\e |
| 106 | 106 |
typedef float Value; |
| 107 | 107 |
|
| 108 | 108 |
///Constructor setting the epsilon tolerance to the default value. |
| 109 | 109 |
Tolerance() : _epsilon(def_epsilon) {}
|
| 110 | 110 |
///Constructor setting the epsilon tolerance. |
| 111 | 111 |
Tolerance(float e) : _epsilon(e) {}
|
| 112 | 112 |
|
| 113 | 113 |
///Return the epsilon value. |
| 114 | 114 |
Value epsilon() const {return _epsilon;}
|
| 115 | 115 |
///Set the epsilon value. |
| 116 | 116 |
void epsilon(Value e) {_epsilon=e;}
|
| 117 | 117 |
|
| 118 | 118 |
///Return the default epsilon value. |
| 119 | 119 |
static Value defaultEpsilon() {return def_epsilon;}
|
| 120 | 120 |
///Set the default epsilon value. |
| 121 | 121 |
static void defaultEpsilon(Value e) {def_epsilon=e;}
|
| 122 | 122 |
|
| 123 | 123 |
///\name Comparisons |
| 124 | 124 |
///See class Tolerance for more details. |
| 125 | 125 |
|
| 126 | 126 |
///@{
|
| 127 | 127 |
|
| 128 | 128 |
///Returns \c true if \c a is \e surely strictly less than \c b |
| 129 | 129 |
bool less(Value a,Value b) const {return a+_epsilon<b;}
|
| 130 | 130 |
///Returns \c true if \c a is \e surely different from \c b |
| 131 | 131 |
bool different(Value a,Value b) const { return less(a,b)||less(b,a); }
|
| 132 | 132 |
///Returns \c true if \c a is \e surely positive |
| 133 | 133 |
bool positive(Value a) const { return _epsilon<a; }
|
| 134 | 134 |
///Returns \c true if \c a is \e surely negative |
| 135 | 135 |
bool negative(Value a) const { return -_epsilon>a; }
|
| 136 | 136 |
///Returns \c true if \c a is \e surely non-zero |
| 137 | 137 |
bool nonZero(Value a) const { return positive(a)||negative(a); }
|
| 138 | 138 |
|
| 139 | 139 |
///@} |
| 140 | 140 |
|
| 141 | 141 |
///Returns zero |
| 142 | 142 |
static Value zero() {return 0;}
|
| 143 | 143 |
}; |
| 144 | 144 |
|
| 145 |
///Double specialization of |
|
| 145 |
///Double specialization of Tolerance. |
|
| 146 | 146 |
|
| 147 |
///Double specialization of |
|
| 147 |
///Double specialization of Tolerance. |
|
| 148 | 148 |
///\sa Tolerance |
| 149 | 149 |
///\relates Tolerance |
| 150 | 150 |
template<> |
| 151 | 151 |
class Tolerance<double> |
| 152 | 152 |
{
|
| 153 | 153 |
static double def_epsilon; |
| 154 | 154 |
double _epsilon; |
| 155 | 155 |
public: |
| 156 | 156 |
///\e |
| 157 | 157 |
typedef double Value; |
| 158 | 158 |
|
| 159 | 159 |
///Constructor setting the epsilon tolerance to the default value. |
| 160 | 160 |
Tolerance() : _epsilon(def_epsilon) {}
|
| 161 | 161 |
///Constructor setting the epsilon tolerance. |
| 162 | 162 |
Tolerance(double e) : _epsilon(e) {}
|
| 163 | 163 |
|
| 164 | 164 |
///Return the epsilon value. |
| 165 | 165 |
Value epsilon() const {return _epsilon;}
|
| 166 | 166 |
///Set the epsilon value. |
| 167 | 167 |
void epsilon(Value e) {_epsilon=e;}
|
| 168 | 168 |
|
| 169 | 169 |
///Return the default epsilon value. |
| 170 | 170 |
static Value defaultEpsilon() {return def_epsilon;}
|
| 171 | 171 |
///Set the default epsilon value. |
| 172 | 172 |
static void defaultEpsilon(Value e) {def_epsilon=e;}
|
| 173 | 173 |
|
| 174 | 174 |
///\name Comparisons |
| 175 | 175 |
///See class Tolerance for more details. |
| 176 | 176 |
|
| 177 | 177 |
///@{
|
| 178 | 178 |
|
| 179 | 179 |
///Returns \c true if \c a is \e surely strictly less than \c b |
| 180 | 180 |
bool less(Value a,Value b) const {return a+_epsilon<b;}
|
| 181 | 181 |
///Returns \c true if \c a is \e surely different from \c b |
| 182 | 182 |
bool different(Value a,Value b) const { return less(a,b)||less(b,a); }
|
| 183 | 183 |
///Returns \c true if \c a is \e surely positive |
| 184 | 184 |
bool positive(Value a) const { return _epsilon<a; }
|
| 185 | 185 |
///Returns \c true if \c a is \e surely negative |
| 186 | 186 |
bool negative(Value a) const { return -_epsilon>a; }
|
| 187 | 187 |
///Returns \c true if \c a is \e surely non-zero |
| 188 | 188 |
bool nonZero(Value a) const { return positive(a)||negative(a); }
|
| 189 | 189 |
|
| 190 | 190 |
///@} |
| 191 | 191 |
|
| 192 | 192 |
///Returns zero |
| 193 | 193 |
static Value zero() {return 0;}
|
| 194 | 194 |
}; |
| 195 | 195 |
|
| 196 |
///Long double specialization of |
|
| 196 |
///Long double specialization of Tolerance. |
|
| 197 | 197 |
|
| 198 |
///Long double specialization of |
|
| 198 |
///Long double specialization of Tolerance. |
|
| 199 | 199 |
///\sa Tolerance |
| 200 | 200 |
///\relates Tolerance |
| 201 | 201 |
template<> |
| 202 | 202 |
class Tolerance<long double> |
| 203 | 203 |
{
|
| 204 | 204 |
static long double def_epsilon; |
| 205 | 205 |
long double _epsilon; |
| 206 | 206 |
public: |
| 207 | 207 |
///\e |
| 208 | 208 |
typedef long double Value; |
| 209 | 209 |
|
| 210 | 210 |
///Constructor setting the epsilon tolerance to the default value. |
| 211 | 211 |
Tolerance() : _epsilon(def_epsilon) {}
|
| 212 | 212 |
///Constructor setting the epsilon tolerance. |
| 213 | 213 |
Tolerance(long double e) : _epsilon(e) {}
|
| 214 | 214 |
|
| 215 | 215 |
///Return the epsilon value. |
| 216 | 216 |
Value epsilon() const {return _epsilon;}
|
| 217 | 217 |
///Set the epsilon value. |
| 218 | 218 |
void epsilon(Value e) {_epsilon=e;}
|
| 219 | 219 |
|
| 220 | 220 |
///Return the default epsilon value. |
| 221 | 221 |
static Value defaultEpsilon() {return def_epsilon;}
|
| 222 | 222 |
///Set the default epsilon value. |
| 223 | 223 |
static void defaultEpsilon(Value e) {def_epsilon=e;}
|
| 224 | 224 |
|
| 225 | 225 |
///\name Comparisons |
| 226 | 226 |
///See class Tolerance for more details. |
| 227 | 227 |
|
| 228 | 228 |
///@{
|
| 229 | 229 |
|
| 230 | 230 |
///Returns \c true if \c a is \e surely strictly less than \c b |
| 231 | 231 |
bool less(Value a,Value b) const {return a+_epsilon<b;}
|
| 232 | 232 |
///Returns \c true if \c a is \e surely different from \c b |
| 233 | 233 |
bool different(Value a,Value b) const { return less(a,b)||less(b,a); }
|
| 234 | 234 |
///Returns \c true if \c a is \e surely positive |
| 235 | 235 |
bool positive(Value a) const { return _epsilon<a; }
|
| 236 | 236 |
///Returns \c true if \c a is \e surely negative |
| 237 | 237 |
bool negative(Value a) const { return -_epsilon>a; }
|
| 238 | 238 |
///Returns \c true if \c a is \e surely non-zero |
| 239 | 239 |
bool nonZero(Value a) const { return positive(a)||negative(a); }
|
| 240 | 240 |
|
| 241 | 241 |
///@} |
| 242 | 242 |
|
| 243 | 243 |
///Returns zero |
| 244 | 244 |
static Value zero() {return 0;}
|
| 245 | 245 |
}; |
| 246 | 246 |
|
| 247 |
///Integer specialization of |
|
| 247 |
///Integer specialization of Tolerance. |
|
| 248 | 248 |
|
| 249 |
///Integer specialization of |
|
| 249 |
///Integer specialization of Tolerance. |
|
| 250 | 250 |
///\sa Tolerance |
| 251 | 251 |
template<> |
| 252 | 252 |
class Tolerance<int> |
| 253 | 253 |
{
|
| 254 | 254 |
public: |
| 255 | 255 |
///\e |
| 256 | 256 |
typedef int Value; |
| 257 | 257 |
|
| 258 | 258 |
///\name Comparisons |
| 259 |
///See |
|
| 259 |
///See Tolerance for more details. |
|
| 260 | 260 |
|
| 261 | 261 |
///@{
|
| 262 | 262 |
|
| 263 | 263 |
///Returns \c true if \c a is \e surely strictly less than \c b |
| 264 | 264 |
static bool less(Value a,Value b) { return a<b;}
|
| 265 | 265 |
///Returns \c true if \c a is \e surely different from \c b |
| 266 | 266 |
static bool different(Value a,Value b) { return a!=b; }
|
| 267 | 267 |
///Returns \c true if \c a is \e surely positive |
| 268 | 268 |
static bool positive(Value a) { return 0<a; }
|
| 269 | 269 |
///Returns \c true if \c a is \e surely negative |
| 270 | 270 |
static bool negative(Value a) { return 0>a; }
|
| 271 | 271 |
///Returns \c true if \c a is \e surely non-zero |
| 272 | 272 |
static bool nonZero(Value a) { return a!=0; }
|
| 273 | 273 |
|
| 274 | 274 |
///@} |
| 275 | 275 |
|
| 276 | 276 |
///Returns zero |
| 277 | 277 |
static Value zero() {return 0;}
|
| 278 | 278 |
}; |
| 279 | 279 |
|
| 280 |
///Unsigned integer specialization of |
|
| 280 |
///Unsigned integer specialization of Tolerance. |
|
| 281 | 281 |
|
| 282 | 282 |
///Unsigned integer specialization of \ref Tolerance. |
| 283 | 283 |
///\sa Tolerance |
| 284 | 284 |
template<> |
| 285 | 285 |
class Tolerance<unsigned int> |
| 286 | 286 |
{
|
| 287 | 287 |
public: |
| 288 | 288 |
///\e |
| 289 | 289 |
typedef unsigned int Value; |
| 290 | 290 |
|
| 291 | 291 |
///\name Comparisons |
| 292 |
///See |
|
| 292 |
///See Tolerance for more details. |
|
| 293 | 293 |
|
| 294 | 294 |
///@{
|
| 295 | 295 |
|
| 296 | 296 |
///Returns \c true if \c a is \e surely strictly less than \c b |
| 297 | 297 |
static bool less(Value a,Value b) { return a<b;}
|
| 298 | 298 |
///Returns \c true if \c a is \e surely different from \c b |
| 299 | 299 |
static bool different(Value a,Value b) { return a!=b; }
|
| 300 | 300 |
///Returns \c true if \c a is \e surely positive |
| 301 | 301 |
static bool positive(Value a) { return 0<a; }
|
| 302 | 302 |
///Returns \c true if \c a is \e surely negative |
| 303 | 303 |
static bool negative(Value) { return false; }
|
| 304 | 304 |
///Returns \c true if \c a is \e surely non-zero |
| 305 | 305 |
static bool nonZero(Value a) { return a!=0; }
|
| 306 | 306 |
|
| 307 | 307 |
///@} |
| 308 | 308 |
|
| 309 | 309 |
///Returns zero |
| 310 | 310 |
static Value zero() {return 0;}
|
| 311 | 311 |
}; |
| 312 | 312 |
|
| 313 | 313 |
|
| 314 |
///Long integer specialization of |
|
| 314 |
///Long integer specialization of Tolerance. |
|
| 315 | 315 |
|
| 316 |
///Long integer specialization of |
|
| 316 |
///Long integer specialization of Tolerance. |
|
| 317 | 317 |
///\sa Tolerance |
| 318 | 318 |
template<> |
| 319 | 319 |
class Tolerance<long int> |
| 320 | 320 |
{
|
| 321 | 321 |
public: |
| 322 | 322 |
///\e |
| 323 | 323 |
typedef long int Value; |
| 324 | 324 |
|
| 325 | 325 |
///\name Comparisons |
| 326 |
///See |
|
| 326 |
///See Tolerance for more details. |
|
| 327 | 327 |
|
| 328 | 328 |
///@{
|
| 329 | 329 |
|
| 330 | 330 |
///Returns \c true if \c a is \e surely strictly less than \c b |
| 331 | 331 |
static bool less(Value a,Value b) { return a<b;}
|
| 332 | 332 |
///Returns \c true if \c a is \e surely different from \c b |
| 333 | 333 |
static bool different(Value a,Value b) { return a!=b; }
|
| 334 | 334 |
///Returns \c true if \c a is \e surely positive |
| 335 | 335 |
static bool positive(Value a) { return 0<a; }
|
| 336 | 336 |
///Returns \c true if \c a is \e surely negative |
| 337 | 337 |
static bool negative(Value a) { return 0>a; }
|
| 338 | 338 |
///Returns \c true if \c a is \e surely non-zero |
| 339 | 339 |
static bool nonZero(Value a) { return a!=0;}
|
| 340 | 340 |
|
| 341 | 341 |
///@} |
| 342 | 342 |
|
| 343 | 343 |
///Returns zero |
| 344 | 344 |
static Value zero() {return 0;}
|
| 345 | 345 |
}; |
| 346 | 346 |
|
| 347 |
///Unsigned long integer specialization of |
|
| 347 |
///Unsigned long integer specialization of Tolerance. |
|
| 348 | 348 |
|
| 349 | 349 |
///Unsigned long integer specialization of \ref Tolerance. |
| 350 | 350 |
///\sa Tolerance |
| 351 | 351 |
template<> |
| 352 | 352 |
class Tolerance<unsigned long int> |
| 353 | 353 |
{
|
| 354 | 354 |
public: |
| 355 | 355 |
///\e |
| 356 | 356 |
typedef unsigned long int Value; |
| 357 | 357 |
|
| 358 | 358 |
///\name Comparisons |
| 359 |
///See |
|
| 359 |
///See Tolerance for more details. |
|
| 360 | 360 |
|
| 361 | 361 |
///@{
|
| 362 | 362 |
|
| 363 | 363 |
///Returns \c true if \c a is \e surely strictly less than \c b |
| 364 | 364 |
static bool less(Value a,Value b) { return a<b;}
|
| 365 | 365 |
///Returns \c true if \c a is \e surely different from \c b |
| 366 | 366 |
static bool different(Value a,Value b) { return a!=b; }
|
| 367 | 367 |
///Returns \c true if \c a is \e surely positive |
| 368 | 368 |
static bool positive(Value a) { return 0<a; }
|
| 369 | 369 |
///Returns \c true if \c a is \e surely negative |
| 370 | 370 |
static bool negative(Value) { return false; }
|
| 371 | 371 |
///Returns \c true if \c a is \e surely non-zero |
| 372 | 372 |
static bool nonZero(Value a) { return a!=0;}
|
| 373 | 373 |
|
| 374 | 374 |
///@} |
| 375 | 375 |
|
| 376 | 376 |
///Returns zero |
| 377 | 377 |
static Value zero() {return 0;}
|
| 378 | 378 |
}; |
| 379 | 379 |
|
| 380 | 380 |
#if defined __GNUC__ && !defined __STRICT_ANSI__ |
| 381 | 381 |
|
| 382 |
///Long long integer specialization of |
|
| 382 |
///Long long integer specialization of Tolerance. |
|
| 383 | 383 |
|
| 384 | 384 |
///Long long integer specialization of \ref Tolerance. |
| 385 | 385 |
///\warning This class (more exactly, type <tt>long long</tt>) |
| 386 | 386 |
///is not ansi compatible. |
| 387 | 387 |
///\sa Tolerance |
| 388 | 388 |
template<> |
| 389 | 389 |
class Tolerance<long long int> |
| 390 | 390 |
{
|
| 391 | 391 |
public: |
| 392 | 392 |
///\e |
| 393 | 393 |
typedef long long int Value; |
| 394 | 394 |
|
| 395 | 395 |
///\name Comparisons |
| 396 | 396 |
///See \ref Tolerance for more details. |
| 397 | 397 |
|
| 398 | 398 |
///@{
|
| 399 | 399 |
|
| 400 | 400 |
///Returns \c true if \c a is \e surely strictly less than \c b |
| 401 | 401 |
static bool less(Value a,Value b) { return a<b;}
|
| 402 | 402 |
///Returns \c true if \c a is \e surely different from \c b |
| 403 | 403 |
static bool different(Value a,Value b) { return a!=b; }
|
| 404 | 404 |
///Returns \c true if \c a is \e surely positive |
| 405 | 405 |
static bool positive(Value a) { return 0<a; }
|
| 406 | 406 |
///Returns \c true if \c a is \e surely negative |
| 407 | 407 |
static bool negative(Value a) { return 0>a; }
|
| 408 | 408 |
///Returns \c true if \c a is \e surely non-zero |
| 409 | 409 |
static bool nonZero(Value a) { return a!=0;}
|
| 410 | 410 |
|
| 411 | 411 |
///@} |
| 412 | 412 |
|
| 413 | 413 |
///Returns zero |
| 414 | 414 |
static Value zero() {return 0;}
|
| 415 | 415 |
}; |
| 416 | 416 |
|
| 417 |
///Unsigned long long integer specialization of |
|
| 417 |
///Unsigned long long integer specialization of Tolerance. |
|
| 418 | 418 |
|
| 419 | 419 |
///Unsigned long long integer specialization of \ref Tolerance. |
| 420 | 420 |
///\warning This class (more exactly, type <tt>unsigned long long</tt>) |
| 421 | 421 |
///is not ansi compatible. |
| 422 | 422 |
///\sa Tolerance |
| 423 | 423 |
template<> |
| 424 | 424 |
class Tolerance<unsigned long long int> |
| 425 | 425 |
{
|
| 426 | 426 |
public: |
| 427 | 427 |
///\e |
| 428 | 428 |
typedef unsigned long long int Value; |
| 429 | 429 |
|
| 430 | 430 |
///\name Comparisons |
| 431 | 431 |
///See \ref Tolerance for more details. |
| 432 | 432 |
|
| 433 | 433 |
///@{
|
| 434 | 434 |
|
| 435 | 435 |
///Returns \c true if \c a is \e surely strictly less than \c b |
| 436 | 436 |
static bool less(Value a,Value b) { return a<b;}
|
| 437 | 437 |
///Returns \c true if \c a is \e surely different from \c b |
| 438 | 438 |
static bool different(Value a,Value b) { return a!=b; }
|
| 439 | 439 |
///Returns \c true if \c a is \e surely positive |
| 440 | 440 |
static bool positive(Value a) { return 0<a; }
|
| 441 | 441 |
///Returns \c true if \c a is \e surely negative |
| 442 | 442 |
static bool negative(Value) { return false; }
|
| 443 | 443 |
///Returns \c true if \c a is \e surely non-zero |
| 444 | 444 |
static bool nonZero(Value a) { return a!=0;}
|
| 445 | 445 |
|
| 446 | 446 |
///@} |
| 447 | 447 |
|
| 448 | 448 |
///Returns zero |
| 449 | 449 |
static Value zero() {return 0;}
|
| 450 | 450 |
}; |
| 451 | 451 |
|
| 452 | 452 |
#endif |
| 453 | 453 |
|
| 454 | 454 |
/// @} |
| 455 | 455 |
|
| 456 | 456 |
} //namespace lemon |
| 457 | 457 |
|
| 458 | 458 |
#endif //LEMON_TOLERANCE_H |
0 comments (0 inline)