R是否有像Java的PriorityQueue这样的优先级队列?

时间:2021-11-21 17:55:27

I am looking for a general purpose priority queue in R. Does R has any general purpose priority queue implementation (package) like Java PriorityQueue class or Python heapq?

我在R中寻找通用优先级队列.R。是否有任何通用优先级队列实现(包),如Java PriorityQueue类或Python heapq?

3 个解决方案

#1


5  

I went ahead and implemented a basic queue as an R Reference Class. The details can be found here. It's been extended to handle a priority queue, as documented here.

我继续将基本队列实现为R参考类。细节可以在这里找到。它已被扩展为处理优先级队列,如此处所述。

The basic and priority queue implementations are now available as the liqueueR package on CRAN, with a development version on GitHub.

基本和优先级队列实现现在可以作为CRAN上的liqueueR包使用,在GitHub上有一个开发版本。

#2


2  

You can use the following implementation from Rosetta Code, but beware that insertion takes O(n log n)

您可以使用Rosetta Code中的以下实现,但要注意插入需要O(n log n)

PriorityQueue <- function() {
  keys <<- values <<- NULL
  insert <- function(key, value) {
    temp <- c(keys, key)
    ord <- order(temp)
    keys <<- temp[ord]
    values <<- c(values, list(value))[ord]
  }
  pop <- function() {
    head <- values[[1]]
    values <<- values[-1]
    keys <<- keys[-1]
    return(head)
  }
  empty <- function() length(keys) == 0
  list(insert = insert, pop = pop, empty = empty)
}

#3


1  

You could probably create this quite easily yourself, either using classes (Reference classes fit best), or using a data.frame with a custom type, combined with some functions that operate on it (add_to_queue(element, queue_object, priority), get_item(queue_object)). These functions would be the methods in case of the reference class. I like the reference class solution better as it stores both the state and the logic in one place.

您可以自己很容易地创建它,使用类(引用类最适合),或者使用带有自定义类型的data.frame,以及对其进行操作的一些函数(add_to_queue(element,queue_object,priority),get_item( queue_object))。这些函数将是引用类的方法。我更喜欢参考类解决方案,因为它将状态和逻辑存储在一个地方。

#1


5  

I went ahead and implemented a basic queue as an R Reference Class. The details can be found here. It's been extended to handle a priority queue, as documented here.

我继续将基本队列实现为R参考类。细节可以在这里找到。它已被扩展为处理优先级队列,如此处所述。

The basic and priority queue implementations are now available as the liqueueR package on CRAN, with a development version on GitHub.

基本和优先级队列实现现在可以作为CRAN上的liqueueR包使用,在GitHub上有一个开发版本。

#2


2  

You can use the following implementation from Rosetta Code, but beware that insertion takes O(n log n)

您可以使用Rosetta Code中的以下实现,但要注意插入需要O(n log n)

PriorityQueue <- function() {
  keys <<- values <<- NULL
  insert <- function(key, value) {
    temp <- c(keys, key)
    ord <- order(temp)
    keys <<- temp[ord]
    values <<- c(values, list(value))[ord]
  }
  pop <- function() {
    head <- values[[1]]
    values <<- values[-1]
    keys <<- keys[-1]
    return(head)
  }
  empty <- function() length(keys) == 0
  list(insert = insert, pop = pop, empty = empty)
}

#3


1  

You could probably create this quite easily yourself, either using classes (Reference classes fit best), or using a data.frame with a custom type, combined with some functions that operate on it (add_to_queue(element, queue_object, priority), get_item(queue_object)). These functions would be the methods in case of the reference class. I like the reference class solution better as it stores both the state and the logic in one place.

您可以自己很容易地创建它,使用类(引用类最适合),或者使用带有自定义类型的data.frame,以及对其进行操作的一些函数(add_to_queue(element,queue_object,priority),get_item( queue_object))。这些函数将是引用类的方法。我更喜欢参考类解决方案,因为它将状态和逻辑存储在一个地方。