QMCPACK
PooledData.h
Go to the documentation of this file.
1 //////////////////////////////////////////////////////////////////////////////////////
2 // This file is distributed under the University of Illinois/NCSA Open Source License.
3 // See LICENSE file in top directory for details.
4 //
5 // Copyright (c) 2016 Jeongnim Kim and QMCPACK developers.
6 //
7 // File developed by: Ken Esler, kpesler@gmail.com, University of Illinois at Urbana-Champaign
8 // Jeongnim Kim, jeongnim.kim@gmail.com, University of Illinois at Urbana-Champaign
9 // Jeremy McMinnis, jmcminis@gmail.com, University of Illinois at Urbana-Champaign
10 // Ye Luo, yeluo@anl.gov, Argonne National Laboratory
11 // Mark A. Berrill, berrillma@ornl.gov, Oak Ridge National Laboratory
12 //
13 // File created by: Jeongnim Kim, jeongnim.kim@gmail.com, University of Illinois at Urbana-Champaign
14 //////////////////////////////////////////////////////////////////////////////////////
15 
16 
17 /** @file PooledData.h
18  * @brief Define a serialized buffer to store anonymous data
19  *
20  * JK: Remove iterator version on 2016-01-04
21  */
22 #ifndef QMCPLUSPLUS_POOLEDDATA_H
23 #define QMCPLUSPLUS_POOLEDDATA_H
24 #include <vector>
25 #include <complex>
26 #include <limits>
27 #include <iterator>
28 
29 template<class T>
30 struct PooledData
31 {
32  using value_type = T;
33  using size_type = typename std::vector<T>::size_type;
34 
36  std::vector<T> myData;
37 
38  ///default constructor
39  inline PooledData() : Current(0) {}
40 
41  ///constructor with a size
42  inline PooledData(size_type n) : Current(0) { myData.resize(n, T()); }
43 
44  ///return the size of the data
45  inline size_type byteSize() const { return sizeof(T) * myData.size(); }
46 
47  ///return the size of the data
48  inline size_type size() const { return myData.size(); }
49 
50  //inline void clear() { std::vector<T>::clear(); Current=0;}
51  inline size_type current() const { return Current; }
52 
53  /** set the Current to a cursor
54  * @param cur locator to which Current is assigned
55  */
56  inline void rewind(size_type cur = 0) { Current = cur; }
57 
58  ///return the starting iterator
59  inline typename std::vector<T>::iterator begin() { return myData.begin(); }
60  ///return the ending iterator
61  inline typename std::vector<T>::iterator end() { return myData.end(); }
62 
63  /*@{ matching functions to std::vector functions */
64  ///clear the data and set Current=0
65  inline void clear()
66  {
67  myData.clear();
68  Current = 0;
69  }
70  ///reserve the memory using std::vector<T>::reserve
71  inline void reserve(size_type n)
72  {
73  myData.reserve(n);
74  Current = 0;
75  }
76  ///resize
77  inline void resize(size_type n, T val = T())
78  {
79  myData.resize(n, val);
80  Current = 0;
81  }
82  ///return i-th value
83  inline T operator[](size_type i) const { return myData[i]; }
84  ///return i-th value to assign
85  inline T& operator[](size_type i) { return myData[i]; }
86  /*@}*/
87 
88  inline void add(T& x)
89  {
90  Current++;
91  myData.push_back(x);
92  }
93 
94  inline void add(std::complex<T>& x)
95  {
96  Current += 2;
97  myData.push_back(x.real());
98  myData.push_back(x.imag());
99  }
100 
101  template<class T1>
102  inline void add(T1& x)
103  {
104  Current++;
105  myData.push_back(static_cast<T>(x));
106  }
107 
108  template<class _InputIterator>
109  inline void add(_InputIterator first, _InputIterator last)
110  {
111  Current += last - first;
112  myData.insert(myData.end(), first, last);
113  }
114 
115  template<typename T1>
116  inline void add(T1* first, T1* last)
117  {
118  Current += last - first;
119  myData.insert(myData.end(), first, last);
120  }
121 
122  template<typename T1>
123  inline void add(std::complex<T1>* first, std::complex<T1>* last)
124  {
125  size_type dn = 2 * (last - first);
126  T1* t = reinterpret_cast<T1*>(first);
127  myData.insert(myData.end(), t, t + dn);
128  Current += dn;
129  }
130 
131  inline void get(T& x) { x = myData[Current++]; }
132 
133  inline void get(std::complex<T>& x)
134  {
135  x = std::complex<T>(myData[Current], myData[Current + 1]);
136  Current += 2;
137  }
138 
139  template<class T1>
140  inline void get(T1& x)
141  {
142  x = static_cast<T1>(myData[Current++]);
143  }
144 
145  template<class _OutputIterator>
146  inline void get(_OutputIterator first, _OutputIterator last)
147  {
148  size_type now = Current;
149  Current += last - first;
150  copy(myData.begin() + now, myData.begin() + Current, first);
151  }
152 
153  template<typename T1>
154  inline void get(T1* first, T1* last)
155  {
156  size_type now = Current;
157  Current += last - first;
158  std::copy(myData.begin() + now, myData.begin() + Current, first);
159  }
160 
161  template<typename T1>
162  inline void get(std::complex<T1>* first, std::complex<T1>* last)
163  {
164  while (first != last)
165  {
166  (*first) = std::complex<T1>(myData[Current], myData[Current + 1]);
167  ++first;
168  Current += 2;
169  }
170  }
171 
172  inline void put(T& x) { myData[Current++] = x; }
173 
174  inline void put(std::complex<T>& x)
175  {
176  myData[Current++] = x.real();
177  myData[Current++] = x.imag();
178  }
179 
180  template<typename T1>
181  inline void put(T1& x)
182  {
183  myData[Current++] = static_cast<T>(x);
184  }
185 
186  template<class _InputIterator>
187  inline void put(_InputIterator first, _InputIterator last)
188  {
189  copy(first, last, myData.begin() + Current);
190  Current += last - first;
191  }
192 
193  template<typename T1>
194  inline void put(T1* first, T1* last)
195  {
196  std::copy(first, last, myData.begin() + Current);
197  Current += last - first;
198  }
199 
200  template<typename T1>
201  inline void put(std::complex<T1>* first, std::complex<T1>* last)
202  {
203  while (first != last)
204  {
205  myData[Current++] = (*first).real();
206  myData[Current++] = (*first).imag();
207  ++first;
208  }
209  }
210 
211  /** return the address of the first element **/
212  inline T* data() { return &(myData[0]); }
213 
214  inline void print(std::ostream& os) { copy(myData.begin(), myData.end(), std::ostream_iterator<T>(os, " ")); }
215 
216  template<class Msg>
217  inline Msg& putMessage(Msg& m)
218  {
219  m.Pack(&(myData[0]), myData.size());
220  return m;
221  }
222 
223  template<class Msg>
224  inline Msg& getMessage(Msg& m)
225  {
226  m.Unpack(&(myData[0]), myData.size());
227  return m;
228  }
229 
231  {
232  for (int i = 0; i < myData.size(); ++i)
233  myData[i] += s[i];
234  return *this;
235  }
236  inline PooledData<T>& operator*=(T scale)
237  {
238  for (int i = 0; i < myData.size(); ++i)
239  myData[i] *= scale;
240  return *this;
241  }
242 };
243 
244 /** operator to check if two buffers are identical */
245 template<class T>
246 bool operator==(const PooledData<T>& a, const PooledData<T>& b)
247 {
248  if (a.size() != b.size())
249  return false;
250  //if(a.Current != b.Current) return false;
251  for (typename PooledData<T>::size_type i = 0; i < a.size(); ++i)
252  {
253  if (std::abs(a[i] - b[i]) > std::numeric_limits<T>::epsilon())
254  return false;
255  }
256  return true;
257 }
258 
259 /** operator to check if two buffers are different */
260 template<class T>
261 bool operator!=(const PooledData<T>& a, const PooledData<T>& b)
262 {
263  if (a.size() != b.size())
264  return true;
265  //if(a.Current != b.Current) return true;
266  for (typename PooledData<T>::size_type i = 0; i < a.size(); ++i)
267  {
268  if (std::abs(a[i] - b[i]) > std::numeric_limits<T>::epsilon())
269  return true;
270  }
271  return false;
272 }
273 #endif
size_type size() const
return the size of the data
Definition: PooledData.h:48
void add(T1 *first, T1 *last)
Definition: PooledData.h:116
void add(std::complex< T1 > *first, std::complex< T1 > *last)
Definition: PooledData.h:123
void put(T1 *first, T1 *last)
Definition: PooledData.h:194
void put(_InputIterator first, _InputIterator last)
Definition: PooledData.h:187
T * data()
return the address of the first element
Definition: PooledData.h:212
MakeReturn< UnaryNode< FnFabs, typename CreateLeaf< Vector< T1, C1 > >::Leaf_t > >::Expression_t abs(const Vector< T1, C1 > &l)
std::vector< T >::iterator end()
return the ending iterator
Definition: PooledData.h:61
void put(T1 &x)
Definition: PooledData.h:181
PooledData< T > & operator*=(T scale)
Definition: PooledData.h:236
void put(std::complex< T1 > *first, std::complex< T1 > *last)
Definition: PooledData.h:201
std::vector< T >::iterator begin()
return the starting iterator
Definition: PooledData.h:59
void rewind(size_type cur=0)
set the Current to a cursor
Definition: PooledData.h:56
size_type Current
Definition: PooledData.h:35
void copy(const Array< T1, 3 > &src, Array< T2, 3 > &dest)
Definition: Blitz.h:639
void add(T &x)
Definition: PooledData.h:88
void add(std::complex< T > &x)
Definition: PooledData.h:94
void clear()
clear the data and set Current=0
Definition: PooledData.h:65
PooledData(size_type n)
constructor with a size
Definition: PooledData.h:42
size_type current() const
Definition: PooledData.h:51
T & operator[](size_type i)
return i-th value to assign
Definition: PooledData.h:85
void add(_InputIterator first, _InputIterator last)
Definition: PooledData.h:109
void resize(size_type n, T val=T())
resize
Definition: PooledData.h:77
size_type byteSize() const
return the size of the data
Definition: PooledData.h:45
T operator[](size_type i) const
return i-th value
Definition: PooledData.h:83
void reserve(size_type n)
reserve the memory using std::vector<T>::reserve
Definition: PooledData.h:71
bool operator==(const PooledData< T > &a, const PooledData< T > &b)
operator to check if two buffers are identical
Definition: PooledData.h:246
Msg & getMessage(Msg &m)
Definition: PooledData.h:224
PooledData()
default constructor
Definition: PooledData.h:39
std::vector< T > myData
Definition: PooledData.h:36
void put(T &x)
Definition: PooledData.h:172
void print(std::ostream &os)
Definition: PooledData.h:214
typename std::vector< T >::size_type size_type
Definition: PooledData.h:33
void put(std::complex< T > &x)
Definition: PooledData.h:174
Msg & putMessage(Msg &m)
Definition: PooledData.h:217
bool operator!=(const PooledData< T > &a, const PooledData< T > &b)
operator to check if two buffers are different
Definition: PooledData.h:261
void add(T1 &x)
Definition: PooledData.h:102
PooledData< T > & operator+=(const PooledData< T > &s)
Definition: PooledData.h:230