gctl/example/sparray_ex.cpp
2025-04-08 16:23:40 +08:00

114 lines
3.3 KiB
C++

/********************************************************
* ██████╗ ██████╗████████╗██╗
* ██╔════╝ ██╔════╝╚══██╔══╝██║
* ██║ ███╗██║ ██║ ██║
* ██║ ██║██║ ██║ ██║
* ╚██████╔╝╚██████╗ ██║ ███████╗
* ╚═════╝ ╚═════╝ ╚═╝ ╚══════╝
* 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;
}