lemon/xy.h
author alpar
Wed, 06 Sep 2006 08:36:52 +0000
changeset 2197 921923afd57a
parent 2006 00d59f733817
permissions -rw-r--r--
Disable mip_test when no LP solver is available.
alpar@906
     1
/* -*- C++ -*-
alpar@906
     2
 *
alpar@1956
     3
 * This file is a part of LEMON, a generic C++ optimization library
alpar@1956
     4
 *
alpar@1956
     5
 * Copyright (C) 2003-2006
alpar@1956
     6
 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
alpar@1359
     7
 * (Egervary Research Group on Combinatorial Optimization, EGRES).
alpar@906
     8
 *
alpar@906
     9
 * Permission to use, modify and distribute this software is granted
alpar@906
    10
 * provided that this copyright notice appears in all copies. For
alpar@906
    11
 * precise terms see the accompanying LICENSE file.
alpar@906
    12
 *
alpar@906
    13
 * This software is provided "AS IS" with no warranty of any kind,
alpar@906
    14
 * express or implied, and with no claim as to its suitability for any
alpar@906
    15
 * purpose.
alpar@906
    16
 *
alpar@906
    17
 */
alpar@906
    18
alpar@921
    19
#ifndef LEMON_XY_H
alpar@921
    20
#define LEMON_XY_H
athos@201
    21
athos@201
    22
#include <iostream>
deba@1993
    23
#include <lemon/bits/utility.h>
athos@201
    24
klao@491
    25
///\ingroup misc
alpar@249
    26
///\file
alpar@249
    27
///\brief A simple two dimensional vector and a bounding box implementation 
alpar@249
    28
///
alpar@921
    29
/// The class \ref lemon::xy "xy" implements
alpar@249
    30
///a two dimensional vector with the usual
alpar@249
    31
/// operations.
alpar@249
    32
///
alpar@921
    33
/// The class \ref lemon::BoundingBox "BoundingBox" can be used to determine
ladanyi@1426
    34
/// the rectangular bounding box of a set of \ref lemon::xy "xy"'s.
alpar@458
    35
///
alpar@458
    36
///\author Attila Bernath
alpar@249
    37
alpar@249
    38
alpar@921
    39
namespace lemon {
alpar@431
    40
alpar@431
    41
  /// \addtogroup misc
alpar@431
    42
  /// @{
alpar@431
    43
alpar@1257
    44
  /// A simple two dimensional vector (plainvector) implementation
alpar@242
    45
alpar@1257
    46
  /// A simple two dimensional vector (plainvector) implementation
alpar@458
    47
  ///with the usual vector
alpar@458
    48
  /// operators.
alpar@458
    49
  ///
alpar@2157
    50
  ///\note As you might have noticed, this class does not follow the
alpar@2157
    51
  ///\ref naming_conv "LEMON Coding Style" (it should be called \c Xy
alpar@2157
    52
  ///according to it). There is a stupid Hungarian proverb, "A kiv&eacute;tel
alpar@2157
    53
  ///er&otilde;s&iacute;ti a szab&aacute;lyt" ("An exception
alpar@2157
    54
  ///reinforces a rule", which is
alpar@2157
    55
  ///actually a mistranslation of the Latin proverb "Exceptio probat regulam").
alpar@2157
    56
  ///This class is an example for that.
alpar@458
    57
  ///\author Attila Bernath
athos@207
    58
  template<typename T>
athos@207
    59
    class xy {
athos@201
    60
athos@207
    61
    public:
athos@240
    62
alpar@987
    63
      typedef T Value;
alpar@964
    64
alpar@1974
    65
      ///First co-ordinate
alpar@1974
    66
      T x;
alpar@1974
    67
      ///Second co-ordinate
alpar@1974
    68
      T y;     
athos@207
    69
      
alpar@1257
    70
      ///Default constructor
alpar@1257
    71
      xy() {}
athos@201
    72
alpar@2157
    73
      ///Construct an instance from coordinates
athos@514
    74
      xy(T a, T b) : x(a), y(b) { }
athos@201
    75
athos@201
    76
alpar@1049
    77
      ///Conversion constructor
alpar@1049
    78
      template<class TT> xy(const xy<TT> &p) : x(p.x), y(p.y) {}
alpar@1049
    79
alpar@2157
    80
      ///Give back the square of the norm of the vector
alpar@1257
    81
      T normSquare() const {
ladanyi@1426
    82
        return x*x+y*y;
alpar@1391
    83
      }
athos@201
    84
  
alpar@2157
    85
      ///Increment the left hand side by u
alpar@1257
    86
      xy<T>& operator +=(const xy<T>& u) {
ladanyi@1426
    87
        x += u.x;
ladanyi@1426
    88
        y += u.y;
ladanyi@1426
    89
        return *this;
alpar@1391
    90
      }
athos@201
    91
  
alpar@2157
    92
      ///Decrement the left hand side by u
alpar@1257
    93
      xy<T>& operator -=(const xy<T>& u) {
ladanyi@1426
    94
        x -= u.x;
ladanyi@1426
    95
        y -= u.y;
ladanyi@1426
    96
        return *this;
alpar@1391
    97
      }
athos@201
    98
alpar@2157
    99
      ///Multiply the left hand side with a scalar
alpar@1257
   100
      xy<T>& operator *=(const T &u) {
ladanyi@1426
   101
        x *= u;
ladanyi@1426
   102
        y *= u;
ladanyi@1426
   103
        return *this;
alpar@1391
   104
      }
athos@207
   105
alpar@2157
   106
      ///Divide the left hand side by a scalar
alpar@1257
   107
      xy<T>& operator /=(const T &u) {
ladanyi@1426
   108
        x /= u;
ladanyi@1426
   109
        y /= u;
ladanyi@1426
   110
        return *this;
alpar@1391
   111
      }
athos@201
   112
  
alpar@2157
   113
      ///Return the scalar product of two vectors
alpar@1257
   114
      T operator *(const xy<T>& u) const {
ladanyi@1426
   115
        return x*u.x+y*u.y;
alpar@1391
   116
      }
athos@201
   117
  
alpar@2157
   118
      ///Return the sum of two vectors
athos@207
   119
      xy<T> operator+(const xy<T> &u) const {
ladanyi@1426
   120
        xy<T> b=*this;
ladanyi@1426
   121
        return b+=u;
alpar@1391
   122
      }
athos@201
   123
alpar@2157
   124
      ///Return the neg of the vectors
alpar@1049
   125
      xy<T> operator-() const {
ladanyi@1426
   126
        xy<T> b=*this;
ladanyi@1426
   127
        b.x=-b.x; b.y=-b.y;
ladanyi@1426
   128
        return b;
alpar@1391
   129
      }
alpar@1049
   130
alpar@2157
   131
      ///Return the difference of two vectors
athos@207
   132
      xy<T> operator-(const xy<T> &u) const {
ladanyi@1426
   133
        xy<T> b=*this;
ladanyi@1426
   134
        return b-=u;
alpar@1391
   135
      }
athos@201
   136
alpar@2157
   137
      ///Return a vector multiplied by a scalar
athos@207
   138
      xy<T> operator*(const T &u) const {
ladanyi@1426
   139
        xy<T> b=*this;
ladanyi@1426
   140
        return b*=u;
alpar@1391
   141
      }
athos@201
   142
alpar@2157
   143
      ///Return a vector divided by a scalar
athos@207
   144
      xy<T> operator/(const T &u) const {
ladanyi@1426
   145
        xy<T> b=*this;
ladanyi@1426
   146
        return b/=u;
alpar@1391
   147
      }
athos@201
   148
alpar@2157
   149
      ///Test equality
alpar@1257
   150
      bool operator==(const xy<T> &u) const {
ladanyi@1426
   151
        return (x==u.x) && (y==u.y);
alpar@1391
   152
      }
athos@201
   153
alpar@2157
   154
      ///Test inequality
alpar@1257
   155
      bool operator!=(xy u) const {
ladanyi@1426
   156
        return  (x!=u.x) || (y!=u.y);
alpar@1391
   157
      }
athos@201
   158
athos@207
   159
    };
athos@201
   160
alpar@2157
   161
  ///Return an xy 
deba@1999
   162
alpar@2157
   163
  ///Return an xy
deba@1999
   164
  ///\relates xy
deba@1999
   165
  template <typename T>
deba@1999
   166
  inline xy<T> make_xy(const T& x, const T& y) {
deba@1999
   167
    return xy<T>(x, y);
deba@1999
   168
  }
deba@1999
   169
alpar@2157
   170
  ///Return a vector multiplied by a scalar
alpar@1083
   171
alpar@2157
   172
  ///Return a vector multiplied by a scalar
alpar@1083
   173
  ///\relates xy
alpar@1071
   174
  template<typename T> xy<T> operator*(const T &u,const xy<T> &x) {
alpar@1071
   175
    return x*u;
alpar@1391
   176
  }
alpar@1071
   177
alpar@814
   178
  ///Read a plainvector from a stream
alpar@814
   179
alpar@967
   180
  ///Read a plainvector from a stream
alpar@814
   181
  ///\relates xy
alpar@814
   182
  ///
athos@207
   183
  template<typename T>
deba@1392
   184
  inline std::istream& operator>>(std::istream &is, xy<T> &z) {
deba@1392
   185
    char c;
deba@1392
   186
    if (is >> c) {
deba@1392
   187
      if (c != '(') is.putback(c);
deba@1392
   188
    } else {
deba@1392
   189
      is.clear();
deba@1392
   190
    }
deba@1392
   191
    if (!(is >> z.x)) return is;
deba@1392
   192
    if (is >> c) {
deba@1392
   193
      if (c != ',') is.putback(c);
deba@1392
   194
    } else {
deba@1392
   195
      is.clear();
deba@1392
   196
    }
deba@1392
   197
    if (!(is >> z.y)) return is;
deba@1392
   198
    if (is >> c) {
deba@1392
   199
      if (c != ')') is.putback(c);
deba@1392
   200
    } else {
deba@1392
   201
      is.clear();
deba@1392
   202
    }
athos@207
   203
    return is;
athos@207
   204
  }
athos@201
   205
alpar@814
   206
  ///Write a plainvector to a stream
alpar@814
   207
alpar@967
   208
  ///Write a plainvector to a stream
alpar@814
   209
  ///\relates xy
alpar@814
   210
  ///
athos@207
   211
  template<typename T>
deba@1392
   212
  inline std::ostream& operator<<(std::ostream &os, const xy<T>& z)
athos@207
   213
  {
athos@240
   214
    os << "(" << z.x << ", " << z.y << ")";
athos@207
   215
    return os;
athos@207
   216
  }
athos@207
   217
alpar@1202
   218
  ///Rotate by 90 degrees
alpar@1202
   219
alpar@1202
   220
  ///Returns its parameter rotated by 90 degrees in positive direction.
alpar@1202
   221
  ///\relates xy
alpar@1202
   222
  ///
alpar@1202
   223
  template<typename T>
alpar@1202
   224
  inline xy<T> rot90(const xy<T> &z)
alpar@1202
   225
  {
alpar@1202
   226
    return xy<T>(-z.y,z.x);
alpar@1202
   227
  }
alpar@1202
   228
alpar@2157
   229
  ///Rotate by 180 degrees
alpar@2157
   230
alpar@2157
   231
  ///Returns its parameter rotated by 180 degrees.
alpar@2157
   232
  ///\relates xy
alpar@2157
   233
  ///
alpar@2157
   234
  template<typename T>
alpar@2157
   235
  inline xy<T> rot180(const xy<T> &z)
alpar@2157
   236
  {
alpar@2157
   237
    return xy<T>(-z.x,-z.y);
alpar@2157
   238
  }
alpar@2157
   239
alpar@1202
   240
  ///Rotate by 270 degrees
alpar@1202
   241
alpar@1202
   242
  ///Returns its parameter rotated by 90 degrees in negative direction.
alpar@1202
   243
  ///\relates xy
alpar@1202
   244
  ///
alpar@1202
   245
  template<typename T>
alpar@1202
   246
  inline xy<T> rot270(const xy<T> &z)
alpar@1202
   247
  {
alpar@1202
   248
    return xy<T>(z.y,-z.x);
alpar@1202
   249
  }
alpar@1202
   250
alpar@1202
   251
  
athos@244
   252
alpar@458
   253
  /// A class to calculate or store the bounding box of plainvectors.
alpar@458
   254
alpar@458
   255
  /// A class to calculate or store the bounding box of plainvectors.
alpar@458
   256
  ///
alpar@458
   257
  ///\author Attila Bernath
athos@244
   258
  template<typename T>
athos@244
   259
    class BoundingBox {
athos@244
   260
      xy<T> bottom_left, top_right;
athos@244
   261
      bool _empty;
athos@244
   262
    public:
athos@244
   263
      
ladanyi@1426
   264
      ///Default constructor: creates an empty bounding box
athos@244
   265
      BoundingBox() { _empty = true; }
athos@244
   266
alpar@2157
   267
      ///Construct an instance from one point
athos@244
   268
      BoundingBox(xy<T> a) { bottom_left=top_right=a; _empty = false; }
athos@244
   269
ladanyi@1426
   270
      ///Were any points added?
athos@244
   271
      bool empty() const {
ladanyi@1426
   272
        return _empty;
athos@244
   273
      }
athos@244
   274
alpar@2157
   275
      ///Make the BoundingBox empty
alpar@1391
   276
      void clear() {
ladanyi@1426
   277
        _empty=1;
alpar@1391
   278
      }
alpar@1391
   279
alpar@2157
   280
      ///Give back the bottom left corner
alpar@2157
   281
alpar@2157
   282
      ///Give back the bottom left corner.
alpar@2157
   283
      ///If the bounding box is empty, then the return value is not defined.
athos@244
   284
      xy<T> bottomLeft() const {
ladanyi@1426
   285
        return bottom_left;
alpar@1391
   286
      }
athos@244
   287
alpar@2157
   288
      ///Set the bottom left corner
alpar@2157
   289
alpar@2157
   290
      ///Set the bottom left corner.
alpar@2157
   291
      ///It should only bee used for non-empty box.
alpar@1927
   292
      void bottomLeft(xy<T> p) {
alpar@1927
   293
	bottom_left = p;
alpar@1927
   294
      }
alpar@1927
   295
alpar@2157
   296
      ///Give back the top right corner
alpar@2157
   297
alpar@2157
   298
      ///Give back the top right corner.
alpar@2157
   299
      ///If the bounding box is empty, then the return value is not defined.
athos@244
   300
      xy<T> topRight() const {
ladanyi@1426
   301
        return top_right;
alpar@1391
   302
      }
athos@244
   303
alpar@2157
   304
      ///Set the top right corner
alpar@2157
   305
alpar@2157
   306
      ///Set the top right corner.
alpar@2157
   307
      ///It should only bee used for non-empty box.
alpar@1927
   308
      void topRight(xy<T> p) {
alpar@1927
   309
	top_right = p;
alpar@1927
   310
      }
alpar@1927
   311
alpar@2157
   312
      ///Give back the bottom right corner
alpar@2157
   313
alpar@2157
   314
      ///Give back the bottom right corner.
alpar@2157
   315
      ///If the bounding box is empty, then the return value is not defined.
alpar@1045
   316
      xy<T> bottomRight() const {
ladanyi@1426
   317
        return xy<T>(top_right.x,bottom_left.y);
alpar@1391
   318
      }
alpar@1045
   319
alpar@2157
   320
      ///Set the bottom right corner
alpar@2157
   321
alpar@2157
   322
      ///Set the bottom right corner.
alpar@2157
   323
      ///It should only bee used for non-empty box.
alpar@1927
   324
      void bottomRight(xy<T> p) {
alpar@1927
   325
	top_right.x = p.x;
alpar@1927
   326
	bottom_left.y = p.y;
alpar@1927
   327
      }
alpar@2157
   328
 
alpar@2157
   329
      ///Give back the top left corner
alpar@1927
   330
alpar@2157
   331
      ///Give back the top left corner.
alpar@2157
   332
      ///If the bounding box is empty, then the return value is not defined.
alpar@1045
   333
      xy<T> topLeft() const {
ladanyi@1426
   334
        return xy<T>(bottom_left.x,top_right.y);
alpar@1391
   335
      }
alpar@1045
   336
alpar@2157
   337
      ///Set the top left corner
alpar@2157
   338
alpar@2157
   339
      ///Set the top left corner.
alpar@2157
   340
      ///It should only bee used for non-empty box.
alpar@1927
   341
      void topLeft(xy<T> p) {
alpar@1927
   342
	top_right.y = p.y;
alpar@1927
   343
	bottom_left.x = p.x;
alpar@1927
   344
      }
alpar@1927
   345
alpar@2157
   346
      ///Give back the bottom of the box
alpar@2157
   347
alpar@2157
   348
      ///Give back the bottom of the box.
alpar@2157
   349
      ///If the bounding box is empty, then the return value is not defined.
alpar@1045
   350
      T bottom() const {
ladanyi@1426
   351
        return bottom_left.y;
alpar@1391
   352
      }
alpar@1045
   353
alpar@2157
   354
      ///Set the bottom of the box
alpar@2157
   355
alpar@2157
   356
      ///Set the bottom of the box.
alpar@2157
   357
      ///It should only bee used for non-empty box.
alpar@1927
   358
      void bottom(T t) {
alpar@1927
   359
	bottom_left.y = t;
alpar@1927
   360
      }
alpar@1927
   361
alpar@2157
   362
      ///Give back the top of the box
alpar@2157
   363
alpar@2157
   364
      ///Give back the top of the box.
alpar@2157
   365
      ///If the bounding box is empty, then the return value is not defined.
alpar@1045
   366
      T top() const {
ladanyi@1426
   367
        return top_right.y;
alpar@1391
   368
      }
alpar@1045
   369
alpar@2157
   370
      ///Set the top of the box
alpar@2157
   371
alpar@2157
   372
      ///Set the top of the box.
alpar@2157
   373
      ///It should only bee used for non-empty box.
alpar@1927
   374
      void top(T t) {
alpar@1927
   375
	top_right.y = t;
alpar@1927
   376
      }
alpar@1927
   377
alpar@2157
   378
      ///Give back the left side of the box
alpar@2157
   379
alpar@2157
   380
      ///Give back the left side of the box.
alpar@2157
   381
      ///If the bounding box is empty, then the return value is not defined.
alpar@1045
   382
      T left() const {
ladanyi@1426
   383
        return bottom_left.x;
alpar@1391
   384
      }
alpar@2157
   385
 
alpar@2157
   386
      ///Set the left side of the box
alpar@1045
   387
alpar@2157
   388
      ///Set the left side of the box.
alpar@2157
   389
      ///It should only bee used for non-empty box
alpar@1927
   390
      void left(T t) {
alpar@1927
   391
	bottom_left.x = t;
alpar@1927
   392
      }
alpar@1927
   393
alpar@2157
   394
      /// Give back the right side of the box
alpar@2157
   395
alpar@2157
   396
      /// Give back the right side of the box.
alpar@2157
   397
      ///If the bounding box is empty, then the return value is not defined.
alpar@1045
   398
      T right() const {
ladanyi@1426
   399
        return top_right.x;
alpar@1391
   400
      }
alpar@1045
   401
alpar@2157
   402
      ///Set the right side of the box
alpar@2157
   403
alpar@2157
   404
      ///Set the right side of the box.
alpar@2157
   405
      ///It should only bee used for non-empty box
alpar@1927
   406
      void right(T t) {
alpar@1927
   407
	top_right.x = t;
alpar@1927
   408
      }
alpar@1927
   409
alpar@2157
   410
      ///Give back the height of the box
alpar@2157
   411
alpar@2157
   412
      ///Give back the height of the box.
alpar@2157
   413
      ///If the bounding box is empty, then the return value is not defined.
alpar@1102
   414
      T height() const {
ladanyi@1426
   415
        return top_right.y-bottom_left.y;
alpar@1391
   416
      }
alpar@1102
   417
alpar@2157
   418
      ///Give back the width of the box
alpar@2157
   419
alpar@2157
   420
      ///Give back the width of the box.
alpar@2157
   421
      ///If the bounding box is empty, then the return value is not defined.
alpar@1102
   422
      T width() const {
ladanyi@1426
   423
        return top_right.x-bottom_left.x;
alpar@1391
   424
      }
alpar@1102
   425
athos@244
   426
      ///Checks whether a point is inside a bounding box
athos@244
   427
      bool inside(const xy<T>& u){
ladanyi@1426
   428
        if (_empty)
ladanyi@1426
   429
          return false;
ladanyi@1426
   430
        else{
ladanyi@1426
   431
          return ((u.x-bottom_left.x)*(top_right.x-u.x) >= 0 &&
ladanyi@1426
   432
              (u.y-bottom_left.y)*(top_right.y-u.y) >= 0 );
ladanyi@1426
   433
        }
athos@244
   434
      }
athos@244
   435
  
athos@244
   436
      ///Increments a bounding box with a point
alpar@1588
   437
      BoundingBox& add(const xy<T>& u){
ladanyi@1426
   438
        if (_empty){
ladanyi@1426
   439
          bottom_left=top_right=u;
ladanyi@1426
   440
          _empty = false;
ladanyi@1426
   441
        }
ladanyi@1426
   442
        else{
ladanyi@1426
   443
          if (bottom_left.x > u.x) bottom_left.x = u.x;
ladanyi@1426
   444
          if (bottom_left.y > u.y) bottom_left.y = u.y;
ladanyi@1426
   445
          if (top_right.x < u.x) top_right.x = u.x;
ladanyi@1426
   446
          if (top_right.y < u.y) top_right.y = u.y;
ladanyi@1426
   447
        }
ladanyi@1426
   448
        return *this;
alpar@1391
   449
      }
athos@244
   450
  
alpar@1588
   451
//       ///Sums a bounding box and a point
alpar@1588
   452
//       BoundingBox operator +(const xy<T>& u){
alpar@1588
   453
//         BoundingBox b = *this;
alpar@1588
   454
//         return b += u;
alpar@1588
   455
//       }
athos@244
   456
alpar@2006
   457
      ///Increments a bounding box with another bounding box
alpar@1588
   458
      BoundingBox& add(const BoundingBox &u){
ladanyi@1426
   459
        if ( !u.empty() ){
alpar@1588
   460
          this->add(u.bottomLeft());
alpar@1588
   461
	  this->add(u.topRight());
ladanyi@1426
   462
        }
ladanyi@1426
   463
        return *this;
alpar@1391
   464
      }
athos@244
   465
  
athos@244
   466
      ///Sums two bounding boxes
athos@244
   467
      BoundingBox operator +(const BoundingBox& u){
ladanyi@1426
   468
        BoundingBox b = *this;
alpar@1588
   469
        return b.add(u);
alpar@1588
   470
      }
alpar@1588
   471
alpar@1588
   472
alpar@1588
   473
      ///Intersection of two bounding boxes
alpar@1588
   474
      BoundingBox operator &(const BoundingBox& u){
alpar@1588
   475
        BoundingBox b;
alpar@1588
   476
	b.bottom_left.x=std::max(this->bottom_left.x,u.bottom_left.x);
alpar@1588
   477
	b.bottom_left.y=std::max(this->bottom_left.y,u.bottom_left.y);
alpar@1588
   478
	b.top_right.x=std::min(this->top_right.x,u.top_right.x);
alpar@1588
   479
	b.top_right.y=std::min(this->top_right.y,u.top_right.y);
alpar@1588
   480
	b._empty = this->_empty || u._empty ||
alpar@1588
   481
	  b.bottom_left.x>top_right.x && b.bottom_left.y>top_right.y;
alpar@1588
   482
        return b;
alpar@1391
   483
      }
athos@244
   484
athos@244
   485
    };//class Boundingbox
athos@244
   486
athos@244
   487
alpar@1317
   488
  ///Map of x-coordinates of an xy<>-map
alpar@1317
   489
alpar@1317
   490
  ///\ingroup maps
alpar@1317
   491
  ///
alpar@1317
   492
  template<class M>
alpar@1317
   493
  class XMap 
alpar@1317
   494
  {
deba@1706
   495
    M& _map;
alpar@1317
   496
  public:
deba@1420
   497
alpar@1317
   498
    typedef typename M::Value::Value Value;
alpar@1317
   499
    typedef typename M::Key Key;
alpar@1317
   500
    ///\e
deba@1706
   501
    XMap(M& map) : _map(map) {}
alpar@1317
   502
    Value operator[](Key k) const {return _map[k].x;}
alpar@1352
   503
    void set(Key k,Value v) {_map.set(k,typename M::Value(v,_map[k].y));}
alpar@1317
   504
  };
alpar@1317
   505
    
alpar@1317
   506
  ///Returns an \ref XMap class
alpar@1317
   507
alpar@1317
   508
  ///This function just returns an \ref XMap class.
alpar@1317
   509
  ///
alpar@1317
   510
  ///\ingroup maps
alpar@1317
   511
  ///\relates XMap
alpar@1317
   512
  template<class M> 
alpar@1317
   513
  inline XMap<M> xMap(M &m) 
alpar@1317
   514
  {
alpar@1317
   515
    return XMap<M>(m);
alpar@1317
   516
  }
alpar@1317
   517
deba@1420
   518
  template<class M> 
deba@1420
   519
  inline XMap<M> xMap(const M &m) 
deba@1420
   520
  {
deba@1420
   521
    return XMap<M>(m);
deba@1420
   522
  }
deba@1420
   523
alpar@1317
   524
  ///Constant (read only) version of \ref XMap
alpar@1317
   525
alpar@1317
   526
  ///\ingroup maps
alpar@1317
   527
  ///
alpar@1317
   528
  template<class M>
alpar@1317
   529
  class ConstXMap 
alpar@1317
   530
  {
deba@1706
   531
    const M& _map;
alpar@1317
   532
  public:
deba@1420
   533
alpar@1317
   534
    typedef typename M::Value::Value Value;
alpar@1317
   535
    typedef typename M::Key Key;
alpar@1317
   536
    ///\e
alpar@1317
   537
    ConstXMap(const M &map) : _map(map) {}
alpar@1317
   538
    Value operator[](Key k) const {return _map[k].x;}
alpar@1317
   539
  };
alpar@1317
   540
    
alpar@1317
   541
  ///Returns a \ref ConstXMap class
alpar@1317
   542
alpar@1317
   543
  ///This function just returns an \ref ConstXMap class.
alpar@1317
   544
  ///
alpar@1317
   545
  ///\ingroup maps
alpar@1317
   546
  ///\relates ConstXMap
alpar@1317
   547
  template<class M> 
alpar@1317
   548
  inline ConstXMap<M> xMap(const M &m) 
alpar@1317
   549
  {
alpar@1317
   550
    return ConstXMap<M>(m);
alpar@1317
   551
  }
alpar@1317
   552
alpar@1317
   553
  ///Map of y-coordinates of an xy<>-map
alpar@1317
   554
    
alpar@1317
   555
  ///\ingroup maps
alpar@1317
   556
  ///
alpar@1317
   557
  template<class M>
alpar@1317
   558
  class YMap 
alpar@1317
   559
  {
deba@1706
   560
    M& _map;
alpar@1317
   561
  public:
deba@1420
   562
alpar@1317
   563
    typedef typename M::Value::Value Value;
alpar@1317
   564
    typedef typename M::Key Key;
alpar@1317
   565
    ///\e
deba@1706
   566
    YMap(M& map) : _map(map) {}
alpar@1317
   567
    Value operator[](Key k) const {return _map[k].y;}
alpar@1352
   568
    void set(Key k,Value v) {_map.set(k,typename M::Value(_map[k].x,v));}
alpar@1317
   569
  };
alpar@1317
   570
alpar@1317
   571
  ///Returns an \ref YMap class
alpar@1317
   572
alpar@1317
   573
  ///This function just returns an \ref YMap class.
alpar@1317
   574
  ///
alpar@1317
   575
  ///\ingroup maps
alpar@1317
   576
  ///\relates YMap
alpar@1317
   577
  template<class M> 
alpar@1317
   578
  inline YMap<M> yMap(M &m) 
alpar@1317
   579
  {
alpar@1317
   580
    return YMap<M>(m);
alpar@1317
   581
  }
alpar@1317
   582
deba@1420
   583
  template<class M> 
deba@1420
   584
  inline YMap<M> yMap(const M &m) 
deba@1420
   585
  {
deba@1420
   586
    return YMap<M>(m);
deba@1420
   587
  }
deba@1420
   588
alpar@1317
   589
  ///Constant (read only) version of \ref YMap
alpar@1317
   590
alpar@1317
   591
  ///\ingroup maps
alpar@1317
   592
  ///
alpar@1317
   593
  template<class M>
alpar@1317
   594
  class ConstYMap 
alpar@1317
   595
  {
deba@1706
   596
    const M& _map;
alpar@1317
   597
  public:
deba@1420
   598
alpar@1317
   599
    typedef typename M::Value::Value Value;
alpar@1317
   600
    typedef typename M::Key Key;
alpar@1317
   601
    ///\e
alpar@1317
   602
    ConstYMap(const M &map) : _map(map) {}
alpar@1317
   603
    Value operator[](Key k) const {return _map[k].y;}
alpar@1317
   604
  };
alpar@1317
   605
    
alpar@1317
   606
  ///Returns a \ref ConstYMap class
alpar@1317
   607
alpar@1317
   608
  ///This function just returns an \ref ConstYMap class.
alpar@1317
   609
  ///
alpar@1317
   610
  ///\ingroup maps
alpar@1317
   611
  ///\relates ConstYMap
alpar@1317
   612
  template<class M> 
alpar@1317
   613
  inline ConstYMap<M> yMap(const M &m) 
alpar@1317
   614
  {
alpar@1317
   615
    return ConstYMap<M>(m);
alpar@1317
   616
  }
alpar@1317
   617
alpar@1317
   618
alpar@1352
   619
  ///Map of the \ref xy::normSquare() "normSquare()" of an \ref xy "xy"-map
alpar@1352
   620
alpar@1352
   621
  ///Map of the \ref xy::normSquare() "normSquare()" of an \ref xy "xy"-map
alpar@1352
   622
  ///\ingroup maps
alpar@1352
   623
  ///
alpar@1352
   624
  template<class M>
alpar@1352
   625
  class NormSquareMap 
alpar@1352
   626
  {
deba@1706
   627
    const M& _map;
alpar@1352
   628
  public:
deba@1420
   629
alpar@1352
   630
    typedef typename M::Value::Value Value;
alpar@1352
   631
    typedef typename M::Key Key;
alpar@1352
   632
    ///\e
alpar@1352
   633
    NormSquareMap(const M &map) : _map(map) {}
alpar@1352
   634
    Value operator[](Key k) const {return _map[k].normSquare();}
alpar@1352
   635
  };
alpar@1352
   636
    
alpar@1352
   637
  ///Returns a \ref NormSquareMap class
alpar@1352
   638
alpar@1352
   639
  ///This function just returns an \ref NormSquareMap class.
alpar@1352
   640
  ///
alpar@1352
   641
  ///\ingroup maps
alpar@1352
   642
  ///\relates NormSquareMap
alpar@1352
   643
  template<class M> 
alpar@1352
   644
  inline NormSquareMap<M> normSquareMap(const M &m) 
alpar@1352
   645
  {
alpar@1352
   646
    return NormSquareMap<M>(m);
alpar@1352
   647
  }
alpar@1352
   648
alpar@431
   649
  /// @}
athos@244
   650
athos@244
   651
alpar@921
   652
} //namespace lemon
athos@201
   653
alpar@921
   654
#endif //LEMON_XY_H