util.queue: Add :items() iterator
authorMatthew Wild <mwild1@gmail.com>
Sun, 18 Oct 2015 20:42:33 +0000 (21:42 +0100)
committerMatthew Wild <mwild1@gmail.com>
Sun, 18 Oct 2015 20:42:33 +0000 (21:42 +0100)
util/queue.lua

index 203da0e3f6ee590a7dc3ec3631a7a90b5de0fbb9..ac782e84613f799707b20f651fe059482dcfffa6 100644 (file)
@@ -18,6 +18,7 @@ local function new(size, allow_wrapping)
        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 @@ local function new(size, allow_wrapping)
                        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