Software /
code /
prosody
Diff
util/queue.lua @ 6911:56c9bc4ba247
util.queue: Add :items() iterator
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Sun, 18 Oct 2015 21:42:33 +0100 |
parent | 6731:d4a6c9ee4bc5 |
child | 6912:cb5b14c95b7b |
line wrap: on
line diff
--- a/util/queue.lua Sun Oct 18 21:35:21 2015 +0100 +++ b/util/queue.lua Sun Oct 18 21:42:33 2015 +0100 @@ -18,6 +18,7 @@ local t = have_utable and utable.create(size, 0) or {}; -- Table to hold items return { + _items = t; size = size; count = function (self) return items; end; push = function (self, item) @@ -50,6 +51,18 @@ end return t[tail]; end; + items = function (self) + return function (t, pos) + if pos >= t:count() then + return nil; + end + local read_pos = tail + pos; + if read_pos > t.size then + read_pos = (read_pos%size); + end + return pos+1, t._items[read_pos]; + end, self, 0; + end; }; end