TiledArray  0.7.0
size_array.h
Go to the documentation of this file.
1 /*
2  * This file is a part of TiledArray.
3  * Copyright (C) 2013 Virginia Tech
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  */
19 
20 #ifndef TILEDARRAY_SIZE_ARRAY_H__INCLUDED
21 #define TILEDARRAY_SIZE_ARRAY_H__INCLUDED
22 
23 #include <TiledArray/math/outer.h>
25 #include <TiledArray/permutation.h>
26 #include <cstddef>
27 
28 namespace TiledArray {
29  namespace detail {
30 
32 
38  template <typename T>
39  class SizeArray {
40  private:
41  T* first_ = nullptr;
42  T* last_ = nullptr;
43 
44  public:
45  // type definitions
46  typedef T value_type;
47  typedef T* iterator;
48  typedef const T* const_iterator;
49  typedef T& reference;
50  typedef const T& const_reference;
51  typedef T* pointer;
52  typedef const T* const_pointer;
53  typedef std::size_t size_type;
54  typedef std::ptrdiff_t difference_type;
55 
56  // Compiler generated functions
57  SizeArray() = default;
58  SizeArray(const SizeArray&) = default;
59  SizeArray(SizeArray<T>&&) = default;
60  ~SizeArray() = default;
61 
62  SizeArray(pointer const first, pointer const last) :
63  first_(first), last_(last)
64  { }
65 
66  SizeArray(pointer const first, const size_type n) :
67  first_(first), last_(first + n)
68  { }
69 
70 
71  void set(pointer const first, const size_type n) {
72  first_ = first;
73  last_ = first + n;
74  }
75 
76  void set(pointer const first, pointer const last) {
77  first_ = first;
78  last_ = last;
79  }
80 
82  TA_ASSERT(size() == other.size());
83  math::copy_vector(last_ - first_, other.data(), first_);
84  return *this;
85  }
86 
87  template <typename U>
88  SizeArray<T>& operator=(const U& other) {
89  TA_ASSERT(size() == size(other));
90  std::copy(std::begin(other), std::end(other), first_);
91  return *this;
92  }
93 
94  template <typename U>
95  operator std::vector<U> () const {
96  return std::vector<U>(first_, last_);
97  }
98 
99  template <typename U, std::size_t N>
100  operator std::array<U, N> () const {
101  TA_ASSERT(N == size());
102  std::array<U, N> temp;
103  math::copy_vector(last_ - first_, first_, temp.begin());
104 
105  return temp;
106  }
107 
108  // iterator support
109  iterator begin() { return first_; }
110  const_iterator begin() const { return first_; }
111  iterator end() { return last_; }
112  const_iterator end() const { return last_; }
113 
114  // reverse iterator support
115  typedef std::reverse_iterator<iterator> reverse_iterator;
116  typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
117 
122 
123  // operator[]
124  reference operator[](size_type i) { return first_[i]; }
125  const_reference operator[](size_type i) const { return first_[i]; }
126 
127  // at() with range check
129  TA_ASSERT(i < size());
130  return first_[i];
131  }
132 
134  TA_ASSERT(i < size());
135  return first_[i];
136  }
137 
138  // front() and back()
140  TA_ASSERT(first_);
141  return first_[0];
142  }
143 
145  TA_ASSERT(first_);
146  return first_[0];
147  }
148 
150  TA_ASSERT(first_);
151  return *(last_ - 1);
152  }
153 
155  TA_ASSERT(first_);
156  return *(last_ - 1);
157  }
158 
159  // size is constant
160  size_type size() const { return last_ - first_; }
161  bool empty() const { return first_ == NULL; }
162  size_type max_size() const { return last_ - first_; }
163 
164  // swap (note: linear complexity in N, constant for given instantiation)
165  void swap (SizeArray<T>& other) {
166  std::swap_ranges(begin(), end(), other.begin());
167  }
168 
169  // direct access to data (read-only)
170  const_pointer data() const { return first_; }
171 
172  // use array as C array (direct read/write access to data)
173  pointer data() { return first_; }
174 
175  // assign one value to all elements
176  void assign (const_reference value) { math::fill_vector(last_ - first_, value, first_); }
177 
178  // Comparison operators
179  template <typename U>
180  bool operator==(const SizeArray<U>& other) const {
181  if(first_ == other.data())
182  return true;
183 
184  const std::size_t n = size();
185  if(n != other.size())
186  return false;
187 
188  for(std::size_t i = 0; i < n; ++i)
189  if(first_[i] != other[i])
190  return false;
191 
192  return true;
193  }
194 
195  template <typename U>
196  bool operator!=(const SizeArray<U>& other) const {
197  if(first_ == other.data())
198  return false;
199 
200  const std::size_t n = size();
201  if(n != other.size())
202  return true;
203 
204  for(std::size_t i = 0; i < n; ++i)
205  if(first_[i] == other[i])
206  return false;
207 
208  return true;
209  }
210 
211  // Vector operations
212 
214 
221  template <typename Arg, typename Op>
222  void binary(const Arg* const arg, const Op& op) {
223  math::vector_op(op, last_ - first_, first_, arg);
224  }
225 
227 
237  template <typename Left, typename Right, typename Op>
238  void binary(const Left* const left, const Right* const right, const Op& op) {
239  math::vector_op(op, last_ - first_, first_, left, right);
240  }
241 
243 
248  template <typename Op>
249  void unary(const Op& op) {
250  math::vector_op(op, last_ - first_, first_);
251  }
252 
254 
262  template <typename Arg, typename Op>
263  void unary(const Arg* const arg, const Op& op) {
264  math::vector_op(op, last_ - first_, first_, arg);
265  }
266 
268 
282  template <typename Arg, typename Result, typename ReduceOp, typename JoinOp>
283  Result reduce(const Arg* const arg, Result result, const ReduceOp& reduce_op, const JoinOp& join_op) const {
284  math::reduce_op(reduce_op, join_op, last_ - first_, result, first_, arg);
285  return result;
286  }
287 
289 
301  template <typename Result, typename ReduceOp, typename JoinOp>
302  Result reduce(Result result, const ReduceOp& reduce_op, const JoinOp& join_op) const {
303  math::reduce_op(reduce_op, join_op, last_ - first_, result, first_);
304  return result;
305  }
306 
308 
318  template <typename Left, typename Right, typename Op>
319  void row_reduce(const size_type n, const Left* const left, const Right* right, const Op& op) {
320  math::row_reduce(last_ - first_, n, left, right, first_, op);
321  }
322 
324 
332  template <typename Arg, typename Op>
333  void row_reduce(const size_type n, const Arg* const arg, const Op& op) {
334  math::row_reduce(last_ - first_, n, arg, first_, op);
335  }
336 
337 
339 
349  template <typename Left, typename Right, typename Op>
350  void col_reduce(const size_type m, const Left* const left, const Right* right, const Op& op) {
351  math::col_reduce(m, last_ - first_, left, right, first_, op);
352  }
353 
355 
363  template <typename Arg, typename Op>
364  void col_reduce(const size_type m, const Arg* const arg, const Op& op) {
365  math::col_reduce(m, last_ - first_, arg, first_, op);
366  }
367 
369 
380  template <typename Left, typename Right, typename Op>
381  void outer(const size_type m, const size_type n, const Left* const left,
382  const Right* const right, const Op& op)
383  {
384  TA_ASSERT((m * n) == size());
385  math::outer(m, n, left, right, first_, op);
386  }
387 
389 
400  template <typename Left, typename Right, typename Op>
401  void outer_fill(const size_type m, const size_type n, const Left* const left,
402  const Right* const right, const Op& op)
403  {
404  TA_ASSERT((m * n) == size());
405  math::outer_fill(m, n, left, right, first_, op);
406  }
407 
409 
422  template <typename Left, typename Right, typename Base, typename Op>
423  void outer_fill(const size_type m, const size_type n, const Left* const left,
424  const Right* const right, const Base* const base, const Op& op)
425  {
426  TA_ASSERT((m * n) == size());
427  math::outer_fill(m, n, left, right, base, first_, op);
428  }
429 
430  // Common reduction operations
431 
432 
433 
434  }; // class SizeArray
435 
436  template <typename T>
437  inline std::vector<T> operator*(const Permutation& perm, const SizeArray<T>& orig) {
438  std::vector<T> result(orig.size());
439  permute_array(perm, orig, result);
440  return result;
441  }
442 
443  template <typename T>
444  inline std::ostream& operator<<(std::ostream& os, const SizeArray<T>& size_array) {
445  print_array(os, size_array);
446  return os;
447  }
448 
449  } // namespace detail
450 } // namespace TiledArray
451 
452 #endif // TILEDARRAY_SIZE_ARRAY_H__INCLUDED
const_reference back() const
Definition: size_array.h:154
void row_reduce(const std::size_t m, const std::size_t n, const Left *MADNESS_RESTRICT const left, const Right *MADNESS_RESTRICT const right, Result *MADNESS_RESTRICT const result, const Op &op)
Reduce the rows of a matrix.
std::enable_if<!(std::is_same< Arg, Result >::value &&std::is_scalar< Arg >::value)>::type copy_vector(const std::size_t n, const Arg *const arg, Result *const result)
Definition: vector_op.h:659
SizeArray< T > & operator=(const SizeArray< T > &other)
Definition: size_array.h:81
size_type max_size() const
Definition: size_array.h:162
const_iterator begin() const
Definition: size_array.h:110
void outer_fill(const size_type m, const size_type n, const Left *const left, const Right *const right, const Op &op)
Outer fill operation.
Definition: size_array.h:401
void permute_array(const Perm &perm, const Arg &arg, Result &result)
Create a permuted copy of an array.
Definition: permutation.h:57
std::reverse_iterator< iterator > reverse_iterator
Definition: size_array.h:115
void col_reduce(const size_type m, const Left *const left, const Right *right, const Op &op)
Column reduce operation.
Definition: size_array.h:350
std::ptrdiff_t difference_type
Definition: size_array.h:54
const_reference at(size_type i) const
Definition: size_array.h:133
const_pointer data() const
Definition: size_array.h:170
reference operator[](size_type i)
Definition: size_array.h:124
void col_reduce(const size_type m, const Arg *const arg, const Op &op)
Columns reduce operation.
Definition: size_array.h:364
reverse_iterator rbegin()
Definition: size_array.h:118
void row_reduce(const size_type n, const Left *const left, const Right *right, const Op &op)
Row reduce operation.
Definition: size_array.h:319
void outer(const std::size_t m, const std::size_t n, const X *const x, const Y *const y, A *a, const Op &op)
Compute the outer of x and y to modify a.
Definition: outer.h:248
void fill_vector(const std::size_t n, const Arg &arg, Result *const result)
Definition: vector_op.h:680
void assign(const_reference value)
Definition: size_array.h:176
Result reduce(Result result, const ReduceOp &reduce_op, const JoinOp &join_op) const
Unary reduction operation.
Definition: size_array.h:302
void outer_fill(const size_type m, const size_type n, const Left *const left, const Right *const right, const Base *const base, const Op &op)
Outer operation.
Definition: size_array.h:423
void row_reduce(const size_type n, const Arg *const arg, const Op &op)
Row reduce operation.
Definition: size_array.h:333
void outer_fill(const std::size_t m, const std::size_t n, const X *const x, const Y *const y, A *a, const Op &op)
Compute and store outer of x and y in a.
Definition: outer.h:180
reverse_iterator rend()
Definition: size_array.h:120
void vector_op(Op &&op, const std::size_t n, Result *const result, const Args *const ... args)
Definition: vector_op.h:478
void print_array(std::ostream &out, const A &a, const std::size_t n)
Print the content of an array like object.
Definition: utility.h:153
#define TA_ASSERT(a)
Definition: error.h:107
SizeArray(pointer const first, const size_type n)
Definition: size_array.h:66
void binary(const Left *const left, const Right *const right, const Op &op)
Binary vector operation.
Definition: size_array.h:238
void reduce_op(ReduceOp &&reduce_op, JoinOp &&join_op, const Result &identity, const std::size_t n, Result &result, const Args *const ... args)
Definition: vector_op.h:640
reference at(size_type i)
Definition: size_array.h:128
void unary(const Arg *const arg, const Op &op)
Unary vector operation.
Definition: size_array.h:263
size_type size() const
Definition: size_array.h:160
SizeArray< T > & operator=(const U &other)
Definition: size_array.h:88
DistArray< Tile, Policy > copy(const DistArray< Tile, Policy > &a)
Definition: utils.h:58
const_iterator end() const
Definition: size_array.h:112
void binary(const Arg *const arg, const Op &op)
Binary vector operation.
Definition: size_array.h:222
std::vector< T > operator*(const Permutation &perm, const SizeArray< T > &orig)
Definition: size_array.h:437
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition: size_array.h:116
Permutation of a sequence of objects indexed by base-0 indices.
Definition: permutation.h:119
Result reduce(const Arg *const arg, Result result, const ReduceOp &reduce_op, const JoinOp &join_op) const
Binary reduction operation.
Definition: size_array.h:283
bool operator!=(const SizeArray< U > &other) const
Definition: size_array.h:196
const_reference operator[](size_type i) const
Definition: size_array.h:125
void swap(SizeArray< T > &other)
Definition: size_array.h:165
const_reverse_iterator rbegin() const
Definition: size_array.h:119
void unary(const Op &op)
Unary vector operation.
Definition: size_array.h:249
const_reverse_iterator rend() const
Definition: size_array.h:121
void outer(const size_type m, const size_type n, const Left *const left, const Right *const right, const Op &op)
Outer operation.
Definition: size_array.h:381
bool operator==(const SizeArray< U > &other) const
Definition: size_array.h:180
void col_reduce(const std::size_t m, const std::size_t n, const Left *MADNESS_RESTRICT const left, const Right *MADNESS_RESTRICT const right, Result *MADNESS_RESTRICT const result, const Op &op)
Reduce the columns of a matrix.
const_reference front() const
Definition: size_array.h:144
SizeArray(pointer const first, pointer const last)
Definition: size_array.h:62