Thrill  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
vector.hpp
Go to the documentation of this file.
1 /*******************************************************************************
2  * thrill/common/vector.hpp
3  *
4  * Part of Project Thrill - http://project-thrill.org
5  *
6  * Copyright (C) 2015 Timo Bingmann <[email protected]>
7  *
8  * All rights reserved. Published under the BSD-2 license in the LICENSE file.
9  ******************************************************************************/
10 
11 #pragma once
12 #ifndef THRILL_COMMON_VECTOR_HEADER
13 #define THRILL_COMMON_VECTOR_HEADER
14 
16 
17 #include <algorithm>
18 #include <cassert>
19 #include <functional>
20 #include <vector>
21 
22 namespace thrill {
23 namespace common {
24 
25 //! A compile-time fixed-length D-dimensional point with double precision.
26 template <size_t D, typename Type = double>
27 class Vector
28 {
29 public:
30  //! coordinates array
31  Type x[D];
32 
33  using type = Type;
34 
35  static size_t dim() { return D; }
36 
37  static Vector Make(size_t D_) {
38  die_unless(D_ == D);
39  return Vector();
40  }
41  static Vector Origin() {
42  Vector p;
43  std::fill(p.x, p.x + D, 0.0);
44  return p;
45  }
46  template <typename Distribution, typename Generator>
47  static Vector Random(size_t dim, Distribution& dist, Generator& gen) {
48  die_unless(dim == D);
49  Vector p;
50  for (size_t i = 0; i < D; ++i) p.x[i] = dist(gen);
51  return p;
52  }
53  Type DistanceSquare(const Vector& b) const {
54  Type sum = 0.0;
55  for (size_t i = 0; i < D; ++i) sum += (x[i] - b.x[i]) * (x[i] - b.x[i]);
56  return sum;
57  }
58  Type Distance(const Vector& b) const {
59  return std::sqrt(DistanceSquare(b));
60  }
61  Vector operator + (const Vector& b) const {
62  Vector p;
63  for (size_t i = 0; i < D; ++i) p.x[i] = x[i] + b.x[i];
64  return p;
65  }
66  Vector& operator += (const Vector& b) {
67  for (size_t i = 0; i < D; ++i) x[i] += b.x[i];
68  return *this;
69  }
70  Vector operator / (const Type& s) const {
71  Vector p;
72  for (size_t i = 0; i < D; ++i) p.x[i] = x[i] / s;
73  return p;
74  }
75  Vector& operator /= (const Type& s) {
76  for (size_t i = 0; i < D; ++i) x[i] /= s;
77  return *this;
78  }
79  friend std::ostream& operator << (std::ostream& os, const Vector& a) {
80  os << '(' << a.x[0];
81  for (size_t i = 1; i != D; ++i) os << ',' << a.x[i];
82  return os << ')';
83  }
84 };
85 
86 //! A variable-length D-dimensional point with double precision
87 template <typename Type = double>
88 class VVector
89 {
90 public:
91  using TypeVector = std::vector<Type>;
92 
93  using type = Type;
94 
95  //! coordinates array
97 
98  explicit VVector(size_t D = 0) : x(D) { }
99  explicit VVector(TypeVector&& v) : x(std::move(v)) { }
100 
101  size_t dim() const { return x.size(); }
102 
103  static VVector Make(size_t D) {
104  return VVector(D);
105  }
106  template <typename Distribution, typename Generator>
107  static VVector Random(size_t D, Distribution& dist, Generator& gen) {
108  VVector p(D);
109  for (size_t i = 0; i < D; ++i) p.x[i] = dist(gen);
110  return p;
111  }
112  Type DistanceSquare(const VVector& b) const {
113  assert(x.size() == b.x.size());
114  Type sum = 0.0;
115  for (size_t i = 0; i < x.size(); ++i)
116  sum += (x[i] - b.x[i]) * (x[i] - b.x[i]);
117  return sum;
118  }
119  Type Distance(const VVector& b) const {
120  return std::sqrt(DistanceSquare(b));
121  }
122  VVector operator + (const VVector& b) const {
123  assert(x.size() == b.x.size());
124  VVector p(x.size());
125  for (size_t i = 0; i < x.size(); ++i) p.x[i] = x[i] + b.x[i];
126  return p;
127  }
129  assert(x.size() == b.x.size());
130  for (size_t i = 0; i < x.size(); ++i) x[i] += b.x[i];
131  return *this;
132  }
133  VVector operator / (const Type& s) const {
134  VVector p(x.size());
135  for (size_t i = 0; i < x.size(); ++i) p.x[i] = x[i] / s;
136  return p;
137  }
138  VVector& operator /= (const Type& s) {
139  for (size_t i = 0; i < x.size(); ++i) x[i] /= s;
140  return *this;
141  }
142  friend std::ostream& operator << (std::ostream& os, const VVector& a) {
143  os << '(' << a.x[0];
144  for (size_t i = 1; i != a.x.size(); ++i) os << ',' << a.x[i];
145  return os << ')';
146  }
147 
148  template <typename Archive>
149  void serialize(Archive& archive) {
150  archive(x);
151  }
152 };
153 
154 } // namespace common
155 } // namespace thrill
156 
157 #endif // !THRILL_COMMON_VECTOR_HEADER
158 
159 /******************************************************************************/