xerus
a general purpose tensor library
basicArraySupport.h
Go to the documentation of this file.
1 // Xerus - A General Purpose Tensor Library
2 // Copyright (C) 2014-2017 Benjamin Huber and Sebastian Wolf.
3 //
4 // Xerus is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as published
6 // by the Free Software Foundation, either version 3 of the License,
7 // or (at your option) any later version.
8 //
9 // Xerus is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
13 //
14 // You should have received a copy of the GNU Affero General Public License
15 // along with Xerus. If not, see <http://www.gnu.org/licenses/>.
16 //
17 // For further information on Xerus visit https://libXerus.org
18 // or contact us at contact@libXerus.org.
19 
25 #pragma once
26 
27 #include <type_traits>
28 #include <memory>
29 #include <cstring>
30 
31 #include "standard.h"
32 
33 namespace xerus {
34  namespace misc {
35  template<class T>
36  std::unique_ptr<T[]> make_unique_array(T* _ptr) {
37  return std::unique_ptr<T[]>(_ptr);
38  }
39 
44  template <typename T, typename std::enable_if<std::is_trivial<T>::value, int>::type = 0>
45  inline void set_zero(T* const __restrict _x, const size_t _n) noexcept {
46  memset(_x, 0, _n*sizeof(T));
47  }
48 
49 
54  template <typename T, typename std::enable_if<std::is_trivial<T>::value, int>::type = 0>
55  inline void copy(T* const __restrict _dest, const T* const __restrict _src, const size_t _n) noexcept {
56  memcpy(_dest, _src, _n*sizeof(T));
57  }
58 
59 
64  template <typename T, typename std::enable_if<std::is_trivial<T>::value, int>::type = 0>
65  inline void copy_inplace(T* const _x, const T* const _y, const size_t _n) noexcept {
66  memmove(_x, _y, _n*sizeof(T));
67  }
68 
69 
73  template <typename T>
74  inline void copy_scaled(T* const __restrict _x, const T _alpha, const T* const _y, const size_t _n) noexcept {
75  for(size_t i = 0; i < _n; ++i) {
76  _x[i] = _alpha*_y[i];
77  }
78  }
79 
80 
84  template <typename T>
85  inline void scale(T* const __restrict _x, const T _alpha, const size_t _n) noexcept {
86  for(size_t i = 0; i < _n; i++) {
87  _x[i] *= _alpha;
88  }
89  }
90 
91 
95  template <typename T>
96  inline void add(T* const __restrict _x, const T* const __restrict _y, const size_t _n) noexcept {
97  for(size_t i = 0; i < _n; i++) {
98  _x[i] += _y[i];
99  }
100  }
101 
102 
106  template <typename T>
107  inline void add_scaled(T* const __restrict _x, const T _alpha, const T* const __restrict _y, const size_t _n) noexcept {
108  for(size_t i = 0; i < _n; i++) {
109  _x[i] += _alpha*_y[i];
110  }
111  }
112  }
113 }
void add(T *const __restrict _x, const T *const __restrict _y, const size_t _n) noexcept
Adds _n entries of _y to the ones of _x. I.e. x += y.
void copy_inplace(T *const _x, const T *const _y, const size_t _n) noexcept
Copys _n entries from _y to _x, allowing the accessed memry regions of _y and _x to overlap...
std::unique_ptr< T[]> make_unique_array(T *_ptr)
The main namespace of xerus.
Definition: basic.h:37
void copy_scaled(T *const __restrict _x, const T _alpha, const T *const _y, const size_t _n) noexcept
Copys _n entries from _y to _x, simulationously scaling each entry by the factor _alpha. I.e x = alpha*y.
void scale(T *const __restrict _x, const T _alpha, const size_t _n) noexcept
Scales _n entries of _x by the factor _alpha. I.e. x = alpha*x.
void copy(T *const __restrict _dest, const T *const __restrict _src, const size_t _n) noexcept
Copys _n entries from _y to _x, where _y and _x must be disjunkt memory regions.
void set_zero(T *const __restrict _x, const size_t _n) noexcept
Sets all entries equal to zero.
void add_scaled(T *const __restrict _x, const T _alpha, const T *const __restrict _y, const size_t _n) noexcept
Adds _n entries of _y, scaled by _alpha to the ones of _x. I.e. x += alpha*y.
Header file for global shorthand notations of elementary integer types and attribute lists...