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 <boost/container/small_vector.hpp>
27 #include <cstddef>
28 
29 namespace TiledArray {
30 namespace detail {
31 
33 
39 template <typename T>
40 class SizeArray {
41  private:
42  T* first_ = nullptr;
43  T* last_ = nullptr;
44 
45  public:
46  // type definitions
47  typedef T value_type;
48  typedef T* iterator;
49  typedef const T* const_iterator;
50  typedef T& reference;
51  typedef const T& const_reference;
52  typedef T* pointer;
53  typedef const T* const_pointer;
54  typedef std::size_t size_type;
55  typedef std::ptrdiff_t difference_type;
56 
57  // Compiler generated functions
58  SizeArray() = default;
59  SizeArray(const SizeArray&) = default;
60  SizeArray(SizeArray<T>&&) = default;
61  ~SizeArray() = default;
62 
63  SizeArray(pointer const first, pointer const last)
64  : first_(first), last_(last) {}
65 
66  SizeArray(pointer const first, const size_type n)
67  : first_(first), last_(first + n) {}
68 
69  void set(pointer const first, const size_type n) {
70  first_ = first;
71  last_ = first + n;
72  }
73 
74  void set(pointer const first, pointer const last) {
75  first_ = first;
76  last_ = last;
77  }
78 
80  TA_ASSERT(size() == other.size());
81  math::copy_vector(last_ - first_, other.data(), first_);
82  return *this;
83  }
84 
85  template <typename U>
86  SizeArray<T>& operator=(const U& other) {
87  TA_ASSERT(size() == size(other));
88  std::copy(std::begin(other), std::end(other), first_);
89  return *this;
90  }
91 
92  template <typename U>
93  operator std::vector<U>() const {
94  return std::vector<U>(first_, last_);
95  }
96 
97  template <typename U, std::size_t N>
98  operator std::array<U, N>() const {
99  TA_ASSERT(N == size());
100  std::array<U, N> temp;
101  math::copy_vector(last_ - first_, first_, temp.begin());
102 
103  return temp;
104  }
105 
106  template <typename U, std::size_t Size>
107  operator boost::container::small_vector<U, Size>() const {
108  return boost::container::small_vector<U, Size>(first_, last_);
109  }
110 
111  // iterator support
112  iterator begin() { return first_; }
113  const_iterator begin() const { return first_; }
114  iterator end() { return last_; }
115  const_iterator end() const { return last_; }
116 
117  // reverse iterator support
118  typedef std::reverse_iterator<iterator> reverse_iterator;
119  typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
120 
123  return const_reverse_iterator(end());
124  }
127  return const_reverse_iterator(begin());
128  }
129 
130  // operator[]
131  reference operator[](size_type i) { return first_[i]; }
132  const_reference operator[](size_type i) const { return first_[i]; }
133 
134  // at() with range check
136  TA_ASSERT(i < size());
137  return first_[i];
138  }
139 
141  TA_ASSERT(i < size());
142  return first_[i];
143  }
144 
145  // front() and back()
147  TA_ASSERT(first_);
148  return first_[0];
149  }
150 
152  TA_ASSERT(first_);
153  return first_[0];
154  }
155 
157  TA_ASSERT(first_);
158  return *(last_ - 1);
159  }
160 
162  TA_ASSERT(first_);
163  return *(last_ - 1);
164  }
165 
166  // size is constant
167  size_type size() const { return last_ - first_; }
168  bool empty() const { return first_ == NULL; }
169  size_type max_size() const { return last_ - first_; }
170 
171  // swap (note: linear complexity in N, constant for given instantiation)
172  void swap(SizeArray<T>& other) {
173  std::swap_ranges(begin(), end(), other.begin());
174  }
175 
176  // direct access to data (read-only)
177  const_pointer data() const { return first_; }
178 
179  // use array as C array (direct read/write access to data)
180  pointer data() { return first_; }
181 
182  // assign one value to all elements
183  void assign(const_reference value) {
184  math::fill_vector(last_ - first_, value, first_);
185  }
186 
187  // Comparison operators
188  template <typename U>
189  bool operator==(const SizeArray<U>& other) const {
190  if (first_ == other.data()) return true;
191 
192  const std::size_t n = size();
193  if (n != other.size()) return false;
194 
195  for (std::size_t i = 0; i < n; ++i)
196  if (first_[i] != other[i]) return false;
197 
198  return true;
199  }
200 
201  template <typename U>
202  bool operator!=(const SizeArray<U>& other) const {
203  if (first_ == other.data()) return false;
204 
205  const std::size_t n = size();
206  if (n != other.size()) return true;
207 
208  for (std::size_t i = 0; i < n; ++i)
209  if (first_[i] == other[i]) return false;
210 
211  return true;
212  }
213 
214  // Vector operations
215 
217 
224  template <typename Arg, typename Op>
225  void binary(const Arg* const arg, const Op& op) {
226  math::vector_op(op, last_ - first_, first_, arg);
227  }
228 
230 
240  template <typename Left, typename Right, typename Op>
241  void binary(const Left* const left, const Right* const right, const Op& op) {
242  math::vector_op(op, last_ - first_, first_, left, right);
243  }
244 
246 
251  template <typename Op>
252  void unary(const Op& op) {
253  math::vector_op(op, last_ - first_, first_);
254  }
255 
257 
265  template <typename Arg, typename Op>
266  void unary(const Arg* const arg, const Op& op) {
267  math::vector_op(op, last_ - first_, first_, arg);
268  }
269 
271 
282  template <typename Arg, typename Result, typename ReduceOp, typename JoinOp>
283  Result reduce(const Arg* const arg, Result result, const ReduceOp& reduce_op,
284  const JoinOp& join_op) const {
285  math::reduce_op(reduce_op, join_op, last_ - first_, result, first_, arg);
286  return result;
287  }
288 
290 
300  template <typename Result, typename ReduceOp, typename JoinOp>
301  Result reduce(Result result, const ReduceOp& reduce_op,
302  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,
320  const Op& op) {
321  math::row_reduce(last_ - first_, n, left, right, first_, op);
322  }
323 
325 
333  template <typename Arg, typename Op>
334  void row_reduce(const size_type n, const Arg* const arg, const Op& op) {
335  math::row_reduce(last_ - first_, n, arg, first_, op);
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,
351  const Op& op) {
352  math::col_reduce(m, last_ - first_, left, right, first_, op);
353  }
354 
356 
364  template <typename Arg, typename Op>
365  void col_reduce(const size_type m, const Arg* const arg, const Op& op) {
366  math::col_reduce(m, last_ - first_, arg, first_, op);
367  }
368 
370 
381  template <typename Left, typename Right, typename Op>
382  void outer(const size_type m, const size_type n, const Left* const left,
383  const Right* const right, const Op& op) {
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  TA_ASSERT((m * n) == size());
404  math::outer_fill(m, n, left, right, first_, op);
405  }
406 
408 
421  template <typename Left, typename Right, typename Base, typename Op>
422  void outer_fill(const size_type m, const size_type n, const Left* const left,
423  const Right* const right, const Base* const base,
424  const Op& op) {
425  TA_ASSERT((m * n) == size());
426  math::outer_fill(m, n, left, right, base, first_, op);
427  }
428 
429 }; // class SizeArray
430 
431 template <typename T>
432 inline std::vector<T> operator*(const Permutation& perm,
433  const SizeArray<T>& orig) {
434  std::vector<T> result(orig.size());
435  permute_array(perm, orig, result);
436  return result;
437 }
438 
439 template <typename T>
440 inline std::ostream& operator<<(std::ostream& os,
441  const SizeArray<T>& size_array) {
442  print_array(os, size_array);
443  return os;
444 }
445 
446 } // namespace detail
447 } // namespace TiledArray
448 
449 #endif // TILEDARRAY_SIZE_ARRAY_H__INCLUDED
const_reference at(size_type i) const
Definition: size_array.h:140
void set(pointer const first, pointer const last)
Definition: size_array.h:74
const_iterator end() const
Definition: size_array.h:115
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:175
std::reverse_iterator< iterator > reverse_iterator
Definition: size_array.h:118
std::reverse_iterator< const_iterator > const_reverse_iterator
Definition: size_array.h:119
::blas::Op Op
Definition: blas.h:46
Result reduce(Result result, const ReduceOp &reduce_op, const JoinOp &join_op) const
Unary reduction operation.
Definition: size_array.h:301
std::ptrdiff_t difference_type
Definition: size_array.h:55
void permute_array(const Perm &perm, const Arg &arg, Result &result)
Create a permuted copy of an array.
Definition: permutation.h:66
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:239
std::vector< T > operator*(const Permutation &perm, const SizeArray< T > &orig)
Definition: size_array.h:432
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
Permutation of a sequence of objects indexed by base-0 indices.
Definition: permutation.h:130
void col_reduce(const size_type m, const Arg *const arg, const Op &op)
Columns reduce operation.
Definition: size_array.h:365
void unary(const Op &op)
Unary vector operation.
Definition: size_array.h:252
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:628
void vector_op(Op &&op, const std::size_t n, Result *const result, const Args *const ... args)
Definition: vector_op.h:472
bool operator==(const SizeArray< U > &other) const
Definition: size_array.h:189
std::ostream & operator<<(std::ostream &os, const TileReference< Impl > &a)
redirect operator to std::ostream for TileReference objects
Definition: array_impl.h:132
void fill_vector(const std::size_t n, const Arg &arg, Result *const result)
Definition: vector_op.h:666
const_reverse_iterator rend() const
Definition: size_array.h:126
const_pointer data() const
Definition: size_array.h:177
size_type size() const
Definition: size_array.h:167
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:382
reference operator[](size_type i)
Definition: size_array.h:131
#define TA_ASSERT(EXPR,...)
Definition: error.h:39
const_reference operator[](size_type i) const
Definition: size_array.h:132
void row_reduce(const size_type n, const Arg *const arg, const Op &op)
Row reduce operation.
Definition: size_array.h:334
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.
SizeArray< T > & operator=(const U &other)
Definition: size_array.h:86
SizeArray(pointer const first, const size_type n)
Definition: size_array.h:66
SizeArray< T > & operator=(const SizeArray< T > &other)
Definition: size_array.h:79
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:422
const_iterator begin() const
Definition: size_array.h:113
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
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
void assign(const_reference value)
Definition: size_array.h:183
const_reference front() const
Definition: size_array.h:151
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:48
const_reverse_iterator rbegin() const
Definition: size_array.h:122
std::enable_if<!(std::is_same< Arg, Result >::value &&detail::is_scalar_v< Arg >)>::type copy_vector(const std::size_t n, const Arg *const arg, Result *const result)
Definition: vector_op.h:648
SizeArray(const SizeArray &)=default
void binary(const Left *const left, const Right *const right, const Op &op)
Binary vector operation.
Definition: size_array.h:241
void swap(SizeArray< T > &other)
Definition: size_array.h:172
void binary(const Arg *const arg, const Op &op)
Binary vector operation.
Definition: size_array.h:225
bool operator!=(const SizeArray< U > &other) const
Definition: size_array.h:202
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
SizeArray(pointer const first, pointer const last)
Definition: size_array.h:63
void unary(const Arg *const arg, const Op &op)
Unary vector operation.
Definition: size_array.h:266
size_type max_size() const
Definition: size_array.h:169
const_reference back() const
Definition: size_array.h:161
reverse_iterator rend()
Definition: size_array.h:125
void set(pointer const first, const size_type n)
Definition: size_array.h:69
reference at(size_type i)
Definition: size_array.h:135
SizeArray(SizeArray< T > &&)=default
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.
reverse_iterator rbegin()
Definition: size_array.h:121