basicArraySupport.h 3.6 KB
Newer Older
1
// Xerus - A General Purpose Tensor Library
2
// Copyright (C) 2014-2017 Benjamin Huber and Sebastian Wolf. 
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
// 
// Xerus is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License,
// or (at your option) any later version.
// 
// Xerus is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
// 
// You should have received a copy of the GNU Affero General Public License
// along with Xerus. If not, see <http://www.gnu.org/licenses/>.
//
// For further information on Xerus visit https://libXerus.org 
// or contact us at contact@libXerus.org.

/**
* @file
* @brief Header file for the low level array support function.
*/

#pragma once

#include <type_traits>
Sebastian Wolf's avatar
Sebastian Wolf committed
28
#include <memory>
29 30 31 32 33 34
#include <cstring>

#include "standard.h"

namespace xerus {
	namespace misc {
Sebastian Wolf's avatar
Sebastian Wolf committed
35 36 37 38 39
		template<class T>
		std::unique_ptr<T[]> make_unique_array(T* _ptr) {
			return std::unique_ptr<T[]>(_ptr);
		}
		
40 41 42 43 44
		/** 
		* @brief Sets all entries equal to zero.
		* @details This is done directly by memset and therefore requires the type to be trivial.
		*/
		template <typename T, typename std::enable_if<std::is_trivial<T>::value, int>::type = 0>
Benjamin Huber's avatar
Benjamin Huber committed
45
		inline void set_zero(T* const __restrict _x, const size_t _n) noexcept {
46 47 48 49 50 51 52 53
			memset(_x, 0, _n*sizeof(T));
		}
		
		
		/** 
		* @brief Copys @a _n entries from @a _y to @a _x, where @a _y and @a _x must be disjunkt memory regions.
		* @details For trivial copyable types this is only a slim wrapper for memcpy.
		*/
54
		template <typename T, typename std::enable_if<std::is_trivial<T>::value, int>::type = 0>
Benjamin Huber's avatar
Benjamin Huber committed
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 60 61 62 63
		}
		
		
		/** 
		* @brief Copys @a _n entries from @a _y to @a _x, allowing the accessed memry regions of @a _y and @a _x to overlap.
		* @details For trivial copyable types this is only a slim wrapper for memmove.
		*/
64
		template <typename T, typename std::enable_if<std::is_trivial<T>::value, int>::type = 0>
Benjamin Huber's avatar
Benjamin Huber committed
65
		inline void copy_inplace(T* const _x, const T* const _y, const size_t _n) noexcept {
66 67 68 69 70 71 72 73
			memmove(_x, _y, _n*sizeof(T));
		}
		
		
		/** 
		* @brief Copys @a _n entries from @a _y to @a _x, simulationously scaling each entry by the factor @a _alpha. I.e x = alpha*y.
		*/
		template <typename T>
Benjamin Huber's avatar
Benjamin Huber committed
74
		inline void copy_scaled(T* const __restrict _x, const T _alpha, const T* const _y, const size_t _n) noexcept {
75 76 77 78 79 80 81 82 83 84
			for(size_t i = 0; i < _n; ++i) {
				_x[i] = _alpha*_y[i];
			}
		}
		
		
		/** 
		* @brief Scales @a _n entries of @a _x by the factor @a _alpha. I.e. x = alpha*x.
		*/
		template <typename T>
Benjamin Huber's avatar
Benjamin Huber committed
85
		inline void scale(T* const __restrict _x, const T _alpha, const size_t _n) noexcept {
86 87 88 89 90 91 92 93 94 95
			for(size_t i = 0; i < _n; i++) {
				_x[i] *= _alpha;
			}
		}
		
		
		/** 
		* @brief Adds @a _n entries of @a _y to the ones of @a _x. I.e. x += y.
		*/
		template <typename T>
Benjamin Huber's avatar
Benjamin Huber committed
96
		inline void add(T* const __restrict _x, const T* const __restrict _y, const size_t _n) noexcept {
97 98 99 100 101 102 103 104 105 106
			for(size_t i = 0; i < _n; i++) {
				_x[i] += _y[i];
			}
		}
		
		
		/** 
		* @brief Adds @a _n entries of @a _y, scaled by @a _alpha to the ones of @a _x. I.e. x += alpha*y.
		*/
		template <typename T>
Benjamin Huber's avatar
Benjamin Huber committed
107
		inline void add_scaled(T* const __restrict _x, const T _alpha, const T* const __restrict _y, const size_t _n) noexcept {
108 109 110 111 112 113
			for(size_t i = 0; i < _n; i++) {
				_x[i] += _alpha*_y[i];
			}
		}
	}
}