DIY 3.0 data-parallel out-of-core C++ library

- Main Page
- Modules
- Namespaces
- Classes
- Files
- Examples

- File List

All Classes Namespaces Functions Typedefs Groups Pages
point.hpp
1 #ifndef DIY_POINT_HPP
2 #define DIY_POINT_HPP
3
4 #include <iostream>
5 #include <vector>
6 #include <string>
7 #include <sstream>
8
9 #include <array>
10
11 namespace diy
12 {
13
14 template<class Coordinate_, unsigned D>
15 class Point: public std::array<Coordinate_, D>
16 {
17 public:
18 typedef Coordinate_ Coordinate;
19 typedef std::array<Coordinate, D> ArrayParent;
20
21 typedef Point<Coordinate, D-1> LPoint;
22 typedef Point<Coordinate, D+1> UPoint;
23
24 template<class U>
25 struct rebind { typedef Point<U,D> type; };
26
27 public:
28 Point() { for (unsigned i = 0; i < D; ++i) (*this)[i] = 0; }
29 Point(const ArrayParent& a):
30 ArrayParent(a) {}
31 template<class T> Point(const Point<T, D>& p) { for (size_t i = 0; i < D; ++i) (*this)[i] = p[i]; }
32 template<class T> Point(const T* a) { for (unsigned i = 0; i < D; ++i) (*this)[i] = a[i]; }
33 template<class T> Point(const std::vector<T>& a) { for (unsigned i = 0; i < D; ++i) (*this)[i] = a[i]; }
34 Point(std::initializer_list<Coordinate> lst) { unsigned i = 0; for (Coordinate x : lst) (*this)[i++] = x; }
35
36 Point(Point&&) =default;
37 Point(const Point&) =default;
38 Point& operator=(const Point&) =default;
39
40 static constexpr
41 unsigned dimension() { return D; }
42
43 static Point zero() { return Point(); }
44 static Point one() { Point p; for (unsigned i = 0; i < D; ++i) p[i] = 1; return p; }
45
46 LPoint drop(int dim) const { LPoint p; unsigned c = 0; for (unsigned i = 0; i < D; ++i) { if (i == dim) continue; p[c++] = (*this)[i]; } return p; }
47 UPoint lift(int dim, Coordinate x) const { UPoint p; for (unsigned i = 0; i < D+1; ++i) { if (i < dim) p[i] = (*this)[i]; else if (i == dim) p[i] = x; else if (i > dim) p[i] = (*this)[i-1]; } return p; }
48
49 using ArrayParent::operator[];
50
51 Point& operator+=(const Point& y) { for (unsigned i = 0; i < D; ++i) (*this)[i] += y[i]; return *this; }
52 Point& operator-=(const Point& y) { for (unsigned i = 0; i < D; ++i) (*this)[i] -= y[i]; return *this; }
53 Point& operator*=(Coordinate a) { for (unsigned i = 0; i < D; ++i) (*this)[i] *= a; return *this; }
54 Point& operator/=(Coordinate a) { for (unsigned i = 0; i < D; ++i) (*this)[i] /= a; return *this; }
55
56 Coordinate norm() const { return (*this)*(*this); }
57
58 std::ostream& operator<<(std::ostream& out) const { out << (*this)[0]; for (unsigned i = 1; i < D; ++i) out << " " << (*this)[i]; return out; }
59 std::istream& operator>>(std::istream& in);
60
61 friend
62 Point operator+(Point x, const Point& y) { x += y; return x; }
63
64 friend
65 Point operator-(Point x, const Point& y) { x -= y; return x; }
66
67 friend
68 Point operator/(Point x, Coordinate y) { x /= y; return x; }
69
70 friend
71 Point operator*(Point x, Coordinate y) { x *= y; return x; }
72
73 friend
74 Point operator*(Coordinate y, Point x) { x *= y; return x; }
75
76 friend
77 Coordinate operator*(const Point& x, const Point& y) { Coordinate n = 0; for (size_t i = 0; i < D; ++i) n += x[i] * y[i]; return n; }
78
79 template<class T>
80 friend
81 Coordinate operator*(const Point<T,D>& x, const Point& y) { Coordinate n = 0; for (size_t i = 0; i < D; ++i) n += x[i] * y[i]; return n; }
82 };
83
84 template<class C, unsigned D>
85 std::istream&
86 Point<C,D>::
87 operator>>(std::istream& in)
88 {
89 std::string point_str;
90 in >> point_str; // read until ' '
91 std::stringstream ps(point_str);
92
93 char x;
94 for (unsigned i = 0; i < dimension(); ++i)
95 {
96 ps >> (*this)[i];
97 ps >> x;
98 }
99
100 return in;
101 }
102
103
104 template<class Coordinate, unsigned D>
105 Coordinate norm2(const Point<Coordinate,D>& p)
106 { Coordinate res = 0; for (unsigned i = 0; i < D; ++i) res += p[i]*p[i]; return res; }
107
108 template<class C, unsigned D>
109 std::ostream&
110 operator<<(std::ostream& out, const Point<C,D>& p)
111 { return p.operator<<(out); }
112
113 template<class C, unsigned D>
114 std::istream&
115 operator>>(std::istream& in, Point<C,D>& p)
116 { return p.operator>>(in); }
117
118 }
119
120 #endif // DIY_POINT_HPP
diy::Point::rebind
Definition: point.hpp:25
diy::in
void in(const RegularLink< Bounds > &link, const Point &p, OutIter out, const Bounds &domain)
Finds the neighbor(s) containing the target point.
Definition: pick.hpp:102
diy::Point
Definition: point.hpp:15