pax_global_header00006660000000000000000000000064125601332230014507gustar00rootroot0000000000000052 comment=2e627a090f303e6a8f3f707b7e5c858c63da2fea fifo.lua-0.2/000077500000000000000000000000001256013322300130535ustar00rootroot00000000000000fifo.lua-0.2/.gitignore000066400000000000000000000000311256013322300150350ustar00rootroot00000000000000*.rock luacov.report.out fifo.lua-0.2/.luacheckrc000066400000000000000000000001451256013322300151600ustar00rootroot00000000000000-- Tell luacheck about busted globals files["spec/"] = { new_read_globals = { "describe", "it" }; } fifo.lua-0.2/.luacov000066400000000000000000000002231256013322300143420ustar00rootroot00000000000000return { statsfile = "luacov.stats.out"; reportfile = "luacov.report.out"; deletestats = true; include = { "./fifo$"; }; exclude = { }; } fifo.lua-0.2/.travis.yml000066400000000000000000000006701256013322300151670ustar00rootroot00000000000000language: c env: global: - LUAROCKS=2.2.1 matrix: - LUA=lua5.1 - LUA=lua5.2 - LUA=lua5.3 - LUA=luajit2.0 - LUA=luajit2.1 before_install: - bash .travis/setup_lua.sh - sudo luarocks install luacov-coveralls - sudo luarocks install busted install: - sudo luarocks make script: - busted -c after_success: - luacov-coveralls -v notifications: email: on_success: change on_failure: always fifo.lua-0.2/.travis/000077500000000000000000000000001256013322300144415ustar00rootroot00000000000000fifo.lua-0.2/.travis/LICENSE000066400000000000000000000020731256013322300154500ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) 2014 Alexey Melnichuk 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. fifo.lua-0.2/.travis/platform.sh000066400000000000000000000003711256013322300166220ustar00rootroot00000000000000if [ -z "${PLATFORM:-}" ]; then PLATFORM=$TRAVIS_OS_NAME; fi if [ "$PLATFORM" == "osx" ]; then PLATFORM="macosx"; fi if [ -z "$PLATFORM" ]; then if [ "$(uname)" == "Linux" ]; then PLATFORM="linux"; else PLATFORM="macosx"; fi; fi fifo.lua-0.2/.travis/setup_lua.sh000066400000000000000000000045761256013322300170120ustar00rootroot00000000000000#! /bin/bash # A script for setting up environment for travis-ci testing. # Sets up Lua and Luarocks. # LUA must be "lua5.1", "lua5.2" or "luajit". # luajit2.0 - master v2.0 # luajit2.1 - master v2.1 set -eufo pipefail LUAJIT_BASE="LuaJIT-2.0.3" source .travis/platform.sh LUAJIT="no" if [ "$PLATFORM" == "macosx" ]; then if [ "$LUA" == "luajit" ]; then LUAJIT="yes"; fi if [ "$LUA" == "luajit2.0" ]; then LUAJIT="yes"; fi if [ "$LUA" == "luajit2.1" ]; then LUAJIT="yes"; fi; elif [ "$(expr substr $LUA 1 6)" == "luajit" ]; then LUAJIT="yes"; fi if [ "$LUAJIT" == "yes" ]; then if [ "$LUA" == "luajit" ]; then curl http://luajit.org/download/$LUAJIT_BASE.tar.gz | tar xz; else git clone http://luajit.org/git/luajit-2.0.git $LUAJIT_BASE; fi cd $LUAJIT_BASE if [ "$LUA" == "luajit2.1" ]; then git checkout v2.1; fi make && sudo make install if [ "$LUA" == "luajit2.1" ]; then sudo ln -s /usr/local/bin/luajit-2.1.0-alpha /usr/local/bin/luajit sudo ln -s /usr/local/bin/luajit /usr/local/bin/lua; else sudo ln -s /usr/local/bin/luajit /usr/local/bin/lua; fi; else if [ "$LUA" == "lua5.1" ]; then curl http://www.lua.org/ftp/lua-5.1.5.tar.gz | tar xz cd lua-5.1.5; elif [ "$LUA" == "lua5.2" ]; then curl http://www.lua.org/ftp/lua-5.2.4.tar.gz | tar xz cd lua-5.2.4; elif [ "$LUA" == "lua5.3" ]; then curl http://www.lua.org/ftp/lua-5.3.0.tar.gz | tar xz cd lua-5.3.0; fi sudo make $PLATFORM install; fi cd $TRAVIS_BUILD_DIR; LUAROCKS_BASE=luarocks-$LUAROCKS # curl http://luarocks.org/releases/$LUAROCKS_BASE.tar.gz | tar xz git clone https://github.com/keplerproject/luarocks.git $LUAROCKS_BASE cd $LUAROCKS_BASE git checkout v$LUAROCKS if [ "$LUA" == "luajit" ]; then ./configure --lua-suffix=jit --with-lua-include=/usr/local/include/luajit-2.0; elif [ "$LUA" == "luajit2.0" ]; then ./configure --lua-suffix=jit --with-lua-include=/usr/local/include/luajit-2.0; elif [ "$LUA" == "luajit2.1" ]; then ./configure --lua-suffix=jit --with-lua-include=/usr/local/include/luajit-2.1; else ./configure; fi make build && sudo make install cd $TRAVIS_BUILD_DIR rm -rf $LUAROCKS_BASE if [ "$LUAJIT" == "yes" ]; then rm -rf $LUAJIT_BASE; elif [ "$LUA" == "lua5.1" ]; then rm -rf lua-5.1.5; elif [ "$LUA" == "lua5.2" ]; then rm -rf lua-5.2.3; elif [ "$LUA" == "lua5.3" ]; then rm -rf lua-5.3.0; fi fifo.lua-0.2/LICENSE000066400000000000000000000020661256013322300140640ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) 2015 daurnimator 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. fifo.lua-0.2/README.md000066400000000000000000000023021256013322300143270ustar00rootroot00000000000000A lua library/'class' that implements a FIFO. Objects in the fifo can be of any type, including `nil`. Compatible with Lua 5.1, 5.2, 5.3 and LuaJIT # Status This project has been used in production since 2012. [![Build Status](https://travis-ci.org/daurnimator/fifo.lua.svg)](https://travis-ci.org/daurnimator/fifo.lua) [![Coverage Status](https://coveralls.io/repos/daurnimator/fifo.lua/badge.svg?branch=master)](https://coveralls.io/r/daurnimator/fifo.lua?branch=master) # Documentation Please see the `doc/` folder. You can generate a man page using [pandoc](http://pandoc.org/): ``` pandoc doc/index.md -s -t man -o /usr/local/share/man/man3/fifo.lua.3 ``` # Installation Available via luarocks: `luarocks install fifo` Alternatively, you may just copy fifo.lua to your own project. # Tests You will need - [busted](http://olivinelabs.com/busted/) - [luacov](https://keplerproject.github.io/luacov/) Run `busted -c` to run tests You can then generate a coverage report by running `luacov` (it will write the report to luacov.report.out) # History This was previously a component of [lomp](https://github.com/daurnimator/lomp2) but was useful enough in other projects that I split it out. fifo.lua-0.2/doc/000077500000000000000000000000001256013322300136205ustar00rootroot00000000000000fifo.lua-0.2/doc/index.md000066400000000000000000000052471256013322300152610ustar00rootroot00000000000000--- title: fifo.lua author: - daurnimator section: 3 --- # DESCRIPTION A lua library/'class' that implements a FIFO. Objects in the fifo can be of any type, including `nil`. # USAGE The library returns the constructor `fifo.new`: ```lua new_fifo = require "fifo" ``` ## `myfifo:fifo = new_fifo(...)` Create a new fifo by calling the constructor; it optionally takes the initial state. ```lua myfifo = new_fifo("foo", "bar") ``` ## `myfifo = myfifo:setempty(f:function)` The behaviour when trying to `:pop()` or `:remove()` too many items from an empty list is configurable. Returns the fifo itself By default an error will be thrown. You can set a custom behaviour by providing a function to `:setempty()`. The return values of your function will be returned by `:pop()` or `:remove()` ```lua myfifo:setempty(function(myfifo) return nil end) ``` This method returns self, which makes it easy to use at construction time: e.g. to create a new fifo where `:pop()` returns `nil` when empty: ```lua myfifo = new_fifo():setempty(function() return nil end) ``` ## `fifo:push(object:*)` Use the `:push()` method to append an object to the fifo ```lua myfifo:push({"an object"}) ``` ## `object:*, exists:bool = fifo:peek(n:number|none)` Allows you to inspect a fifo without removing items from it. Returns the item at the given index (or `nil`) and whether it existed (as `nil` is a valid value). By default uses the next item from the fifo. ```lua exists, myobject = myfifo:peek() ``` ## `object:* = fifo:pop()` Returns the next item from the fifo, removing it. ```lua myobject = myfifo:pop() ``` ## `fifo:insert(index:number, object:*)` This can be used to insert an item into the middle of a fifo. The index is from the output of the fifo where `1` would be the next item popped from the fifo, and `myfifo:length() + 1` would be equivalent to `:push()` The efficiency of this operation is proportional to the distance from either end of the fifo. ```lua myobject = myfifo:insert(1, {"some object"}) ``` ## `object:* = fifo:remove(index:number)` This can be used to remove an item from the middle of a fifo. The index is from the output of the fifo where `1` would be the next item popped from the fifo, and `myfifo:length()` would be the input (i.e equivalent to `:push()`) The efficiency of this operation is proportional to the distance from either end of the fifo. The object removed is returned. ```lua myobject = myfifo:remove(2) ``` ## `length:number = fifo:length()` and `length:number = #fifo` operator Returns the current number of items in the fifo. Available as `:length()` as the `__len` metamethod doesn't work for tables in lua versions 5.1 and earlier. fifo.lua-0.2/fifo-0.2-0.rockspec000066400000000000000000000006121256013322300161620ustar00rootroot00000000000000package = "fifo" version = "0.2-0" description= { summary = "A lua library/'class' that implements a FIFO"; homepage = "https://github.com/daurnimator/fifo.lua"; license = "MIT/X11"; } dependencies = { "lua"; } source = { url = "https://github.com/daurnimator/fifo.lua/archive/0.2.zip"; dir = "fifo.lua-0.2"; } build = { type = "builtin"; modules = { ["fifo"] = "fifo.lua"; }; } fifo.lua-0.2/fifo.lua000066400000000000000000000040611256013322300145020ustar00rootroot00000000000000local select , setmetatable = select , setmetatable local function is_integer(x) return x % 1 == 0 end local fifo = {} local fifo_mt = { __index = fifo ; __newindex = function ( f , k , v ) error("Tried to set table field in fifo") end ; } local empty_default = function ( _ ) error ( "Fifo empty" ) end function fifo.new ( ... ) return setmetatable({ empty = empty_default; head = 1; tail = select("#",...); data = {...}; }, fifo_mt) end function fifo:length ( ) return self.tail - self.head + 1 end fifo_mt.__len = fifo.length -- Peek at the nth item function fifo:peek ( n ) n = n or 1 assert(is_integer(n), "bad index to :peek()") local index = self.head - 1 + n if index > self.tail then return nil, false else return self.data[index], true end end function fifo:push ( v ) self.tail = self.tail + 1 self.data[self.tail] = v end function fifo:pop ( ) local head , tail = self.head , self.tail if head > tail then return self:empty() end local v = self.data[head] self.data[head] = nil self.head = head + 1 return v end function fifo:insert ( n , v ) local head , tail = self.head , self.tail if n <= 0 or head + n > tail + 2 or not is_integer(n) then error("bad index to :insert()") end local p = head + n - 1 if p <= (head + tail)/2 then for i = head , p do self.data[i- 1] = self.data[i] end self.data[p- 1] = v self.head = head - 1 else for i = tail , p , -1 do self.data[i+ 1] = self.data[i] end self.data[p] = v self.tail = tail + 1 end end function fifo:remove ( n ) local head , tail = self.head , self.tail if n <= 0 or not is_integer(n) then error("bad index to :remove()") end if head + n - 1 > tail then return self:empty() end local p = head + n - 1 local v = self.data[p] if p <= (head + tail)/2 then for i = p , head , -1 do self.data[i] = self.data[i-1] end self.head = head + 1 else for i = p , tail do self.data[i] = self.data[i+1] end self.tail = tail - 1 end return v end function fifo:setempty ( func ) self.empty = func return self end return fifo.new fifo.lua-0.2/spec/000077500000000000000000000000001256013322300140055ustar00rootroot00000000000000fifo.lua-0.2/spec/fifo_spec.lua000066400000000000000000000036531256013322300164540ustar00rootroot00000000000000local new_fifo = require "fifo" describe("Everything works.", function() it("doesn't let you set a field", function() local f = new_fifo() assert.errors(function() f.foo = "bar" end) end) it("peek works", function() local f = new_fifo() f:push("foo") assert.same("foo", (f:peek())) f:push("bar") assert.same("foo", (f:peek())) assert.same("bar", (f:peek(2))) f:pop() assert.same("bar", (f:peek())) assert.same({nil, false}, {f:peek(20)}) end) it("length works", function() local f = new_fifo("foo", "bar") assert.same(2, f:length()) f:push("baz") assert.same(3, f:length()) f:pop() f:pop() f:pop() assert.same(0, f:length()) end) if _VERSION > "Lua 5.1" then it("length operator works", load([[ local new_fifo = require "fifo" local f = new_fifo("foo", "bar") assert.same(2, #f) f:push("baz") assert.same(3, #f) f:pop() f:pop() f:pop() assert.same(0, #f) ]], nil, "t", _ENV)) end it("insert works", function() local f = new_fifo("foo") f:insert(1, "baz") f:insert(f:length()+1, "bar") f:insert(f:length(), "corge") -- 2nd from end assert.same("baz", f:pop()) assert.same("foo", f:pop()) assert.same("corge", f:pop()) assert.same("bar", f:pop()) assert.errors(function() f:pop() end) f:insert(1, "qux") assert.same("qux", f:pop()) assert.errors(function() f:insert(2, "too far") end) assert.errors(function() f:insert(0) end) end) it("remove works", function() local f = new_fifo() assert.errors(function() f:remove(1) end) f:setempty(function() return "EMPTY" end) assert.same("EMPTY", f:remove(1)) f:push("foo") f:push("bar") f:push("baz") f:push("qux") f:push("corge") assert.same("baz", f:remove(3)) assert.same("corge", f:remove(4)) assert.same("foo", f:remove(1)) assert.same("qux", f:remove(2)) assert.same("bar", f:remove(1)) assert.same(0, f:length()) assert.errors(function() f:remove(50.5) end) end) end)