pax_global_header00006660000000000000000000000064123270024060014506gustar00rootroot0000000000000052 comment=08dde64384699438cc82150e2e779fc0ce770f6c lru-queue-0.1.0/000077500000000000000000000000001232700240600134305ustar00rootroot00000000000000lru-queue-0.1.0/.gitignore000066400000000000000000000000631232700240600154170ustar00rootroot00000000000000.DS_Store /node_modules /npm-debug.log /.lintcache lru-queue-0.1.0/.lint000066400000000000000000000000741232700240600144000ustar00rootroot00000000000000@root module tabs indent 2 maxlen 100 ass nomen plusplus lru-queue-0.1.0/.travis.yml000066400000000000000000000001621232700240600155400ustar00rootroot00000000000000language: node_js node_js: - 0.8 - 0.10 - 0.11 notifications: email: - medikoo+lru-queue@medikoo.com lru-queue-0.1.0/CHANGES000066400000000000000000000000701232700240600144200ustar00rootroot00000000000000v0.1.0 -- 2013.04.26 Initial (derived from memoizee) lru-queue-0.1.0/LICENCE000066400000000000000000000020631232700240600144160ustar00rootroot00000000000000Copyright (C) 2014 Mariusz Nowak (www.medikoo.com) 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. lru-queue-0.1.0/README.md000066400000000000000000000033301232700240600147060ustar00rootroot00000000000000# lru-queue ## Size limited queue based on [LRU](http://en.wikipedia.org/wiki/Least_Recently_Used#LRU) algorithm _Originally derived from [memoizee](https://github.com/medikoo/memoize) package._ It's low-level utility meant to be used internally within cache algorithms. It backs up [`max`](https://github.com/medikoo/memoize#limiting-cache-size) functionality in [memoizee](https://github.com/medikoo/memoize) project. ### Installation $ npm install lru-queue To port it to Browser or any other (non CJS) environment, use your favorite CJS bundler. No favorite yet? Try: [Browserify](http://browserify.org/), [Webmake](https://github.com/medikoo/modules-webmake) or [Webpack](http://webpack.github.io/) ### Usage Create queue, and provide a limit ```javascript var lruQueue = require('lru-queue'); var queue = lruQueue(3); // limit size to 3 ``` Each queue exposes three methods: #### queue.hit(id) Registers hit for given _id_ (must be plain string). ```javascript queue.hit('raz'); // size: 1 ``` If hit doesn't remove any old item from list it returns `undefined`, otherwise it returns removed _id_. ```javascript queue.hit('dwa'); // undefined, size: 2 queue.hit('trzy'); // undefined, size: 3 (at max) queue.hit('raz'); // undefined, size: 3 (at max) queue.hit('dwa'); // undefined, size: 3 (at max) queue.hit('cztery'); // 'trzy', size: 3 (at max) ``` #### queue.delete(id); _id's_ can be cleared from queue externally ```javascript queue.delete('raz'); // size: 2 queue.delete('cztery'); // size: 1 ``` #### queue.clear(); Resets queue ```javascript queue.clear(); // size: 0 ``` ### Tests [![Build Status](https://travis-ci.org/medikoo/lru-queue.png)](https://travis-ci.org/medikoo/lru-queue) $ npm test lru-queue-0.1.0/index.js000066400000000000000000000021321232700240600150730ustar00rootroot00000000000000'use strict'; var toPosInt = require('es5-ext/number/to-pos-integer') , create = Object.create, hasOwnProperty = Object.prototype.hasOwnProperty; module.exports = function (limit) { var size = 0, base = 1, queue = create(null), map = create(null), index = 0, del; limit = toPosInt(limit); return { hit: function (id) { var oldIndex = map[id], nuIndex = ++index; queue[nuIndex] = id; map[id] = nuIndex; if (!oldIndex) { ++size; if (size <= limit) return; id = queue[base]; del(id); return id; } delete queue[oldIndex]; if (base !== oldIndex) return; while (!hasOwnProperty.call(queue, ++base)) continue; //jslint: skip }, delete: del = function (id) { var oldIndex = map[id]; if (!oldIndex) return; delete queue[oldIndex]; delete map[id]; --size; if (base !== oldIndex) return; if (!size) { index = 0; base = 1; return; } while (!hasOwnProperty.call(queue, ++base)) continue; //jslint: skip }, clear: function () { size = 0; base = 1; queue = create(null); map = create(null); index = 0; } }; }; lru-queue-0.1.0/package.json000066400000000000000000000007471232700240600157260ustar00rootroot00000000000000{ "name": "lru-queue", "version": "0.1.0", "description": "LRU Queue", "author": "Mariusz Nowak (http://www.medikoo.com/)", "repository": { "type": "git", "url": "git://github.com/medikoo/lru-queue.git" }, "keywords": [ "lru", "cache", "queue" ], "dependencies": { "es5-ext": "~0.10.2" }, "devDependencies": { "tad": "~0.1.21" }, "scripts": { "test": "node node_modules/tad/bin/tad" }, "license": "MIT" } lru-queue-0.1.0/test/000077500000000000000000000000001232700240600144075ustar00rootroot00000000000000lru-queue-0.1.0/test/index.js000066400000000000000000000015351232700240600160600ustar00rootroot00000000000000'use strict'; module.exports = function (t, a) { var queue = t(3); a(queue.hit('raz'), undefined, "Hit #1"); a(queue.hit('raz'), undefined, "Hit #2"); a(queue.hit('dwa'), undefined, "Hit #3"); a(queue.hit('raz'), undefined, "Hit #4"); a(queue.hit('dwa'), undefined, "Hit #5"); a(queue.hit('trzy'), undefined, "Hit #6"); a(queue.hit('raz'), undefined, "Hit #7"); a(queue.hit('dwa'), undefined, "Hit #8"); a(queue.hit('cztery'), 'trzy', "Overflow #1"); a(queue.hit('dwa'), undefined, "Hit #9"); a(queue.hit('trzy'), 'raz', "Overflow #2"); a(queue.hit('raz'), 'cztery', "Overflow #3"); a(queue.hit('cztery'), 'dwa', "Overflow #4"); a(queue.hit('trzy'), undefined, "Hit #10"); a(queue.hit('dwa'), 'raz', "Overflow #4"); a(queue.hit('cztery'), undefined, "Hit #11"); queue.delete('cztery'); a(queue.hit('cztery'), undefined, "Hit #12"); };