simepleStack: A simple stack structure and methods

Description Value Methods Examples

Description

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

 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
my_stack <- simepleStack$new()
# check length
my_stack$len()
# add some thing
my_stack$push(list(1, 2, 3))
# print current stack
str(my_stack$get())
# check length
my_stack$len()
# add before the current first
my_stack$push(list(0), after = 0)
# print current stack
str(my_stack$get())
# pop one item
my_stack$pop()
# print current stack
str(my_stack$get())
# pop one item from the tail
my_stack$pop(tail = TRUE)
# print current stack
str(my_stack$get())
# pop more than one items
my_stack$pop(2)
# print current stack
str(my_stack$get()) # nothing left

spsUtil documentation built on Oct. 31, 2021, 1:06 a.m.