bug fixed in sparray

This commit is contained in:
张壹 2025-04-08 16:23:40 +08:00
parent 6e944f182a
commit 0e1f69c3d3
5 changed files with 165 additions and 3 deletions

View File

@ -7,6 +7,9 @@ include(CMakePackageConfigHelpers)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
# ExprTKmacOS 15.4
add_compile_options(-Wno-missing-template-arg-list-after-template-kw)
#
option(GCTL_OPENMP "Use the OpenMP library" ON)
option(GCTL_NETCDF "Use the NetCDF library" ON)

View File

@ -21,7 +21,7 @@ add_example(fir_filter_ex OFF)
add_example(fft_filter_ex OFF)
add_example(windowfunc_ex OFF)
add_example(legendre_ex OFF)
add_example(refellipsoid_ex ON)
add_example(refellipsoid_ex OFF)
add_example(kde_ex OFF)
add_example(meshio_ex OFF)
add_example(autodiff_ex OFF)
@ -34,4 +34,6 @@ add_example(gmt_ex OFF)
add_example(gnuplot_ex OFF)
add_example(cliplot_ex OFF)
add_example(stl_io_ex OFF)
add_example(ply_io_ex OFF)
add_example(ply_io_ex OFF)
add_example(sparray_ex OFF)
add_example(sparray2d_ex OFF)

43
example/sparray2d_ex.cpp Normal file
View File

@ -0,0 +1,43 @@
/********************************************************
*
*
*
*
*
*
* Geophysical Computational Tools & Library (GCTL)
*
* Copyright (c) 2022 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.
******************************************************/
#include "gctl/core.h"
int main(int argc, char const *argv[])
{
gctl::sparray2d<double> M(10, 10, 0.0);
M.at(4)->set(4, 5);
M.at(2)->set(3, 8);
M.at(6)->set(8, 2);
for (int i = 0; i < M.size(); i++)
{
M.at(i)->show_full();
}
return 0;
}

114
example/sparray_ex.cpp Normal file
View File

@ -0,0 +1,114 @@
/********************************************************
*
*
*
*
*
*
* Geophysical Computational Tools & Library (GCTL)
*
* Copyright (c) 2022 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.
******************************************************/
#include "../lib/core.h"
#include "../lib/algorithms.h"
int main(int argc, char const *argv[])
{
srand(time(0));
gctl::sparray<double> M(50, 0.0);
gctl::sparray<double> L;
int tmp_id[13] = {4, 2, 6, 7, 8, 12, 23, 43, 33, 47, 38, 15, 1};
int tmp_size = 13;
for (int i = 0; i < tmp_size; i++)
{
M.set(tmp_id[i], gctl::random(10.0, 20.0));
}
M.show_list();
std::cout << "************" << std::endl;
M.copy_to(L, 2);
L.show_list();
M.set(18, 100);
M.set(38, 100);
M.remove(12);
std::cout << "************" << std::endl;
M.show_list();
gctl::array<double> C(50, -1.0);
M.export_dense(C, 1.0, gctl::AppendVal);
for (int i = 0; i < C.size(); i++)
{
std::cout << C.at(i) << " ";
}
std::cout << std::endl;
gctl::sparray<double> N(C, -1, 1e-10);
N.show_list();
std::cout << "Test 2" << std::endl;
M.clear();
M.malloc(20, 0.0);
M.set(5, 6.5);
M.set(9, 9.1);
M.set(3, 4.3);
M.set(17, 1.4);
M.set(10, 3.5);
M.set(7, 7.4);
M.show_list();
M.remove(17);
M.remove(9);
std::cout << "**********" << std::endl;
M.show_list();
std::cout << "Test 3" << std::endl;
int test_size = 500000;
M.clear();
M.malloc(test_size, 0.0);
clock_t start = clock();
for (int i = 0; i < 300000; i++)
{
M.set(i, gctl::random(10.0, 20.0));
}
for (int i = 300001; i < test_size; i++)
{
M.set(i, gctl::random(10.0, 20.0));
}
M.set(300000, 15.8888);
clock_t end = clock();
std::cout << "sparray set() time: " << 1000.0*(end - start)/(double)CLOCKS_PER_SEC << " ms" << std::endl;
std::cout << "M.at(300000) = " << M.value(300000) << std::endl;
M.show_list(299995, 300005);
return 0;
}

View File

@ -433,7 +433,7 @@ namespace gctl
for (int i = 0; i < arr1d.size(); i++)
{
b.insert(arr1d[i].id, multipler*arr1d[i].val);
b.set(arr1d[i].id, multipler*arr1d[i].val);
}
return;
}