本文整理汇总了Scala中scala.collection.mutable.PriorityQueue类的典型用法代码示例。如果您正苦于以下问题:Scala PriorityQueue类的具体用法?Scala PriorityQueue怎么用?Scala PriorityQueue使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了PriorityQueue类的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: MergedBufferedIterator
//设置package包名称以及导入依赖的类
package eu.shiftforward.apso.iterator
import scala.collection.mutable.PriorityQueue
case class MergedBufferedIterator[T](iterators: List[BufferedIterator[T]])(implicit ord: Ordering[T]) extends BufferedIterator[T] {
private[this] implicit def bufferedIteratorOrdering = new Ordering[BufferedIterator[T]] {
def compare(i1: BufferedIterator[T], i2: BufferedIterator[T]) =
ord.compare(i2.head, i1.head)
}
private[this] lazy val nonEmptyIterators = {
val pq = new PriorityQueue[BufferedIterator[T]]
pq.enqueue(iterators.filter(_.hasNext): _*)
pq
}
def head = nonEmptyIterators.head.head
def next() = {
val topIt = nonEmptyIterators.dequeue()
val res = topIt.next()
if (topIt.hasNext)
nonEmptyIterators.enqueue(topIt)
res
}
def hasNext: Boolean = nonEmptyIterators.nonEmpty
def mergeSorted[U >: T](thatIt: BufferedIterator[U])(implicit ord: Ordering[U]): BufferedIterator[U] =
thatIt match {
case mIt @ MergedBufferedIterator(thatIts) => MergedBufferedIterator[U](nonEmptyIterators.toList ++ thatIts)
case _ => MergedBufferedIterator[U](thatIt :: nonEmptyIterators.toList)
}
}