2

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.

MarkSoulz
  • 33
  • 4

2 Answers2

2

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

Matheus Farias
  • 716
  • 1
  • 10
1

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

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!