Title: | 'C++' Standard Template Library Containers |
---|---|
Description: | Use 'C++' Standard Template Library containers interactively in R. Includes sets, unordered sets, multisets, unordered multisets, maps, unordered maps, multimaps, unordered multimaps, stacks, queues, priority queues, vectors, deques, forward lists, and lists. |
Authors: | Christian Düben [aut, cre] |
Maintainer: | Christian Düben <[email protected]> |
License: | MIT + file LICENSE |
Version: | 1.0.2 |
Built: | 2024-11-22 08:40:08 UTC |
Source: | https://github.com/cdueben/cppcontainers |
Read or insert a value by key in a CppMap or CppUnorderedMap. Read a value by index in a CppVector or CppDeque.
## S4 method for signature 'CppMap' x[i] ## S4 method for signature 'CppUnorderedMap' x[i] ## S4 method for signature 'CppVector' x[i] ## S4 method for signature 'CppDeque' x[i]
## S4 method for signature 'CppMap' x[i] ## S4 method for signature 'CppUnorderedMap' x[i] ## S4 method for signature 'CppVector' x[i] ## S4 method for signature 'CppDeque' x[i]
x |
A CppMap, CppUnorderedMap, CppVector, or CppDeque object. |
i |
A key (CppMap, CppUnorderedMap) or index (CppVector, CppDeque). |
In the two associative container types (CppMap, CppUnorderedMap), []
accesses a value by its key. If the key does not exist, it enters
the key with a default value into the container. The default value is 0 for integer and double, an empty string for string, and FALSE
for
boolean.
In the two sequence container types (CppVector, CppDeque), []
accesses a value by its index. If the index is outside the container, this crashes
the program.
at and []
both access elements. Unlike []
, at checks the bounds of the container and throws an error, if the element does
not exist.
Returns the value associated with i
.
at, back, contains, front, top.
m <- cpp_map(4:6, seq.int(0, 1, by = 0.5)) m # [4,0] [5,0.5] [6,1] m[6L] # [1] 1 m # [4,0] [5,0.5] [6,1] m[8L] # [1] 0 m # [4,0] [5,0.5] [6,1] [8,0] v <- cpp_vector(4:6) v # 4 5 6 v[1L] # [1] 4 v # 4 5 6
m <- cpp_map(4:6, seq.int(0, 1, by = 0.5)) m # [4,0] [5,0.5] [6,1] m[6L] # [1] 1 m # [4,0] [5,0.5] [6,1] m[8L] # [1] 0 m # [4,0] [5,0.5] [6,1] [8,0] v <- cpp_vector(4:6) v # 4 5 6 v[1L] # [1] 4 v # 4 5 6
Check, if two containers hold identical data.
## S4 method for signature 'CppSet,CppSet' e1 == e2 ## S4 method for signature 'CppUnorderedSet,CppUnorderedSet' e1 == e2 ## S4 method for signature 'CppMultiset,CppMultiset' e1 == e2 ## S4 method for signature 'CppUnorderedMultiset,CppUnorderedMultiset' e1 == e2 ## S4 method for signature 'CppMap,CppMap' e1 == e2 ## S4 method for signature 'CppUnorderedMap,CppUnorderedMap' e1 == e2 ## S4 method for signature 'CppMultimap,CppMultimap' e1 == e2 ## S4 method for signature 'CppUnorderedMultimap,CppUnorderedMultimap' e1 == e2 ## S4 method for signature 'CppStack,CppStack' e1 == e2 ## S4 method for signature 'CppQueue,CppQueue' e1 == e2 ## S4 method for signature 'CppVector,CppVector' e1 == e2 ## S4 method for signature 'CppDeque,CppDeque' e1 == e2 ## S4 method for signature 'CppForwardList,CppForwardList' e1 == e2 ## S4 method for signature 'CppList,CppList' e1 == e2
## S4 method for signature 'CppSet,CppSet' e1 == e2 ## S4 method for signature 'CppUnorderedSet,CppUnorderedSet' e1 == e2 ## S4 method for signature 'CppMultiset,CppMultiset' e1 == e2 ## S4 method for signature 'CppUnorderedMultiset,CppUnorderedMultiset' e1 == e2 ## S4 method for signature 'CppMap,CppMap' e1 == e2 ## S4 method for signature 'CppUnorderedMap,CppUnorderedMap' e1 == e2 ## S4 method for signature 'CppMultimap,CppMultimap' e1 == e2 ## S4 method for signature 'CppUnorderedMultimap,CppUnorderedMultimap' e1 == e2 ## S4 method for signature 'CppStack,CppStack' e1 == e2 ## S4 method for signature 'CppQueue,CppQueue' e1 == e2 ## S4 method for signature 'CppVector,CppVector' e1 == e2 ## S4 method for signature 'CppDeque,CppDeque' e1 == e2 ## S4 method for signature 'CppForwardList,CppForwardList' e1 == e2 ## S4 method for signature 'CppList,CppList' e1 == e2
e1 |
A CppSet, CppUnorderedSet, CppMultiset, CppUnorderedMultiset, CppMap, CppUnorderedMap, CppMultimap, CppUnorderedMultimap, CppStack, CppQueue, CppVector, CppDeque, CppForwardList, or CppList object. |
e2 |
A CppSet, CppUnorderedSet, CppMultiset, CppUnorderedMultiset, CppMap, CppUnorderedMap, CppMultimap, CppUnorderedMultimap, CppStack, CppQueue,
CppVector, CppDeque, CppForwardList, or CppList object of the same class and data type as |
Returns TRUE
, if the containers hold the same data and FALSE
otherwise.
x <- cpp_set(1:10) y <- cpp_set(1:10) x == y # [1] TRUE y <- cpp_set(1:11) x == y # [1] FALSE
x <- cpp_set(1:10) y <- cpp_set(1:10) x == y # [1] TRUE y <- cpp_set(1:11) x == y # [1] FALSE
Replace all elements in a container by reference.
assign(x, value, pos, envir, inherits, immediate)
assign(x, value, pos, envir, inherits, immediate)
x |
A CppVector, CppDeque, CppForwardList, or CppList object. |
value |
A vector. It is called |
pos |
Ignored. |
envir |
Ignored. |
inherits |
Ignored. |
immediate |
Ignored. |
Replaces all elements in x
with the elements in value
.
The parameters pos
, envir
, inherits
, and immediate
are only included for compatibility with the generic base::assign
method and have no effect.
Invisibly returns NULL
.
emplace, emplace_after, emplace_back, emplace_front, insert, insert_after, insert_or_assign.
v <- cpp_vector(4:9) v # 4 5 6 7 8 9 assign(v, 12:14) v # 12 13 14
v <- cpp_vector(4:9) v # 4 5 6 7 8 9 assign(v, 12:14) v # 12 13 14
Read a value at a certain position with bounds checking.
at(x, position)
at(x, position)
x |
A CppMap, CppUnorderedMap, CppVector, or CppDeque object. |
position |
A key (CppMap, CppUnorderedMap) or index (CppVector, CppDeque). |
In the two associative container types (CppMap, CppUnorderedMap), []
accesses a value by its key. If the key does not exist, the
function throws an error.
In the two sequence container types (CppVector, CppDeque), []
accesses a value by its index. If the index is outside the container, this throws
an error.
at and []
both access elements. Unlike []
, at checks the bounds of the container and throws an error, if the element does
not exist.
Returns the value at the position.
[, back, contains, front, top.
m <- cpp_map(4:6, seq.int(0, 1, by = 0.5)) m # [4,0] [5,0.5] [6,1] at(m, 4L) # [1] 0 d <- cpp_deque(c("hello", "world")) d # "hello" "world" at(d, 2) # [1] "world"
m <- cpp_map(4:6, seq.int(0, 1, by = 0.5)) m # [4,0] [5,0.5] [6,1] at(m, 4L) # [1] 0 d <- cpp_deque(c("hello", "world")) d # "hello" "world" at(d, 2) # [1] "world"
Access the last element in a container without removing it.
back(x)
back(x)
x |
A CppQueue, CppVector, CppDeque, or CppList object. |
In a CppQueue, the last element is the last inserted one.
Returns the last element.
front, top, push_back, emplace_back, pop_back.
q <- cpp_queue(1:4) q # First element: 1 back(q) # [1] 4 l <- cpp_list(1:4) l # 1 2 3 4 back(l) # [1] 4
q <- cpp_queue(1:4) q # First element: 1 back(q) # [1] 4 l <- cpp_list(1:4) l # 1 2 3 4 back(l) # [1] 4
Obtain the container's number of buckets.
bucket_count(x)
bucket_count(x)
x |
A CppUnorderedSet, CppUnorderedMultiset, CppUnorderedMap, CppUnorderedMultimap object. |
Returns the container's number of buckets.
max_bucket_count, load_factor, size.
s <- cpp_unordered_set(6:10) s # 10 9 8 7 6 bucket_count(s) # [1] 13
s <- cpp_unordered_set(6:10) s # 10 9 8 7 6 bucket_count(s) # [1] 13
Get the capacity of a CppVector.
capacity(x)
capacity(x)
x |
A CppVector object. |
The capacity is the space reserved for the vector, which can exceed its size. Additional capacity ensures that the vector can be extended efficiently, without having to reallocate its current elements to a new memory location.
Returns a numeric.
v <- cpp_vector(4:9) v # 4 5 6 7 8 9 capacity(v) # [1] 6 reserve(v, 20) size(v) #[1] 6 capacity(v) # [1] 20 v # 4 5 6 7 8 9
v <- cpp_vector(4:9) v # 4 5 6 7 8 9 capacity(v) # [1] 6 reserve(v, 20) size(v) #[1] 6 capacity(v) # [1] 20 v # 4 5 6 7 8 9
Remove all elements from a container by reference.
clear(x)
clear(x)
x |
A CppSet, CppUnorderedSet, CppMultiset, CppUnorderedMultiset, CppMap, CppUnorderedMap, CppMultimap, CppUnorderedMultimap, CppVector, CppDeque, CppForwardList, or CppList object. |
Invisibly returns NULL
.
l <- cpp_forward_list(4:9) l # 4 5 6 7 8 9 clear(l) l # empty(l) # [1] TRUE
l <- cpp_forward_list(4:9) l # 4 5 6 7 8 9 clear(l) l # empty(l) # [1] TRUE
Check, if elements are part of a container.
contains(x, values)
contains(x, values)
x |
A CppSet, CppUnorderedSet, CppMultiset, CppUnorderedMultiset, CppMap, CppUnorderedMap, CppMultimap, or CppUnorderedMultimap object. |
values |
Values whose existence to assess. Refers to keys in the case of CppMap, CppUnorderedMap, CppMultimap, and CppUnorderedMultimap objects. |
Returns a logical vector of the same length as values
, denoting for each value whether it is part of x
(TRUE
) or not
(FALSE
).
s <- cpp_multiset(4:9) s # 4 5 6 7 8 9 contains(s, 9:11) # [1] TRUE FALSE FALSE m <- cpp_unordered_map(c("hello", "world"), 3:4) m # ["world",4] ["hello",3] contains(m, c("world", "there")) # [1] TRUE FALSE
s <- cpp_multiset(4:9) s # 4 5 6 7 8 9 contains(s, 9:11) # [1] TRUE FALSE FALSE m <- cpp_unordered_map(c("hello", "world"), 3:4) m # ["world",4] ["hello",3] contains(m, c("world", "there")) # [1] TRUE FALSE
Count how often elements occur in a container.
count(x, values)
count(x, values)
x |
A CppSet, CppUnorderedSet, CppMultiset, CppUnorderedMultiset, CppMap, CppUnorderedMap, CppMultimap, or CppUnorderedMultimap object. |
values |
A vector of elements to check. Refers to keys in the case of CppMap, CppUnorderedMap, CppMultimap, and CppUnorderedMultimap objects. |
Returns a vector of the same length as values
, denoting for each value how often it occurs.
[, ==, at, contains, size, empty.
s <- cpp_set(4:9) s # 4 5 6 7 8 9 count(s, 9:11) # [1] 1 0 0 m <- cpp_map(c("hello", "there"), c(1.2, 1.3)) m # ["hello",1.2] ["there",1.3] count(m, c("hello", "world")) # [1] 1 0
s <- cpp_set(4:9) s # 4 5 6 7 8 9 count(s, 9:11) # [1] 1 0 0 m <- cpp_map(c("hello", "there"), c(1.2, 1.3)) m # ["hello",1.2] ["there",1.3] count(m, c("hello", "world")) # [1] 1 0
Create a deque, i.e. a double-ended queue.
cpp_deque(x)
cpp_deque(x)
x |
An integer, numeric, character, or logical vector. |
C++ deque methods implemented in this package are assign, at, back, clear, emplace, emplace_back, emplace_front, empty, erase, front, insert, max_size, pop_back, pop_front, push_back, push_front, resize, shrink_to_fit, and size. The package also adds the == and [ operators and various helper functions (print, to_r, type).
All object-creating methods in this package begin with cpp_
to avoid clashes with functions from other packages, such as utils::stack
and
base::vector
.
Returns a CppDeque object referencing a deque in C++.
cpp_vector, cpp_forward_list, cpp_list.
d <- cpp_deque(4:6) d # 4 5 6 push_back(d, 1L) d # 4 5 6 1 push_front(d, 2L) d # 2 4 5 6 1
d <- cpp_deque(4:6) d # 4 5 6 push_back(d, 1L) d # 4 5 6 1 push_front(d, 2L) d # 2 4 5 6 1
Create a forward list, i.e. a singly-linked list.
cpp_forward_list(x)
cpp_forward_list(x)
x |
An integer, numeric, character, or logical vector. |
@details Singly-linked means that list elements store a reference only to the following element. This container type, thus, requires less RAM than a doubly-linked list does, but can only be iterated in the forward direction.
C++ forward_list methods implemented in this package are assign, clear, emplace_after, emplace_front, empty, erase_after, front, insert_after, max_size, pop_front, push_front, remove., resize, reverse, sort, splice_after, and unique. The package also adds the == operator and various helper functions (print, to_r, type).
All object-creating methods in this package begin with cpp_
to avoid clashes with functions from other packages, such as utils::stack
and
base::vector
.
Returns a CppForwardList object referencing a forward_list in C++.
cpp_vector, cpp_deque, cpp_list.
v <- cpp_forward_list(4:6) v # 4 5 6 push_front(v, 10L) v # 10 4 5 6 pop_front(v) v # 4 5 6
v <- cpp_forward_list(4:6) v # 4 5 6 push_front(v, 10L) v # 10 4 5 6 pop_front(v) v # 4 5 6
Create a list, i.e. a doubly-linked list.
cpp_list(x)
cpp_list(x)
x |
An integer, numeric, character, or logical vector. |
Doubly-linked means that list elements store a reference both to the previous element and to the following element. This container type, thus, requires more RAM than a singly-linked list does, but can be iterated in both directions.
C++ list methods implemented in this package are assign, back, clear, emplace, emplace_back, emplace_front, empty, erase, front, insert, max_size, merge, pop_back, pop_front, push_back, push_front, remove., resize, reverse, size, sort, splice, and unique. The package also adds the == operator and various helper functions (print, to_r, type).
All object-creating methods in this package begin with cpp_
to avoid clashes with functions from other packages, such as utils::stack
and
base::vector
.
Returns a CppList object referencing a list in C++.
cpp_vector, cpp_deque, cpp_forward_list.
l <- cpp_list(4:6) l # 4 5 6 push_back(l, 1L) l # 4 5 6 1 push_front(l, 2L) l # 2 4 5 6 1
l <- cpp_list(4:6) l # 4 5 6 push_back(l, 1L) l # 4 5 6 1 push_front(l, 2L) l # 2 4 5 6 1
Create a map. Maps are key-value pairs sorted by unique keys.
cpp_map(keys, values)
cpp_map(keys, values)
keys |
An integer, numeric, character, or logical vector. |
values |
An integer, numeric, character, or logical vector. |
Maps are associative containers. They do not provide random access through an index. I.e. m[2]
does not return the second element.
C++ map methods implemented in this package are at, clear, contains, count, emplace, empty, erase, insert, insert_or_assign, max_size, merge, size, and try_emplace. The package also adds the == and [ operators and various helper functions (print, to_r, type).
All object-creating methods in this package begin with cpp_
to avoid clashes with functions from other packages, such as utils::stack
and
base::vector
.
Returns a CppMap object referencing a map in C++.
cpp_unordered_map, cpp_multimap, cpp_unordered_multimap.
m <- cpp_map(4:6, seq.int(1, by = 0.5, length.out = 3L)) m # [4,1] [5,1.5] [6,2] insert(m, seq.int(100, by = 0.1, length.out = 3L), 14:16) m # [4,1] [5,1.5] [6,2] [14,100] [15,100.1] [16,100.2] print(m, from = 6) # [6,2] [14,100] [15,100.1] [16,100.2] m <- cpp_map(c("world", "hello", "there"), 4:6) m # ["hello",5] ["there",6] ["world",4] erase(m, "there") m # ["hello",5] ["world",4]
m <- cpp_map(4:6, seq.int(1, by = 0.5, length.out = 3L)) m # [4,1] [5,1.5] [6,2] insert(m, seq.int(100, by = 0.1, length.out = 3L), 14:16) m # [4,1] [5,1.5] [6,2] [14,100] [15,100.1] [16,100.2] print(m, from = 6) # [6,2] [14,100] [15,100.1] [16,100.2] m <- cpp_map(c("world", "hello", "there"), 4:6) m # ["hello",5] ["there",6] ["world",4] erase(m, "there") m # ["hello",5] ["world",4]
Create a multimap. Multimaps are key-value pairs sorted by non-unique keys.
cpp_multimap(keys, values)
cpp_multimap(keys, values)
keys |
An integer, numeric, character, or logical vector. |
values |
An integer, numeric, character, or logical vector. |
Multimaps are associative containers. They do not provide random access through an index. I.e. m[2]
does not return the second element.
C++ multimap methods implemented in this package are clear, contains, count, emplace, empty, erase, insert, max_size, merge, and size. The package also adds the == operator and various helper functions (print, to_r, type).
All object-creating methods in this package begin with cpp_
to avoid clashes with functions from other packages, such as utils::stack
and
base::vector
.
Returns a CppMultimap object referencing a multimap in C++.
cpp_map, cpp_unordered_map, cpp_unordered_multimap.
m <- cpp_multimap(4:6, seq.int(1, by = 0.5, length.out = 3L)) m # [4,1] [5,1.5] [6,2] insert(m, seq.int(100, by = 0.1, length.out = 3L), 5:7) m # [4,1] [5,1.5] [5,100] [6,2] [6,100.1] [7,100.2] print(m, from = 6) # [6,2] [6,100.1] [7,100.2] m <- cpp_multimap(c("world", "hello", "there", "world"), 3:6) m # ["hello",4] ["there",5] ["world",3] ["world",6] erase(m, "world") m # ["hello",4] ["there",5]
m <- cpp_multimap(4:6, seq.int(1, by = 0.5, length.out = 3L)) m # [4,1] [5,1.5] [6,2] insert(m, seq.int(100, by = 0.1, length.out = 3L), 5:7) m # [4,1] [5,1.5] [5,100] [6,2] [6,100.1] [7,100.2] print(m, from = 6) # [6,2] [6,100.1] [7,100.2] m <- cpp_multimap(c("world", "hello", "there", "world"), 3:6) m # ["hello",4] ["there",5] ["world",3] ["world",6] erase(m, "world") m # ["hello",4] ["there",5]
Create a multiset. Multisets are containers of sorted, non-unique elements.
cpp_multiset(x)
cpp_multiset(x)
x |
An integer, numeric, character, or logical vector. |
Multisets are associative containers. They do not provide random access through an index. I.e. s[2]
does not return the second element.
C++ multiset methods implemented in this package are clear, contains, count, emplace, empty, erase, insert, max_size, merge, and size. The package also adds the == operator and various helper functions (print, to_r, type).
All object-creating methods in this package begin with cpp_
to avoid clashes with functions from other packages, such as utils::stack
and
base::vector
.
Returns a CppMultiset object referencing a multiset in C++.
cpp_set, cpp_unordered_set, cpp_unordered_multiset.
s <- cpp_multiset(c(6:9, 6L)) s # 6 6 7 8 9 insert(s, 4:7) s # 4 5 6 6 6 7 7 8 9 print(s, from = 6) # 6 6 6 7 7 8 9 s <- cpp_multiset(c("world", "hello", "world", "there")) s # "hello" "there" "world" "world" erase(s, "world") s # "hello" "there"
s <- cpp_multiset(c(6:9, 6L)) s # 6 6 7 8 9 insert(s, 4:7) s # 4 5 6 6 6 7 7 8 9 print(s, from = 6) # 6 6 6 7 7 8 9 s <- cpp_multiset(c("world", "hello", "world", "there")) s # "hello" "there" "world" "world" erase(s, "world") s # "hello" "there"
Create a priority queue. Priority queues are hold ordered, non-unique elements.
cpp_priority_queue(x, sorting = c("descending", "ascending"))
cpp_priority_queue(x, sorting = c("descending", "ascending"))
x |
An integer, numeric, character, or logical vector. |
sorting |
|
A priority queue is a container, in which the order of the elements depends on their size rather than their time of insertion. As in a stack, elements are removed from the top.
C++ priority queue methods implemented in this package are emplace, empty, pop, push, size, and top. The package also adds various helper functions (print, sorting, to_r, type).
All object-creating methods in this package begin with cpp_
to avoid clashes with functions from other packages, such as utils::stack
and
base::vector
.
Returns a CppPriorityQueue object referencing a priority_queue in C++.
q <- cpp_priority_queue(4:6) q # First element: 6 emplace(q, 10L) q # First element: 10 emplace(q, 3L) q # First element: 10 top(q) # [1] 10 q <- cpp_priority_queue(4:6, "ascending") q # First element: 4 push(q, 10L) q # First element: 4
q <- cpp_priority_queue(4:6) q # First element: 6 emplace(q, 10L) q # First element: 10 emplace(q, 3L) q # First element: 10 top(q) # [1] 10 q <- cpp_priority_queue(4:6, "ascending") q # First element: 4 push(q, 10L) q # First element: 4
Create a queue. Queues are first-in, first-out containers.
cpp_queue(x)
cpp_queue(x)
x |
An integer, numeric, character, or logical vector. |
The first element added to a queue is the first one to remove.
C++ queue methods implemented in this package are back, emplace, empty, front, pop, push, and size. The package also adds the == operator and various helper functions (print, to_r, type).
All object-creating methods in this package begin with cpp_
to avoid clashes with functions from other packages, such as utils::stack
and
base::vector
.
Returns a CppQueue object referencing a queue in C++.
q <- cpp_queue(1:4) q # First element: 1 push(q, 9L) q # First element: 1 back(q) # [1] 9 emplace(q, 10L) back(q) # [1] 10
q <- cpp_queue(1:4) q # First element: 1 push(q, 9L) q # First element: 1 back(q) # [1] 9 emplace(q, 10L) back(q) # [1] 10
Create a set. Sets are containers of unique, sorted elements.
cpp_set(x)
cpp_set(x)
x |
An integer, numeric, character, or logical vector. |
Sets are associative containers. They do not provide random access through an index. I.e., s[2]
does not return the second element.
C++ set methods implemented in this package are clear, contains, count, emplace, empty, erase, insert, max_size, merge, and size. The package also adds the == operator and various helper functions (print, to_r, type).
All object-creating methods in this package begin with cpp_
to avoid clashes with functions from other packages, such as utils::stack
and
base::vector
.
Returns a CppSet object referencing a set in C++.
cpp_unordered_set, cpp_multiset, cpp_unordered_multiset.
s <- cpp_set(6:9) s # 6 7 8 9 insert(s, 4:7) s # 4 5 6 7 8 9 print(s, from = 6) # 6 7 8 9 s <- cpp_set(c("world", "hello", "there")) s # "hello" "there" "world" erase(s, "there") s # "hello" "world"
s <- cpp_set(6:9) s # 6 7 8 9 insert(s, 4:7) s # 4 5 6 7 8 9 print(s, from = 6) # 6 7 8 9 s <- cpp_set(c("world", "hello", "there")) s # "hello" "there" "world" erase(s, "there") s # "hello" "world"
Create a stack. Stacks are last-in, first-out containers.
cpp_stack(x)
cpp_stack(x)
x |
An integer, numeric, character, or logical vector. |
The last element added to a stack is the first one to remove.
C++ stack methods implemented in this package are emplace, empty, pop, push, size, and top. The package also adds the == operator and various helper functions (print, to_r, type).
All object-creating methods in this package begin with cpp_
to avoid clashes with functions from other packages, such as utils::stack
and
base::vector
.
Returns a CppStack object referencing a stack in C++.
cpp_queue, cpp_priority_queue.
s <- cpp_stack(4:6) s # Top element: 6 emplace(s, 3L) s # Top element: 3 push(s, 9L) s # Top element: 9 pop(s) s # Top element: 3
s <- cpp_stack(4:6) s # Top element: 6 emplace(s, 3L) s # Top element: 3 push(s, 9L) s # Top element: 9 pop(s) s # Top element: 3
Create an unordered map. Unordered maps are key-value pairs with unique keys.
cpp_unordered_map(keys, values)
cpp_unordered_map(keys, values)
keys |
An integer, numeric, character, or logical vector. |
values |
An integer, numeric, character, or logical vector. |
Unordered maps are associative containers. They do not provide random access through an index. I.e. m[2]
does not return the second
element.
Unordered means that the container does not enforce elements to be stored in a particular order. This makes unordered maps in some applications faster than maps.
C++ unordered_map methods implemented in this package are at, bucket_count, clear, contains, count, emplace, empty, erase, insert, insert_or_assign, load_factor, max_bucket_count, max_load_factor, max_size, merge, rehash, reserve, size, and try_emplace. The package also adds the == and [ operators and various helper functions (print, to_r, type).
All object-creating methods in this package begin with cpp_
to avoid clashes with functions from other packages, such as utils::stack
and
base::vector
.
Returns a CppUnorderedMap object referencing an unordered_map in C++.
cpp_map, cpp_multimap, cpp_unordered_multimap.
m <- cpp_unordered_map(4:6, seq.int(1, by = 0.5, length.out = 3L)) m # [6,2] [5,1.5] [4,1] insert(m, seq.int(100, by = 0.1, length.out = 3L), 14:16) m # [16,100.2] [15,100.1] [14,100] [6,2] [5,1.5] [4,1] print(m, n = 3) # [16,100.2] [15,100.1] [14,100] m <- cpp_unordered_map(c("world", "hello", "there"), 4:6) m # ["there",6] ["hello",5] ["world",4] erase(m, "there") m # ["hello",5] ["world",4]
m <- cpp_unordered_map(4:6, seq.int(1, by = 0.5, length.out = 3L)) m # [6,2] [5,1.5] [4,1] insert(m, seq.int(100, by = 0.1, length.out = 3L), 14:16) m # [16,100.2] [15,100.1] [14,100] [6,2] [5,1.5] [4,1] print(m, n = 3) # [16,100.2] [15,100.1] [14,100] m <- cpp_unordered_map(c("world", "hello", "there"), 4:6) m # ["there",6] ["hello",5] ["world",4] erase(m, "there") m # ["hello",5] ["world",4]
Create an unordered multimap. Unordered multimaps are key-value pairs with non-unique keys.
cpp_unordered_multimap(keys, values)
cpp_unordered_multimap(keys, values)
keys |
An integer, numeric, character, or logical vector. |
values |
An integer, numeric, character, or logical vector. |
Unordered multimaps are associative containers. They do not provide random access through an index. I.e. m[2]
does not return the second
element.
Unordered means that the container does not enforce elements to be stored in a particular order. This makes unordered multimaps in some applications faster than multimaps.
C++ unordered_multimap methods implemented in this package are bucket_count, clear, contains, count, emplace, empty, erase, insert, load_factor, max_bucket_count, max_load_factor, max_size, merge, rehash, reserve, and size. The package also adds the == operator and various helper functions (print, to_r, type).
All object-creating methods in this package begin with cpp_
to avoid clashes with functions from other packages, such as utils::stack
and
base::vector
.
Returns a CppUnorderedMultimap object referencing an unordered_multimap in C++.
cpp_map, cpp_unordered_map, cpp_multimap.
m <- cpp_unordered_multimap(c("world", "hello", "there", "hello"), 4:7) m # ["there",6] ["hello",5] ["hello",7] ["world",4] print(m, n = 2) # erase(m, "hello") m # ["there",6] ["world",4] contains(m, "there") # [1] TRUE
m <- cpp_unordered_multimap(c("world", "hello", "there", "hello"), 4:7) m # ["there",6] ["hello",5] ["hello",7] ["world",4] print(m, n = 2) # erase(m, "hello") m # ["there",6] ["world",4] contains(m, "there") # [1] TRUE
Create an unordered multiset. Unordered multisets are containers of non-unique, unsorted elements.
cpp_unordered_multiset(x)
cpp_unordered_multiset(x)
x |
An integer, numeric, character, or logical vector. |
Unordered sets are associative containers. They do not provide random access through an index. I.e. s[2]
does not return the second
element.
Unordered means that the container does not enforce elements to be stored in a particular order. This makes unordered multisets in some applications faster than multisets. I.e., elements in unordered multisets are neither unique nor sorted.
C++ unordered_multiset methods implemented in this package are bucket_count, clear, contains, count, emplace, empty, erase, insert, load_factor, max_bucket_count, max_load_factor, max_size, merge, rehash, reserve, and size. The package also adds the == operator and various helper functions (print, to_r, type).
All object-creating methods in this package begin with cpp_
to avoid clashes with functions from other packages, such as utils::stack
and
base::vector
.
Returns a CppUnorderedMultiset object referencing an unordered_multiset in C++.
cpp_set, cpp_unordered_set, cpp_multiset.
s <- cpp_unordered_multiset(c(6:10, 7L)) s # 10 9 8 7 7 6 insert(s, 4:7) s # 5 4 6 6 7 7 7 8 9 10 print(s, n = 3L) # 5 4 6 erase(s, 6L) s # 5 4 7 7 7 8 9 10
s <- cpp_unordered_multiset(c(6:10, 7L)) s # 10 9 8 7 7 6 insert(s, 4:7) s # 5 4 6 6 7 7 7 8 9 10 print(s, n = 3L) # 5 4 6 erase(s, 6L) s # 5 4 7 7 7 8 9 10
Create an unordered set. Unordered sets are containers of unique, unsorted elements.
cpp_unordered_set(x)
cpp_unordered_set(x)
x |
An integer, numeric, character, or logical vector. |
Unordered sets are associative containers. They do not provide random access through an index. I.e. s[2]
does not return the second
element.
Unordered means that the container does not enforce elements to be stored in a particular order. This makes unordered sets in some applications faster than sets. I.e., elements in unordered sets are unique, but not sorted.
C++ unordered_set methods implemented in this package are bucket_count, clear, contains, count, emplace, empty, erase, insert, load_factor, max_bucket_count, max_load_factor, max_size, merge, rehash, reserve, and size. The package also adds the == operator and various helper functions (print, to_r, type).
All object-creating methods in this package begin with cpp_
to avoid clashes with functions from other packages, such as utils::stack
and
base::vector
.
Returns a CppUnorderedSet object referencing an unordered_set in C++.
cpp_set, cpp_multiset, cpp_unordered_multiset.
s <- cpp_unordered_set(6:10) s # 10 9 8 7 6 insert(s, 4:7) s # 5 4 10 9 8 7 6 print(s, n = 3L) # 5 4 10 s <- cpp_unordered_set(c("world", "hello", "there")) s # "there" "hello" "world" erase(s, "there") s # "hello" "world"
s <- cpp_unordered_set(6:10) s # 10 9 8 7 6 insert(s, 4:7) s # 5 4 10 9 8 7 6 print(s, n = 3L) # 5 4 10 s <- cpp_unordered_set(c("world", "hello", "there")) s # "there" "hello" "world" erase(s, "there") s # "hello" "world"
Create a vector. Vectors are dynamic, contiguous arrays.
cpp_vector(x)
cpp_vector(x)
x |
An integer, numeric, character, or logical vector. |
R vectors are similar to C++ vectors. These sequence containers allow for random access. I.e., you can directly access the fourth element via
its index x[4]
, without iterating through the first three elements before. Vectors are comparatively space-efficient, requiring less RAM per
element than many other container types.
One advantage of C++ vectors over R vectors is their ability to reduce the number of copies made during modifications. reserve, e.g., reserves space for future vector extensions.
C++ vector methods implemented in this package are assign, at, back, capacity, clear, emplace, emplace_back, empty, erase, flip, front, insert, max_size, pop_back, push_back, reserve, resize, shrink_to_fit, and size. The package also adds the == and [ operators and various helper functions (print, to_r, type).
All object-creating methods in this package begin with cpp_
to avoid clashes with functions from other packages, such as utils::stack
and
base::vector
.
Returns a CppVector object referencing a vector in C++.
cpp_deque, cpp_forward_list, cpp_list.
v <- cpp_vector(4:6) v # 4 5 6 push_back(v, 3L) v # 4 5 6 3 print(v, from = 3) # 6 3 print(v, n = -2) # 3 6 pop_back(v) v # 4 5 6
v <- cpp_vector(4:6) v # 4 5 6 push_back(v, 3L) v # 4 5 6 3 print(v, from = 3) # 6 3 print(v, n = -2) # 3 6 pop_back(v) v # 4 5 6
Add an element to a container by reference in place.
emplace(x, value, key = NULL, position = NULL)
emplace(x, value, key = NULL, position = NULL)
x |
A CppSet, CppUnorderedSet, CppMultiset, CppUnorderedMultiset, CppMap, CppUnorderedMap, CppMultimap, CppUnorderedMultimap, CppStack, CppQueue, CppPriorityQueue, CppVector, CppDeque, or CppList object. |
value |
A value to add to |
key |
A key to add to |
position |
The index at which to add the element. Only relevant for CppVector, CppDeque, and CppList objects. Indices start at 1. |
Existing container values are not overwritten. I.e. inserting a key-value pair into a map that already contains that key preserves the old value and discards the new one. Use insert_or_assign to overwrite values.
emplace
and try_emplace produce the same results in the context of this package. try_emplace can be minimally more computationally
efficient than emplace
.
The emplace methods only add single elements. Use insert to add multiple elements in one call.
Invisibly returns NULL
.
assign, emplace_after, emplace_back, emplace_front, insert, try_emplace.
s <- cpp_set(c(1.5, 2.3, 4.1)) s # 1.5 2.3 4.1 emplace(s, 3.1) s # 1.5 2.3 3.1 4.1 m <- cpp_unordered_map(c("hello", "there"), c(TRUE, FALSE)) m # ["there",FALSE] ["hello",TRUE] emplace(m, TRUE, "world") m # ["world",TRUE] ["there",FALSE] ["hello",TRUE] d <- cpp_deque(4:6) d # 4 5 6 emplace(d, 9, position = 2) d # 4 9 5 6
s <- cpp_set(c(1.5, 2.3, 4.1)) s # 1.5 2.3 4.1 emplace(s, 3.1) s # 1.5 2.3 3.1 4.1 m <- cpp_unordered_map(c("hello", "there"), c(TRUE, FALSE)) m # ["there",FALSE] ["hello",TRUE] emplace(m, TRUE, "world") m # ["world",TRUE] ["there",FALSE] ["hello",TRUE] d <- cpp_deque(4:6) d # 4 5 6 emplace(d, 9, position = 2) d # 4 9 5 6
Add an element to a forward list by reference in place.
emplace_after(x, value, position)
emplace_after(x, value, position)
x |
A CppForwardList object. |
value |
Value to add to |
position |
Index after which to insert the element. Indices start at 1. The function does not perform bounds checks. Indices outside |
The emplace methods only add single elements. Use insert to add multiple elements in one call.
Invisibly returns NULL
.
emplace, emplace_back, emplace_front, insert.
l <- cpp_forward_list(4:6) l # 4 5 6 emplace_after(l, 10L, 2) l # 4 5 10 6
l <- cpp_forward_list(4:6) l # 4 5 6 emplace_after(l, 10L, 2) l # 4 5 10 6
Add an element to the back of a container by reference in place.
emplace_back(x, value)
emplace_back(x, value)
x |
A CppVector, CppDeque, CppList object. |
value |
A value to add to |
The emplace methods only add single elements. Use insert to add multiple elements in one call.
Invisibly returns NULL
.
emplace, emplace_after, emplace_front, insert, pop_back, push_back.
v <- cpp_vector(4:6) v # 4 5 6 emplace_back(v, 12L) v # 4 5 6 12
v <- cpp_vector(4:6) v # 4 5 6 emplace_back(v, 12L) v # 4 5 6 12
Add an element to the front of a container by reference in place.
emplace_front(x, value)
emplace_front(x, value)
x |
A CppDeque, CppForwardList, or CppList object. |
value |
A value to add to |
The emplace methods only add single elements. Use insert to add multiple elements in one call.
Invisibly returns NULL
.
emplace, emplace_after, emplace_back, insert, pop_front, push_front.
l <- cpp_forward_list(4:6) l # 4 5 6 emplace_front(l, 12L) l # 12 4 5 6
l <- cpp_forward_list(4:6) l # 4 5 6 emplace_front(l, 12L) l # 12 4 5 6
Check, if a container is empty, i.e. does not contain elements.
empty(x)
empty(x)
x |
A |
Returns TRUE
, if the container is empty, and FALSE
otherwise.
v <- cpp_vector(4:6) v # 4 5 6 clear(v) empty(v) # [1] TRUE
v <- cpp_vector(4:6) v # 4 5 6 clear(v) empty(v) # [1] TRUE
Delete elements from a container by reference.
erase(x, values = NULL, from = NULL, to = NULL)
erase(x, values = NULL, from = NULL, to = NULL)
x |
A CppSet, CppUnorderedSet, CppMultiset, CppUnorderedMultiset, CppMap, CppUnorderedMap, CppMultimap, CppUnorderedMultimap, CppVector, CppDeque, or CppList object. |
values |
A vector of values to delete from |
from |
Index of the first element to be deleted in CppVector, CppDeque, and CppList objects. Ignored for other classes. |
to |
Index of the last element to be deleted in CppVector, CppDeque, and CppList objects. Ignored for other classes. |
Invisibly returns NULL
.
clear, empty, erase_after, remove..
s <- cpp_multiset(c(2, 2.1, 3, 3, 4.3, 6)) s # 2 2.1 3 3 4.3 6 erase(s, c(2, 3)) s # 2.1 4.3 6 m <- cpp_unordered_multimap(c(2:3, 3L), c("hello", "there", "world")) m # [3,"world"] [3,"there"] [2,"hello"] erase(m, 2L) m # [3,"world"] [3,"there"] d <- cpp_deque(4:9) d # 4 5 6 7 8 9 erase(d, from = 2, to = 3) d # 4 7 8 9
s <- cpp_multiset(c(2, 2.1, 3, 3, 4.3, 6)) s # 2 2.1 3 3 4.3 6 erase(s, c(2, 3)) s # 2.1 4.3 6 m <- cpp_unordered_multimap(c(2:3, 3L), c("hello", "there", "world")) m # [3,"world"] [3,"there"] [2,"hello"] erase(m, 2L) m # [3,"world"] [3,"there"] d <- cpp_deque(4:9) d # 4 5 6 7 8 9 erase(d, from = 2, to = 3) d # 4 7 8 9
Delete elements from a forward list by reference.
erase_after(x, from, to)
erase_after(x, from, to)
x |
A CppForwardList object. |
from |
Index after which to delete. |
to |
Index until including which to delete. Indices start at 1. The function does not perform bounds checks. Indices outside |
Invisibly returns NULL
.
l <- cpp_forward_list(4:9) l # 4 5 6 7 8 9 erase_after(l, 2L, 4L) l # 4 5 8 9
l <- cpp_forward_list(4:9) l # 4 5 6 7 8 9 erase_after(l, 2L, 4L) l # 4 5 8 9
Toggle boolean values in a vector.
flip(x)
flip(x)
x |
A CppVector object of type boolean. |
Sets TRUE to FALSE and FALSE to TRUE.
Invisibly returns NULL
.
v <- cpp_vector(c(TRUE, TRUE, FALSE)) v # TRUE TRUE FALSE flip(v) v # FALSE FALSE TRUE
v <- cpp_vector(c(TRUE, TRUE, FALSE)) v # TRUE TRUE FALSE flip(v) v # FALSE FALSE TRUE
Access first element in a container without removing it.
front(x)
front(x)
x |
A CppQueue, CppVector, CppDeque, CppForwardList, or CppList object. |
Returns the front element.
back, emplace_front, pop, pop_front, push_front.
q <- cpp_queue(4:6) q # First element: 4 front(q) # [1] 4 q # First element: 4 v <- cpp_vector(c(TRUE, FALSE, FALSE)) v # TRUE FALSE FALSE front(v) # [1] TRUE
q <- cpp_queue(4:6) q # First element: 4 front(q) # [1] 4 q # First element: 4 v <- cpp_vector(c(TRUE, FALSE, FALSE)) v # TRUE FALSE FALSE front(v) # [1] TRUE
Add elements to a container by reference.
insert(x, values, keys = NULL, position = NULL)
insert(x, values, keys = NULL, position = NULL)
x |
A CppSet, CppUnorderedSet, CppMultiset, CppUnorderedMultiset, CppMap, CppUnorderedMap, CppMultimap, CppUnorderedMultimap, CppVector, CppDeque, or CppList object. |
values |
Values to add to |
keys |
Keys to add to |
position |
Index at which to insert elements into |
Existing container values are not overwritten. I.e. inserting a key-value pair into a map that already contains that key preserves the old value and discards the new one. Use insert_or_assign to overwrite values.
Invisibly returns NULL
.
assign, emplace, insert_after, insert_or_assign, push.
s <- cpp_multiset(4:6) s # 4 5 6 insert(s, 6:7) s # 4 5 6 6 7 m <- cpp_map(c("hello", "there", "world"), 9:11) m # ["hello",9] ["there",10] ["world",11] insert(m, 12L, "there") m # ["hello",9] ["there",10] ["world",11]
s <- cpp_multiset(4:6) s # 4 5 6 insert(s, 6:7) s # 4 5 6 6 7 m <- cpp_map(c("hello", "there", "world"), 9:11) m # ["hello",9] ["there",10] ["world",11] insert(m, 12L, "there") m # ["hello",9] ["there",10] ["world",11]
Add elements to a forward list by reference.
insert_after(x, values, position = NULL)
insert_after(x, values, position = NULL)
x |
A CppForwardList object. |
values |
Values to add to |
position |
Index behind which to insert elements. |
Invisibly returns NULL
.
emplace, emplace_after, insert, insert_or_assign.
v <- cpp_forward_list(4:6) v # 4 5 6 insert_after(v, 10:11, 2) v # 4 5 10 11 6
v <- cpp_forward_list(4:6) v # 4 5 6 insert_after(v, 10:11, 2) v # 4 5 10 11 6
Add elements to a container by reference. Overwrites existing container values tied to the same keys.
insert_or_assign(x, values, keys)
insert_or_assign(x, values, keys)
x |
A CppMap or CppUnorderedMap object. |
values |
Values to add to |
keys |
Keys to add to |
Use insert to avoid overwriting values.
Invisibly returns NULL
.
insert, insert_after, emplace
, try_emplace
.
m <- cpp_map(4:6, 9:11) m # [4,9] [5,10] [6,11] insert_or_assign(m, 12:13, 6:7) m # [4,9] [5,10] [6,12] [7,13]
m <- cpp_map(4:6, 9:11) m # [4,9] [5,10] [6,11] insert_or_assign(m, 12:13, 6:7) m # [4,9] [5,10] [6,12] [7,13]
Get the mean number of elements per bucket.
load_factor(x)
load_factor(x)
x |
A CppUnorderedSet, CppUnorderedMultiset, CppUnorderedMap, or CppUnorderedMultimap object. |
Returns a numeric.
bucket_count, max_bucket_count, max_load_factor.
s <- cpp_unordered_set(6:9) load_factor(s) # [1] 0.3076923
s <- cpp_unordered_set(6:9) load_factor(s) # [1] 0.3076923
Obtain the maximum number of buckets the container can hold.
max_bucket_count(x)
max_bucket_count(x)
x |
A CppUnorderedSet, CppUnorderedMultiset, CppUnorderedMap, or CppUnorderedMultimap object. |
Returns a numeric.
bucket_count, load_factor, max_load_factor.
s <- cpp_unordered_set(6:10) max_bucket_count(s) # [1] 1.152922e+18
s <- cpp_unordered_set(6:10) max_bucket_count(s) # [1] 1.152922e+18
Get or set the maximum load factor by reference, i.e. the number of elements per bucket.
max_load_factor(x, max_load = NULL)
max_load_factor(x, max_load = NULL)
x |
A CppUnorderedSet, CppUnorderedMultiset, CppUnorderedMap, or CppUnorderedMultimap object. |
max_load |
The containers maximum load factor. If |
Returns a numeric, if max_load
is NULL
. Invisibly returns NULL
, if max_load
is numeric.
bucket_count, load_factor, max_bucket_count.
s <- cpp_unordered_set(4:6) max_load_factor(s) # [1] 1 max_load_factor(s, 3) max_load_factor(s) # [1] 3
s <- cpp_unordered_set(4:6) max_load_factor(s) # [1] 1 max_load_factor(s, 3) max_load_factor(s) # [1] 3
Obtain the maximum number of elements the container can hold.
max_size(x)
max_size(x)
x |
A CppSet, CppUnorderedSet, CppMultiset, CppUnorderedMultiset, CppMap, CppUnorderedMap, CppMultimap, CppUnorderedMultimap, CppVector, CppDeque, CppForwardList, or CppList object. |
Returns a numeric.
capacity, max_bucket_count, max_load_factor, size.
s <- cpp_deque(4:6) s # 4 5 6 max_size(s) # [1] 4.611686e+18
s <- cpp_deque(4:6) s # 4 5 6 max_size(s) # [1] 4.611686e+18
Merge two objects by reference.
merge(x, y, ...)
merge(x, y, ...)
x |
A CppSet, CppUnorderedSet, CppMultiset, CppUnorderedMultiset, CppMap, CppUnorderedMap, CppMultimap, CppUnorderedMultimap, CppForwardList, or CppList object. |
y |
A CppSet, CppUnorderedSet, CppMultiset, CppUnorderedMultiset, CppMap, CppUnorderedMap, CppMultimap, CppUnorderedMultimap, CppForwardList, or
CppList object of the same class and data type as |
... |
Ignored. Only included for compatibility with generic |
In containers enforcing uniqueness (CppSet, CppUnorderedSet, CppMap, CppUnorderedMap), the function merges elements from y
that are not
in x
into x
and deletes them from y
. In other container types, it transfers all elements.
Invisibly returns NULL
.
x <- cpp_set(c("hello", "there")) y <- cpp_set(c("hello", "world")) merge(x, y) x # "hello" "there" "world" y # "hello" x <- cpp_forward_list(c(1, 3, 4, 3)) y <- cpp_forward_list(c(2, 3, 5)) merge(x, y) x # 1 2 3 3 4 3 5 y #
x <- cpp_set(c("hello", "there")) y <- cpp_set(c("hello", "world")) merge(x, y) x # "hello" "there" "world" y # "hello" x <- cpp_forward_list(c(1, 3, 4, 3)) y <- cpp_forward_list(c(2, 3, 5)) merge(x, y) x # 1 2 3 3 4 3 5 y #
Remove top element in a stack or priority queue or the first element in a queue by reference.
pop(x)
pop(x)
x |
A CppStack, CppQueue, or CppPriorityQueue object. |
In a stack, it is the last inserted element. In a queue, it is the first inserted element. In a descending (ascending) priority queue, it is the largest (smallest) value.
Invisibly returns NULL
.
back, emplace, front, push, top.
s <- cpp_stack(4:6) s # Top element: 6 pop(s) s # Top element: 5 q <- cpp_queue(4:6) q # First element: 4 pop(q) q # First element: 5 p <- cpp_priority_queue(4:6) p # First element: 6 pop(p) p # First element: 5
s <- cpp_stack(4:6) s # Top element: 6 pop(s) s # Top element: 5 q <- cpp_queue(4:6) q # First element: 4 pop(q) q # First element: 5 p <- cpp_priority_queue(4:6) p # First element: 6 pop(p) p # First element: 5
Remove an element from the back of the container by reference.
pop_back(x)
pop_back(x)
x |
A CppVector, CppDeque, or CppList object. |
Invisibly returns NULL
.
back, emplace_back, pop, pop_front, push_back.
l <- cpp_list(4:6) l # 4 5 6 pop_back(l) l # 4 5
l <- cpp_list(4:6) l # 4 5 6 pop_back(l) l # 4 5
Remove an element from the front of the container by reference.
pop_front(x)
pop_front(x)
x |
A CppDeque, CppForwardList, or CppList object. |
Invisibly returns NULL
.
emplace_front, front, pop, pop_back, push_front.
d <- cpp_deque(4:6) d # 4 5 6 pop_front(d) d # 5 6
d <- cpp_deque(4:6) d # 4 5 6 pop_front(d) d # 5 6
Print the data in a container.
print(x, ...)
print(x, ...)
x |
A |
... |
An ellipsis for compatibility with the generic method. Accepts the parameters |
print
has no side effects. Unlike to_r, it does not remove elements from stacks or queues.
Invisibly returns NULL
.
s <- cpp_set(4:9) print(s) # 4 5 6 7 8 9 print(s, n = 3) # 4 5 6 print(s, n = -3) # 9 8 7 print(s, from = 5, to = 7) # 5 6 7 v <- cpp_vector(4:9) print(v, n = 2) # 4 5 print(v, from = 2, to = 3) # 5 6 print(v, from = 3) # 6 7 8 9
s <- cpp_set(4:9) print(s) # 4 5 6 7 8 9 print(s, n = 3) # 4 5 6 print(s, n = -3) # 9 8 7 print(s, from = 5, to = 7) # 5 6 7 v <- cpp_vector(4:9) print(v, n = 2) # 4 5 print(v, from = 2, to = 3) # 5 6 print(v, from = 3) # 6 7 8 9
Add elements to the top of a stack, to the back of a queue, or to a priority queue by reference.
push(x, values)
push(x, values)
x |
A CppStack, CppQueue, or CppPriorityQueue object. |
values |
Values to add to |
The method iterates through values
starting at the front of the vector. I.e., the last element of values
is added last.
Invisibly returns NULL
.
back, emplace, front, pop, push, top.
s <- cpp_stack(1:4) s # Top element: 4 push(s, 8:9) s # Top element: 9
s <- cpp_stack(1:4) s # Top element: 4 push(s, 8:9) s # Top element: 9
Add an element to the back of a container by reference.
push_back(x, value)
push_back(x, value)
x |
A CppVector, CppDeque, or CppList object. |
value |
A value to add to |
Invisibly returns NULL
.
back, emplace_back, insert, pop_back, push_front.
v <- cpp_vector(4:6) v # 4 5 6 push_back(v, 14L) v # 4 5 6 14
v <- cpp_vector(4:6) v # 4 5 6 push_back(v, 14L) v # 4 5 6 14
Add an element to the front of a container by reference.
push_front(x, value)
push_front(x, value)
x |
A CppDeque, CppForwardList, or CppList object. |
value |
A value to add to |
Invisibly returns NULL
.
emplace_front, front, insert, pop_front, push_back.
d <- cpp_deque(4:6) d # 4 5 6 push_front(d, 14L) d # 14 4 5 6
d <- cpp_deque(4:6) d # 4 5 6 push_front(d, 14L) d # 14 4 5 6
Set a container's minimum bucket count and rehash by reference.
rehash(x, n = 0)
rehash(x, n = 0)
x |
A CppUnorderedSet, CppUnorderedMultiset, CppUnorderedMap, or CppUnorderedMultimap object. |
n |
The minimum number of buckets. A value of |
Invisibly returns NULL
.
bucket_count, load_factor, max_bucket_count, max_load_factor, reserve.
s <- cpp_unordered_set(4:6) rehash(s) rehash(s, 3)
s <- cpp_unordered_set(4:6) rehash(s) rehash(s, 3)
Remove elements from a container by reference.
remove.(x, value)
remove.(x, value)
x |
A CppForwardList or CppList object. |
value |
A value to delete from |
The method ends with a dot to avoid compatibility issues with the generic base::remove
.
Invisibly returns NULL
.
l <- cpp_forward_list(4:6) l # 4 5 6 remove.(l, 5L) l # 4 6
l <- cpp_forward_list(4:6) l # 4 5 6 remove.(l, 5L) l # 4 6
Reserve space for the container by reference.
reserve(x, n)
reserve(x, n)
x |
A CppUnorderedSet, CppUnorderedMultiset, CppUnorderedMap, CppUnorderedMultimap, or CppVector object. |
n |
The minimum number of elements per bucket. |
In case of a CppUnorderedSet, CppUnorderedMultiset, CppUnorderedMap, CppUnorderedMultimap, the method sets the number of buckets to be able to
hold at least n
elements and rehashes. In case of a CppVector, the method sets the capacity to n
.
Invisibly returns NULL
.
bucket_count, capacity, load_factor, max_bucket_count, max_load_factor.
s <- cpp_unordered_set(4:6) bucket_count(s) # [1] 13 reserve(s, 3) bucket_count(s) # [1] 5 v <- cpp_vector(4:6) capacity(v) # [1] 3 reserve(v, 10) capacity(v) # [1] 10
s <- cpp_unordered_set(4:6) bucket_count(s) # [1] 13 reserve(s, 3) bucket_count(s) # [1] 5 v <- cpp_vector(4:6) capacity(v) # [1] 3 reserve(v, 10) capacity(v) # [1] 10
Alter the size of the container by reference.
resize(x, size, value = NULL)
resize(x, size, value = NULL)
x |
A |
size |
The new size of the container. |
value |
The value of new elements. It defaults to |
If the new size is larger than the former size, the function sets newly added elements in the back to value
.
Invisibly returns NULL
.
v <- cpp_vector(4:9) v # 4 5 6 7 8 9 size(v) # 6 resize(v, 10) v # 4 5 6 7 8 9 0 0 0 0 resize(v, 3) v # 4 5 6
v <- cpp_vector(4:9) v # 4 5 6 7 8 9 size(v) # 6 resize(v, 10) v # 4 5 6 7 8 9 0 0 0 0 resize(v, 3) v # 4 5 6
Reverses the order of the elements in the container by reference.
reverse(x)
reverse(x)
x |
A CppForwardList or CppList object. |
Invisibly returns NULL
.
l <- cpp_forward_list(4:9) l # 4 5 6 7 8 9 reverse(l) l #
l <- cpp_forward_list(4:9) l # 4 5 6 7 8 9 reverse(l) l #
Shrink the capacity of a container to its size by reference.
shrink_to_fit(x)
shrink_to_fit(x)
x |
A CppVector or CppDeque object. |
The capacity is the space, in terms of the number of elements, reserved for a container. The size is the number of elements in the container.
Invisibly returns NULL
.
v <- cpp_vector(4:6) capacity(v) # [1] 3 reserve(v, 10) capacity(v) # [1] 10 shrink_to_fit(v) capacity(v) # [1] 3
v <- cpp_vector(4:6) capacity(v) # [1] 3 reserve(v, 10) capacity(v) # [1] 10 shrink_to_fit(v) capacity(v) # [1] 3
Obtain the number of elements in a container.
size(x)
size(x)
x |
A CppSet, CppUnorderedSet, CppMultiset, CppUnorderedMultiset, CppMap, CppUnorderedMap, CppMultimap, CppUnorderedMultimap, CppStack, CppQueue, CppPriorityQueue, CppVector, CppDeque, or CppList object. |
Returns a numeric.
bucket_count, capacity, load_factor, max_size, resize.
s <- cpp_unordered_set(4:6) s # 6 5 4 size(s) # [1] 3
s <- cpp_unordered_set(4:6) s # 6 5 4 size(s) # [1] 3
Sorts the elements in a container by reference.
sort(x, decreasing, ...)
sort(x, decreasing, ...)
x |
A CppForwardList or CppList object. |
decreasing |
Ignored. |
... |
Ignored. |
decreasing
and ...
are only included for compatibility with the generic base::sort
method and have no effect.
Invisibly returns NULL
.
l <- cpp_forward_list(c(3, 2, 4)) l # 3 2 4 sort(l) l # 2 3 4
l <- cpp_forward_list(c(3, 2, 4)) l # 3 2 4 sort(l) l # 2 3 4
Print the sorting order of a priority queue.
sorting(x)
sorting(x)
x |
An CppPriorityQueue object. |
Returns "ascending"
or "descending"
.
q <- cpp_priority_queue(4:6) sorting(q) # [1] "descending" q <- cpp_priority_queue(4:6, "ascending") sorting(q) # [1] "ascending"
q <- cpp_priority_queue(4:6) sorting(q) # [1] "descending" q <- cpp_priority_queue(4:6, "ascending") sorting(q) # [1] "ascending"
Move elements from one list to another list by reference.
splice(x, y, x_position, y_from, y_to)
splice(x, y, x_position, y_from, y_to)
x |
A CppList object to which to add elements. |
y |
A CppList object, of the same data type as |
x_position |
Index at which to insert elements in |
y_from |
Index of the first element to extract from |
y_to |
Index of the last element to extract from |
Invisibly returns NULL
.
x <- cpp_list(4:9) x # 4 5 6 7 8 9 y <- cpp_list(10:12) y # 10 11 12 splice(x, y, 3, 2, 3) x # 4 5 11 12 6 7 8 9 y # 10
x <- cpp_list(4:9) x # 4 5 6 7 8 9 y <- cpp_list(10:12) y # 10 11 12 splice(x, y, 3, 2, 3) x # 4 5 11 12 6 7 8 9 y # 10
Move elements from one forward list to another forward list by reference.
splice_after(x, y, x_position, y_from, y_to)
splice_after(x, y, x_position, y_from, y_to)
x |
A CppForwardList object to which to add elements. |
y |
A CppForwardList object, of the same data type as |
x_position |
Index after which to insert elements in |
y_from |
Index after which to extract elements from |
y_to |
Index of the last element to extract from |
Indices start at 1, which is also the minimum value permitted. Thus, the current implementation in this package does not allow to move the
first element of y
.
Invisibly returns NULL
.
x <- cpp_forward_list(4:9) x # 4 5 6 7 8 9 y <- cpp_forward_list(10:12) y # 10 11 12 splice_after(x, y, 3, 1, 3) x # 4 5 6 11 12 7 8 9 y # 10
x <- cpp_forward_list(4:9) x # 4 5 6 7 8 9 y <- cpp_forward_list(10:12) y # 10 11 12 splice_after(x, y, 3, 1, 3) x # 4 5 6 11 12 7 8 9 y # 10
Export C++ data to an R object.
to_r(x, n = NULL, from = NULL, to = NULL)
to_r(x, n = NULL, from = NULL, to = NULL)
x |
A |
n |
The number of elements to export. If |
from |
The first value in CppSet, CppMultiset, CppMap, CppMultimap objects to export. If it is not a member of |
to |
The last value in CppSet, CppMultiset, CppMap, CppMultimap objects to export. If it is not a member of |
to_r
has side effects, when applied to stacks, queues, or priority queues. These container types are not iterable. Hence,
to_r
removes elements from the CppStack, CppQueue, and CppPriorityQueue objects when exporting them to R. When n
is specified,
the method removes the top n
elements from a stack or priority queue or the first n
elements from a queue. Otherwise, it removes all
elements. Other container types, like sets, etc., are unaffected.
Returns a vector in case of CppSet, CppUnorderedSet, CppMultiset, CppUnorderedMultiset, CppStack, CppQueue, CppPriorityQueue, CppVector, CppDeque, CppForwardList, and CppList objects. Returns a data frame in case of CppMap, CppUnorderedMap, CppMultimap, and CppUnorderedMultimap objects.
s <- cpp_set(11:20) to_r(s) # [1] 11 12 13 14 15 16 17 18 19 20 to_r(s, n = 4) # [1] 11 12 13 14 to_r(s, n = -4) # [1] 20 19 18 17 to_r(s, from = 14) # [1] 14 15 16 17 18 19 20 to_r(s, to = 18) # [1] 11 12 13 14 15 16 17 18 to_r(s, from = 14, to = 18) # [1] 14 15 16 17 18 m <- cpp_unordered_multimap(c("hello", "hello", "there"), 4:6) to_r(m) # key value # 1 there 6 # 2 hello 4 # 3 hello 5 s <- cpp_stack(11:20) to_r(s, n = 3) # [1] 20 19 18 s # Top element: 17
s <- cpp_set(11:20) to_r(s) # [1] 11 12 13 14 15 16 17 18 19 20 to_r(s, n = 4) # [1] 11 12 13 14 to_r(s, n = -4) # [1] 20 19 18 17 to_r(s, from = 14) # [1] 14 15 16 17 18 19 20 to_r(s, to = 18) # [1] 11 12 13 14 15 16 17 18 to_r(s, from = 14, to = 18) # [1] 14 15 16 17 18 m <- cpp_unordered_multimap(c("hello", "hello", "there"), 4:6) to_r(m) # key value # 1 there 6 # 2 hello 4 # 3 hello 5 s <- cpp_stack(11:20) to_r(s, n = 3) # [1] 20 19 18 s # Top element: 17
Access the top element in a container without removing it.
top(x)
top(x)
x |
A CppStack or CppPriorityQueue object. |
Returns the top element.
s <- cpp_stack(1:4) s # Top element: 4 top(s) # [1] 4 s # Top element: 4
s <- cpp_stack(1:4) s # Top element: 4 top(s) # [1] 4 s # Top element: 4
Add an element to a container by reference in place, if it does not exist yet.
try_emplace(x, value, key)
try_emplace(x, value, key)
x |
A CppMap or CppUnorderedMap object. |
value |
A value to add to |
key |
A key to add to |
Existing container values are not overwritten. I.e., inserting a key-value pair into a map that already contains that key preserves the old value and discards the new one. Use insert_or_assign to overwrite values.
emplace and try_emplace
produce the same results in the context of this package. try_emplace
can be minimally more computationally
efficient than emplace.
Invisibly returns NULL
.
emplace, emplace_after, emplace_back, emplace_front, insert, insert_or_assign.
m <- cpp_map(4:6, 9:11) m # [4,9] [5,10] [6,11] try_emplace(m, 13L, 8L) m # [4,9] [5,10] [6,11] [8,13] try_emplace(m, 12L, 4L) m # [4,9] [5,10] [6,11] [8,13]
m <- cpp_map(4:6, 9:11) m # [4,9] [5,10] [6,11] try_emplace(m, 13L, 8L) m # [4,9] [5,10] [6,11] [8,13] try_emplace(m, 12L, 4L) m # [4,9] [5,10] [6,11] [8,13]
Obtain the data type of a container.
type(x)
type(x)
x |
A |
The available types are integer, double, string, and boolean. They correspond to the integer, numeric/ double, character, and logical types in R.
A named character vector for CppMap, CppUnorderedMap, CppMultimap, and CppUnorderedMultimap objects. A character otherwise.
s <- cpp_set(4:6) type(s) # [1] "integer" m <- cpp_unordered_map(c("hello", "world"), c(0.5, 1.5)) type(m) # key value # "string" "double"
s <- cpp_set(4:6) type(s) # [1] "integer" m <- cpp_unordered_map(c("hello", "world"), c(0.5, 1.5)) type(m) # key value # "string" "double"
Erases consecutive duplicated values from the container by reference.
unique(x, incomparables, ...)
unique(x, incomparables, ...)
x |
A CppForwardList or CppList object. |
incomparables |
Ignored. |
... |
Ignored. |
Duplicated, non-consecutive elements are not removed.
incomparables
and ...
are only included for compatibility with the generic base::unique
method and have no effect.
Returns the number of deleted elements.
l <- cpp_forward_list(c(4, 5, 6, 6, 4)) l # 4 5 6 6 4 unique(l) # [1] 1 l # 4 5 6 4
l <- cpp_forward_list(c(4, 5, 6, 6, 4)) l # 4 5 6 6 4 unique(l) # [1] 1 l # 4 5 6 4