在Google Go中可重用的优先队列实现

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

Reusable Priority queue implementation in google go

问题

如何编写可重用的优先队列代码在Google Go中,或者是否期望每次需要实现优先队列时都要定义LessPushPop函数?

英文:

How would one go about writing the code for a reusable priority queue in Google Go or is one expected to define the Less Push and Pop function everytime a priority queue implementation is needed?

答案1

得分: 3

后一种情况是必须要做的。由于Go语言没有泛型,这是目前唯一可用的选项。

英文:

The later case is what one has to do. As far as Go doesn't have generics, it's the only available option at the moment.

答案2

得分: 1

尚未尝试过,但也许你可以使用反射和结构标签,如果你的情况恰好符合某些限制条件。你需要确保你的可堆叠类型是一个带有类似于pq:"Key"的标签的结构体,该标签用于指定用于排序的字段,并且该字段的类型是可比较的。这种方法远不如使用Less方法强大,但可能满足你的需求。

抱歉,我没有给你提供示例代码。我认为这并不是非常困难,但需要一些时间。留给你作为一个练习。

如果我遇到需要处理任意结构体并且可以接受简单的键限制的情况,我可能会尝试这种技术。但对于有限的一组类型,我不会这样做。我会按照规范实现每种类型的heap.Interface。这实际上并不需要太多工作或太多代码行数。

英文:

Haven't tried it, but maybe you could use reflection and struct tags, if your cases happened to fit certain restrictons. You would require that your heapable type be a struct with a tag like `pq:"Key"` on the field you use for ordering, and that that field type be < comparable. It's far less powerful than a Less method but it might meet your needs.

Sorry I have no example code for you. I don't think it wouldn be terribly hard, but it would take me some time. Left for an exercise.

I might try this technique if I had a situation where I needed to handle arbitrary structs and I could live with the simplistic key restriction. For a finite set of types though, I wouldn't do this. I would just do it by the book, implementing heap.Interface separately for each type. It's really not that much work nor that many lines of code.

答案3

得分: 0

在标准库的container/vector模块中,曾经有一些向量类型实现了这些方法,基于可调整大小的切片,并且完美地作为与heap模块一起使用的容器。不幸的是,他们摒弃了vector,我从未理解为什么,因为它实现了对切片变量的良好的方法级抽象。所以现在,每当我看到有人在Go中使用heap模块时,他们基本上都需要重新实现vector的一部分 - 根据切片变量编写PushPopLength等方法。

英文:

There used to be vector types in the container/vector module of the standard library that implemented these methods, based on a resizable slice, and perfectly worked as the container to be used with the heap module. Unfortunately, they got rid of vector, which I never understood as it implemented a nice method-level abstraction over a slice variable. So now, every time I see someone using the heap module in Go, they have to basically re-implement part of vector -- writing Push, Pop, Length, etc. based on a slice variable.

答案4

得分: 0

我不确定我理解这个问题。

我在这里使用interface{}作为存储类型实现了一个队列(和栈和环):

https://github.com/iNamik/go_container

使用interface{}允许您存储任何类型的数据 - 尽管它不能像泛型那样帮助您强制类型,但它可以完成工作。

我可以轻松地创建一个优先级队列。

我有什么遗漏吗?

如果您认为有用,我很乐意添加一个优先级队列容器。

英文:

I'm not sure I understand the question.

I've implemented a queue (and stack and ring) using interface{} as the stored type here:

https://github.com/iNamik/go_container

Using interface{} allows you to store any type you want - although it doesn't help you enforce types ala generics, it gets the job done.

I could see creating a priority queue without much hassle.

Am I missing something?

I'd be happy to add a priority queue container if you think you'd find it useful.

huangapple
  • 本文由 发表于 2012年12月7日 01:08:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/13748826.html
匿名

发表评论

匿名网友

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

确定