Changeset

6911:56c9bc4ba247

util.queue: Add :items() iterator
author Matthew Wild <mwild1@gmail.com>
date Sun, 18 Oct 2015 21:42:33 +0100
parents 6910:82765a4ec799
children 6912:cb5b14c95b7b
files util/queue.lua
diffstat 1 files changed, 13 insertions(+), 0 deletions(-) [+]
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