gctl/lib/core/vector_t.h

134 lines
4.6 KiB
C
Raw Permalink Normal View History

2024-09-10 15:45:07 +08:00
/********************************************************
*
*
*
*
*
*
* Geophysical Computational Tools & Library (GCTL)
*
* Copyright (c) 2023 Yi Zhang (yizhang-geo@zju.edu.cn)
*
* GCTL is distributed under a dual licensing scheme. You can redistribute
* it and/or modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, either version 2
* of the License, or (at your option) any later version. You should have
* received a copy of the GNU Lesser General Public License along with this
* program. If not, see <http://www.gnu.org/licenses/>.
*
* If the terms and conditions of the LGPL v.2. would prevent you from using
* the GCTL, please consider the option to obtain a commercial license for a
* fee. These licenses are offered by the GCTL's original author. As a rule,
* licenses are provided "as-is", unlimited in time for a one time fee. Please
* send corresponding requests to: yizhang-geo@zju.edu.cn. Please do not forget
* to include some description of your company and the realm of its activities.
* Also add information on how to contact you by electronic and paper mail.
******************************************************/
#ifndef _GCTL_VECTOR_TYPE_H
#define _GCTL_VECTOR_TYPE_H
#include "vector"
#include "complex"
#include "iostream"
namespace gctl
{
/*
* Here we define some commonly used vector types.
*/
typedef std::vector<int> _1i_vector; ///< 1-D integer vector
typedef std::vector<float> _1f_vector; ///< 1-D single-precision floating-point vector
typedef std::vector<double> _1d_vector; ///< 1-D double-precision floating-point vector
typedef std::vector<std::string> _1s_vector; ///< 1-D string vector
typedef std::vector<std::complex<float>> _1cf_vector; ///< 1-D complex float vector
typedef std::vector<std::complex<double>> _1cd_vector; ///< 1-D complex double vector
typedef std::vector<std::vector<int>> _2i_vector; ///< 2-D integer vector
typedef std::vector<std::vector<float>> _2f_vector; ///< 2-D single-precision floating-point vector
typedef std::vector<std::vector<double>> _2d_vector; ///< 2-D double-precision floating-point vector
typedef std::vector<std::vector<std::string>> _2s_vector; ///< 2-D string vector
typedef std::vector<std::vector<std::complex<float>>> _2cf_vector; ///< 2-D complex float vector
typedef std::vector<std::vector<std::complex<double>>> _2cd_vector; ///< 2-D complex double vector
/**
* @brief Clear the memory space used by a 1-D vector
*
* @param[in] a 1-D vector object
*
* @tparam VecValType template type
*/
template <typename VecValType>
void destroy_vector(std::vector<VecValType> a)
{
if (!a.empty())
{
a.clear();
std::vector<VecValType>().swap(a);
}
return;
}
/**
* @brief Clear the memory space used by a 2-D vector
*
* @param[in] a 2-D vector object
*
* @tparam VecValType template type
*/
template <typename VecValType>
void destroy_vector(std::vector<std::vector<VecValType> > a)
{
if (!a.empty())
{
for (int i = 0; i < a.size(); i++)
{
a[i].clear();
std::vector<VecValType>().swap(a[i]);
}
a.clear();
std::vector<std::vector<VecValType> >().swap(a);
}
return;
}
template <typename VecValType>
void display_vector(const std::vector<VecValType> &a, std::ostream &os = std::cout, char sep = ' ')
{
if (!a.empty())
{
os << a[0];
for (size_t i = 1; i < a.size(); i++)
{
os << sep << a[i];
}
os << std::endl;
}
return;
}
template <typename VecValType>
std::ostream &operator <<(std::ostream &os, const std::vector<VecValType> &a)
{
if (!a.empty())
{
os << a[0];
for (size_t i = 1; i < a.size(); i++)
{
os << " " << a[i];
}
}
return os;
}
template <typename VecValType>
std::istream &operator >>(std::istream &os, std::vector<VecValType> &a)
{
VecValType t;
// 在 Windows 系统之下:输入 ctrl + z 后按下回车,就代表标准输入流的结束
// 在 Unix/Linux/MacOS 系统之下:输入 ctrl + D就表示输入流结束了
while (os >> t){a.push_back(t);}
return os;
}
}
#endif // _GCTL_VECTOR_TYPE_H