Comparison

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
comparison
equal deleted inserted replaced
6910:82765a4ec799 6911:56c9bc4ba247
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
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 return function (t, pos)
56 if pos >= t:count() then
57 return nil;
58 end
59 local read_pos = tail + pos;
60 if read_pos > t.size then
61 read_pos = (read_pos%size);
62 end
63 return pos+1, t._items[read_pos];
64 end, self, 0;
65 end;
53 }; 66 };
54 end 67 end
55 68
56 return { 69 return {
57 new = new; 70 new = new;