免責聲明:本文作者是C++初學者。如何將Erlang數據結構轉換爲C++鄰接列表(圖)?
以我的Erlang代碼,我有一個數據結構,它代表一個有向圖:
-record(vertex_entry, {vertex_name, outgoing_edges}).
-record(outgoing_edge, {edge_weight, neighbour_name}).
Edge1 = #outgoing_edge{edge_weight = 12.5, neighbour_name = 2},
Edge2 = #outgoing_edge{edge_weight = 11.2, neighbour_name = 3},
Edges = [Edge1,Edge2],
Vertex1 = #vertex_entry{vertex_name = 1, outgoing_edges = Edges},
Vertex4 = #vertex_entry{vertex_name = 4, outgoing_edges = Edges},
Graph = [Vertex1,Vertex4].
我想通過Erlang的端口功能,以該曲線圖中傳遞到C++。
在C++側我想此二郎圖形轉換成以下結構的曲線圖(它是一個CUDA優化的鄰接表structure):
typedef struct
{
// (V) This contains a pointer to the edge list for each vertex
int *vertexArray;
// Vertex count
int vertexCount;
// (E) This contains pointers to the vertices that each edge
// is attached to
int *edgeArray;
// Edge count
int edgeCount;
// (W) Weight array
float *weightArray;
} GraphData;
或許,C++從圖表生成代碼相同的源(書的第16章),將有助於解決這一問題:
void generateRandomGraph(GraphData *graph, int numVertices, int neighborsPerVertex)
{
graph->vertexCount = numVertices;
graph->vertexArray = (int*) malloc(graph->vertexCount * sizeof(int));
graph->edgeCount = numVertices * neighborsPerVertex;
graph->edgeArray = (int*)malloc(graph->edgeCount * sizeof(int));
graph->weightArray = (float*)malloc(graph->edgeCount * sizeof(float));
for(int i = 0; i < graph->vertexCount; i++)
{
graph->vertexArray[i] = i * neighborsPerVertex;
}
for(int i = 0; i < graph->edgeCount; i++)
{
graph->edgeArray[i] = (rand() % graph->vertexCount);
graph->weightArray[i] = (float)(rand() % 1000)/1000.0f;
}
}
我假設Erlang的港口功能將幫助我從二郎將數據傳遞到C++以二進制格式。
因此,問題是: 如何將C++代碼中的某個Erlang數據結構(代表圖形)恢復並轉換爲上述用戶指定的C++圖形數據結構?
從一些簡單的東西開始不是更好嗎? – Beta
我很樂意從一些簡單的東西開始,但是我想用一段已經寫好的C++代碼,並且有自己的數據結構。 – skanatek
我對Erlang一無所知,但如果沒有專家出現,我很樂意提供簡單的工作C++,從簡單的變量開始並着手。 – Beta