Software /
code /
prosody
Changeset
6731:d4a6c9ee4bc5
util.queue: Allow optional wrap-around when pushing, overwriting oldest unread item
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Wed, 03 Jun 2015 15:51:07 +0100 |
parents | 6730:7889515bac86 |
children | 6732:b5cf091d8c45 |
files | util/queue.lua |
diffstat | 1 files changed, 7 insertions(+), 2 deletions(-) [+] |
line wrap: on
line diff
--- a/util/queue.lua Sat May 30 22:23:19 2015 +0100 +++ b/util/queue.lua Wed Jun 03 15:51:07 2015 +0100 @@ -11,7 +11,7 @@ local have_utable, utable = pcall(require, "util.table"); -- For pre-allocation of table -local function new(size) +local function new(size, allow_wrapping) -- Head is next insert, tail is next read local head, tail = 1, 1; local items = 0; -- Number of stored items @@ -22,7 +22,12 @@ count = function (self) return items; end; push = function (self, item) if items >= size then - return nil, "queue full"; + if allow_wrapping then + tail = (tail%size)+1; -- Advance to next oldest item + items = items - 1; + else + return nil, "queue full"; + end end t[head] = item; items = items + 1;