<commit_before>// Xerus - A General Purpose Tensor Library
// Copyright (C) 2014-2016 <NAME> and <NAME>. 
// 
// 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 <EMAIL>.

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

#pragma once

#include <type_traits>
#include <cstring>

#include "standard.h"

namespace xerus {
	namespace misc {
		/** 
		* @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>
		inline void set_zero(T* const __restrict _x, const size_t _n) {
			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.
		*/
		template <typename T, typename std::enable_if<std::is_trivially_copyable<T>::value, int>::type = 0>
		inline void copy(T* const __restrict _x, const T* const __restrict _y, const size_t _n) {
			memcpy(_x, _y, _n*sizeof(T));
		}
		
		
		/** 
		* @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.
		*/
		template <typename T, typename std::enable_if<std::is_trivially_copyable<T>::value, int>::type = 0>
		inline void copy_inplace(T* const _x, const T* const _y, const size_t _n) {
			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>
		inline void copy_scaled(T* const __restrict _x, const T _alpha, const T* const _y, const size_t _n) {
			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>
		inline void scale(T* const __restrict _x, const T _alpha, const size_t _n) {
			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.<commit_msg>Downgrade enable_if to support outdated compilers<commit_after>// Xerus - A General Purpose Tensor Library
// Copyright (C) 2014-2016 <NAME> and <NAME>. 
// 
// 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 <EMAIL>.

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

#pragma once

#include <type_traits>
#include <cstring>

#include "standard.h"

namespace xerus {
	namespace misc {
		/** 
		* @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>
		inline void set_zero(T* const __restrict _x, const size_t _n) {
			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.
		*/
		template <typename T, typename std::enable_if<std::is_trivial<T>::value, int>::type = 0>
		inline void copy(T* const __restrict _x, const T* const __restrict _y, const size_t _n) {
			memcpy(_x, _y, _n*sizeof(T));
		}
		
		
		/** 
		* @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.
		*/
		template <typename T, typename std::enable_if<std::is_trivial<T>::value, int>::type = 0>
		inline void copy_inplace(T* const _x, const T* const _y, const size_t _n) {
			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>
		inline void copy_scaled(T* const __restrict _x, const T _alpha, const T* const _y, const size_t _n) {
			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>
		inline void scale(T* const __restrict _x, const T _alpha, const size_t _n) {
			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.
{'fraction_non_alphanumeric': 0.11468812877263582, 'fraction_numerical': 0.005030181086519115, 'mean_word_length': 3.1596931659693164, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 37, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3615058', 'n_tokens_mistral': 2222, 'n_tokens_neox': 2008, 'n_words': 828}