README.md (6319B)
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/utils/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/utils/fifo' ); 258 259 var queue; 260 var iter; 261 var len; 262 var v; 263 var i; 264 265 // Create a new FIFO queue: 266 queue = fifo(); 267 268 // Add some values to the queue: 269 queue.push( 'foo' ); 270 queue.push( 'bar' ); 271 queue.push( 'beep' ); 272 queue.push( 'boop' ); 273 274 // Peek at the first and last queue values: 275 v = queue.first(); 276 // returns 'foo' 277 278 v = queue.last(); 279 // returns 'boop' 280 281 // Inspect the queue length: 282 len = queue.length; 283 // returns 4 284 285 // Remove the "oldest" queue value: 286 v = queue.pop(); 287 // returns 'foo' 288 289 // Inspect the queue length: 290 len = queue.length; 291 // returns 3 292 293 // Iterate over the queue: 294 iter = queue.iterator(); 295 for ( i = 0; i < len; i++ ) { 296 console.log( 'Queue value #%d: %s', i+1, iter.next().value ); 297 } 298 299 // Clear the queue: 300 queue.clear(); 301 302 // Inspect the queue length: 303 len = queue.length; 304 // returns 0 305 ``` 306 307 </section> 308 309 <!-- /.examples --> 310 311 <!-- 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. --> 312 313 <section class="references"> 314 315 </section> 316 317 <!-- /.references --> 318 319 <!-- Section for all links. Make sure to keep an empty line after the `section` element and another before the `/section` close. --> 320 321 <section class="links"> 322 323 </section> 324 325 <!-- /.links -->