如何将使用go代码编写的示例转换为C代码? [英] How do I translate this sample written in go code to C code ?

查看:158
本文介绍了如何将使用go代码编写的示例转换为C代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前正在将加权DAG转换为C代码,该代码使用Go语言编写并进行拓扑排序。实际上我错过了下面代码的几部分代码。在topoSort函数中,我无法获得访问声明。它是另一个函数中的函数声明吗?另一部分是在下面你将看到的main函数中,无法在循环中使用range关键字进行切片处理。如果你大致或简单地翻译成C语法就会很棒。



// topoSort()

Currently translating weighted DAG to C code which is written in Go language and topologically sorted. Actually I missed a couple part of the code that is the code below sample. In topoSort function, I couldn't get what "visit" declaration is. Is it function declaration within another function ? Another part is that in main function that you'll see it below, couldn't get slicing processes with range keyword in loops. If you translate into C syntax roughly or briefly that would be great.

// topoSort()

func (g *graph) topoSort() []int {
    result := make([]int, g.size())
    marks := make([]bool, g.size())
    resultIndex := g.size() - 1

    var visit func(int)
    visit = func(u int) {
        for _, item := range g.adjList[u] {
            if !marks[item.vertex] {
                visit(item.vertex)
            }
        }

        marks[u] = true
        result[resultIndex] = u
        resultIndex--
    }

    for u := range g.adjList {
        if !marks[u] {
            visit(u)
        }
    }

    return result
}





// main()



// main()

i := 0
	for rowIndex, row := range input {
		for _, number := range row {
			if rowIndex+1 >= len(input) { // Last row.
				g.addEdge(i, vertexesQuantity, number)
			} else { // Not the last row.
				g.addEdge(i, i + rowIndex + 1, number)
				g.addEdge(i, i + rowIndex + 2, number)
			}

			i++
		}
	}





我的尝试:



我做了它并从该样本中转换了基本图形(加权DAG)结构,并且没有任何问题。我正在努力将其中的两个函数转换为C代码,如上所述。



What I have tried:

I made it and converted basic graph ( weighted DAG ) structure from that sample and there no issue about that. I just struggled with that two of them functions to convert to C code as I described above.

推荐答案

C中没有数组切片。你必须创建一个新阵列。



访问 - Go编程语言 [ ^ ] [ ^ ]





访问函数的源代码在这里,所以我假设你可以从这里找出它的功能
There's no array slice in C. You'd have to create a new array.

Visit - The Go Programming Language[^][^]


The source code for the visit function is here, so I assume you can work out what it does from here


这篇关于如何将使用go代码编写的示例转换为C代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆