Train module is an implementation of a Queue data structure. It uses 2 arrays, to simulate and perform a fast element shift operation on its head, without using the Array#shift() method.
npm install train
|1||downloads in the last week|
|24||downloads in the last month|
|Version||1.3.3 last updated a year ago|
The Train module is an implementation of a ( FIFO ) Queue data structure.
It uses 2 arrays, to simulate and perform a fast element shift/pop operation, without using the Array#shift() method.
$ npm install train [-g]
var Train = require( 'train' );
$ cd train/ $ npm test
Create an instance, optionally with an Array of elements.
Train( [ Array elements ] ) // or new Train( [ Array elements ] )
// a property to get the queue size // NOTE that accessors are very slow, use the size method Train.length : Number
// get an element at certain index Train#get( Number index ) : Object // get the head element from the queue Train#shift() : Object /* * The same as shift, if it was called without argument. * if a number k was specified, it returns an array of K elements ( K <= k ) * If k > queue size, it returns all the elements until the end of queue. */ Train#pop( [ Number k ] ) : Object /* * a method to ( circular ) get an element at the specified index, * or at current iterator position, starting from the head of queue; then * it automatically increments the current iterator position, without * evicting the element from the queue. */ Train#next( [ Number index ] ) : Object /* * push an element to the end of the queue; * optionally if el was an array, and concat flag was true, * every element will be pushed in the queue; * then it returns the resulting queue length. */ Train#push( Object el [, Boolean concat ] ) : Number // get the queue size/length Train#size() : Number /* * a method to empty the queue. * it returns the number of elements flushed. */ Train#flush() : Number /* * apply a fn to every element of the queue, like Array#forEach * fn will get 2 args : ( Object element, Number index ) */ Train#forEach( Function fn [, Object scope ] ) : null /* * apply a fn to every element of the queue, like Array#forEach * fn will get 3 args : ( Object element, Number index, Function done ) * after that every fn will have called done(), the function callback will be * executed ( with an err argument if any has occurred ). */ Train#iterate( Function fn [, Object scope, [, Function callback ] ] ) : null
Copyright (c) 2012 < Guglielmo Ferri : firstname.lastname@example.org >
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.