Debian Codegraph @

R/priorityqueue.R

raw
1 # "...like a regular queue or stack data structure, but where additionally each
2 # element has a "priority" associated with it. In a priority queue, an element
3 # with high priority is served before an element with low priority. If two
4 # elements have the same priority, they are served according to their order in
5 # the queue." (http://en.wikipedia.org/wiki/Priority_queue)
6
7 PriorityQueue <- R6Class(
8 'PriorityQueue',
9 portable = FALSE,
10 class = FALSE,
11 public = list(
12 # Keys are priorities, values are subqueues (implemented as list)
13 .itemsByPriority = 'Map',
14 # Sorted vector (largest first)
15 .priorities = numeric(0),
16
17 initialize = function() {
18 .itemsByPriority <<- Map$new()
19 },
20 # Enqueue an item, with the given priority level (must be integer). Higher
21 # priority numbers are dequeued earlier than lower.
22 enqueue = function(item, priority) {
23 priority <- normalizePriority(priority)
24
25 if (!(priority %in% .priorities)) {
26 .priorities <<- c(.priorities, priority)
27 .priorities <<- sort(.priorities, decreasing=TRUE)
28 .itemsByPriority$set(.key(priority), list(item))
29 } else {
30 .itemsByPriority$set(
31 .key(priority),
32 c(.itemsByPriority$get(.key(priority)), item)
33 )
34 }
35 return(invisible())
36 },
37 # Retrieve a single item by 1) priority number (highest first) and then 2)
38 # insertion order (first in, first out). If there are no items to be
39 # dequeued, then NULL is returned. If it is necessary to distinguish between
40 # a NULL value and the empty case, call isEmpty() before dequeue().
41 dequeue = function() {
42 if (length(.priorities) == 0)
43 return(NULL)
44
45 maxPriority <- .priorities[[1]]
46 items <- .itemsByPriority$get(.key(maxPriority))
47 firstItem <- items[[1]]
48 if (length(items) == 1) {
49 # This is the last item at this priority. Remove both the list and the
50 # priority level.
51 .itemsByPriority$remove(.key(maxPriority))
52 .priorities <<- .priorities[-1]
53 } else {
54 # There are still items at this priority. Remove the current item from
55 # the list, and save it.
56 items <- items[-1]
57 .itemsByPriority$set(.key(maxPriority), items)
58 }
59 return(firstItem)
60 },
61 # Returns TRUE if no items are in the queue, otherwise FALSE.
62 isEmpty = function() {
63 length(.priorities) == 0
64 },
65 # Translates a priority integer to a character that is suitable for using as
66 # a key.
67 .key = function(priority) {
68 sprintf('%a', priority)
69 }
70 )
71 )
72
73 normalizePriority <- function(priority) {
74
75 if (is.null(priority))
76 priority <- 0
77
78 # Cast integers to numeric to prevent any inconsistencies
79 if (is.integer(priority))
80 priority <- as.numeric(priority)
81
82 if (!is.numeric(priority))
83 stop('priority must be an integer or numeric')
84
85 # Check length
86 if (length(priority) == 0) {
87 warning('Zero-length priority vector was passed; using 0')
88 priority <- 0
89 } else if (length(priority) > 1) {
90 warning('Priority has length > 1 and only the first element will be used')
91 priority <- priority[1]
92 }
93
94 # NA == 0
95 if (is.na(priority))
96 priority <- 0
97
98 return(priority)
99 }
100
101 # pq <- PriorityQueue$new()
102 # pq$enqueue('a', 1)
103 # pq$enqueue('b', 1L)
104 # pq$enqueue('c', 1)
105 # pq$enqueue('A', 2)
106 # pq$enqueue('B', 2L)
107 # pq$enqueue('C', 2)
108 # pq$enqueue('d', 1)
109 # pq$enqueue('e', 1L)
110 # pq$enqueue('f', 1)
111 # pq$enqueue('D', 2)
112 # pq$enqueue('E', 2L)
113 # pq$enqueue('F', 2)
114 # # Expect ABCDEFabcdef