问题描述
我正在处理一个需要处理巨大矩阵的项目,特别是用于 copula 计算的金字塔求和.
I am working on a project that requires the manipulation of enormous matrices, specifically pyramidal summation for a copula calculation.
简而言之,我需要在矩阵(多维数组)的零海中跟踪相对较少的值(通常为 1,在极少数情况下大于 1).
In short, I need to keep track of a relatively small number of values (usually a value of 1, and in rare cases more than 1) in a sea of zeros in the matrix (multidimensional array).
稀疏数组允许用户存储少量值,并假设所有未定义的记录都是预设值.由于物理上不可能将所有值存储在内存中,因此我只需要存储少数非零元素.这可能是几百万个条目.
A sparse array allows the user to store a small number of values, and assume all undefined records to be a preset value. Since it is not physically possibly to store all values in memory, I need to store only the few non-zero elements. This could be several million entries.
速度是重中之重,我还想在运行时动态选择类中的变量数量.
Speed is a huge priority, and I would also like to dynamically choose the number of variables in the class at runtime.
我目前在一个使用二叉搜索树 (b-tree) 存储条目的系统上工作.有人知道更好的系统吗?
I currently work on a system that uses a binary search tree (b-tree) to store entries. Does anyone know of a better system?
推荐答案
对于 C++,地图效果很好.数百万个对象不会有问题.在我的电脑上处理 1000 万个项目需要大约 4.4 秒和大约 57 meg.
For C++, a map works well. Several million objects won't be a problem. 10 million items took about 4.4 seconds and about 57 meg on my computer.
我的测试应用如下:
#include <stdio.h>
#include <stdlib.h>
#include <map>
class triple {
public:
int x;
int y;
int z;
bool operator<(const triple &other) const {
if (x < other.x) return true;
if (other.x < x) return false;
if (y < other.y) return true;
if (other.y < y) return false;
return z < other.z;
}
};
int main(int, char**)
{
std::map<triple,int> data;
triple point;
int i;
for (i = 0; i < 10000000; ++i) {
point.x = rand();
point.y = rand();
point.z = rand();
//printf("%d %d %d %d
", i, point.x, point.y, point.z);
data[point] = i;
}
return 0;
}
现在要动态选择变量的个数,最简单的办法就是将index表示为字符串,然后用字符串作为map的key.例如,位于 [23][55] 的项目可以通过23,55"字符串表示.我们还可以将这个解决方案扩展到更高的维度;例如对于三个维度,任意索引看起来像34,45,56".这种技术的简单实现如下:
Now to dynamically choose the number of variables, the easiest solution is to represent index as a string, and then use string as a key for the map. For instance, an item located at [23][55] can be represented via "23,55" string. We can also extend this solution for higher dimensions; such as for three dimensions an arbitrary index will look like "34,45,56". A simple implementation of this technique is as follows:
std::map data<string,int> data;
char ix[100];
sprintf(ix, "%d,%d", x, y); // 2 vars
data[ix] = i;
sprintf(ix, "%d,%d,%d", x, y, z); // 3 vars
data[ix] = i;
这篇关于在 C++ 中创建稀疏数组的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!