Unionfind changes induced some bugs here. Also some augmentations made.
3 * This file is a part of LEMON, a generic C++ optimization library
5 * Copyright (C) 2003-2006
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
9 * Permission to use, modify and distribute this software is granted
10 * provided that this copyright notice appears in all copies. For
11 * precise terms see the accompanying LICENSE file.
13 * This software is provided "AS IS" with no warranty of any kind,
14 * express or implied, and with no claim as to its suitability for any
23 #include <lemon/bits/utility.h>
27 ///\brief A simple two dimensional vector and a bounding box implementation
29 /// The class \ref lemon::xy "xy" implements
30 ///a two dimensional vector with the usual
33 /// The class \ref lemon::BoundingBox "BoundingBox" can be used to determine
34 /// the rectangular bounding box of a set of \ref lemon::xy "xy"'s.
36 ///\author Attila Bernath
44 /// A simple two dimensional vector (plainvector) implementation
46 /// A simple two dimensional vector (plainvector) implementation
47 ///with the usual vector
50 ///\author Attila Bernath
63 ///Default constructor
66 ///Constructing the instance from coordinates
67 xy(T a, T b) : x(a), y(b) { }
70 ///Conversion constructor
71 template<class TT> xy(const xy<TT> &p) : x(p.x), y(p.y) {}
73 ///Gives back the square of the norm of the vector
74 T normSquare() const {
78 ///Increments the left hand side by u
79 xy<T>& operator +=(const xy<T>& u) {
85 ///Decrements the left hand side by u
86 xy<T>& operator -=(const xy<T>& u) {
92 ///Multiplying the left hand side with a scalar
93 xy<T>& operator *=(const T &u) {
99 ///Dividing the left hand side by a scalar
100 xy<T>& operator /=(const T &u) {
106 ///Returns the scalar product of two vectors
107 T operator *(const xy<T>& u) const {
111 ///Returns the sum of two vectors
112 xy<T> operator+(const xy<T> &u) const {
117 ///Returns the neg of the vectors
118 xy<T> operator-() const {
124 ///Returns the difference of two vectors
125 xy<T> operator-(const xy<T> &u) const {
130 ///Returns a vector multiplied by a scalar
131 xy<T> operator*(const T &u) const {
136 ///Returns a vector divided by a scalar
137 xy<T> operator/(const T &u) const {
143 bool operator==(const xy<T> &u) const {
144 return (x==u.x) && (y==u.y);
147 ///Testing inequality
148 bool operator!=(xy u) const {
149 return (x!=u.x) || (y!=u.y);
158 template <typename T>
159 inline xy<T> make_xy(const T& x, const T& y) {
163 ///Returns a vector multiplied by a scalar
165 ///Returns a vector multiplied by a scalar
167 template<typename T> xy<T> operator*(const T &u,const xy<T> &x) {
171 ///Read a plainvector from a stream
173 ///Read a plainvector from a stream
177 inline std::istream& operator>>(std::istream &is, xy<T> &z) {
180 if (c != '(') is.putback(c);
184 if (!(is >> z.x)) return is;
186 if (c != ',') is.putback(c);
190 if (!(is >> z.y)) return is;
192 if (c != ')') is.putback(c);
199 ///Write a plainvector to a stream
201 ///Write a plainvector to a stream
205 inline std::ostream& operator<<(std::ostream &os, const xy<T>& z)
207 os << "(" << z.x << ", " << z.y << ")";
211 ///Rotate by 90 degrees
213 ///Returns its parameter rotated by 90 degrees in positive direction.
217 inline xy<T> rot90(const xy<T> &z)
219 return xy<T>(-z.y,z.x);
222 ///Rotate by 270 degrees
224 ///Returns its parameter rotated by 90 degrees in negative direction.
228 inline xy<T> rot270(const xy<T> &z)
230 return xy<T>(z.y,-z.x);
235 /// A class to calculate or store the bounding box of plainvectors.
237 /// A class to calculate or store the bounding box of plainvectors.
239 ///\author Attila Bernath
242 xy<T> bottom_left, top_right;
246 ///Default constructor: creates an empty bounding box
247 BoundingBox() { _empty = true; }
249 ///Constructing the instance from one point
250 BoundingBox(xy<T> a) { bottom_left=top_right=a; _empty = false; }
252 ///Were any points added?
257 ///Makes the BoundingBox empty
262 ///\brief Gives back the bottom left corner
263 ///(if the bounding box is empty, then the return value is not defined)
264 xy<T> bottomLeft() const {
268 ///\brief Sets the bottom left corner
269 ///(should only bee used for non-empty box)
270 void bottomLeft(xy<T> p) {
274 ///\brief Gives back the top right corner
275 ///(if the bounding box is empty, then the return value is not defined)
276 xy<T> topRight() const {
280 ///\brief Sets the top right corner
281 ///(should only bee used for non-empty box)
282 void topRight(xy<T> p) {
286 ///\brief Gives back the bottom right corner
287 ///(if the bounding box is empty, then the return value is not defined)
288 xy<T> bottomRight() const {
289 return xy<T>(top_right.x,bottom_left.y);
292 ///\brief Sets the bottom right corner
293 ///(should only bee used for non-empty box)
294 void bottomRight(xy<T> p) {
299 ///\brief Gives back the top left corner
300 ///(if the bounding box is empty, then the return value is not defined)
301 xy<T> topLeft() const {
302 return xy<T>(bottom_left.x,top_right.y);
305 ///\brief Sets the top left corner
306 ///(should only bee used for non-empty box)
307 void topLeft(xy<T> p) {
312 ///\brief Gives back the bottom of the box
313 ///(if the bounding box is empty, then the return value is not defined)
315 return bottom_left.y;
318 ///\brief Sets the bottom of the box
319 ///(should only bee used for non-empty box)
324 ///\brief Gives back the top of the box
325 ///(if the bounding box is empty, then the return value is not defined)
330 ///\brief Sets the top of the box
331 ///(should only bee used for non-empty box)
336 ///\brief Gives back the left side of the box
337 ///(if the bounding box is empty, then the return value is not defined)
339 return bottom_left.x;
342 ///\brief Sets the left side of the box
343 ///(should only bee used for non-empty box)
348 ///\brief Gives back the right side of the box
349 ///(if the bounding box is empty, then the return value is not defined)
354 ///\brief Sets the right side of the box
355 ///(should only bee used for non-empty box)
360 ///\brief Gives back the height of the box
361 ///(if the bounding box is empty, then the return value is not defined)
363 return top_right.y-bottom_left.y;
366 ///\brief Gives back the width of the box
367 ///(if the bounding box is empty, then the return value is not defined)
369 return top_right.x-bottom_left.x;
372 ///Checks whether a point is inside a bounding box
373 bool inside(const xy<T>& u){
377 return ((u.x-bottom_left.x)*(top_right.x-u.x) >= 0 &&
378 (u.y-bottom_left.y)*(top_right.y-u.y) >= 0 );
382 ///Increments a bounding box with a point
383 BoundingBox& add(const xy<T>& u){
385 bottom_left=top_right=u;
389 if (bottom_left.x > u.x) bottom_left.x = u.x;
390 if (bottom_left.y > u.y) bottom_left.y = u.y;
391 if (top_right.x < u.x) top_right.x = u.x;
392 if (top_right.y < u.y) top_right.y = u.y;
397 // ///Sums a bounding box and a point
398 // BoundingBox operator +(const xy<T>& u){
399 // BoundingBox b = *this;
403 ///Increments a bounding box with another bounding box
404 BoundingBox& add(const BoundingBox &u){
406 this->add(u.bottomLeft());
407 this->add(u.topRight());
412 ///Sums two bounding boxes
413 BoundingBox operator +(const BoundingBox& u){
414 BoundingBox b = *this;
419 ///Intersection of two bounding boxes
420 BoundingBox operator &(const BoundingBox& u){
422 b.bottom_left.x=std::max(this->bottom_left.x,u.bottom_left.x);
423 b.bottom_left.y=std::max(this->bottom_left.y,u.bottom_left.y);
424 b.top_right.x=std::min(this->top_right.x,u.top_right.x);
425 b.top_right.y=std::min(this->top_right.y,u.top_right.y);
426 b._empty = this->_empty || u._empty ||
427 b.bottom_left.x>top_right.x && b.bottom_left.y>top_right.y;
431 };//class Boundingbox
434 ///Map of x-coordinates of an xy<>-map
444 typedef typename M::Value::Value Value;
445 typedef typename M::Key Key;
447 XMap(M& map) : _map(map) {}
448 Value operator[](Key k) const {return _map[k].x;}
449 void set(Key k,Value v) {_map.set(k,typename M::Value(v,_map[k].y));}
452 ///Returns an \ref XMap class
454 ///This function just returns an \ref XMap class.
459 inline XMap<M> xMap(M &m)
465 inline XMap<M> xMap(const M &m)
470 ///Constant (read only) version of \ref XMap
480 typedef typename M::Value::Value Value;
481 typedef typename M::Key Key;
483 ConstXMap(const M &map) : _map(map) {}
484 Value operator[](Key k) const {return _map[k].x;}
487 ///Returns a \ref ConstXMap class
489 ///This function just returns an \ref ConstXMap class.
492 ///\relates ConstXMap
494 inline ConstXMap<M> xMap(const M &m)
496 return ConstXMap<M>(m);
499 ///Map of y-coordinates of an xy<>-map
509 typedef typename M::Value::Value Value;
510 typedef typename M::Key Key;
512 YMap(M& map) : _map(map) {}
513 Value operator[](Key k) const {return _map[k].y;}
514 void set(Key k,Value v) {_map.set(k,typename M::Value(_map[k].x,v));}
517 ///Returns an \ref YMap class
519 ///This function just returns an \ref YMap class.
524 inline YMap<M> yMap(M &m)
530 inline YMap<M> yMap(const M &m)
535 ///Constant (read only) version of \ref YMap
545 typedef typename M::Value::Value Value;
546 typedef typename M::Key Key;
548 ConstYMap(const M &map) : _map(map) {}
549 Value operator[](Key k) const {return _map[k].y;}
552 ///Returns a \ref ConstYMap class
554 ///This function just returns an \ref ConstYMap class.
557 ///\relates ConstYMap
559 inline ConstYMap<M> yMap(const M &m)
561 return ConstYMap<M>(m);
565 ///Map of the \ref xy::normSquare() "normSquare()" of an \ref xy "xy"-map
567 ///Map of the \ref xy::normSquare() "normSquare()" of an \ref xy "xy"-map
576 typedef typename M::Value::Value Value;
577 typedef typename M::Key Key;
579 NormSquareMap(const M &map) : _map(map) {}
580 Value operator[](Key k) const {return _map[k].normSquare();}
583 ///Returns a \ref NormSquareMap class
585 ///This function just returns an \ref NormSquareMap class.
588 ///\relates NormSquareMap
590 inline NormSquareMap<M> normSquareMap(const M &m)
592 return NormSquareMap<M>(m);