Skip to content

Commit 9f66b7f

Browse files
committed
feat: add dstructs/fifo
--- 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 e208fa6 commit 9f66b7f

File tree

11 files changed

+2074
-0
lines changed

11 files changed

+2074
-0
lines changed
Lines changed: 340 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,340 @@
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+
# FIFO
22+
23+
> First-in-first-out (FIFO) queue.
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 fifo = require( '@stdlib/dstructs/fifo' );
41+
```
42+
43+
#### fifo()
44+
45+
Returns a new first-in-first-out (FIFO) queue instance.
46+
47+
```javascript
48+
var queue = fifo();
49+
// returns <FIFO>
50+
```
51+
52+
##### queue.clear()
53+
54+
Clears a queue.
55+
56+
```javascript
57+
var queue = fifo();
58+
// returns <FIFO>
59+
60+
// Add values to the queue:
61+
queue.push( 'foo' ).push( 'bar' );
62+
63+
// Peek at the first value:
64+
var v = queue.first();
65+
// returns 'foo'
66+
67+
// Examine the queue length:
68+
var len = queue.length;
69+
// returns 2
70+
71+
// Clear all queue items:
72+
queue.clear();
73+
74+
// Peek at the first value:
75+
v = queue.first();
76+
// returns undefined
77+
78+
// Examine the queue length:
79+
len = queue.length;
80+
// returns 0
81+
```
82+
83+
##### queue.first()
84+
85+
Returns the "oldest" queue value (i.e., the value which is "first-out"). If the queue is currently empty, the returned value is `undefined`.
86+
87+
```javascript
88+
var queue = fifo();
89+
// returns <FIFO>
90+
91+
// Add values to the queue:
92+
queue.push( 'foo' ).push( 'bar' );
93+
94+
// Peek at the first value:
95+
var v = queue.first();
96+
// returns 'foo'
97+
```
98+
99+
##### queue.iterator()
100+
101+
Returns an iterator for iterating over a queue. If an environment supports `Symbol.iterator`, the returned iterator is iterable.
102+
103+
```javascript
104+
var queue = fifo();
105+
106+
// Add values to the queue:
107+
queue.push( 'foo' ).push( 'bar' );
108+
109+
// Create an iterator:
110+
var it = queue.iterator();
111+
112+
// Iterate over the queue...
113+
var v = it.next().value;
114+
// returns 'foo'
115+
116+
v = it.next().value;
117+
// returns 'bar'
118+
119+
var bool = it.next().done;
120+
// returns true
121+
```
122+
123+
**Note**: in order to prevent confusion arising from queue mutation during iteration, a returned iterator **always** iterates over a queue "snapshot", which is defined as the list of queue elements at the time of `queue.iterator()` invocation.
124+
125+
##### queue.last()
126+
127+
Returns the "newest" queue value (i.e., the value which is "last-out"). If the queue is currently empty, the returned value is `undefined`.
128+
129+
```javascript
130+
var queue = fifo();
131+
// returns <FIFO>
132+
133+
// Add values to the queue:
134+
queue.push( 'foo' ).push( 'bar' );
135+
136+
// Peek at the last value:
137+
var v = queue.last();
138+
// returns 'bar'
139+
```
140+
141+
##### queue.length
142+
143+
Queue length.
144+
145+
```javascript
146+
var queue = fifo();
147+
148+
// Examine the initial queue length:
149+
var len = queue.length;
150+
// returns 0
151+
152+
// Add values to the queue:
153+
queue.push( 'foo' ).push( 'bar' );
154+
155+
// Retrieve the current queue length:
156+
len = queue.length;
157+
// returns 2
158+
```
159+
160+
##### queue.pop()
161+
162+
Removes a value from the queue. If the queue is currently empty, the returned value is `undefined`.
163+
164+
```javascript
165+
var queue = fifo();
166+
167+
// Add values to the queue:
168+
queue.push( 'foo' ).push( 'bar' );
169+
170+
// Remove the first value:
171+
var v = queue.pop();
172+
// returns 'foo'
173+
174+
// Add a new value to the queue:
175+
queue.push( 'beep' );
176+
177+
// Remove the "oldest" queue value:
178+
v = queue.pop();
179+
// returns 'bar'
180+
```
181+
182+
##### queue.push( value )
183+
184+
Adds a value to the queue.
185+
186+
```javascript
187+
var queue = fifo();
188+
189+
// Add values to the queue:
190+
queue.push( 'foo' ).push( 'bar' );
191+
192+
// Remove the first value:
193+
var v = queue.pop();
194+
// returns 'foo'
195+
196+
// Add a new value to the queue:
197+
queue.push( 'beep' );
198+
199+
// Remove the "oldest" queue value:
200+
v = queue.pop();
201+
// returns 'bar'
202+
```
203+
204+
##### queue.toArray()
205+
206+
Returns an array of queue values.
207+
208+
```javascript
209+
var queue = fifo();
210+
211+
// Add values to the queue:
212+
queue.push( 'foo' ).push( 'bar' );
213+
214+
// Get an array of queue values:
215+
var vals = queue.toArray();
216+
// returns [ 'foo', 'bar' ]
217+
```
218+
219+
##### queue.toJSON()
220+
221+
Serializes a queue as JSON.
222+
223+
```javascript
224+
var queue = fifo();
225+
226+
// Add values to the queue:
227+
queue.push( 'foo' ).push( 'bar' );
228+
229+
// Serialize to JSON:
230+
var o = queue.toJSON();
231+
// returns { 'type': 'fifo', 'data': [ 'foo', 'bar' ] }
232+
```
233+
234+
**Note**: `JSON.stringify()` implicitly calls this method when stringifying a queue instance.
235+
236+
</section>
237+
238+
<!-- /.usage -->
239+
240+
<!-- Package usage notes. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
241+
242+
<section class="notes">
243+
244+
</section>
245+
246+
<!-- /.notes -->
247+
248+
<!-- Package usage examples. -->
249+
250+
<section class="examples">
251+
252+
## Examples
253+
254+
<!-- eslint no-undef: "error" -->
255+
256+
```javascript
257+
var fifo = require( '@stdlib/dstructs/fifo' );
258+
259+
// Create a new FIFO queue:
260+
var queue = fifo();
261+
262+
// Add some values to the queue:
263+
queue.push( 'foo' );
264+
queue.push( 'bar' );
265+
queue.push( 'beep' );
266+
queue.push( 'boop' );
267+
268+
// Peek at the first and last queue values:
269+
var v = queue.first();
270+
// returns 'foo'
271+
272+
v = queue.last();
273+
// returns 'boop'
274+
275+
// Inspect the queue length:
276+
var len = queue.length;
277+
// returns 4
278+
279+
// Remove the "oldest" queue value:
280+
v = queue.pop();
281+
// returns 'foo'
282+
283+
// Inspect the queue length:
284+
len = queue.length;
285+
// returns 3
286+
287+
// Iterate over the queue:
288+
var iter = queue.iterator();
289+
var i;
290+
for ( i = 0; i < len; i++ ) {
291+
console.log( 'Queue value #%d: %s', i+1, iter.next().value );
292+
}
293+
294+
// Clear the queue:
295+
queue.clear();
296+
297+
// Inspect the queue length:
298+
len = queue.length;
299+
// returns 0
300+
```
301+
302+
</section>
303+
304+
<!-- /.examples -->
305+
306+
<!-- 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. -->
307+
308+
<section class="references">
309+
310+
</section>
311+
312+
<!-- /.references -->
313+
314+
<!-- Section for related `stdlib` packages. Do not manually edit this section, as it is automatically populated. -->
315+
316+
<section class="related">
317+
318+
* * *
319+
320+
## See Also
321+
322+
- <span class="package-name">[`@stdlib/utils/stack`][@stdlib/utils/stack]</span><span class="delimiter">: </span><span class="description">stack.</span>
323+
324+
</section>
325+
326+
<!-- /.related -->
327+
328+
<!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. -->
329+
330+
<section class="links">
331+
332+
<!-- <related-links> -->
333+
334+
[@stdlib/utils/stack]: https://github.com/stdlib-js/stdlib/tree/develop/lib/node_modules/%40stdlib/utils/stack
335+
336+
<!-- </related-links> -->
337+
338+
</section>
339+
340+
<!-- /.links -->

0 commit comments

Comments
 (0)