/**++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ * * PANIC * Portable Algorithms and Numerics In C++ * * Scientific computing from scratch, with feeling. * * Copyright (c) 2026 Michelle Bausager * * This file is part of PANIC. * * PANIC is free software licensed under the GNU General Public License v3.0 or later. * You may redistribute and/or modify it under the terms of the GPL. * * PANIC 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 LICENSE file for the full license text. * * SPDX-License-Identifier: GPL-3.0-or-later * *++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ * * Project Name: PANIC * Module Name: tensor * File Name: vector.cpp * Revision: 0.1.0 * Date: 23-06-2026 * Author: Michelle Bausager * * Description: * Defines the vector object that will be used in the rest of the libraries. * *++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ //--------------------------------------------------------------------------------------------------------------------------- // INCLUDE DESCRIPTION //--------------------------------------------------------------------------------------------------------------------------- #include #include //--------------------------------------------------------------------------------------------------------------------------- // DEFINE DESCRIPTION //--------------------------------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------------------------------- // TYPE DESCRIPTION //--------------------------------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------------------------------- // VARIABLE DESCRIPTION //--------------------------------------------------------------------------------------------------------------------------- static const panic::types::uint_t vector_omp_min_size = 10000; //--------------------------------------------------------------------------------------------------------------------------- // FUNCTION PROTOTYPE //--------------------------------------------------------------------------------------------------------------------------- namespace panic{ namespace tensor{ //-------------------------------------------------------------------------------------------------------------------------- // Constructor Name : panic::tensor::vector::vector // // Description: // Creates an empty vector. //-------------------------------------------------------------------------------------------------------------------------- template vector::vector() { length = 0; data = 0; } //-------------------------------------------------------------------------------------------------------------------------- // Constructor Name : panic::tensor::vector::vector // // Description: // Creates a vector with size allocation and initializes all values to zero. //-------------------------------------------------------------------------------------------------------------------------- template vector::vector(panic::types::uint_t size){ length = size; if (length == 0){ data = 0; return; } data = new T[length]; PANIC_OMP_PARALLEL_FOR_IF(length > vector_omp_min_size) for (panic::types::uint_t i = 0; i < size; ++i){ data[i] = static_cast(0); } } //-------------------------------------------------------------------------------------------------------------------------- // Constructor Name : panic::tensor::vector::vector // // Description: // Creates a vector with size allocation and initializes all values. //-------------------------------------------------------------------------------------------------------------------------- template vector::vector(panic::types::uint_t size, T value){ length = size; if (size == 0){ data = 0; return; } data = new T[size]; PANIC_OMP_PARALLEL_FOR_IF(length > vector_omp_min_size) for (panic::types::uint_t i = 0; i < size; ++i){ data[i] = value; } } //-------------------------------------------------------------------------------------------------------------------------- // Constructor Name : panic::tensor::vector::vector // // Description: // Copy-contructor, makes a deep copy of another vector like this: // vector a(3); // vector b = a; //-------------------------------------------------------------------------------------------------------------------------- template vector::vector(const vector& other){ length = other.length; if (length == 0){ data = 0; return; } data = new T[length]; PANIC_OMP_PARALLEL_FOR_IF(length > vector_omp_min_size) for (panic::types::uint_t i = 0; i < length; ++i){ data[i] = other.data[i]; } } //-------------------------------------------------------------------------------------------------------------------------- // Deconstructor Name : panic::tensor::vector::~vector // // Description: // Deletes the data and releases the memory. //-------------------------------------------------------------------------------------------------------------------------- template vector::~vector(){ delete[] data; data = 0; length = 0; } //-------------------------------------------------------------------------------------------------------------------------- // Function Name : panic::tensor::vector::operator= // // Description: // Copy-assignment. Copies from another vector like this: // vector a(5); // vector b(3); // b = a; //-------------------------------------------------------------------------------------------------------------------------- template vector& vector::operator=(const vector& other){ if (this == &other){ return *this; } T* new_data = 0; if (other.length > 0){ new_data = new T[other.length]; PANIC_OMP_PARALLEL_FOR_IF(length > vector_omp_min_size) for (panic::types::uint_t i = 0; i < other.length; ++i){ new_data[i] = other.data[i]; } } delete[] data; data = new_data; length = other.length; return *this; } //-------------------------------------------------------------------------------------------------------------------------- // Function Name : panic::tensor::vector::resize // // Description: // Returns the length/size of the vector //-------------------------------------------------------------------------------------------------------------------------- template panic::types::uint_t vector::size() const{ return length; } //-------------------------------------------------------------------------------------------------------------------------- // Function Name : panic::tensor::vector::resize // // Description: // Resizes the vector to new length and keeps old values //-------------------------------------------------------------------------------------------------------------------------- template bool vector::resize(panic::types::uint_t new_size){ if (new_size == length){ return true; } if (new_size == 0){ data = 0; length = 0; return true; } T* new_data = new T[new_size]; panic::types::uint_t copy_size = length; if (new_size < length){ copy_size = new_size; } PANIC_OMP_PARALLEL_FOR_IF(copy_size > vector_omp_min_size) for (panic::types::uint_t i = 0; i < copy_size; ++i){ new_data[i] = data[i]; } PANIC_OMP_PARALLEL_FOR_IF(copy_size - new_size > vector_omp_min_size) for (panic::types::uint_t i = copy_size; i < new_size; ++i){ new_data[i] = static_cast(0); } delete[] data; data = new_data; length = new_size; return true; } //-------------------------------------------------------------------------------------------------------------------------- // Function Name : panic::tensor::vector::fill // // Description: // Fills te vector with a value //-------------------------------------------------------------------------------------------------------------------------- template bool vector::fill(T value){ PANIC_OMP_PARALLEL_FOR_IF(length > vector_omp_min_size) for (panic::types::uint_t i = 0; i < length; ++i){ data[i] = value; } return true; } //-------------------------------------------------------------------------------------------------------------------------- // Function Name : panic::tensor::vector::operator[] // // Description: // Lets you read and write v[index] //-------------------------------------------------------------------------------------------------------------------------- template T& vector::operator[](panic::types::uint_t index){ return data[index]; } //-------------------------------------------------------------------------------------------------------------------------- // Function Name : panic::tensor::vector::operator[] // // Description: // Lets you read v[index] from a const vector //-------------------------------------------------------------------------------------------------------------------------- template const T& vector::operator[](panic::types::uint_t index) const{ return data[index]; } //-------------------------------------------------------------------------------------------------------------------------- // Function Name : panic::tensor::vector::at // // Description: // Lets you read and write v.at(index) with index bounse //-------------------------------------------------------------------------------------------------------------------------- template T& vector::at(panic::types::uint_t index){ if (index >= length){ return data[length-1]; } return data[index]; } //-------------------------------------------------------------------------------------------------------------------------- // Function Name : panic::tensor::vector::at // // Description: // Lets you read v[index] from a const vector with index bounse //-------------------------------------------------------------------------------------------------------------------------- template const T& vector::at(panic::types::uint_t index) const{ if (index >= length){ return data[length-1]; } return data[index]; } //--------------------------------------------------------------------------------------------------------------------------- // EXPLICIT TEMPLATE INSTANTIATION //--------------------------------------------------------------------------------------------------------------------------- template struct vector; template struct vector; template struct vector; } // namespace tensor } // namespace panic