我正在为Apache AGE创建一个函数,我需要迭代graphids
的List
,我应该如何正确地做?
- 我指的
List
是PostgreSQL定义的结构体,在这个文件中-> Postgres' GitHub repository链接 graphid
只是一个int64
。
我主要想做的是
graphid new_graphid = 0;
graphid vertex_to_connect = 0;
List* graphids_array = NIL;
(...)
for (int64 i=(int64)1;i<graph.graph_size;i++)
{
new_graphid = create_vertex(&graph);
graphids_array = lappend_int(graphids_array, new_graphid);
for(int64 j = 0; j<graphids_array->length-1; j++)
{
vertex_to_connect = list_nth_int(graphids_array, j);
connect_vertexes_by_graphid(&graph,
new_graphid,
vertex_to_connect);
}
}
(...)
发生的问题是,vertex_to_connect
变量没有接收到正确的graphid
(应该是一个很大的数字,如844424930131970),相反,它得到的小值似乎来自j
变量。
任何帮助显示我可能会错了,我很感激。
2条答案
按热度按时间zf2sa74q1#
为了迭代
List
,可以使用foreach
循环和ListCell
迭代器。下面是一个示例,说明如何做到这一点:或者,您可以使用
ListGraphId
结构来创建列表。因为它只有graphid
s,我相信它会成功的。然后,您需要创建一个迭代器,以GraphIdNode
的形式查看列表中的每个元素。我认为有必要为此创建一个for
或while
循环,但在循环结束时更新迭代器,使其成为GraphIdNode
的下一个值。所以,像这样的东西会遍历整个列表:您可以在
src/backend/utils/adt/age_graphid_ds.c
和src/include/utils/age_grapid_ds.h
上找到更多信息。https://github.com/apache/age/blob/master/src/backend/utils/adt/age_graphid_ds.c
h43kikqp2#
以下是更新后的代码,可能对您的情况有所帮助:
在这个更新的版本中,我们使用lappend()代替lappend_int()进行追加。要转换为指针值,可以使用graphid_to_const_ptr()
此外,我们将list_nth_int()替换为**list_nth()**进行检索。
希望对你有帮助!