time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

README.md (6340B)


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