Igraph_vector_init_finally 和 igraph_vector_cumsum

Igraph_vector_init_finally and igraph_vector_cumsum

所以我正在查看 c 的 igraph 库的源代码,因为我需要创建一种新的图形,它不包含在该库中,但它在某种程度上与 free-scale 的健身模型图形相关网络。在阅读与构建此类图表相关的代码时,我发现这些函数在很多情况下都会被调用:

(void) IGRAPH_VECTOR_INIT_FINALLY(igraph_vector*,long_int);
(void) igraph_vector_cumsum(igraph_vector*,igraph_vector*);

我似乎无法通过文件夹找到它,我在网上搜索过,但似乎我无法找到它的作用。例如,在我的部分代码中:

/* Calculate the cumulative fitness scores */
  IGRAPH_VECTOR_INIT_FINALLY(&cum_fitness_out, no_of_nodes);
  IGRAPH_CHECK(igraph_vector_cumsum(&cum_fitness_out, fitness_out));
  max_out = igraph_vector_tail(&cum_fitness_out);
  p_cum_fitness_out = &cum_fitness_out;

其中cum_fitness_out是一个空向量,no_ofnodes是节点数,igraph check it's a function 来检查函数return的igraph_cumsum, vector tail returns 向量的最后一个元素...

IGRAPH_VECTOR_INIT_FINALLY(vector, size) 是 shorthand 用于:

IGRAPH_CHECK(igraph_vector_init(vector, size));
IGRAPH_FINALLY(igraph_vector_destroy, vector);

基本上,它用给定数量的未定义元素初始化一个vector,检查内存分配是否成功,然后将vector放在所谓的finally stack[=25=之上],其中包含在随后的代码中出现错误时应销毁的指针列表。有关 IGRAPH_FINALLY 的更多信息,请参见 here

igraph_vector_cumsum()计算一个向量的累加和;它的来源可以在 src/vector.pmt 中找到。 .pmt代表"poor man's templates";它本质上是一个带有一堆宏的 C 源文件,允许库使用一些宏技巧快速 "generate" 不同基本类型(整数、双精度、布尔值等)的相同数据类型(例如向量)。