linkedList: Linked List manipulation functions

Description Usage Arguments Details

Description

linkedList() creates an empty linked list object.

.node() creates a node with specified content

.push() adds a new element to a list, and returns the larger list

.drop() removes the first occurrence of 'content' in a list and returns the resulting list. .drop() compares elements using the function identical.

Both .map() and .apply() apply a function over all the elements of a list. Their difference is that .apply returns a numerical vector with the same length as the linkedList, while .map simply applies a function and does not collect any result.

as.list() creates a list with the content of each node, and as.vector() creates a similar vector, with mode inferred from the first element. Notice that converting to vector might incur type errors, so use with caution.

The [[ ]] operator is provided for accessing the content of the i-th element in a linked list, but notice that this is a time consuming operation!

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
linkedList()

## S3 method for class 'linkedList'
print(list)

## S3 method for class 'linkedList'
length(list)

.node(content, points.to = NULL)

.push(list, content)

.drop(list, content)

.map(list, FUN, ...)

.apply(list, FUN, ...)

## S3 method for class 'linkedList'
as.list(list)

as.vector.linkedList(list, mode)

## S3 method for class 'linkedList'
list[[i]]

Arguments

list

an object of class linkedList

content

the content for the node that's being generated

points.to

next node in the list (if 'pushing', the old list head)

FUN

function to be applied

mode

optional character string specifying the mode of the vector created by as.vector(). If left blank, mode will be inferred from the first element.

i

position in the list that should be returned. Note that the last element pushed is the element 1!

...

additional arguments to be passed to FUN

Details

A linked list is a data structure that's specially suitable for insertion/deletion of objects in its middle. Deleting an element in the middle of a vector causes the rest of the vector to be shifted, which can be a performance issue if this operation is carried over many times. In a linked list, on the other hand, deleting an element in the middle is a constant-time operation. The main disadvantage of using linked lists comes from the fact that vectors are optimized for random access - in other words, x[25] is a very fast operation for a vector, but may take a long time in a linked list.

The current implementation is VERY minimalistic, as it strives for being a reasonably efficient replacement for the base "list" type in the specific context of a population that frequently loses members. Also note that environment R objects are large, so these functions may lead to heavier memory usage in comparison with standard vectors or lists (in the order of kilobytes per list item).


andrechalom/TWoLifR documentation built on May 12, 2019, 3:34 a.m.