-
Notifications
You must be signed in to change notification settings - Fork 4
/
Toolkit.Collections.Queue.lua
87 lines (87 loc) · 2.74 KB
/
Toolkit.Collections.Queue.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
-------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------
-- Toolkit.Collections library extention
-- Toolkit.Collections.Queue provide implementation for queue process
-- Tested on Lua 5.1 with HC2 3.580
--
-- Copyright 2014 Jean-christophe Vermandé
-- Inspired by http://www.lua.org/pil/11.4.html
--
-- Version 1.0.0 [01-12-2014]
-------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------
if not Toolkit then error("You must add Toolkit", 2) end
if not Toolkit.Collections then Toolkit.Collections = {} end
if not Toolkit.Collections.Queue then Toolkit.Collections.Queue = {
-- private properties
__header = "Toolkit.Collections.Queue",
__version = "1.0.0",
__base = {
__first = 0,
__count = 0,
toArray = (function(self)
local r = {};
for i=1, self:count() do
--Tk:trace("add %s in table at pos # %d", tostring(self[i]), i);
r[i] = self[i];
end
return r;
end),
clear = (function(self)
for i=1, self:count() do
self[i] = nil;
end
self.__first = 0;
self.__count = 0;
end),
enqueue = (function(self, value)
assert(value ~= nil);
local n = self.__first + 1;
self.__count = self.__count + 1;
self.__first = n;
self[n] = value;
Tk:trace("add at pos %d value with %s type", n, tostring(self[n]));
end),
dequeue = (function(self)
local o = self:peek();
self[1] = nil;
self.__first = self.__first - 1;
self.__count = self.__count -1;
for i=1, self:count() do
self[i] = self[i+1];
end
return o;
end),
contains = (function(self, value)
assert(value ~= nil);
for i=1, self:count() do
if (self[i] == value) then
return true;
end
end
return false;
end),
peek = (function(self)
return self[1];
end),
count = (function(self)
return tonumber(self.__count);
end),
clone = (function(self)
return self;
end)
},
new = (function()
-- make sure all free-able memory is freed to help process
collectgarbage("collect");
return Toolkit.Collections.Queue.__base;
end),
-- version()
version = (function()
return Toolkit.Collections.Queue.__version;
end)
};
Toolkit:traceEx("red", Toolkit.Collections.Queue.__header.." loaded in memory...");
-- benchmark code
if (Toolkit.Debug) then Toolkit.Debug:benchmark(Toolkit.Collections.Queue.__header.." lib", "elapsed time: %.3f cpu secs\n", "fragment", true); end;
end;