Software /
code /
prosody
Comparison
util/queue.lua @ 6920:7596c37e0a63
Merge 0.10->trunk
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Mon, 09 Nov 2015 22:56:32 +0100 |
parent | 6912:cb5b14c95b7b |
child | 9901:c8b75239846c |
child | 11103:73b8aaf55775 |
comparison
equal
deleted
inserted
replaced
6909:494938dec5d8 | 6920:7596c37e0a63 |
---|---|
14 local function new(size, allow_wrapping) | 14 local function new(size, allow_wrapping) |
15 -- Head is next insert, tail is next read | 15 -- Head is next insert, tail is next read |
16 local head, tail = 1, 1; | 16 local head, tail = 1, 1; |
17 local items = 0; -- Number of stored items | 17 local items = 0; -- Number of stored items |
18 local t = have_utable and utable.create(size, 0) or {}; -- Table to hold items | 18 local t = have_utable and utable.create(size, 0) or {}; -- Table to hold items |
19 | 19 --luacheck: ignore 212/self |
20 return { | 20 return { |
21 _items = t; | |
21 size = size; | 22 size = size; |
22 count = function (self) return items; end; | 23 count = function (self) return items; end; |
23 push = function (self, item) | 24 push = function (self, item) |
24 if items >= size then | 25 if items >= size then |
25 if allow_wrapping then | 26 if allow_wrapping then |
48 if items == 0 then | 49 if items == 0 then |
49 return nil; | 50 return nil; |
50 end | 51 end |
51 return t[tail]; | 52 return t[tail]; |
52 end; | 53 end; |
54 items = function (self) | |
55 --luacheck: ignore 431/t | |
56 return function (t, pos) | |
57 if pos >= t:count() then | |
58 return nil; | |
59 end | |
60 local read_pos = tail + pos; | |
61 if read_pos > t.size then | |
62 read_pos = (read_pos%size); | |
63 end | |
64 return pos+1, t._items[read_pos]; | |
65 end, self, 0; | |
66 end; | |
53 }; | 67 }; |
54 end | 68 end |
55 | 69 |
56 return { | 70 return { |
57 new = new; | 71 new = new; |