# HG changeset patch # User deba # Date 1157705707 0 # Node ID 0ad3835449f823fadfa32a469271a857e2f7d38c # Parent c790d04e192af5355c1e1a92e0e4a7f49777ebae Some small improvments size() and subscription operators compatibility with higher dimensions diff -r c790d04e192a -r 0ad3835449f8 lemon/dim2.h --- a/lemon/dim2.h Thu Sep 07 14:16:47 2006 +0000 +++ b/lemon/dim2.h Fri Sep 08 08:55:07 2006 +0000 @@ -73,6 +73,14 @@ ///Construct an instance from coordinates Point(T a, T b) : x(a), y(b) { } + ///Size of vector + int size() const { return 2; } + + ///Subscripting operator + T& operator[](int idx) { return idx == 0 ? x : y; } + + ///Const subscripting operator + const T& operator[](int idx) const { return idx == 0 ? x : y; } ///Conversion constructor template Point(const Point &p) : x(p.x), y(p.y) {} @@ -163,7 +171,7 @@ ///Return an Point ///\relates Point template - inline Point make_Point(const T& x, const T& y) { + inline Point makePoint(const T& x, const T& y) { return Point(x, y); } diff -r c790d04e192a -r 0ad3835449f8 test/dim_test.cc --- a/test/dim_test.cc Thu Sep 07 14:16:47 2006 +0000 +++ b/test/dim_test.cc Fri Sep 08 08:55:07 2006 +0000 @@ -29,54 +29,58 @@ typedef dim2::Point Point; - Point seged; - Point a(1,2); - Point b(3,4); + Point seged; + check(seged.size()==2, "Wrong vector addition"); - seged = a+b; - check(seged.x==4 && seged.y==6, "Wrong vector addition"); + Point a(1,2); + Point b(3,4); - seged = a-b; - check(seged.x==-2 && seged.y==-2, "a-b"); + check(a[0]==1 && a[1]==2, "Wrong vector addition"); - check(a.normSquare()==5,"Wrong norm calculation"); - check(a*b==11, "a*b"); + seged = a+b; + check(seged.x==4 && seged.y==6, "Wrong vector addition"); - int l=2; - seged = a*l; - check(seged.x==2 && seged.y==4, "a*l"); + seged = a-b; + check(seged.x==-2 && seged.y==-2, "a-b"); - seged = b/l; - check(seged.x==1 && seged.y==2, "b/l"); + check(a.normSquare()==5,"Wrong norm calculation"); + check(a*b==11, "a*b"); - typedef dim2::BoundingBox BB; - BB doboz1; - check(doboz1.empty(), "It should be empty."); + int l=2; + seged = a*l; + check(seged.x==2 && seged.y==4, "a*l"); + + seged = b/l; + check(seged.x==1 && seged.y==2, "b/l"); + + typedef dim2::BoundingBox BB; + BB doboz1; + check(doboz1.empty(), "It should be empty."); - doboz1.add(a); - check(!doboz1.empty(), "It should not be empty."); - doboz1.add(b); + doboz1.add(a); + check(!doboz1.empty(), "It should not be empty."); + doboz1.add(b); - check(doboz1.bottomLeft().x==1 && - doboz1.bottomLeft().y==2 && - doboz1.topRight().x==3 && - doboz1.topRight().y==4, - "added points to box"); + check(doboz1.bottomLeft().x==1 && + doboz1.bottomLeft().y==2 && + doboz1.topRight().x==3 && + doboz1.topRight().y==4, + "added points to box"); - seged.x=2;seged.y=3; - check(doboz1.inside(seged),"It should be inside."); + seged.x=2;seged.y=3; + check(doboz1.inside(seged),"It should be inside."); - seged.x=1;seged.y=3; - check(doboz1.inside(seged),"It should be inside."); + seged.x=1;seged.y=3; + check(doboz1.inside(seged),"It should be inside."); - seged.x=0;seged.y=3; - check(!doboz1.inside(seged),"It should not be inside."); + seged.x=0;seged.y=3; + check(!doboz1.inside(seged),"It should not be inside."); - BB doboz2(seged); - check(!doboz2.empty(), - "It should not be empty. Constructed from 1 point."); + BB doboz2(seged); + check(!doboz2.empty(), + "It should not be empty. Constructed from 1 point."); - doboz2.add(doboz1); - check(doboz2.inside(seged), - "It should be inside. Incremented a box with another one."); + doboz2.add(doboz1); + check(doboz2.inside(seged), + "It should be inside. Incremented a box with another one."); }