Skip to content

Commit b21584c

Browse files
committed
feat: add dstructs/stack
--- type: pre_commit_static_analysis_report description: Results of running static analysis checks when committing changes. report: - task: lint_filenames status: passed - task: lint_editorconfig status: passed - task: lint_markdown status: passed - task: lint_package_json status: passed - task: lint_repl_help status: passed - task: lint_javascript_src status: passed - task: lint_javascript_cli status: na - task: lint_javascript_examples status: passed - task: lint_javascript_tests status: passed - task: lint_javascript_benchmarks status: passed - task: lint_python status: na - task: lint_r status: na - task: lint_c_src status: na - task: lint_c_examples status: na - task: lint_c_benchmarks status: na - task: lint_c_tests_fixtures status: na - task: lint_shell status: na - task: lint_typescript_declarations status: passed - task: lint_typescript_tests status: passed - task: lint_license_headers status: passed ---
1 parent 54f310f commit b21584c

10 files changed

Lines changed: 2014 additions & 0 deletions

File tree

Lines changed: 348 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,348 @@
1+
<!--
2+
3+
@license Apache-2.0
4+
5+
Copyright (c) 2018 The Stdlib Authors.
6+
7+
Licensed under the Apache License, Version 2.0 (the "License");
8+
you may not use this file except in compliance with the License.
9+
You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software
14+
distributed under the License is distributed on an "AS IS" BASIS,
15+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
See the License for the specific language governing permissions and
17+
limitations under the License.
18+
19+
-->
20+
21+
# Stack
22+
23+
> Stack data structure.
24+
25+
<!-- Section to include introductory text. Make sure to keep an empty line after the intro `section` element and another before the `/section` close. -->
26+
27+
<section class="intro">
28+
29+
</section>
30+
31+
<!-- /.intro -->
32+
33+
<!-- Package usage documentation. -->
34+
35+
<section class="usage">
36+
37+
## Usage
38+
39+
```javascript
40+
var stack = require( '@stdlib/dstructs/stack' );
41+
```
42+
43+
#### stack()
44+
45+
Returns a `Stack` instance.
46+
47+
```javascript
48+
var s = stack();
49+
// returns <Stack>
50+
```
51+
52+
##### s.clear()
53+
54+
Clears a stack.
55+
56+
```javascript
57+
var s = stack();
58+
// returns <Stack>
59+
60+
// Add values to the stack:
61+
s.push( 'foo' ).push( 'bar' );
62+
63+
// Peek at the top value:
64+
var v = s.first();
65+
// returns 'bar'
66+
67+
// Examine the stack length:
68+
var len = s.length;
69+
// returns 2
70+
71+
// Clear all stack items:
72+
s.clear();
73+
74+
// Peek at the top value:
75+
v = s.first();
76+
// returns undefined
77+
78+
// Examine the stack length:
79+
len = s.length;
80+
// returns 0
81+
```
82+
83+
##### s.first()
84+
85+
Returns the top stack value (i.e., the value which is "first-out"). If the stack is currently empty, the returned value is `undefined`.
86+
87+
```javascript
88+
var s = stack();
89+
// returns <Stack>
90+
91+
// Add values to the stack:
92+
s.push( 'foo' ).push( 'bar' );
93+
94+
// Peek at the top value:
95+
var v = s.first();
96+
// returns 'bar'
97+
```
98+
99+
##### s.iterator()
100+
101+
Returns an iterator for iterating over a stack. If an environment supports `Symbol.iterator`, the returned iterator is iterable.
102+
103+
```javascript
104+
var s = stack();
105+
106+
// Add values to the stack:
107+
s.push( 'foo' ).push( 'bar' );
108+
109+
// Create an iterator:
110+
var it = s.iterator();
111+
112+
// Iterate over the stack...
113+
var v = it.next().value;
114+
// returns 'bar'
115+
116+
v = it.next().value;
117+
// returns 'foo'
118+
119+
var bool = it.next().done;
120+
// returns true
121+
```
122+
123+
**Note**: in order to prevent confusion arising from stack mutation during iteration, a returned iterator **always** iterates over a stack "snapshot", which is defined as the list of stack elements at the time of `s.iterator()` invocation.
124+
125+
##### s.last()
126+
127+
Returns the bottom stack value (i.e., the value which is "last-out"). If the stack is currently empty, the returned value is `undefined`.
128+
129+
```javascript
130+
var s = stack();
131+
// returns <Stack>
132+
133+
// Add values to the stack:
134+
s.push( 'foo' ).push( 'bar' );
135+
136+
// Peek at the bottom value:
137+
var v = s.last();
138+
// returns 'foo'
139+
```
140+
141+
##### s.length
142+
143+
Stack length.
144+
145+
```javascript
146+
var s = stack();
147+
148+
// Examine the initial stack length:
149+
var len = s.length;
150+
// returns 0
151+
152+
// Add values to the stack:
153+
s.push( 'foo' ).push( 'bar' );
154+
155+
// Retrieve the current stack length:
156+
len = s.length;
157+
// returns 2
158+
```
159+
160+
##### s.pop()
161+
162+
Removes a value from the stack. If the stack is currently empty, the returned value is `undefined`.
163+
164+
```javascript
165+
var s = stack();
166+
167+
// Add values to the stack:
168+
s.push( 'foo' ).push( 'bar' );
169+
170+
// Remove the top value:
171+
var v = s.pop();
172+
// returns 'bar'
173+
174+
// Add a new value to the stack:
175+
s.push( 'beep' );
176+
177+
// Remove the top value:
178+
v = s.pop();
179+
// returns 'beep'
180+
```
181+
182+
##### s.push( value )
183+
184+
Adds a value to the stack.
185+
186+
```javascript
187+
var s = stack();
188+
189+
// Add values to the stack:
190+
s.push( 'foo' ).push( 'bar' );
191+
192+
// Remove the top value:
193+
var v = s.pop();
194+
// returns 'bar'
195+
196+
// Add a new value to the stack:
197+
s.push( 'beep' );
198+
199+
// Remove the top value:
200+
v = s.pop();
201+
// returns 'beep'
202+
```
203+
204+
##### s.toArray()
205+
206+
Returns an array of stack values.
207+
208+
```javascript
209+
var s = stack();
210+
211+
// Add values to the stack:
212+
s.push( 'foo' ).push( 'bar' );
213+
214+
// Get an array of stack values:
215+
var vals = s.toArray();
216+
// returns [ 'bar', 'foo' ]
217+
```
218+
219+
**Note**: the order of the returned array is reverse stack insertion order (i.e., the "newest" stack elements come before the "oldest" stack elements).
220+
221+
##### s.toJSON()
222+
223+
Serializes a stack as JSON.
224+
225+
```javascript
226+
var s = stack();
227+
228+
// Add values to the stack:
229+
s.push( 'foo' ).push( 'bar' );
230+
231+
// Serialize to JSON:
232+
var o = s.toJSON();
233+
// returns { 'type': 'stack', 'data': [ 'bar', 'foo' ] }
234+
```
235+
236+
**Note**: `JSON.stringify()` implicitly calls this method when stringifying a stack instance.
237+
238+
</section>
239+
240+
<!-- /.usage -->
241+
242+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
243+
244+
<section class="notes">
245+
246+
## Notes
247+
248+
- A stack is also known as a Last-In-First-Out (LIFO) queue.
249+
250+
</section>
251+
252+
<!-- /.notes -->
253+
254+
<!-- Package usage examples. -->
255+
256+
<section class="examples">
257+
258+
## Examples
259+
260+
<!-- eslint no-undef: "error" -->
261+
262+
<!-- eslint-disable stdlib/no-redeclare -->
263+
264+
```javascript
265+
var Stack = require( '@stdlib/dstructs/stack' );
266+
267+
// Create a new stack:
268+
var stack = new Stack();
269+
270+
// Add some values to the stack:
271+
stack.push( 'foo' );
272+
stack.push( 'bar' );
273+
stack.push( 'beep' );
274+
stack.push( 'boop' );
275+
276+
// Peek at the top and bottom stack values:
277+
var v = stack.first();
278+
// returns 'boop'
279+
280+
v = stack.last();
281+
// returns 'foo'
282+
283+
// Inspect the stack length:
284+
var len = stack.length;
285+
// returns 4
286+
287+
// Remove the top value:
288+
v = stack.pop();
289+
// returns 'boop'
290+
291+
// Inspect the stack length:
292+
len = stack.length;
293+
// returns 3
294+
295+
// Iterate over the stack:
296+
var iter = stack.iterator();
297+
var i;
298+
for ( i = 0; i < len; i++ ) {
299+
console.log( 'Stack value #%d: %s', i+1, iter.next().value );
300+
}
301+
302+
// Clear the stack:
303+
stack.clear();
304+
305+
// Inspect the stack length:
306+
len = stack.length;
307+
// returns 0
308+
```
309+
310+
</section>
311+
312+
<!-- /.examples -->
313+
314+
<!-- Section to include cited references. If references are included, add a horizontal rule *before* the section. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
315+
316+
<section class="references">
317+
318+
</section>
319+
320+
<!-- /.references -->
321+
322+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
323+
324+
<section class="related">
325+
326+
* * *
327+
328+
## See Also
329+
330+
- <span class="package-name">[`@stdlib/utils/fifo`][@stdlib/utils/fifo]</span><span class="delimiter">: </span><span class="description">first-in-first-out (FIFO) queue.</span>
331+
332+
</section>
333+
334+
<!-- /.related -->
335+
336+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
337+
338+
<section class="links">
339+
340+
<!-- <related-links> -->
341+
342+
[@stdlib/utils/fifo]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/fifo
343+
344+
<!-- </related-links> -->
345+
346+
</section>
347+
348+
<!-- /.links -->

0 commit comments

Comments
 (0)