【发布时间】:2013-02-13 04:01:28
【问题描述】:
我正在尝试创建一个包含PriorityQueue 的数据结构。我已经成功地制作了它的非通用版本。我可以说它有效,因为它解决了人工智能。我遇到的问题。
这是它的一个sn-p:
class ProntoPriorityQueue { //TODO make generic
implicit def orderedNode(node: Node): Ordered[Node] = new Ordered[Node] {
def compare(other: Node) = node.compare(other)
}
val hashSet = new HashSet[Node]
val priorityQueue = new PriorityQueue[Node]()
...
我正在尝试使其通用,但如果我使用此版本,它将无法解决问题:
class PQ[T <% Ordered[T]] {
//[T]()(implicit val ord: T => Ordered[T]) {
//[T]()(implicit val ord: Ordering[T] {
val hashSet = new HashSet[T]
val priorityQueue = new PriorityQueue[T]
...
我也尝试了注释掉的内容,而不是使用[T <% Ordered[T]]
这里是调用PQ的代码:
//the following def is commented out while using ProntoPriorityQueue
implicit def orderedNode(node: Node): Ordered[Node] = new Ordered[Node] {
def compare(other: Node) = node.compare(other)
} //I've also tried making this return an Ordering[Node]
val frontier = new PQ[Node] //new ProntoPriorityQueue
//have also tried (not together):
val frontier = new PQ[Node]()(orderedNode)
我也尝试将隐式 def 移动到 Node 对象中(并导入它),但基本上是同样的问题。
我在通用版本中做错了什么?我应该把隐式放在哪里?
解决方案
问题不在于我的隐含定义。问题是隐式排序被Set 拾取,它在for(...) yield(...) 语句中自动生成。这会导致产生的集合只包含一个状态的问题。
【问题讨论】:
标签: scala priority-queue implicits