Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 126 additions & 0 deletions lib/node_modules/@stdlib/dstructs/fifo/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,59 @@ len = queue.length;
// returns 0
```

##### queue.every( predicate\[, thisArg] )

Tests whether all elements in a queue pass a test implemented by a predicate function.

```javascript
// Test whether all elements are greater than 0:
function predicate( v ) {
return v > 0;
}

var queue = fifo();
// returns <FIFO>

// Add values to the queue:
queue.push( 1 ).push( 2 ).push( 3 );

var bool = queue.every( predicate );
// returns true
```

The `predicate` function is provided three arguments:

- **value**: current queue element.
- **index**: current queue element index.
- **q**: the queue on which the method was called.

To set the function execution context, provide a `thisArg`.

```javascript
function predicate( v ) {
this.count += 1;
return v > 0;
}

// Create a queue:
var queue = fifo();

// Add values to the queue:
queue.push( 1 ).push( 2 ).push( 3 );

// Define an execution context:
var ctx = {
'count': 0
};

// Test whether all elements are greater than 0:
var bool = queue.every( predicate, ctx );
// returns true

var n = ctx.count;
// returns 3
```

##### queue.first()

Returns the "oldest" queue value (i.e., the value which is "first-out"). If the queue is currently empty, the returned value is `undefined`.
Expand All @@ -96,6 +149,37 @@ var v = queue.first();
// returns 'foo'
```

##### queue.get( index )

Returns a queue element located at a nonnegative integer position (index) `i`.

```javascript
var queue = fifo();

// Add values to the queue:
queue.push( 'foo' ).push( 'bar' ).push( 'baz' );

// Access elements by index via the accessor protocol:
var v = queue.get( 0 );
// returns 'foo'

v = queue.get( 1 );
// returns 'bar'

v = queue.get( 2 );
// returns 'baz'
```

If provided an out-of-bounds index, the method returns `undefined`.

```javascript
var queue = fifo();

// Out-of-bounds indices return undefined:
var v = queue.get( 10 );
// returns undefined
```

##### queue.iterator()

Returns an iterator for iterating over a queue. If an environment supports `Symbol.iterator`, the returned iterator is iterable.
Expand Down Expand Up @@ -201,6 +285,48 @@ v = queue.pop();
// returns 'bar'
```

##### queue.set( value, index )

Sets a queue element at a specified index.

```javascript
var queue = fifo();
// returns <FIFO>

// Add values to the queue:
queue.push( 'foo' ).push( 'bar' ).push( 'baz' );

// Set elements by index:
queue.set( 'beep', 0 );
var v = queue.get( 0 );
// returns 'beep'

queue.set( 'boop', 1 );
v = queue.get( 1 );
// returns 'boop'
```

By default, the method sets queue elements starting at position (index) `i = 0`. To set elements starting elsewhere in the queue, provide an index argument `i`.

```javascript
var queue = fifo();
// returns <FIFO>

// Add values to the queue:
queue.push( 'foo' ).push( 'bar' ).push( 'baz' );

// By default, sets element at index 0:
queue.set( 'beep' );
var v = queue.get( 0 );
// returns 'beep'

// Set element at a specified index:
queue.set( 'boop', 1 );

v = queue.get( 1 );
// returns 'boop'
```

##### queue.toArray()

Returns an array of queue values.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2026 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var bench = require( '@stdlib/bench' );
var format = require( '@stdlib/string/format' );
var pkg = require( './../package.json' ).name;
var FIFO = require( './../lib' );


// MAIN //

bench( format( '%s:every', pkg ), function benchmark( b ) {
var bool;
var q;
var i;

q = new FIFO();
for ( i = 1; i <= 4; i++ ) {
q.push( i );
}

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
bool = q.every( function predicate( v ) {
return v > 0;
});
if ( typeof bool !== 'boolean' ) {
b.fail( 'should return a boolean' );
}
}
b.toc();
if ( typeof bool !== 'boolean' ) {
b.fail( 'should return a boolean' );
}
b.pass( 'benchmark finished' );
b.end();
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
* @license Apache-2.0
*
* Copyright (c) 2026 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var bench = require( '@stdlib/bench' );
var isBoolean = require( '@stdlib/assert/is-boolean' ).isPrimitive;
var pow = require( '@stdlib/math/base/special/pow' );
var format = require( '@stdlib/string/format' );
var pkg = require( './../package.json' ).name;
var FIFO = require( './../lib' );


// FUNCTIONS //

/**
* Predicate function.
*
* @private
* @param {boolean} value - array element
* @param {NonNegativeInteger} idx - array element index
* @param {FIFO} q - queue instance
* @returns {boolean} boolean indicating whether a value passes a test
*/
function predicate( value ) {
return value > 0;
}

/**
* Creates a benchmark function.
*
* @private
* @param {PositiveInteger} len - queue length
* @returns {Function} benchmark function
*/
function createBenchmark( len ) {
var q;
var i;

q = new FIFO();
for ( i = 0; i < len; i++ ) {
q.push( i );
}

return benchmark;

/**
* Benchmark function.
*
* @private
* @param {Benchmark} b - benchmark instance
*/
function benchmark( b ) {
var bool;
var i;

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
bool = q.every( predicate );
if ( typeof bool !== 'boolean' ) {
b.fail( 'should return a boolean' );
}
}
b.toc();
if ( !isBoolean( bool ) ) {
b.fail( 'should return a boolean' );
}
b.pass( 'benchmark finished' );
b.end();
}
}


// MAIN //

/**
* Main execution sequence.
*
* @private
*/
function main() {
var len;
var min;
var max;
var f;
var i;

min = 1; // 10^min
max = 6; // 10^max

for ( i = min; i <= max; i++ ) {
len = pow( 10, i );
f = createBenchmark( len );
bench( format( '%s:every:len=%d', pkg, len ), f );
}
}

main();
54 changes: 54 additions & 0 deletions lib/node_modules/@stdlib/dstructs/fifo/benchmark/benchmark.get.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* @license Apache-2.0
*
* Copyright (c) 2026 The Stdlib Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

'use strict';

// MODULES //

var bench = require( '@stdlib/bench' );
var format = require( '@stdlib/string/format' );
var pkg = require( './../package.json' ).name;
var FIFO = require( './../lib' );


// MAIN //

bench( format( '%s:get', pkg ), function benchmark( b ) {
var q;
var v;
var i;

q = new FIFO();
for ( i = 1; i <= 4; i++ ) {
q.push( i );
}

b.tic();
for ( i = 0; i < b.iterations; i++ ) {
v = q.get( i%4 );
if ( typeof v === 'undefined' ) {
b.fail( 'should return a value' );
}
}
b.toc();
if ( typeof v === 'undefined' ) {
b.fail( 'should return a value' );
}
b.pass( 'benchmark finished' );
b.end();
});
Loading
Loading