-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.Rmd
103 lines (71 loc) · 3.21 KB
/
README.Rmd
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{r, echo = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "README-"
)
```
[![Travis-CI Build Status](https://travis-ci.org/Thormidable/statestackr.svg?branch=master)](https://travis-ci.org/Thormidable/statestackr)
[![AppVeyor Build Status](https://ci.appveyor.com/api/projects/status/github/Thormidable/statestackr?branch=master&svg=true)](https://ci.appveyor.com/project/Thormidable/statestackr)
[![codecov](https://codecov.io/gh/Thormidable/statestackr/branch/master/graph/badge.svg)](https://codecov.io/gh/Thormidable/statestackr)
# StateStack
## Introduction
An R package implementing a push/pop stack which allows for reading and writing to the top value of the stack. An example of a use for this class would be to store a series of states while traversing a tree structure.
The stack will be created with a single element in the stack to be used as the current state, this value can be specified in the constructor or if undefined will default to 0. As such starting `$depth()` is 1 and cannot be reduced beneath 1.
StateStack has the following functions: `$push()`, `$pop()`, `$peek()`, `$set()`, `$depth()` and `$clear()`.
It also has an active binding called `$state` which can be used to read or write to the current state (equivalent to using `$peek()` and `$set()`.)
Calling `$pop()` more times than `$push()` will throw an error.
`$push()` : Will push the supplied value to the top of the stack. If no value is supplied will copy the current state.
`$pop()` : Will pop the current state from the stack and return it.
`$peek()`: Will return the current state from the stack without removing it.
`$set()` : Will change the current state without changing the stack
`$depth()`: Will return the number of layers in the stack.
`$clear()`: Will clear the stack making the current state the supplied value.
## Utilisation
```{r}
library(statestackr)
#instantiate a new state Stack with a starting value of 7L
statestack <- StateStack$new(7L)
#check the current state
statestack$state
statestack$depth()
#Update the current state
statestack$state <- 2L
statestack$state
statestack$depth()
#Store the old state and make a new current state.
#Not supplying an argument will copy the current state and push it onto the stack.
#Changing the current state will not affect the first state on the stack.
statestack$push()
statestack$state
statestack$depth()
#Update the current state
statestack$state <- 3L
statestack$state
statestack$depth()
#Update the current state
statestack$state <- 1L
statestack$state
statestack$depth()
#Remove the top item of the stack and return it
statestack$pop()
statestack$depth()
#Check that the state stored before the previous push() has been retained
statestack$state
statestack$depth()
```
## Installation
To install from GitHub
```
# install.packages("devtools")
devtools::install_github("Thormidable/statestackr")
```
## Contribution
Please report any [issues](https://github.com/Thormidable/statestackr/issues).
[Pull requests](https://github.com/Thormidable/statestackr/pulls) are always welcome.
## Documentation
- [R6 class package](https://cran.r-project.org/package=R6)