Simpatico  v1.10
IntVector.h
1 #ifndef UTIL_INT_VECTOR_H
2 #define UTIL_INT_VECTOR_H
3 
4 /*
5 * Util Package - C++ Utilities for Scientific Computation
6 *
7 * Copyright 2010 - 2017, The Regents of the University of Minnesota
8 * Distributed under the terms of the GNU General Public License.
9 */
10 
11 #include <util/global.h>
12 #include <util/space/Dimension.h>
13 #ifdef UTIL_MPI
14 #include <util/mpi/MpiTraits.h>
15 #endif
16 
17 #include <iostream>
18 
19 namespace Util
20 {
21 
73  class IntVector
74  {
75 
76  public:
77 
79 
80 
85  {}
86 
90  IntVector(const IntVector& v)
91  {
92  elem_[0] = v.elem_[0];
93  elem_[1] = v.elem_[1];
94  elem_[2] = v.elem_[2];
95  }
96 
102  explicit IntVector(int scalar)
103  {
104  elem_[0] = scalar;
105  elem_[1] = scalar;
106  elem_[2] = scalar;
107  }
108 
114  explicit IntVector(const int* v)
115  {
116  elem_[0] = v[0];
117  elem_[1] = v[1];
118  elem_[2] = v[2];
119  }
120 
128  IntVector(int x, int y, int z = 0)
129  {
130  elem_[0] = x;
131  elem_[1] = y;
132  elem_[2] = z;
133  }
134 
136 
141  {
142  elem_[0] = 0;
143  elem_[1] = 0;
144  elem_[2] = 0;
145  return *this;
146  }
147 
156  template <class Archive>
157  void serialize(Archive& ar, const unsigned int version);
158 
160 
161 
168  {
169  elem_[0] = v.elem_[0];
170  elem_[1] = v.elem_[1];
171  elem_[2] = v.elem_[2];
172  return *this;
173  }
174 
180  IntVector& operator=(const int* v)
181  {
182  elem_[0] = v[0];
183  elem_[1] = v[1];
184  elem_[2] = v[2];
185  return *this;
186  }
187 
189 
191 
199  void operator+=(const IntVector& dv)
200  {
201  elem_[0] += dv.elem_[0];
202  elem_[1] += dv.elem_[1];
203  elem_[2] += dv.elem_[2];
204  }
205 
213  void operator-=(const IntVector& dv)
214  {
215  elem_[0] -= dv.elem_[0];
216  elem_[1] -= dv.elem_[1];
217  elem_[2] -= dv.elem_[2];
218  }
219 
227  void operator*=(int s)
228  {
229  elem_[0] *= s;
230  elem_[1] *= s;
231  elem_[2] *= s;
232  }
233 
235 
237 
244  const int& operator[](int i) const
245  {
246  assert(i >= 0);
247  assert(i < Dimension);
248  return elem_[i];
249  }
250 
257  int& operator[](int i)
258  {
259  assert(i >= 0);
260  assert(i < Dimension);
261  return elem_[i];
262  }
263 
265 
267 
273  int square() const
274  { return elem_[0]*elem_[0] + elem_[1]*elem_[1] + elem_[2]*elem_[2]; }
275 
282  int dot(const IntVector& v) const
283  {
284  return elem_[0]*v.elem_[0] + elem_[1]*v.elem_[1] + elem_[2]*v.elem_[2];
285  }
286 
288 
290 
299  IntVector& add(const IntVector& v1, const IntVector& v2)
300  {
301  elem_[0] = v1.elem_[0] + v2.elem_[0];
302  elem_[1] = v1.elem_[1] + v2.elem_[1];
303  elem_[2] = v1.elem_[2] + v2.elem_[2];
304  return *this;
305  }
306 
316  IntVector& subtract(const IntVector& v1, const IntVector& v2)
317  {
318  elem_[0] = v1.elem_[0] - v2.elem_[0];
319  elem_[1] = v1.elem_[1] - v2.elem_[1];
320  elem_[2] = v1.elem_[2] - v2.elem_[2];
321  return *this;
322  }
323 
333  IntVector& multiply(const IntVector& v, int s)
334  {
335  elem_[0] = v.elem_[0]*s;
336  elem_[1] = v.elem_[1]*s;
337  elem_[2] = v.elem_[2]*s;
338  return *this;
339  }
340 
350  IntVector& cross(const IntVector& v1, const IntVector& v2)
351  {
352  elem_[0] = v1.elem_[1]*v2.elem_[2] - v1.elem_[2]*v2.elem_[1];
353  elem_[1] = v1.elem_[2]*v2.elem_[0] - v1.elem_[0]*v2.elem_[2];
354  elem_[2] = v1.elem_[0]*v2.elem_[1] - v1.elem_[1]*v2.elem_[0];
355  return *this;
356  }
357 
359 
361 
362 
364  static const IntVector Zero;
365 
369  static void initStatic();
370 
371  #ifdef UTIL_MPI
372 
375  static void commitMpiType();
376  #endif
377 
379 
380  private:
381 
383  static const int Width = 15;
384 
386  int elem_[Dimension];
387 
388  //friends:
389 
390  friend bool operator == (const IntVector& v1, const IntVector& v2);
391 
392  friend bool operator == (const IntVector& v1, const int* v2);
393 
394  friend std::istream& operator >> (std::istream& in, IntVector &vector);
395 
396  friend std::ostream& operator << (std::ostream& out, const IntVector &vector);
397 
398  };
399 
401  bool operator == (const IntVector& v1, const IntVector& v2);
402 
404  bool operator == (const IntVector& v1, const int* v2);
405 
407  bool operator == (const int* v1, const IntVector& v2);
408 
410  bool operator != (const IntVector& v1, const IntVector& v2);
411 
413  bool operator != (const IntVector& v1, const int* v2);
414 
416  bool operator != (const int* v1, const IntVector& v2);
417 
427  std::istream& operator >> (std::istream& in, IntVector &vector);
428 
437  std::ostream& operator << (std::ostream& out, const IntVector &vector);
438 
439  #ifdef UTIL_MPI
440 
443  template <> class MpiTraits<IntVector>
444  {
445  public:
446  static MPI::Datatype type;
447  static bool hasType;
448  };
449  #endif
450 
451  /*
452  * Serialize to/from an archive.
453  */
454  template <class Archive>
455  inline
456  void IntVector::serialize(Archive& ar, const unsigned int version)
457  {
458  ar & elem_[0];
459  ar & elem_[1];
460  ar & elem_[2];
461  }
462 
463 }
464 #endif
const int Dimension
Dimensionality of space.
Definition: Dimension.h:19
int & operator[](int i)
Return a reference to one element of the vector.
Definition: IntVector.h:257
static void commitMpiType()
Commit MPI datatype MpiTraits<IntVector>::type.
Definition: IntVector.cpp:92
IntVector & zero()
Set all elements of a 3D vector to zero.
Definition: IntVector.h:140
friend std::istream & operator>>(std::istream &in, IntVector &vector)
istream extractor for a IntVector.
Definition: IntVector.cpp:64
static bool hasType
Is the MPI type initialized?
Definition: IntVector.h:447
IntVector & operator=(const IntVector &v)
Copy assignment.
Definition: IntVector.h:167
IntVector & subtract(const IntVector &v1, const IntVector &v2)
Subtract vector v2 from v1.
Definition: IntVector.h:316
friend std::ostream & operator<<(std::ostream &out, const IntVector &vector)
ostream inserter for a IntVector.
Definition: IntVector.cpp:75
void operator+=(const IntVector &dv)
Add vector dv to this vector.
Definition: IntVector.h:199
IntVector(const int *v)
Construct IntVector from C int[3] array.
Definition: IntVector.h:114
IntVector(int x, int y, int z=0)
Construct IntVector from its coordinates.
Definition: IntVector.h:128
File containing preprocessor macros for error handling.
static MPI::Datatype type
MPI Datatype.
Definition: IntVector.h:446
void operator-=(const IntVector &dv)
Subtract vector dv from this vector.
Definition: IntVector.h:213
int square() const
Return square magnitude of this vector.
Definition: IntVector.h:273
int dot(const IntVector &v) const
Return dot product of this vector and vector v.
Definition: IntVector.h:282
Utility classes for scientific computation.
Definition: accumulators.mod:1
Default MpiTraits class.
Definition: MpiTraits.h:39
IntVector(const IntVector &v)
Copy constructor.
Definition: IntVector.h:90
IntVector & operator=(const int *v)
Assignment from C int[] array.
Definition: IntVector.h:180
void serialize(Archive &ar, const unsigned int version)
Serialize to/from an archive.
Definition: IntVector.h:456
static void initStatic()
Initialize static IntVector::Zero.
Definition: IntVector.cpp:113
static const IntVector Zero
Zero IntVector.
Definition: IntVector.h:364
IntVector & multiply(const IntVector &v, int s)
Multiply a vector v by a scalar s.
Definition: IntVector.h:333
An IntVector is an integer Cartesian vector.
Definition: IntVector.h:73
IntVector & add(const IntVector &v1, const IntVector &v2)
Add vectors v1 and v2.
Definition: IntVector.h:299
IntVector(int scalar)
Constructor, initialize all elements to the same scalar.
Definition: IntVector.h:102
IntVector & cross(const IntVector &v1, const IntVector &v2)
Calculate cross product of vectors v1 and v2.
Definition: IntVector.h:350
friend bool operator==(const IntVector &v1, const IntVector &v2)
Equality for IntVectors.
Definition: IntVector.cpp:24
void operator*=(int s)
Multiply this vector by scalar s.
Definition: IntVector.h:227
const int & operator[](int i) const
Return one Cartesian element by value.
Definition: IntVector.h:244
IntVector()
Default constructor.
Definition: IntVector.h:84
bool operator!=(const PointSymmetry &A, const PointSymmetry &B)
Are two PointSymmetry objects not equivalent?