priority_queue creates a priority queue (a.k.a heap).

priority_queue(items = NULL, priorities = rep(0, length(items)))

Arguments

items

a list of items

priorities

a vector of interger valued priorities

Details

Following methods are exposed:


.$push(item, priority = 0)
.$pop()
.$clear()
.$size()
.$as_list()
.$print()
  • item: any R object

  • priority: a real number, item with larger priority pops first

Examples

q <- priority_queue()
q$push("not_urgent")
q$push("urgent", priority = 2)
q$push("not_as_urgent", priority = 1)
q$pop()  # urgent
#> [1] "urgent"
q$pop()  # not_as_urgent
#> [1] "not_as_urgent"
q$pop()  # not_urgent
#> [1] "not_urgent"

q <- priority_queue(list("not_urgent", "urgent"), c(0, 2))
q$push("not_as_urgent", 1)$push("not_urgent2")