我正在为 Apache AGE 创建一个函数,我需要迭代 graphids
的 List
,我应该如何正确执行?
- 我指的
List
是这个文件中PostgreSQL定义的结构 -> Postgres' GitHub repository Link 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
变量。
任何有关我可能犯错的地方的帮助都会受到赞赏。
最佳答案
为了迭代 List
您可以使用foreach
循环 ListCell
迭代器。以下是如何执行此操作的示例:
List *graphid_list = NIL;
ListCell *lc;
// Go through all cells in the list.
foreach (lc, graphid_list)
{
// Code goes here.
}
或者,您可以使用 ListGraphId
结构来创建您的列表。因为它只是graphid
s,我相信它会成功。然后,您需要创建一个迭代器来查看列表中的每个元素,如GraphIdNode
。 。我相信有必要创建一个 for
或while
循环执行此操作,但然后在循环结束时更新迭代器为 GraphIdNode
的下一个值。所以,像这样的东西会遍历整个列表:
ListGraphId *container;
GraphIdNode *current = get_list_head(container);
GraphIdNode *previous = NULL;
while (current != NULL)
{
previous = current;
current = next_GraphIdNode(current);
}
您可以在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
关于c - 如何创建和迭代 graphids 列表(Apache AGE)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76307612/