如何创建并迭代一个图标识(Apache AGE)的列表?

huangapple go评论62阅读模式
英文:

How to create and iterate over a List of graphids (Apache AGE)?

问题

I'm creating a function for Apache AGE, and I need to iterate over a List of graphids, how should I do it properly?

What I'm essentially trying to do is this:

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,
                                        vertex_to_connect);
        }
}

(...)

The problem happening is that the vertex_to_connect variable is not receiving the proper graphid (which should be a big number like 844424930131970), instead, it's getting small values that seem to be from the j variable.

Any help on showing where I could be mistaken is appreciated.

英文:

I'm creating a function for Apache AGE, and I need to iterate over a List of graphids, how should I do it properly?

What I'm essentially trying to do is this:

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);
        }
}

(...)

The problem happening is that the vertex_to_connect variable is not receiving the proper graphid (which should be a big number like 844424930131970), instead, it's getting small values that seems to be from the j variable.

Any help on showing where I could be mistaken is appreciated.

答案1

得分: 2

为了遍历List,您可以使用带有ListCell迭代器的foreach循环。以下是您可以执行此操作的示例:

List *graphid_list = NIL;
ListCell *lc;

// 遍历列表中的所有单元格。
foreach (lc, graphid_list)
{
    // 代码放在这里。
}

或者,您可以使用ListGraphId结构来创建您的列表。由于它仅包含graphid,我认为这样做会奏效。然后,您需要创建一个迭代器来查看每个元素,将其视为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

英文:

In order to iterate through a List you can use the foreach loop with a ListCell iterator. Here is an example of how you can do this:

List *graphid_list = NIL;
ListCell *lc;

// Go through all cells in the list.
foreach (lc, graphid_list)
{
    // Code goes here.
}

Alternatively, you could use the ListGraphId structure to create your list. Since it's only graphids, I believe it will do the trick. Then, you would need to create an iterator to peek at each element of the list as a GraphIdNode. I believe it would be necessary to create a for or while loop for this, but then update at the end of the loop for the iterator to be the next value of the GraphIdNode. So, something like this would traverse the entire list:

    ListGraphId *container;
    GraphIdNode *current = get_list_head(container);
    GraphIdNode *previous = NULL;

    while (current != NULL)
    {
        previous = current;
        current = next_GraphIdNode(current);
    }

You can find more about it at src/backend/utils/adt/age_graphid_ds.c and src/include/utils/age_grapid_ds.h.

https://github.com/apache/age/blob/master/src/backend/utils/adt/age_graphid_ds.c

答案2

得分: 1

以下是您要翻译的代码部分:

这是更新后的代码,对您有帮助:

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()以进行检索。

希望对您有所帮助!
英文:

Here's the updated code which can be helpful in your case:

 graphid new_graphid = 0;
graphid vertex_to_connect = 0;
List* graphids_array = NIL;
// ...
for (int64 i = 1; i &lt; graph.graph_size; i++)
{
new_graphid = create_vertex(&amp;graph);
graphids_array = lappend(graphids_array, graphid_to_const_ptr(new_graphid));
for (int64 j = 0; j &lt; list_length(graphids_array) - 1; j++)
{
vertex_to_connect = *(graphid*)list_nth(graphids_array, j);
connect_vertexes_by_graphid(&amp;graph, new_graphid, vertex_to_connect);
}
}

In this updated version, we used lappend() inn place of lappend_int() for appending. For converting to pointer value you can use graphid_to_const_ptr()

Additionally, we replaced list_nth_int() with list_nth() for the retrieval.

Hope it would help you!

huangapple
  • 本文由 发表于 2023年5月22日 23:14:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76307612.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定