Title: | High Performance Container Data Types |
---|---|
Description: | Provides high performance container data types such as queues, stacks, deques, dicts and ordered dicts. Benchmarks <https://randy3k.github.io/collections/articles/benchmark.html> have shown that these containers are asymptotically more efficient than those offered by other packages. |
Authors: | Randy Lai [aut, cre], Andrea Mazzoleni [cph] (tommy hash table library), Yann Collet [cph] (xxhash algorithm) |
Maintainer: | Randy Lai <[email protected]> |
License: | MIT + file LICENSE |
Version: | 0.3.6 |
Built: | 2024-11-07 03:10:59 UTC |
Source: | https://github.com/randy3k/collections |
Provides high performance container data types such as queues, stacks, deques, dicts and ordered dicts. Benchmarks <https://randy3k.github.io/collections/articles/benchmark.html> have shown that these containers are asymptotically more efficient than those offered by other packages.
Maintainer: Randy Lai [email protected]
Other contributors:
Andrea Mazzoleni (tommy hash table library) [copyright holder]
Yann Collet (xxhash algorithm) [copyright holder]
Useful links:
cls
is a replacement for the class
function
which also works for the collection objects. It falls back to the ordinary class
function
for other objects.
cls(x)
cls(x)
x |
a collection object |
d <- dict() cls(d)
d <- dict() cls(d)
Deprecated Functions
Deque(...) Dict(...) OrderedDict(...) PriorityQueue(...) Queue(...) Stack(...)
Deque(...) Dict(...) OrderedDict(...) PriorityQueue(...) Queue(...) Stack(...)
... |
anything |
deque
creates a double ended queue.
deque(items = NULL)
deque(items = NULL)
items |
a list of items |
Following methods are exposed:
.$push(item) .$pushleft(item) .$pop() .$popleft() .$peek() .$peekleft() .$extend(q) .$extendleft(q) .$remove(item) .$clear() .$size() .$as_list() .$print()
item
: any R object
q
: a deque object
q <- deque() q$push("foo") q$push("bar") q$pushleft("baz") q$pop() # bar q$popleft() # baz q <- deque(list("foo", "bar")) q$push("baz")$pushleft("bla")
q <- deque() q$push("foo") q$push("bar") q$pushleft("baz") q$pop() # bar q$popleft() # baz q <- deque(list("foo", "bar")) q$push("baz")$pushleft("bla")
dict
creates an ordinary (unordered) dictionary (a.k.a. hash).
dict(items = NULL, keys = NULL)
dict(items = NULL, keys = NULL)
items |
a list of items |
keys |
a list of keys, use |
Following methods are exposed:
.$set(key, value) .$get(key, default) .$remove(key, silent = FALSE) .$pop(key, default) .$has(key) .$keys() .$values() .$update(d) .$clear() .$size() .$as_list() .$print()
key
: a scalar character, an atomic vector, an enviroment or a function
value
: any R object, value of the item
default
: optional, the default value of an item if the key is not found
d
: a dict object
d <- dict(list(apple = 5, orange = 10)) d$set("banana", 3) d$get("apple") d$as_list() # unordered d$pop("orange") d$as_list() # "orange" is removed d$set("orange", 3)$set("pear", 7) # chain methods # vector indexing d$set(c(1L, 2L), 3)$set(LETTERS, 26) d$get(c(1L, 2L)) # 3 d$get(LETTERS) # 26 # object indexing e <- new.env() d$set(sum, 1)$set(e, 2) d$get(sum) # 1 d$get(e) # 2
d <- dict(list(apple = 5, orange = 10)) d$set("banana", 3) d$get("apple") d$as_list() # unordered d$pop("orange") d$as_list() # "orange" is removed d$set("orange", 3)$set("pear", 7) # chain methods # vector indexing d$set(c(1L, 2L), 3)$set(LETTERS, 26) d$get(c(1L, 2L)) # 3 d$get(LETTERS) # 26 # object indexing e <- new.env() d$set(sum, 1)$set(e, 2) d$get(sum) # 1 d$get(e) # 2
ordered_dict
creates an ordered dictionary.
ordered_dict(items = NULL, keys = NULL)
ordered_dict(items = NULL, keys = NULL)
items |
a list of items |
keys |
a list of keys, use |
Following methods are exposed:
.$set(key, value) .$get(key, default) .$remove(key, silent = FALSE) .$pop(key, default) .$popitem(last = TRUE) .$has(key) .$keys() .$values() .$update(d) .$clear() .$size() .$as_list() .$print()
key
: scalar character, environment or function
value
: any R object, value of the item
default
: optional, the default value of an item if the key is not found
d
: an ordered_dict object
d <- ordered_dict(list(apple = 5, orange = 10)) d$set("banana", 3) d$get("apple") d$as_list() # the order the item is preserved d$pop("orange") d$as_list() # "orange" is removed d$set("orange", 3)$set("pear", 7) # chain methods
d <- ordered_dict(list(apple = 5, orange = 10)) d$set("banana", 3) d$get("apple") d$as_list() # the order the item is preserved d$pop("orange") d$as_list() # "orange" is removed d$set("orange", 3)$set("pear", 7) # chain methods
priority_queue
creates a priority queue (a.k.a heap).
priority_queue(items = NULL, priorities = rep(0, length(items)))
priority_queue(items = NULL, priorities = rep(0, length(items)))
items |
a list of items |
priorities |
a vector of interger valued priorities |
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
q <- priority_queue() q$push("not_urgent") q$push("urgent", priority = 2) q$push("not_as_urgent", priority = 1) q$pop() # urgent q$pop() # not_as_urgent q$pop() # not_urgent q <- priority_queue(list("not_urgent", "urgent"), c(0, 2)) q$push("not_as_urgent", 1)$push("not_urgent2")
q <- priority_queue() q$push("not_urgent") q$push("urgent", priority = 2) q$push("not_as_urgent", priority = 1) q$pop() # urgent q$pop() # not_as_urgent q$pop() # not_urgent q <- priority_queue(list("not_urgent", "urgent"), c(0, 2)) q$push("not_as_urgent", 1)$push("not_urgent2")
queue
creates a queue.
queue(items = NULL)
queue(items = NULL)
items |
a list of items |
Following methods are exposed:
.$push(item) .$pop() .$peek() .$clear() .$size() .$as_list() .$print()
item
: any R object
q <- queue() q$push("first") q$push("second") q$pop() # first q$pop() # second q <- queue(list("foo", "bar")) q$push("baz")$push("bla")
q <- queue() q$push("first") q$push("second") q$pop() # first q$pop() # second q <- queue(list("foo", "bar")) q$push("baz")$push("bla")
stack
creates a stack.
stack(items = NULL)
stack(items = NULL)
items |
a list of items |
Following methods are exposed:
.$push(item) .$pop() .$peek() .$clear() .$size() .$as_list() .$print()
item
: any R object
s <- stack() s$push("first") s$push("second") s$pop() # second s$pop() # first s <- stack(list("foo", "bar")) s$push("baz")$push("bla")
s <- stack() s$push("first") s$push("second") s$pop() # second s$pop() # first s <- stack(list("foo", "bar")) s$push("baz")$push("bla")