A simple stack data structure in R, with supporting of assiocated methods, like push, pop and others.

Value

an R6 class object

Methods

Public methods


Method new()

initialize a new object

Usage

simepleStack$new(items = list(), limit = Inf)

Arguments

items

list, list of items to add to the initial stack

limit

int, how many items can be pushed to the stack, default is unlimited.


Method len()

returns current length of the stack

Usage

simepleStack$len()


Method get()

returns the full current stack of all items

Usage

simepleStack$get()


Method clear()

remove all items in current stack

Usage

simepleStack$clear()


Method push()

add item(s) to the stack

Usage

simepleStack$push(items, after = self$len())

Arguments

items

list, list of items to add to the stack

after

int, which position to push items after, default is after the current last item. 0 will be before the first item.


Method pop()

remove item(s) from the stack and return as results

Usage

simepleStack$pop(len = 1, tail = FALSE)

Arguments

len

int, how many items to pop from stack, default is 1 item a time.

tail

bool, to pop in the reverse order (from the last item)? Default is FALSE, pop from the top (first item).


Method clone()

The objects of this class are cloneable with this method.

Usage

simepleStack$clone(deep = FALSE)

Arguments

deep

Whether to make a deep clone.

Examples

my_stack <- simepleStack$new()
# check length
my_stack$len()
#> [1] 0
# add some thing
my_stack$push(list(1, 2, 3))
# print current stack
str(my_stack$get())
#> List of 3
#>  $ : num 1
#>  $ : num 2
#>  $ : num 3
# check length
my_stack$len()
#> [1] 3
# add before the current first
my_stack$push(list(0), after = 0)
# print current stack
str(my_stack$get())
#> List of 4
#>  $ : num 0
#>  $ : num 1
#>  $ : num 2
#>  $ : num 3
# pop one item
my_stack$pop()
#> [[1]]
#> [1] 0
#> 
# print current stack
str(my_stack$get())
#> List of 3
#>  $ : num 1
#>  $ : num 2
#>  $ : num 3
# pop one item from the tail
my_stack$pop(tail = TRUE)
#> [[1]]
#> [1] 3
#> 
# print current stack
str(my_stack$get())
#> List of 2
#>  $ : num 1
#>  $ : num 2
# pop more than one items
my_stack$pop(2)
#> [[1]]
#> [1] 1
#> 
#> [[2]]
#> [1] 2
#> 
# print current stack
str(my_stack$get()) # nothing left
#>  list()