如何创建和迭代Graphids列表(Apache AGE)?

lsmd5eda  于 2023-05-28  发布在  Apache
关注(0)|答案(2)|浏览(221)

我正在为Apache AGE创建一个函数,我需要迭代graphidsList,我应该如何正确地做?

  • 我指的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变量。
任何帮助显示我可能会错了,我很感激。

zf2sa74q

zf2sa74q1#

为了迭代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的形式查看列表中的每个元素。我认为有必要为此创建一个forwhile循环,但在循环结束时更新迭代器,使其成为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.csrc/include/utils/age_grapid_ds.h上找到更多信息。
https://github.com/apache/age/blob/master/src/backend/utils/adt/age_graphid_ds.c

h43kikqp

h43kikqp2#

以下是更新后的代码,可能对您的情况有所帮助:

graphid new_graphid = 0;
graphid vertex_to_connect = 0;
List* graphids_array = NIL;

// ...

for (int64 i = 1; i < graph.graph_size; i++)
{
    new_graphid = create_vertex(&graph);

    graphids_array = lappend(graphids_array, graphid_to_const_ptr(new_graphid));

    for (int64 j = 0; j < list_length(graphids_array) - 1; j++)
    {
        vertex_to_connect = *(graphid*)list_nth(graphids_array, j);
        connect_vertexes_by_graphid(&graph, new_graphid, vertex_to_connect);
    }
}

在这个更新的版本中,我们使用lappend()代替lappend_int()进行追加。要转换为指针值,可以使用graphid_to_const_ptr()
此外,我们将list_nth_int()替换为**list_nth()**进行检索。
希望对你有帮助!

相关问题