英文:
Queue of structs in Go
问题
我已经学习了Go,并且正在使用它进行BFS(广度优先搜索)拼图。我选择的队列实现如下:
//定义BFS算法的队列为frontier
type frontier struct {
queue []*graphNode
}
func (F *frontier) buildFrontier() {
F.queue = make([]*graphNode, 0, 1000)
}
func (F *frontier) pop() (g *graphNode) {
if F.isEmpty() {
panic("pop on empty queue!")
}
temp := F.queue[0]
F.queue = F.queue[1:]
return temp
}
func (F *frontier) push(g *graphNode) {
F.queue = append(F.queue, g)
}
func (F *frontier) isEmpty() bool {
return len(F.queue) == 0
}
我有两个问题:
-
这是一个好的实现吗?关于Go中的队列的文档很少,一般有一些关于向量的旧帖子,而列表似乎有太多的开销(虽然对于我的情况并不重要,但我想以最佳方式完成它)。
-
为什么对对象的调用(在这种情况下是结构体指针F *frontier)必须是一个指针?根据语法的工作方式,它似乎应该是指针的默认值,而不是显式的(也就是说,在这些情况下,你为什么会不想使用指针?)
修改后的循环版本如下:
//定义BFS算法的队列为frontier
type frontier struct {
queue []*graphNode
size, head, capacity int
}
func (F *frontier) buildFrontier() {
F.capacity = 1
F.queue = make([]*graphNode, F.capacity)
F.size = 0
F.head = 0
}
func (F *frontier) pop() (g *graphNode) {
if F.isEmpty() {
panic("pop on empty queue!")
}
F.size = (F.size - 1)
temp := F.queue[F.head]
F.head = (F.head + 1) % F.capacity
return temp
}
func (F *frontier) push(g *graphNode) {
if F.isFull() {
newSlice := make([]*graphNode, F.capacity*2)
copy(newSlice, F.queue)
F.queue = newSlice
F.capacity *= 2
}
F.queue[(F.head+F.size)%F.capacity] = g
F.size = (F.size + 1)
}
func (F *frontier) isEmpty() bool {
return F.size == 0
}
func (F *frontier) isFull() bool {
return F.size == F.capacity
}
以上是你要翻译的内容。
英文:
I have been learning Go, and I was doing a BFS puzzle with it. The queue implementation I decided on is:
//define the queue for our BFS algo as frontier.
type frontier struct {
queue []*graphNode
}
func (F *frontier) buildFrontier() {
F.queue = make([]*graphNode, 0, 1000)
}
func (F *frontier) pop() (g *graphNode) {
if F.isEmpty() {
panic("pop on empty queue!")
}
temp := F.queue[0]
F.queue = F.queue[1:]
return temp
}
func (F *frontier) push(g *graphNode) {
F.queue = append(F.queue, g)
}
func (F *frontier) isEmpty() bool {
return len(F.queue) == 0
}
I have 2 questions:
-
Is this a good implementation? Documentation on queues in go is sparse, and in general there are some old posts about vectors, and a list seems to have too much overhead (not that it matters for my case, but I'm trying to do it the best way possible).
-
Why does the call to an object (in this case the struct pointer F *frontier) have to be a pointer? It seems like the way the syntax works that it should be a default for a pointer, not explicit (i.e. why would you ever not want to use a pointer in these instances?)
MODIFIED CIRCULAR VERSION:
//define the queue for our BFS algo as frontier.
type frontier struct {
queue []*graphNode
size, head, capacity int
}
func (F *frontier) buildFrontier() {
F.capacity = 1
F.queue = make([]*graphNode, F.capacity)
F.size = 0
F.head = 0
}
func (F *frontier) pop() (g *graphNode) {
if F.isEmpty() {
panic("pop on empty queue!")
}
F.size = (F.size - 1)
temp := F.queue[F.head]
F.head = (F.head + 1) % F.capacity
return temp
}
func (F *frontier) push(g *graphNode) {
if F.isFull() {
newSlice := make([]*graphNode, F.capacity*2)
copy(newSlice, F.queue)
F.queue = newSlice
F.capacity *= 2
}
F.queue[(F.head+F.size)%F.capacity] = g
F.size = (F.size + 1)
}
func (F *frontier) isEmpty() bool {
return F.size == 0
}
func (F *frontier) isFull() bool {
return F.size == F.capacity
}
答案1
得分: 1
-
看起来你的实现方法是可行的,但效率可能会稍低。在开始时,
queue
是一个容量为1000的切片。这意味着底层数组可以容纳1000个元素。每次调用pop
时,你都会将队列的开头元素向下移动一个位置,从而将容量减少1。最终,当你调用push
时,容量可能不足以容纳新的元素,即使队列中可能没有太多(或没有)元素。这将导致在调用append
时分配一个新的数组。无论push
和pop
调用的模式如何,数组都将被重复重新分配,即使它从未容纳过接近1000个元素。我建议你在这里使用链表,可以使用list
包中的链表,或者设计自己的链表。 -
如果函数将修改接收器的值,或者接收器对象在其他地方被使用且需要共享值,那么接收器需要是一个指针。否则,它不需要是一个指针。如果值很大,你可能还是希望将接收器设为指针,这样就不需要复制它。(接收器的行为类似于任何其他函数参数,相同的考虑因素适用。)
英文:
-
It looks like your implementation will work, but it will end up being a bit inefficient. At the beginning,
queue
is a slice with capacity 1000. That means that the underlying array can hold 1000 elements. Every time you callpop
, you're moving the beginning of the queue one element further down that array, thus reducing the capacity by 1. Eventually, the capacity will be insufficient to hold a new element when you callpush
, even though there may not be many (or any) elements in the queue. That will cause a new array to be allocated whenappend
is called. No matter what the pattern ofpush
andpop
calls is, the array will be repeatedly reallocated, even if it never holds anywhere near 1000 elements. I would suggest using a linked list for this, either the one in thelist
package or one of your own design. -
The receiver needs to be a pointer if the function will modify the receiver's value or if the receiver object is used in other places and the value must be shared. Otherwise, it doesn't need to be a pointer. You may want to make the receiver a pointer anyway if the value is large so that it doesn't have to be copied. (The receiver behaves like any other function parameter and the same considerations apply.)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论