pax_global_header00006660000000000000000000000064144347421460014523gustar00rootroot0000000000000052 comment=b34287f84ee148d2460cc235f884a23a36dddd89 cyclist-1.0.2/000077500000000000000000000000001443474214600131755ustar00rootroot00000000000000cyclist-1.0.2/.gitignore000066400000000000000000000000231443474214600151600ustar00rootroot00000000000000bench node_modules cyclist-1.0.2/.travis.yml000066400000000000000000000000741443474214600153070ustar00rootroot00000000000000language: node_js node_js: - "0.10" - '0.12' - 'iojs' cyclist-1.0.2/LICENSE000066400000000000000000000020671443474214600142070ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) 2015 Mathias Buus 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. cyclist-1.0.2/README.md000066400000000000000000000022601443474214600144540ustar00rootroot00000000000000# Cyclist Cyclist is an efficient [cyclic list](http://en.wikipedia.org/wiki/Circular_buffer) implemention for Javascript. It is available through npm ``` npm install cyclist ``` [![build status](http://img.shields.io/travis/mafintosh/cyclist.svg?style=flat)](http://travis-ci.org/mafintosh/cyclist) ## What? Cyclist allows you to create a list of fixed size that is cyclic. In a cyclist list the element following the last one is the first one. This property can be really useful when for example trying to order data packets that can arrive out of order over a network stream. ## Usage ``` js var cyclist = require('cyclist') var list = cyclist(4) list.put(42, 'hello 42') // store something and index 42 list.put(43, 'hello 43') // store something and index 43 console.log(list.get(42)) // prints hello 42 console.log(list.get(46)) // prints hello 42 again since 46 - 42 == list.size ``` ## API * `cyclist(size)` creates a new buffer * `cyclist#get(index)` get an object stored in the buffer * `cyclist#put(index,value)` insert an object into the buffer * `cyclist#del(index)` delete an object from an index * `cyclist#size` property containing current size of buffer ## License MIT cyclist-1.0.2/index.js000066400000000000000000000012741443474214600146460ustar00rootroot00000000000000/* eslint-disable no-var */ function Cyclist (size) { if (!(this instanceof Cyclist)) return new Cyclist(size) size = twoify(size) this.mask = size - 1 this.size = size this.values = new Array(size) } Cyclist.prototype.put = function (index, val) { var pos = index & this.mask this.values[pos] = val return pos } Cyclist.prototype.get = function (index) { return this.values[index & this.mask] } Cyclist.prototype.del = function (index) { var pos = index & this.mask var val = this.values[pos] this.values[pos] = undefined return val } module.exports = Cyclist function twoify (n) { if (n && !(n & (n - 1))) return n var p = 1 while (p < n) p <<= 1 return p } cyclist-1.0.2/package.json000066400000000000000000000012321443474214600154610ustar00rootroot00000000000000{ "name": "cyclist", "version": "1.0.2", "repository": { "type": "git", "url": "https://github.com/mafintosh/cyclist" }, "description": "Cyclist is an efficient cyclic list implemention.", "devDependencies": { "brittle": "^3.3.0", "standard": "^17.0.0" }, "keywords": [ "circular", "buffer", "ring", "cyclic", "data" ], "author": "Mathias Buus Madsen ", "bugs": { "url": "https://github.com/mafintosh/cyclist/issues" }, "homepage": "https://github.com/mafintosh/cyclist", "main": "index.js", "scripts": { "test": "standard && brittle test.js" }, "license": "MIT" } cyclist-1.0.2/test.js000066400000000000000000000013501443474214600145110ustar00rootroot00000000000000const test = require('brittle') const cyclist = require('./') test('basic put and get', function (t) { const list = cyclist(2) list.put(0, 'hello') list.put(1, 'world') t.is(list.get(0), 'hello') t.is(list.get(1), 'world') t.end() }) test('overflow put and get', function (t) { const list = cyclist(2) list.put(0, 'hello') list.put(1, 'world') list.put(2, 'verden') t.is(list.get(0), 'verden') t.is(list.get(1), 'world') t.is(list.get(2), 'verden') t.end() }) test('del', function (t) { const list = cyclist(2) list.put(0, 'hello') t.is(list.get(0), 'hello') list.del(0) t.ok(!list.get(0)) t.end() }) test('multiple of two', function (t) { const list = cyclist(3) t.is(list.size, 4) t.end() })